简体   繁体   中英

Is it possible to do a dynamic message translation in angular ngx-translate

I have a dynamic validation message from form controls and the message is from backend configuration. Is it possible to translate with angular ngx-translate?

<span> {{products.errors?.error | tanslate }} </span>- not works. This error is dynamic erros not able to define in json.

likewise for

there is a list of dropdown based on category dynamically from the server. is it possible to translate?

else

is it possible to update the translation en.json,es.json,fr.json files based on server values eg) consider usernames,roles,permissions of logged user needs to be translate

How I see the problem, try to find an api as google translate and send your message and language in which you need to be translated. (fantastic resolution)

https://www.npmjs.com/package/google-translate-api . I never used it, but from examples looks pretty friendly.

translate('I spea Dutch!', {from: 'en', to: 'nl'}).then(res => {
    console.log(res.text);
    //=> Ik spreek Nederlands!
    console.log(res.from.text.autoCorrected);
    //=> true
    console.log(res.from.text.value);
    //=> I [speak] Dutch!
    console.log(res.from.text.didYouMean);
    //=> false
}).catch(err => {
    console.error(err);
});

Realistic, you can map your server errors with numerotations: ERROR_1, ERROR_2, ERROR_3

and in your translate file (which I think is a json):

{ 
   ERROR_1: "mesage translated",
   ERROR_2: "another translated message",
   ERROR_3: "etc"
}

In client, save your errors in an errors list (string[]) and your html will become:

<span *ngFor="let error of errors">
 {{error | translate}}
</span>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM