Core: Remember selected language

Created on 24 Mar 2016  路  6Comments  路  Source: ngx-translate/core

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.

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:

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.

All 6 comments

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???

Was this page helpful?
0 / 5 - 0 ratings

Related issues

webprofusion-chrisc picture webprofusion-chrisc  路  4Comments

gmquiroga picture gmquiroga  路  3Comments

crebuh picture crebuh  路  3Comments

briancullinan picture briancullinan  路  3Comments

dankerk picture dankerk  路  3Comments