menu

Questions & Answers

Is there a way of loading different texts in my Angular application if a specific parameter is passed in a configuration script?

I am using ngx-translate module to swtich from english to my native language texts in my Angular application, like this

<h4>{{"USER_ROLE_MODAL.USERS.ERROR_1" | translate}}</h4>

where

USER_ROLE_MODAL.USERS.ERROR_1

is the property in json containing the error string.

I was wondering if it's possible to change those texts (maybe by reading from a different json) if a different script is launched or a different property is passed in the startup script.

if you are having trouble reading my question, i am sorry english is not my native language, just ask me i'll do my best to explain myself better

Answers(1) :

This is a very basic function of ngx-translate.

When a correct loader (I assume you use JSON http loader based on your question) is configured, you can simply call TranslateService.use(lang). To change the language.

Code sample from the docs:

import {Component} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';

@Component({
    selector: 'app',
    template: `
        <div>{{ 'HELLO' | translate:param }}</div>
    `
})
export class AppComponent {
    param = {value: 'world'};

    constructor(translate: TranslateService) {
        // this language will be used as a fallback when a translation isn't found in the current language
        translate.setDefaultLang('en');

         // the lang to use, if the lang isn't available, it will use the current loader to get them
        translate.use('en');
    }
}