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.
5
votes
1 Answers
8
votes
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");
}
}