1

I'm developing a Firefox add-on. I need to determine which language is set in the user's browser.
Is there some kind of object like window.navigator, which contains the browser's current locale?

4

2 回答 2

4

Methods to access the locale

Use window.navigator:
While there may be something else, you could just use window.navigator.

The navigator object may only be available on the window object associated with content (i.e. not the one that is the browser's XUL window). However, even if you need to access it from a content script, the object is available to all extension types.

  • WebExtensions : You will need to access it from a content script.
  • All other types: You can access window.navigator from your main/background script even in multiprocess Firefox. As always in an extension, you have to use the correct <window> object. Not all <window> objects have a navigator property.

Get the Firefox preference:
However, one appears to be the preference general.useragent.locale, or perhaps intl.accept_languages. What type of add-on you are writing will determine how you could access this, or even if it is currently available:

Use getLocale() on a ChromeWindow:
As mentioned by stkvtflw, you can also use getLocale() on the main ChromeWindow. The PanelUI (not to be confused with sdk/panel) includes a function getLocale(). However, there does not appear to be any official documentation for this function. The code for it is:

function getLocale() {
  try {
    let chromeRegistry = Cc["@mozilla.org/chrome/chrome-registry;1"]
                           .getService(Ci.nsIXULChromeRegistry);
    return chromeRegistry.getSelectedLocale("browser");
  } catch (ex) {
    return "en-US";
  }
}

Obviously, this indicates you could use the nsIXULChromeRegistry service directly, should you desire. There also does not appear to be official documentation for this service.

WebExtensions specific method:
WebExtensions has the Internationalization API, which is available from background scripts. The local is available using i18n.getUILanguage(). The method i18n.getAcceptLanguages() may also be of interest.

Complete code for WebExtensions add-ons

main.js

let locale = chrome.i18n.getUILanguage();
console.log('The locale is: ' + locale);

manifest.json

{
    "description": "Log the locale to the console",
    "manifest_version": 2,
    "name": "log_locale",
    "version": "0.1",
    "applications": {
        "gecko": {
            "id": "extention@stick.man",
            "strict_min_version": "45.0"
        }
    },
    "background": {
        "scripts": ["main.js"]
    }
}

Complete code for Add-on SDK extensions

index.js:

//Get the window.navigator from current window/tab

//Get the currently active Browser Window
let activeWindow = require('sdk/window/utils').getMostRecentBrowserWindow();
//activeWindow.document.defaultView is the <window> you want to use
let defaultViewNavigator = activeWindow.document.defaultView.navigator;
//log the language from window.navigator
console.log('navigator language='+defaultViewNavigator.language);


//Get the locale from preferences:
let prefs = require("sdk/preferences/service");
let langFromPref = prefs.get(`general.useragent.locale`);
console.log('language from prefs='+langFromPref);


//Get the locale using getLocale():
let localeGet = require("sdk/window/utils").getMostRecentBrowserWindow().getLocale()
console.log('getLocale language='+localeGet);

package.json:

{
  "title": "Find locale",
  "name": "find_local",
  "version": "0.0.1",
  "description": "Find the locale",
  "main": "index.js",
  "author": "Makyen",
  "engines": {
    "firefox": ">=38.0a1",
    "fennec": ">=38.0a1"
  },
  "license": "MIT",
  "keywords": [
    "jetpack"
  ]
}

Code for bootstrap/legacy add-ons

//Import Services
Components.utils.import("resource://gre/modules/Services.jsm");
//Get the currently active Browser Window
let activeWindow=Services.wm.getMostRecentWindow("navigator:browser");

//Get the window.navigator from current window/tab   
//activeWindow.document.defaultView is the <window> you want to use
let defaultViewNavigator = activeWindow.document.defaultView.navigator;
console.log('navigator language='+defaultViewNavigator.language);

//Get the locale from preferences:
let langFromPref = Services.prefs.getCharPref(`general.useragent.locale`);
console.log('language from prefs='+langFromPref);

//Get the locale using getLocale():
let localeGet = activeWindow.getLocale()
console.log('getLocale language='+localeGet);
于 2016-08-06T14:49:22.897 回答
2

@Mayken's answer is excellent. I just want to add though, you can also get navigator without window, you can do so with WebWorkers or ChromeWorkers they have a bunch of useful stuff available:

https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Functions_and_classes_available_to_workers#workerscope

I wouldn't recommend launching a worker just to get navigator info, it would be probably be easier to use Services.appshell.hiddenDOMWindow, but if you are using workers already, then yes its a nice solution.

于 2016-08-08T09:41:13.730 回答