在Firefox插件中获取浏览器区域设置代码
Get browser locale code in Firefox add-on
我正在开发一个Firefox插件。我需要确定在用户的浏览器中设置了哪种语言
有没有像window.navigator
这样的对象,它包含浏览器的当前区域设置?
访问区域设置的方法
使用window.navigator
:
虽然可能还有其他内容,但您可以使用window.navigator
。
navigator
对象可能仅在与内容相关联的window
对象上可用(即,不是浏览器的XUL window
对象(。但是,即使您需要从内容脚本访问它,该对象也可用于所有扩展类型。
- WebExtensions:您需要从内容脚本访问它
- 所有其他类型:即使在多进程Firefox中,您也可以从主/后台脚本访问
window.navigator
。在扩展中,您必须使用正确的<window>
对象。并非所有<window>
对象都具有navigator
属性
获取Firefox首选项:
然而,一个似乎是偏好general.useragent.locale
,或者可能是intl.accept_languages
。你正在编写的插件类型将决定你如何访问它,甚至它目前是否可用:
- 插件SDK扩展可以使用
preferences/service
访问它们 - 引导程序或旧版加载项可以使用
Services.prefs
,这使您可以访问nsIPrefBranch、nsIPrefBranch2和nsIPrefService - 此时,WebExtensions似乎无法访问首选项。考虑到Firefox的整体偏好,它们很可能在未来可用
在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具有国际化API,可从后台脚本中获得。本地可用i18n.getUILanguage()
。方法CCD_ 20也可能是感兴趣的。
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"
]
}
引导程序/旧版加载项的代码
//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
或ChromeWorker
,他们有很多有用的东西:
https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Functions_and_classes_available_to_workers#workerscope
我不建议仅仅为了获取导航信息而启动worker,使用Services.appshell.hiddenDOMWindow
可能会更容易,但如果你已经在使用worker,那么是的,这是一个不错的解决方案。
- 如何构建'瞬间'在非全局区域设置中工作的函数
- 分析特定区域设置(而不是时区!)中的日期字符串
- JS toLocaleString始终显示货币符号,而不考虑区域设置
- Javascript中与区域设置无关的字符串搜索
- 使用不同的日期格式和区域设置创建时刻
- 如何从区域设置获取日期格式
- 如何使日期区域设置正常工作
- 如何在时间标签和相同的类中以UTC格式重新格式化ISO 8601时间戳,并通过JavaScript根据用户区域设置和时区
- 获取网站访问者的区域设置(语言)
- 无法在文本区域设置文本
- 具体来说,JS引擎对小写和大写区域设置敏感
- 什么是区域设置字符串
- 使用区域设置更改日期格式
- 使用jQuery和PHP加载区域设置变量
- 在HTML中记住用户区域设置选择的最简单方法
- 如何将有角度的文本区域设置为仅以特定单词开头
- 根据区域设置更改 forEach
- 无法读取属性'区域设置'的未定义
- 如何正确设置jQuery日历小部件的区域设置
- i18n:在JavaScript中访问区域设置解析逻辑