简体   繁体   English

如何更改 Chrome 设置以使用 navigator.language 进行测试

[英]How can I change Chrome settings to test using navigator.language

I'm trying to internationalize an app and need to test how it behaves in other locales.我正在尝试将应用程序国际化,并需要测试它在其他语言环境中的行为。

However, navigator.language always returns en-US , even if I change language settings in both Chrome and OS X.但是, navigator.language总是返回en-US ,即使我在 Chrome 和 OS X 中更改语言设置也是如此。

What can I do to the browser to force navigator.language to return something like es-MX ?我可以对浏览器做些什么来强制navigator.language返回类似es-MX

Clarification澄清

I'm trying to make the change as a user, through the GUI, not with code.我正在尝试以用户身份进行更改,通过 GUI 而不是代码。

Use chrome://settings/languages#lang and (important) make sure that the language you selected is the top choice (the preferred language).使用Chrome://设置/语言#郎(重要),确保你选择的语言是首选(首选语言)。

More information: https://alicoding.com/detect-browser-language-preference-in-firefox-and-chrome-using-javascript/更多信息: https : //alicoding.com/detect-browser-language-preference-in-firefox-and-chrome-using-javascript/

On MacOS, the Chrome UI language ( window.navigator.language ) cannot be set via chrome://settings/languages or even via the --lang command line argument (like on Windows) or environment variables (like on Linux).在 MacOS 上,Chrome UI 语言( window.navigator.language )无法通过 chrome://settings/languages 甚至通过--lang命令行参数(如在 Windows 上)或环境变量(如在 Linux 上)设置。

The browser accept languages ( window.navigator.languages array, note the final s ) can be set via chrome://settings/languages and thus via ChromeDriver options.浏览器接受语言( window.navigator.languages数组,注意最后的s )可以通过 chrome://settings/languages 并因此通过 ChromeDriver 选项进行设置。

Therefore, besides manually changing the system language for the OS user, the only option remaining is the -AppleLanguages flag.因此,除了为操作系统用户手动更改系统语言之外,剩下的唯一选项就是-AppleLanguages标志。

For example, to Spanish:例如,西班牙语:

defaults write com.google.Chrome AppleLanguages '(es, es-ES)'

To verify:验证:

defaults read com.google.Chrome

This is the situation as of Chrome build 58.0.3029.110.这是 Chrome 版本 58.0.3029.110 的情况。

同时,有一个关于如何在 Chrome 中测试语言环境的通用描述: https : //developer.chrome.com/extensions/i18n#locales-testing

As of Sept 2021, on Mac (M1):截至 2021 年 9 月,在 Mac (M1) 上:

To change window.navigator.language :要更改window.navigator.language

  1. Settings > Advanced > Language > Add Language设置 > 高级 > 语言 > 添加语言
  2. then bring this language to the top然后将这种语言带到顶部

To change the language of the user interface:要更改用户界面的语言:

(thanks to @AdamBittlingmayer above) (感谢上面的@AdamBittlingmayer)

defaults write com.google.Chrome AppleLanguages '(es, es-ES)'

To verify:验证:

defaults read com.google.Chrome

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何模拟navigator.language以进行打字稿单元测试 - How to mock navigator.language for typescript unit test 无法模拟 navigator.language - Can't mock the navigator.language 在node-webkit中使用navigator.language吗? - Using navigator.language with node-webkit? 如何使用JavaScript获取Chrome中的OS系统语言。 不是浏览器的语言。 (navigator.language是浏览器语言!) - How javascript to get the OS System Language in Chrome. Not the browser's language. (navigator.language is the browser language!) Chrome浏览器 - navigator.language不会返回国家/地区代码 - Chrome browser - navigator.language doesn't return country code navigator.language 所有语言的列表 - navigator.language list of all languages 使用navigator.language获取用户的区域 - Getting the user's region with navigator.language 使用Sinon.js测试navigator.browserLanguage或navigator.language - Testing navigator.browserLanguage or navigator.language with Sinon.js navigator.language何时与navigator.languages [0]不同? - When will navigator.language different to navigator.languages[0]? IE11-使用navigator.language进行语言检测不起作用 - IE11 - Language Detection with navigator.language not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM