In angular-translate there was a possibility to let the library handle the storage of the selected language, e.g. by using useCookieStoreage from angular-translate-storage-cookie.
Before implementing an own language storage mechanism, I'd like to know whether there is an official approach or even a library available.
There is nothing built in, but you can write new loaders, and you can extend the existing file loader for example.
If you do anything that store in localstorage, a PR would be nice because that's something that could be interesting to others.
Thanks for your answer.
For now (just testing), I just made a small service for storing language (and maybe some other local values) and modified the infitialization code from your example, so nothing for a PR yet. There also is almost no error checking in my code yet, so if there is some "bad content" in the local storage it will be used as langauge. However, if anyone interested here is my code:
import {Injectable} from "angular2/core";
@Injectable()
export class LocalSettingsService{
//todo: better error handling!
getLanguage():string{
if (localStorage){
return localStorage['language'] || "";
}
else{
return "";
}
}
setLanguage(language: string){
if (localStorage){
localStorage['language'] = language;
}
}
}
and in my app component, before calling translate.use(userLang); I added these few lines:
// try to get saved language
var storedLang: string = _localSettings.getLanguage();
if (storedLang !== ""){
userLang = storedLang;
}
when changing the language, I just call setLanguage(...) of the service.
If I develop something that would fit in your code, I'll do a pull request.
Thanks ! :+1:
Closing this old issue, let me know if I should reopen it.
Hi guys, what about this one? Nothing new?
Thanks for your answer.
For now (just testing), I just made a small service for storing language (and maybe some other local values) and modified the infitialization code from your example, so nothing for a PR yet. There also is almost no error checking in my code yet, so if there is some "bad content" in the local storage it will be used as langauge. However, if anyone interested here is my code:import {Injectable} from "angular2/core"; @Injectable() export class LocalSettingsService{ //todo: better error handling! getLanguage():string{ if (localStorage){ return localStorage['language'] || ""; } else{ return ""; } } setLanguage(language: string){ if (localStorage){ localStorage['language'] = language; } } }and in my app component, before calling translate.use(userLang); I added these few lines:
// try to get saved language var storedLang: string = _localSettings.getLanguage(); if (storedLang !== ""){ userLang = storedLang; }when changing the language, I just call setLanguage(...) of the service.
If I develop something that would fit in your code, I'll do a pull request.
could you please give me working example link of stackblitz???
Most helpful comment
Thanks for your answer.
For now (just testing), I just made a small service for storing language (and maybe some other local values) and modified the infitialization code from your example, so nothing for a PR yet. There also is almost no error checking in my code yet, so if there is some "bad content" in the local storage it will be used as langauge. However, if anyone interested here is my code:
and in my app component, before calling translate.use(userLang); I added these few lines:
when changing the language, I just call setLanguage(...) of the service.
If I develop something that would fit in your code, I'll do a pull request.