区域设置:浏览器或操作系统设置?
当“语言环境”设置为日语时,这意味着什么?这是否意味着浏览器已设置为可以识别日语字符?或者,它与操作系统设置更相关吗?
与 i18n(国际化和本地化)相关,我们如何使用 JavaScript 检测访问我们网站的用户是否具有日语语言环境?下面的简单检查就足够了吗?
var userLocale = navigator.language || navigator.userLanguage;
if (userLocale.toLowerCase() == 'ja-jp') { ... }
日语语言环境浏览器是否可以返回 ja-jp
之外的其他内容?
预先感谢。
What does it mean when a "locale" is set to e.g. Japanese? Does it mean the browser is set to be ready to recognize Japanese characters? Or, is it more related to an OS setting?
Related to i18n (internationalization and localization), how do we detect that a user who is visiting our site has a e.g. Japanese locale using JavaScript? Will the following simple check suffice?
var userLocale = navigator.language || navigator.userLanguage;
if (userLocale.toLowerCase() == 'ja-jp') { ... }
Can a Japanese locale browser return something else rather than ja-jp
?
Thanks beforehand.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先我们需要定义什么是 Locale。在您使用的上下文中,它是一个 ISO639 语言标识符,可选后跟 ISO3166 国家/地区标识符。这用于确定最终用户的偏好(例如本地化内容的语言或日期和时间格式以及数字格式)。
现在,可以在多个地方设置区域设置。操作系统通常有多种设置,例如键盘布局、格式首选项、语言首选项、代码页(对于非 Unicode 程序)等。
除此之外,网络浏览器通常允许您选择自己的偏好(Safari 是一个例外)。这些首选项通过 HTTP Accept-Language 标头与每个请求一起发送到 Web 服务器。这是您应该以某种方式在服务器端阅读的内容(不幸的是,这意味着 PHP、C#、Java 等中的一些服务器端代码),并且可能会传递到您的客户端脚本。
我必须在这里声明,像 navigator.language 这样的东西不是正确的选择,原因有两个:
这不是跨浏览器兼容的,也就是说其他 Web 浏览器需要不同的代码。也就是说,如果它们首先允许读取此信息。
此设置通常指的是网络浏览器的语言(网络浏览器的用户界面翻译成的语言),与实际用户的偏好无关。
所以回答你的问题:不,这张支票是不够的。
First we need to define what Locale is. In the context you are using it is a ISO639 Language Identifier optionally followed by ISO3166 Country Identifier. This is used to determine end user's preferences (like language of localized content or date and time formats and number formats).
Now, Locale could be set in multiple places. OS usually has multiple settings, for example keyboard layout, formatting preferences, language preferences, code pages (for non-Unicode programs), etc.
Apart from that, web browsers usually allow you to choose your own preferences (Safari is an exception here). These preferences are send along with each request to the web server, via HTTP Accept-Language header. This is something you should somehow read on the server side (which unfortunately means some server-side code in PHP, C#, Java, whatever) and maybe passed to your client-side script.
I must state here that something like navigator.language is not the way to go for two reasons:
This is not cross-browser compatible, that is other web browsers would require different code. That is if they allow reading this information in the first place.
This setting usually refers to web browser's language (the language web browser's user interface is translated to) and has nothing to do with actual user's preference.
So to answer your question: no, this check will not suffice.
这主要是操作系统/浏览器设置,控制例如操作系统的语言、日期/时间格式、小数点/逗号等——换句话说,设置在世界不同区域中有所不同。这与字体/字符支持没有直接关系。
据我所知,
ja-jp
是唯一标准化的日语语言环境/语言组合;但正如 @Siku-siku.com 在评论中建议的那样“(基于使用日语版 IE/WinXP 的实际测试)还要检查ja
因为这是 IE 语言设置中的可用选项之一。 ”That is mostly an OS/browser setting, controlling e.g. language of the OS, date/time format, decimal point/comma, etc. - in other words, settings that vary in different locales of the world. This has no direct correlation with font/character support.
As far as I know,
ja-jp
is the only standardized Japanese locale/language combination; but as @Siku-siku.com suggested in the comments "(based on real testings using Japanese version of IE/WinXP) also check forja
because that's one of the available options in the IE language setting."