Question

I have a simple login page with a "option" for select language :

  <input type="text" class="form-control" id="identifiant" data-i18n="[placeholder]login.placeholders.username" autofocus>
  <input type="password" class="form-control" id="password" data-i18n="[placeholder]login.placeholders.password">
  <input id="loginCheckbox" type="checkbox" value="remember-me"/>&nbsp;<label for="loginCheckbox"  data-i18n="login.rememberme"></label>
  <br>
        <center>
            <div class="form-group col-lg-6 col-lg-offset-3">
                <select id="select-lang" class="form-control">
                    <option value="en-US" data-i18n="lang.english"></option>
                    <option value="fr-FR" data-i18n="lang.french"></option>
                </select>
            </div>

The JS part :

if(Meteor.isClient) {
Meteor.startup(function() {
    i18n
        .init({
            fallbackLng:'en-US',
        })
        .done(function() {
            $('[data-i18n]').i18n();
        }); 
});

}

When you change the language on the login page everything work perfectly :). But when I log a user, I lost the translation for other pages. My question is : How can I save the language setting for all my site ? Cookies ?

Sorry, I'm new with i18next :)

Was it helpful?

Solution

Yep, cookies are the way to go. Session is not enough since it's not persistent.

Cookie.set('lang', 'en-US');

Cookie.get('lang');

OTHER TIPS

Use Meteor Session.

if(Meteor.isClient) {
    Meteor.startup(function() {
    if(typeof Session.get('lang') == 'undefined') {
        //set default lang
        Session.set('lang', 'en-US');
    }
    i18n
        .init({
        fallbackLng: Session.get('lang'),
    })
    .done(function() {
        $('[data-i18n]').i18n();
    }); 
});

And change the Session when changing language. Session is active until user closes his browser. If you want to preserve chosen language after re-opening browser store the lang in collection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top