A reliable way to get the browser's language on the front-end. It uses a back-end service at http://ajaxhttpheaders.appspot.com. That service returns the headers which contains the browser's preferred language in the "Accept-Language" header.
The ML object gets added to the browser's window object as the alias "multilingual". When the page loads you can simply call multilingual.init();
. This will initate an ajax call to the service and set the multilingual.language
property to whatever is returned in the "Accept-Language" header. This value is whichever language is set as the browser's top preference.
multilingual.init()
makes a call to multilingual.getLanguage()
with the multilingual.setLanguage()
method as the callback. If you want to supply your own call back you can call multilingual.getLanguage()
manually and supply your own callback. Your callback will need to accept the language string as the first parameter.
You can also force a language by calling multilingual.setLanguage()
and passing the language string you want to set.
If the back-end service is unreachable after 10 seconds, an error is thrown and the multilanguage.language
property is set to false
.
jQuery is required because it's $.ajax method is used to query the back-end service.