navigator.language

<<

webgapps

Posts: 100

Joined: Sun Jan 17, 2010 5:17 pm

Post Sat May 21, 2011 10:52 am

navigator.language

Yesterday I received the e-mails about the automated tests and version compatibility bumps due to Firefox 5 beta release.

One of my extensions was flagged due to navigator.language and the report suggested using general.useragent.locale pref. So I updated the extension and uploaded the new version.

Everything was working fine. However today, when testing the extension with a different OS version on a VM, I have noticed that something has broken the extension. After some tests, I realized general.useragent.locale was returning "chrome://global/locale/intl.properties" instead of the correct language value.

This problem is not consistent, but when it happens, it breaks the extension.

I have tested with several Firefox versions.

So I have updated the code to deal with this problem. This is what I am using now:
  Code:
         //get browser language
         var language;
         try{
            language = navigator.language;
         }catch(e){

            //access preferences interface
            this.prefs = Components.classes["@mozilla.org/preferences-service;1"]
            .getService(Components.interfaces.nsIPrefService)
            .getBranch("general.useragent.");

            language = this.prefs.getCharPref("locale");
            
            if(language === "chrome://global/locale/intl.properties"){
               language = "en-US";
            }
         }


Am I doing something wrong?
<<

mkaply

Posts: 6

Joined: Thu Mar 25, 2010 8:45 pm

Post Sat May 21, 2011 2:03 pm

Re: navigator.language

It would appear that general.useragent.locale is a complex pref.

There's information on how to access them here:

https://developer.mozilla.org/en/Code_s ... plex_types
<<

daveg

Posts: 40

Joined: Wed Sep 30, 2009 6:58 am

Location: Philadelphia, PA, USA

Post Sat May 21, 2011 4:02 pm

Re: navigator.language

Getting the locale can be a tricky bit, especially because there's not just one "browser locale". There's the locales listed in order of preference the browser requests for content (user set in preferences) the locale the browser UI is in, and also the locale any extension is in (which may be different from the browser, depending on supported locales). You didn't mention what you needed to fetch for so just in case you or anyone else needs them, here's some simplified functions from what I use in Flagfox.

This little complicated-looking thing was the best way I could come up with to get the primary content locale via the preference:
  Code:
function getContentLocale()  // Firefox primary content locale (user set)
{
    try
    {
        var prefService = Components.classes["@mozilla.org/preferences-service;1"]
                                    .getService(Components.interfaces.nsIPrefService);
        try { var accept_languages = prefService.getComplexValue("intl.accept_languages",Components.interfaces.nsIPrefLocalizedString).data; }
        catch (e) { var accept_languages = prefService.getCharPref("intl.accept_languages"); }
        return /^[^\s,;]{2,}/.exec(accept_languages)[0];  // Extract first locale code in pref (space/comma/semicolon delimited list)
    } catch (e) { return "en"; }
}
It looks as though window.navigator.language will now be this in Firefox 5+, whereas before it was the Firefox UI locale. (though, the above will still be needed if you want to support an older Firefox version too)

I think this is somewhere else on this forum too, but this seems to be the simplest way to get the UI locale for either a specific extension or Firefox:
  Code:
function getChromeUILocale(name)  // UI locale (specify chrome namespace of an extension, or "global" for the application)
{
    return Components.classes["@mozilla.org/chrome/chrome-registry;1"]
                     .getService(Components.interfaces.nsIXULChromeRegistry)
                     .getSelectedLocale(name);
}
No playing with preferences with the above method. Rather than checking the setting, it checks what's actually selected to be in use.

Just for good measure, this is how you get the operating system locale if you need it:
  Code:
function getOSLocale()  // Main OS locale
{
    return Components.classes["@mozilla.org/intl/nslocaleservice;1"]
                     .getService(Components.interfaces.nsILocaleService)
                     .getSystemLocale()
                     .getCategory("NSILOCALE_MESSAGES");
}
<<

webgapps

Posts: 100

Joined: Sun Jan 17, 2010 5:17 pm

Post Thu May 26, 2011 3:55 am

Re: navigator.language

Thanks for the suggestions.

What I need is just the browser UI locale, but the system locale will work for me as well.

This seems to be the easier solution:

  Code:
function getOSLocale()  // Main OS locale
{
    return Components.classes["@mozilla.org/intl/nslocaleservice;1"]
                     .getService(Components.interfaces.nsILocaleService)
                     .getSystemLocale()
                     .getCategory("NSILOCALE_MESSAGES");
}
<<

daveg

Posts: 40

Joined: Wed Sep 30, 2009 6:58 am

Location: Philadelphia, PA, USA

Post Thu May 26, 2011 1:22 pm

Re: navigator.language

lovinglinux wrote:What I need is just the browser UI locale, but the system locale will work for me as well.

Then what you want is the second one above. Simplified for just that instead of generalized:
  Code:
function getBrowserUILocale()  // Browser UI locale
{
    return Components.classes["@mozilla.org/chrome/chrome-registry;1"]
                     .getService(Components.interfaces.nsIXULChromeRegistry)
                     .getSelectedLocale("global");
}

If what you're trying to get is the locale the user wants to view their program in, you probably want to use the browser UI locale instead of OS. There are plenty of people who have different languages set up for their browser vs. OS, and the former is what they want for the program, thus probably what they want you to use for other things in the program.
<<

webgapps

Posts: 100

Joined: Sun Jan 17, 2010 5:17 pm

Post Thu May 26, 2011 2:44 pm

Re: navigator.language

daveg wrote:
lovinglinux wrote:What I need is just the browser UI locale, but the system locale will work for me as well.

Then what you want is the second one above. Simplified for just that instead of generalized:
  Code:
function getBrowserUILocale()  // Browser UI locale
{
    return Components.classes["@mozilla.org/chrome/chrome-registry;1"]
                     .getService(Components.interfaces.nsIXULChromeRegistry)
                     .getSelectedLocale("global");
}

If what you're trying to get is the locale the user wants to view their program in, you probably want to use the browser UI locale instead of OS. There are plenty of people who have different languages set up for their browser vs. OS, and the former is what they want for the program, thus probably what they want you to use for other things in the program.


Thanks.
<<

webgapps

Posts: 100

Joined: Sun Jan 17, 2010 5:17 pm

Post Sun Jun 19, 2011 12:57 pm

Re: navigator.language

I figured out the problem with navigator.language. There was a bad entry in Firefox language preferences that was returning the wrong value. To be safe, I will continue to use the suggestion above.

Return to Extension Development

Who is online

Users browsing this forum: No registered users

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
CA Gen2 style designed by Vjacheslav Trushkin.