2

I try setup a JS function to auto load a index page in a different language, regardig the setting device o my reader.

I try with this...but don't work :

<script src="cordova-2.2.0.js"></script>
<script type="text/javascript" charset="utf-8">

            function checkLanguage() {
                if (navigator.globalization.getPreferredLanguage()='en_EN')

                {
                window.location.replace("index_en.html");
            }

            else if (navigator.globalization.getPreferredLanguage()='fr_FR')
            {
                window.location.replace("index_fr.html");
            }
            else
            {
                window.location.replace("index_other.html");
            }

        }
</script>

Is this method can be use, or do I have to consider other option to deal with my multilanguage app ?

Thanks in advance for any help.

Benoit
  • 374
  • 4
  • 20

2 Answers2

1

You need to use the callback of getPreferredLanguage:

var handleDeviceReady = function (event)
{
    navigator.globalization.getPreferredLanguage(
        function (language)
            {
                console.log("language: " + language.value + '\n');
                redirectToLocaleSpecificLogin(language.value);
            },
        function ()
            {
                console.log("Error getting language\n");
                redirectToLocaleSpecificLogin("en");
            }
    );
};

document.addEventListener("deviceready", handleDeviceReady, false);

Then inside of your callback (redirectToLocaleSpecificLogin in this case), you can do your redirects.

Lee
  • 26
  • 2
0

most of the browsers use language property, IE uses userLanguage

var lang = window.navigator.userLanguage || window.navigator.language;

this should work in IE, SAFARI, CHROME and FF

Edit:

JavaScript for detecting browser language preference this link has more detailed discussion on this topic

Community
  • 1
  • 1
Faisal Sayed
  • 783
  • 4
  • 12
  • Thanks, in my case, It will be only use on iPad, that's the reason that I need to use navigator.globalization.getPreferredLanguage wich is describe in PhoneGap API [link]http://docs.phonegap.com/en/2.2.0/cordova_globalization_globalization.md.html#globalization.getPreferredLanguage[link] – Benoit Nov 07 '12 at 09:18