fabi1cazenave / webL10n

Client-side internationalization / localization library
http://fabi1cazenave.github.com/webL10n/
279 stars 71 forks source link

call variable #58

Closed bluetch closed 9 years ago

bluetch commented 9 years ago

Excuse me, I am a beginner for JS. Is possible to load variable in other script with webL10n?

ex: var myLanguage = getLanguage();

thank you.

Rob--W commented 9 years ago

The code snippet that you've given does indeed assign the return value of some method to a variable, but I guess that you're looking for something else. What's the problem you're trying to solve?

bluetch commented 9 years ago

thank for your reply.

I would like to do some UI change via language. ex: language=zn-cn, show 3 div with google map language=en-us, show 5 div width video so I need var language variable to change my web content.

Rob--W commented 9 years ago
var language = document.webL10n.getLanguage();

Or, without this library:

var language = navigator.language || navigator.userLanguage;
bluetch commented 9 years ago

I tried, but didn't get anything.

var language = document.webL10n.getLanguage(); console.log(language);

and I get another bug ..below

l10n

Rob--W commented 9 years ago

Could you get the latest version of l10n.js? It includes several fixes, including a solution to that synchronous XMLHttpRequest warning.

bluetch commented 9 years ago

I have just updated l10n.js, it fix synchronous XMLHttpRequest warning~ but my variable still unavailable.

2015-03-02 19 29 05

Rob--W commented 9 years ago

getLanguage only returns something useful after loadLocale has been invoked. This happens after DOMContentLoaded.

Add an event listener for the "localized" event, and you will get a correct value.

Or, if you are only interested in the user's locale, use the snippet at the end of my first reply.

bluetch commented 9 years ago

Got it, thank u :)