Irrelon / jquery-lang-js

i18n Allow instant language switching on HTML pages without reloading the page.
https://www.irrelon.com
362 stars 132 forks source link

How get 'currentLang'? #68

Closed yura-f closed 8 years ago

yura-f commented 8 years ago

Hi! I want select menu item with current language. As the page loads to learn the current language?

Thank you!

Irrelon commented 8 years ago

I believe you should be able to do:

var myCurrentLang = lang.currentLang;
yura-f commented 8 years ago

It always shows 'en'. Even if the cookie saved another language.

<script type="text/javascript">
        // Create language switcher instance
        var lang = new Lang();
        lang.dynamic('es', 'js/langpack/es.json');

        lang.init({
            defaultLang: 'en'
        });

        console.log(lang.currentLang); //always 'en'
</script>
Irrelon commented 8 years ago

Hmm, it could be because you are effectively checking before the library has had a chance to read the cookie... can you place your console.log line at the end of the body tag? That way it wont execute until the code in the head tag has. If that doesn't fix it the other way would be to just read the cookie yourself. Since the cookie plugin is already loaded you can take advantage of that to get the cookie value.

yura-f commented 8 years ago

Did it read cookies.

console.log ("langCookie =" + Cookies.get ('langCookie')); //current lang

The variant with end 'body' prints always 'en'.

Thank you!

Irrelon commented 8 years ago

Fantastic, glad I could help.