访问语言环境的方法
使用window.navigator:
虽然可能还有其他内容,但您可以使用window.navigator。
navigator object 可能仅在与内容关联的 window 对象上可用(即不是浏览器的 XUL window)。但是,即使您需要从内容脚本访问它,该对象也适用于所有扩展类型。
-
WebExtensions :您需要从内容脚本访问它。
- 所有其他类型:即使在多进程 Firefox 中,您也可以从主/后台脚本访问
window.navigator。与往常一样,您必须使用正确的 <window> 对象。并非所有 <window> 对象都有 navigator 属性。
获取 Firefox 偏好:
然而,一个似乎是偏好general.useragent.locale,或者可能是intl.accept_languages。您正在编写什么类型的插件将决定您如何访问它,或者即使它当前可用:
在 ChromeWindow 上使用 getLocale():
正如 stkvtflw 所述,您还可以在主 ChromeWindow 上使用getLocale()。 PanelUI(不要与sdk/panel 混淆)包括一个函数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,可从后台脚本中获得。本地可使用i18n.getUILanguage()。 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"]
}
}
附加 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 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);