【问题标题】:Get browser locale code in Firefox add-on在 Firefox 插件中获取浏览器区域设置代码
【发布时间】:2016-08-06 05:54:04
【问题描述】:

我正在开发一个 Firefox 插件。我需要确定用户浏览器中设置的语言。
是否有像window.navigator 这样的对象,其中包含浏览器的当前语言环境?

【问题讨论】:

标签: javascript firefox firefox-addon firefox-addon-sdk firefox-addon-webextensions


【解决方案1】:

访问语言环境的方法

使用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);

【讨论】:

  • 它是附加 SDK。抱歉,我不明白,我如何使用preferences/service 访问语言环境?我已经导入了sdk/preferences/service,但是我应该向它请求哪个道具?
  • 另外,我已经尝试过window.navigator,如果我在index.js 中请求它,它是未定义的事件。
  • @stkvtflw,window.navigator 对您不起作用的原因是您没有定义window,或者您将window 指向错误的&lt;window&gt;。刚开始使用扩展(尤其是 Firefox 扩展)的人们最困惑的一件事是,无论他们试图完成什么,都需要将window 设置为正确的&lt;window&gt;。从附加组件的角度来看,有很多 &lt;window&gt; 对象可以使用。这与为网页脚本定义的单个window非常不同。
  • @stkvtflw,我更新了这个,添加了更多描述并测试了所有类型的 Firefox 插件的代码。
  • 感谢您的回答!但我稍微简单地解决了这个问题:require("sdk/window/utils").getMostRecentBrowserWindow().getLocale() - 仅此而已。
【解决方案2】:

@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,那么它是一个不错的解决方案。

【讨论】:

  • 您链接的页面明确指出WorkerNavigator 不支持NavigatorLanguage。但是,WorkerNavigator 页面显示NavigatorLanguage.language 可用。你测试过吗。如果是这样,让我们​​将“Web Workers 可用的函数和类”页面更改为部分支持,而不是“不支持”(红色文本)。
  • 感谢@Mayken 的收获。我不知道文档是这么说的。我刚刚测试了它,它似乎完全可用 - i.imgur.com/mEqovfM.png - 你能从我的截图中验证一下吗?
  • 感谢您的测试,@Noitidart。看起来它对我来说是支持的。我在MDN页面Functions and classes available to Web Workers上把它从{{CompatNo}}改成了{{CompatVersionUnknown}}
  • 非常感谢@Mayken!如果我知道版本号,我会用它更新它并让你知道。
猜你喜欢
  • 1970-01-01
  • 2015-06-18
  • 2013-06-05
  • 1970-01-01
  • 1970-01-01
  • 2011-01-26
  • 1970-01-01
  • 2012-01-25
  • 2016-10-12
相关资源
最近更新 更多