Reputation: 31
I am building a simple app to learn internationalisation using Angular 11. The tutorial I am following is: https://lokalise.com/blog/angular-i18n/
I followed the tutorial and tried to create an app with 2 configurations - an En (english) one and an Ru (russian). When I run the app with
ng serve --configuration=ru --open
I can see the russian translation
when I use:
ng serve
It shows me the English version of the app.
Both these configurations run in different ports. I tried to create a language switcher to be able to switch between the english and russian version of the app here is the code for that in
app.component.ts & app.component.html
import { Component } from '@angular/core';
import {registerLocaleData} from '@angular/common';
import localeRu from '@angular/common/locales/ru';
registerLocaleData(localeRu, 'ru');
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
company = "Ash B.";
created_by = $localize`Created by ${this.company}`
today: number = Date.now();
localesList = [
{ code: "en-US", label: 'English' },
{ code: "ru", label: 'Русский' }
];
tasksCount = 201;
genderCode = 0;
// tslint:disable-next-line:typedef
male() { this.genderCode = 0; }
// tslint:disable-next-line:typedef
female() { this.genderCode = 1; }
// tslint:disable-next-line:typedef
other() { this.genderCode = 2; }
}
<ul>
<li *ngFor="let locale of localesList">
<a href="/{{locale.code}}/">
{{locale.label}}
</a>
</li>
</ul>
When I click on the buttons it changes the url from
localhost:4200/ru to localhost:4200/en-Us
but the content of the app is not translated. In order to see the translation I must run 2 different versions of the app. How can I make the language switch when I click on the buttons ?
Upvotes: 3
Views: 3104
Reputation: 11
The tutorial you are following uses a package called @angular/localize, which is a part of Angular's native i18n system for translating applications.
When you internationalize with @angular/localize, you have to build a separate application for each language.
I recommend using ngx-translate instead, as it allows you to dynamically load translations at runtime without the need to compile your application with a specific locale.
Upvotes: 0
Reputation: 1
to switch between versions you need to serve the build instead of serving the development configuration, each language will be compiled into a separate bundle, watch your dist folder after you run ng build --localize=true to make sure everything went ok.
Upvotes: 0