sample of @ngx-translate library usage for angular application
MIT License
Run nmp start
for a dev server. Navigate to http://localhost:4200/
. The app will automatically reload if you change any of the source files.
Translation and localization is implemented using upgraded @ngx-translate library to add translated strings to markup (html) uses:
<span>{{ '<name_of_string>' | translate }}</span>
to translate string in *.ts
file use
constructor(private translate: TranslateService) {}
...
title = this.translate.instant('KEY'),
in case if you need it at early stages of application (OnInit of AppComponent), use this
constructor(private translate: TranslateService) {}
...
this.translateService.get(['KEY1', 'KEY2']))
.subscribe(translations => {
// init dropdown list with TRANSLATED list of languages from config
console.log(translations['KEY1'])
console.log(translations['KEY2'])
});
If there is a string key, used for translation, but not presented in the current locale json file, you will see the error message in the UI instead of translated value
this behavior is implemented using missing-translation.service.ts
All localization data is kept in folder ./src/assets/locale/
To add new language follow this:
<lang>.json
file to folder ./src/assets/locale/
./src/environments/environment*.ts
, property locales
npm run update-locale
. this command will check all usages of translation and update all existing *.json files, merge if needed, including newly added file./src/assets/locale
folder with JSON files, named by locale name (like en.json
, or en-US.json
)TranslateModule
in app.module.ts
./src/app/services/missing-translation.service.ts
this.translateService.use(environment.defaultLocale);
in AppComponent.OnInitupdate-locale
in package.json
to update locale files and add missing keys