import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { RouterOutlet } from '@angular/router';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, TranslateModule, FormsModule],
templateUrl: './app.component.html',
styleUrl: './app.component.scss'
})
export class AppComponent {
title = 'angular-i18n';
selectedLanguage = 'en';
constructor(private translateService: TranslateService){}
onLanguageChange() {
this.translateService.use(this.selectedLanguage)
}
}
Hey, good blog, say I changed the language to ‘FR’ (french) and then refreshed the page, will the langugae ‘FR’ stay or will it default to ‘EN’?
Hi and thank you for your comment!
If you use the current implementation described in this article, the page will default to ‘EN’ because it does not have persistence. If you implement persistency, so that the language at initialization is read from local storage or cookies, then you can guarantee that on page reload the language sticks.
[…] In the project I’m developing for a client, I use toasts to inform users about potential issues when the backend API is called. For every known exception thrown, the backend responds with an object containing a code and a message. This code maps to the frontend, and a translation service displays it in different languages. If you want to learn how to integrate a translation service into your Angular 17 application, have a look at this easy-to-follow tutorial. […]