Angular 8 language conversion with google api.english to hindi conversion

2.5k Views Asked by At

I have implemented google translation for english to hindi conversion. translation is successfully implemented. but i want if i type in english then it should not translate but only type in hindi as it is. Like : What is your name Result spectated : व्हाट इस योर नेम I searched on google it's providing me result google sapi. but i dont know how to implement it on angular.

Here is my service in angular that i am using

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() { 

  }
}

Here is my component

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() {

  }
}
0

There are 0 best solutions below