国际化如何在JavaScript中运行? [英] How does internationalization work in JavaScript?
问题描述
我想知道如何在JavaScript中处理国际化。我用谷歌搜索,但我没有得到令人信服的答案:
- JavaScript是否有本机支持国际化?
- JavaScript中的i18n是什么?
- 如何处理日历,货币,日期等?
我是已阅读 JavaScript内部国际化。
旧版浏览器的本地化支持很差。最初,这是由于ECMAScript语言规范中的短语看起来像这样:
Number.prototype.toLocaleString()
生成一个字符串值,该值表示根据主机环境当前区域设置的
约定格式化的Number值。这个函数是依赖于实现的,并且
是允许的,但不鼓励它返回与 toString 相同的东西。
规范中定义的每个本地化方法都被定义为依赖于实现,这会导致很多不一致。在这种情况下,Chrome Opera和Safari会返回与 .toString()
相同的内容。 Firefox和IE将返回语言环境格式的字符串,IE甚至包括千位分隔符(非常适合货币字符串)。 Chrome最近更新为返回千位分隔的字符串,但没有固定的小数。
对于现代环境, ECMAScript国际化API规范,一种补充ECMAScript语言规范的新标准,为字符串比较,数字格式化以及日期和时间格式化提供了更好的支持;它还修复了语言规范中的相应功能。可以在此处找到介绍。实施方案如下:
- Chrome 24
- Firefox 29
- Internet Explorer 11
- Opera 15
还有一个兼容性实现,< a href =http://github.com/andyearnshaw/Intl.js =noreferrer> Intl.js ,它将在尚不存在的环境中提供API。
确定用户的首选语言仍然存在问题,因为没有获取当前语言的规范。每个浏览器实现一种获取语言字符串的方法,但这可以基于用户的操作系统语言或仅基于浏览器的语言:
// IE的navigator.userLanguage,其他人的navigator.language
var lang = navigator.language || navigator.userLanguage;
一个很好的解决方法是将Accept-Language标头从服务器转储到客户端。如果格式化为JavaScript,则可以将其传递给Internationalization API构造函数,这将自动选择最佳(或首次支持的)区域设置。
简而言之,您有自己投入大量工作,或使用框架/库,因为你不能依赖浏览器为你做这件事。
用于本地化的各种库和插件: / p>
-
由开放社区(无订单):
- Intl.js - 国际化API的兼容性实现
- i18next (主页)对于i18n(包括jquery插件,翻译ui,...)
- moment.js (主页)了解日期
- numbro.js ( home )(是 numeral.js (主页))数字和货币
- l10n.js ( home )
- L10ns (
- jQuery Local isation(插件)(主页)
- YUI国际化支持
- jquery.i18Now 日期
- browser-i18n
- 对应物的灵感来自Ruby着名的 I18n gem
- jQuery Globalize jQuery自己的i18n库
- js-lingui - JS(ES2016)和React的MessageFormat实现
- 其他:
- jQuery全球化(插件)
- requirejs-i18n 使用RequireJS定义I18N捆绑包。
随意添加/编辑。
I'm wondering how to deal internationalization in JavaScript. I googled but I'm not getting convincing answers for:
- Does JavaScript have native support for internationalization?
- What is i18n in JavaScript?
- How to deal with calendars, currencies, dates, etc.?
I've already read Internationalization inside JavaScript.
Localisation support in legacy browsers is poor. Originally, this was due to phrases in the ECMAScript language spec that look like this:
Number.prototype.toLocaleString()
Produces a string value that represents the value of the Number formatted according to the conventions of the host environment’s current locale. This function is implementation-dependent, and it is permissible, but not encouraged, for it to return the same thing as toString.
Every localisation method defined in the spec is defined as "implementation-dependent", which results in a lot of inconsistencies. In this instance, Chrome Opera and Safari would return the same thing as .toString()
. Firefox and IE will return locale formatted strings, and IE even includes a thousands separator (perfect for currency strings). Chrome was recently updated to return a thousands-separated string, though with no fixed decimal.
For modern environments, the ECMAScript Internationalization API spec, a new standard that complements the ECMAScript Language spec, provides much better support for string comparison, number formatting, and date and time formatting; it also fixes the corresponding functions in the Language Spec. An introduction can be found here. Implementations are available in:
- Chrome 24
- Firefox 29
- Internet Explorer 11
- Opera 15
There is also a compatibility implementation, Intl.js, which will provide the API in environments where it doesn't already exist.
Determining the user's preferred language remains a problem, since there's no specification for obtaining the current language. Each browser implements a method to obtain a language string, but this could be based on the user's operating system language or just the language of the browser:
// navigator.userLanguage for IE, navigator.language for others
var lang = navigator.language || navigator.userLanguage;
A good workaround for this is to dump the Accept-Language header from the server to the client. If formatted as a JavaScript, it can be passed to the Internationalization API constructors, which will automatically pick the best (or first-supported) locale.
In short, you have to put in a lot of the work yourself, or use a framework/library, because you cannot rely on the browser to do it for you.
Various libraries and plugins for localisation:
Mantained by an open community (no order):
- Intl.js - a compatibility implementation of the Internationalisation API
- i18next (home) for i18n (incl. jquery plugin, translation ui,...)
- moment.js (home) for dates
- numbro.js (home) (was numeral.js (home)) for numbers and currency
- l10n.js (home)
- L10ns (home) tool for i18n workflow and complex string formatting
- jQuery Localisation (plugin) (home)
- YUI Internationalization support
- jquery.i18Now for dates
- browser-i18n with support to pluralization
- counterpart is inspired by Ruby's famous I18n gem
- jQuery Globalize jQuery's own i18n library
- js-lingui - MessageFormat implementation for JS (ES2016) and React
- Others:
- jQuery Globalization (plugin)
- requirejs-i18n Define an I18N Bundle with RequireJS.
Feel free to add/edit.
这篇关于国际化如何在JavaScript中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!