首页 > 解决方案 > Angular7 internationalization vs translate module

问题描述

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

标签: internationalizationangular7ngx-translate

解决方案


如果我理解,您可以使用 ngx-translate ,它将按文档中的预期工作:

this.translate.setDefaultLang('en');

虽然您可以在运行时更改语言,例如:

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);
}
}

我使用 ngx-translate 在我的应用程序中没有任何问题


推荐阅读