在 Firefox 附加组件中获取浏览器区域设置代码

Get browser locale code in Firefox add-on

我正在开发 Firefox 附加组件。我需要确定用户浏览器中设置的是哪种语言。
有没有像 window.navigator 这样的对象,它包含浏览器的当前语言环境?

访问语言环境的方法

使用window.navigator:
虽然可能还有其他东西,但您可以只使用 window.navigator.

navigator object 可能仅在与内容关联的 window 对象上可用(即不是浏览器的 XUL window)。但是,即使您需要从内容脚本访问它,该对象也可用于所有扩展类型。

  • WebExtensions : 您需要从内容脚本访问它。
  • 所有其他类型:即使在多进程 Firefox 中,您也可以从 main/background 脚本访问 window.navigator。与往常一样,在扩展中,您必须使用正确的 <window> 对象。并非所有 <window> 对象都有 navigator 属性.

获取 Firefox 首选项:
但是,其中一个似乎是偏好 general.useragent.locale,或者 intl.accept_languages。您正在编写的附加组件类型将决定您如何访问它,或者即使它当前可用:

在 ChromeWindow 上使用 getLocale()
正如 stkvtflw 所提到的,您还可以在主 ChromeWindow 上使用 getLocale()PanelUI (not to be confused with sdk/panel) includes a function getLocale()。但是,似乎没有关于此功能的任何官方文档。它的代码是:

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

显然,这表明您可以直接使用 nsIXULChromeRegistry 服务,如果您愿意的话。此服务似乎也没有官方文档。

WebExtensions具体方法:
WebExtensions 有 Internationalization API, which is available from background scripts. The local is available using i18n.getUILanguage(). The method i18n.getAcceptLanguages() 也可能感兴趣。

WebExtensions 附加组件的完整代码

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"]
    }
}

Add-on SDK 扩展的完整代码

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"
  ]
}

bootstrap/legacy 附加组件的代码

//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);

@Mayken 的回答很好。我只想补充一点,你也可以在没有 window 的情况下获得 navigator,你可以使用 WebWorkers 或 ChromeWorkers 他们有很多有用的东西可用:

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

我不建议启动一个 worker 只是为了获取导航器信息,它可能更容易使用 Services.appshell.hiddenDOMWindow,但是如果你已经在使用 worker,那么它是一个不错的解决方案。