[英]Get browser locale code in Firefox add-on
我正在开发 Firefox 插件。 我需要确定用户浏览器中设置的语言。
有没有像window.navigator
这样的对象,它包含浏览器的当前语言环境?
使用window.navigator
:
虽然可能还有其他东西,但您可以只使用window.navigator
。
navigator
对象可能只在与内容相关联的window
对象上可用(即不是浏览器的 XUL window
)。 但是,即使您需要从内容脚本访问它,该对象也可用于所有扩展类型。
window.navigator
。 在扩展中,您必须使用正确的<window>
对象。 并非所有<window>
对象都具有navigator
属性。 获取 Firefox 首选项:
然而,一个似乎是偏好general.useragent.locale
,或者可能是intl.accept_languages
。 您正在编写的附加组件类型将决定您如何访问它,或者即使它当前可用:
preferences/service
访问它们。Services.prefs
,它使您可以访问nsIPrefBranch 、 nsIPrefBranch2和nsIPrefService 。 在 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
直接使用nsIXULChromeRegistry
服务。 似乎也没有此服务的官方文档。
WebExtensions 具体方法:
WebExtensions 具有Internationalization API ,可从后台脚本获得。 本地可使用i18n.getUILanguage()
。 方法i18n.getAcceptLanguages()
也可能很有趣。
主文件
let locale = chrome.i18n.getUILanguage();
console.log('The locale is: ' + locale);
清单文件
{
"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"]
}
}
索引.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);
包.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"
]
}
//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
,你可以使用WebWorker
s 或ChromeWorker
s 他们有很多有用的东西可用:
我不建议仅仅为了获取导航器信息而启动工作程序,使用Services.appshell.hiddenDOMWindow
可能会更容易,但是如果您已经在使用工作程序,那么它是一个不错的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.