Search code examples
angularinternationalizationnativescriptngx-translate

NativeScript with Angular - ngx-translate how to add translations after the application has initialized


I'm writing an application with NativeScript 6.4.1 and Angular 8.

I want to use the ngx-translate library in my project: https://github.com/ngx-translate/core

Here is my sample repository: https://github.com/aubrey-fowler/translationsTest1

It's working fine but I need to be able to add translations after the app has initialized. It's because it's how the backend developers have implemented the feature. When the user logs in to the app they get some more translations from their profile. I know it's strange but it's how they want it to work.

**Problem: my sample code is working but how can I use the ngx-translate library to add new languages and their associated son? **

e.g. this.translate.add('fr', { "greeting": "bonjour"});

Is it possible to add a language json at runtime and if so how can I do that?

Code snippet:

export class AppComponent {

    constructor(public translate: TranslateService) {

        translate.addLangs(['en', 'nl']);
        translate.setDefaultLang('en');
        translate.use('en');

    }

}

Solution

  • You must focus on the documentation, use setTranslation method

    this.translateService.setTranslation("your-lang-code", { key : "value"}, true);