簡體   English   中英

從 http 請求獲取對象后,如何從一個對象翻譯多個字符串,然后用翻譯的字符串存儲該對象

[英]How do i translate multiple strings from an object after getting object from an http request and then store that object with translated strings

我正在從 http 請求中獲取一個對象。 我正在使用 put 方法通過另一個 http 請求存儲該對象。 我想在存儲該對象之前使用 google translate api 將該對象中的一些字符串翻譯成不同的語言。 我怎樣才能做到這一點。

服務.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; } const incomes = []; if (result.incomeDetail) { result.incomeDetail .sort((a, b) => a.year <= b.year ? -1 : 1) .forEach(income => incomes.push({ income: income['income(Rs.)'], year: income.year })); } const details = []; if (result.applicantDetail) { result.applicantDetail.forEach(detail => details.push({ name: detail.name, gender: detail.gender[0].toLowerCase(), age: detail.age, bankOrPostOffice: detail.bankorpostoffice, aadhaarNo: detail.aadhaarNo, accountNo: detail.accountNo })); } const data = { jobCardId: result.jobcardno, regDate: result.dateOfRegistration, photo: result.photoImageUrl, voterId: result.voterId, fatherOrHusband: result.nameOfFatherOrHusband, family: { ids: [result.familyId1, result.familyId], members: details, head: result.nameOfHead, isBpl: result.bplFamily.toLowerCase() === 'yes' ? true : false, bplId: result.bplFamilyId, }, address: { full: result.address, village: result.village, district: result.district, panchayat: result.panchayat, block: result.block }, category: result.category, incomes: incomes, }; console.log('NREGA: ', data); return data; }); } }

如果我使用 translate 方法來翻譯服務本身中的字符串,那么我得到錯誤可能是因為我沒有使用 .subscribe 方法(我在組件中使用它,我從服務調用 verify() 方法。是否可以轉換多個字符串?在將數據返回給組件之前服務本身?

我認為您正在尋找的是 Rxjs地圖運算符。

首先,在您的 service.ts 文件中導入這個操作符。

import 'rxjs/add/operator/map';

然后,如果我明白你的意思,你想在你的組件調用 get 方法時翻譯一些字符串,在從服務器接收到數據之后但在方法將數據發送回組件之前。

組件 > service.get() > http 請求 > 從服務器接收的數據 >翻譯> 發送到組件的數據

如果我是對的,您唯一要做的就是使用地圖運算符:

get() {
  return this.api.newGet(this.baseUrl)
    .map(data => this.translate(data));
}

讓我們想象一個SourceData類型和一個ModifiedData類型。 map 操作符會讓你返回一個Observable<ModifiedData>而不是你用來發回的Observable<SourceData> 查看此處的文檔以獲得更好的解釋。

請注意,您也可以在調用 save 方法時進行翻譯。

save(type, data) {
  data = this.translate(data)
  return this.api.put(this.baseUrl, { type: type, data: data });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM