Reputation: 827
I've got huge doubts on how to internationalize my angular7 webapp. What I want is to understand from browser locale the user language and to display the webapp in that language. Then if the user wants to change it, he can choose the language in the combobox. Now...I've spent two days reading all the official and unofficial documentation but I can't understand which is the best way to achieve this goal. Reading the official documentation I can use the new default i18n system but I can't understand how to change the language at runtime.
Should I use translate module?
Thanks
Upvotes: 0
Views: 123
Reputation: 136
default i18n Currently you can not change the Translation during Runtime. You need to switch to a different instance of your application in that language a user selected. I Like how you translate your app. Translations which are done by code in controller are not possible any way. Sometimes you need that. Not recommended currently. But perhaps later it's the more preferable solution.
Translation Service Everything is possible.
I would use TranslationService or translate module because it's easier. Did the same investigations as you did now ;-)
Kind Regards
Upvotes: 0
Reputation: 229
If I understood, you could use ngx-translate and it will work as expected from docs:
this.translate.setDefaultLang('en');
while you could change language in runtime with something like:
useLang(){
const lang = localStorage.getItem('language');
if (lang === null) {
this.translate.use('en');
console.log('Language is null! Using english');
} else {
this.translate.use(lang);
console.log(lang);
}
}
I had no issues in my app using ngx-translate
Upvotes: 1