I have switched my site language to spanish in angular4 using ngx-translate,so when i refresh the page it switches back to english language. So,is there any way to keep spanish as a language of my site,after i refresh the page using ngx-translate.
Add TranslateService to the constructor parameters to make it available in the component. Set the default language of your application using translate. setDefaultLang('en') . The default language is the fall-back language, that is used if a translation can not be found.
NGX-Translate is an internationalization library for Angular. Internationalization is the process of translating an application into multiple languages. Using this library, we can translate our application language into multiple languages. This will work with not only static data, but dynamic data as well.
The service also contains a method called translate. It is later called by the pipe to get the translation for a specific key. It always uses the language specified in the 'language'-variable of the service, to lookup the translation. That's it.
when you set your language spanish then store in localstore
Like ::
below code when you select switched your site language to spanish
this.translate.setDefaultLang('es');
this.translate.use('es');
this.localStorage.setItem("language","es");
and below code when you first time set language
constructor(public translate: TranslateService) {
if(localStorage.getItem('language')){
translate.setDefaultLang(localStorage.getItem('language'));
translate.use(localStorage.getItem('language'));
}else {
translate.setDefaultLang('en');
translate.use('en');
localStorage.setItem("language","en");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With