-1

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

I am considering using Transloco for my translations library.

I need to be able to change the language at runtime as well as add a new language at runtime.

How can I do this in Transloco?

I see in the docs it says you can add a language with the setLanguage function: https://ngneat.github.io/transloco/docs/language-api

I tried it myself and it doesn't work.

Here is my sample project: https://github.com/aubrey-fowler/Transloco-Test

Here is a code snippet:

export class ItemsComponent implements OnInit {

    constructor(private translate: TranslocoService) { }

    ngOnInit(): void {
        console.log(' ItemsComponent ', this.translate.getActiveLang());
        console.log(' ItemsComponent 2 ', this.translate.getAvailableLangs());
    }

    switchLang(lang: string) {
        this.translate.setActiveLang(lang);
        console.log(' setActiveLang ', this.translate.getActiveLang());
    }

    useLanguage(language: string) {
        this.translate.setActiveLang(language);
        console.log(' setActiveLang ', this.translate.getActiveLang());
    }

    add() {
        console.log(' a ');
        this.translate.setTranslation({ Sitetitle : "bonjour"}, 'fr', { merge: true });
        console.log(' ItemsComponent ', this.translate.getActiveLang());
        console.log(' ItemsComponent 3 ', this.translate.getAvailableLangs());
    }

}
user1261710
  • 2,539
  • 5
  • 41
  • 72

1 Answers1

1

you can define select and the language available, see below:

  <ion-select [(ngModel)]="Locale"
        interface="action-sheet" (ionChange)="SetLanguage()">
        <ion-select-option value="en-US">{{'language.englishUsa' | transloco}}</ion-select-option>
        <ion-select-option value="pt-BR">{{'language.portugueseBrasil' | transloco}}</ion-select-option>
      </ion-select>

And set language selected by user. Save to Storage and define as language by default.

  public SetLanguage() {
    this._Storage.set('Locale', Locale);
    this._TranslocoService.setActiveLang(Locale);
  }
RRV
  • 143
  • 10