我已经实现了英语到印地语转换的谷歌翻译。翻译成功实施。但我想如果我输入英文,那么它不应该翻译,而只输入印地语。喜欢 : 你叫什么名字 但我不知道如何在角度上实现它。
这是我正在使用的角度服务
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class GoogleService {
constructor(private _http: HttpClient) {
}
translate(obj: GoogleObj, key: string) {
return this._http.post(url + key, obj);
}
}
const url = 'https://translation.googleapis.com/language/translate/v2?key=';
export class GoogleObj {
q: string;
readonly source: string = 'en';
readonly target: string = 'hi';
readonly format: string = 'text';
constructor() {
}
}
这是我的组件
import { Component, OnInit } from '@angular/core';
import { DashboardMenu } from '../Models/DashboardMenu';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { MenuServiceService } from '../../app/service/menu-service.service'
import { GoogleService, GoogleObj } from '../../app/service/google.service';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss'],
providers: [GoogleService]
})
export class DashboardComponent implements OnInit {
public googleObj: GoogleObj = new GoogleObj();
public key: string;
public result = '';
private btnSubmit: any;
dashboardMenu: DashboardMenu[] = [];
filterDashboardMenu: DashboardMenu[] = [];
private UserId: any;
constructor(private _google: GoogleService) {
}
send() {
// this.btnSubmit.disabled = true;
this.key = "AIzaSyBG9MiPh7-7W8LhBgFdPaq1peio6Mosh8s";
this._google.translate(this.googleObj, this.key).subscribe(
(res: any) => {
// this.btnSubmit.disabled = false;
this.result = res.data.translations[0].translatedText;
},
err => {
console.log(err);
}
);
}
ngOnInit() {
}
}