Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After page refresh ngx-translate in angular4 is swiching back to english

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.

like image 571
kishan agarwal Avatar asked Dec 08 '17 07:12

kishan agarwal


People also ask

How do I change the default language in NGX?

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.

How does Ngx translator work?

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.

What is translate pipe in Angular?

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.


1 Answers

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");
    }
}
like image 171
AddWeb Solution Pvt Ltd Avatar answered Oct 17 '22 06:10

AddWeb Solution Pvt Ltd