Is there a way for me to translate some strings in an object before storing it in another http request using the Google Translate API? I am currently getting the object from one http request and saving it with a put method. How can this be achieved?
service.ts
import { Injectable } from '@angular/core'; import { HttpClient, HttpErrorResponse } from '@angular/common/http'; import { Observable } from 'rxjs/Rx'; import { BaseApi } from '../../../../laas/base-api.service'; import { ApplicantService } from '../../../../laas/applicant.service'; import { environment as ENV } from '../../../../../environments/environment' @Injectable() export class NregaService { private applicantId: string; private baseUrl: string; constructor( private http: HttpClient, private api: BaseApi, private applicant: ApplicantService ) { this.applicantId = applicant.getApplicantID() this.baseUrl = `/applicants/${this.applicantId}/id/nrega`; } get() { return this.api.newGet(this.baseUrl); } save(type, data) { return this.api.put(this.baseUrl, { type: type, data: data }); } translate(q) { const translateUrl = ENV.googleAddress.translateUrl; const apiKey = ENV.googleAddress.apiKey; const target = 'en'; const model = 'base'; return this.http.post(`${translateUrl}${apiKey}`, { q, target, model }).map(res => { const value = res['data'].translations[0]; console.log(`Translated ${q}:`, value); return value.translatedText; }); } verify(id) { return this.api.post('nrega', { jobcardid: id }).map(result => { if (typeof result === typeof '') { throw result; } // Code for processing result omitted for brevity return data; }); } }
If I try to use the translate method within the service itself without the .subscribe method, I encounter an error. Is there a way to translate multiple strings within the service before returning the data to the component?