[英]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) 上:
window.navigator.language
:window.navigator.language
: (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.