JavaScript 中的国际化是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3084675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:14:01  来源:igfitidea点击:

How does internationalization work in JavaScript?

javascriptinternationalization

提问by rajakvk

I'm wondering how to deal internationalization in JavaScript. I googled but I'm not getting convincing answers for:

我想知道如何在 JavaScript 中处理国际化。我用谷歌搜索但我没有得到令人信服的答案:

  • Does JavaScript have native support for internationalization?
  • What is i18n in JavaScript?
  • How to deal with calendars, currencies, dates, etc.?
  • JavaScript 是否具有对国际化的本机支持?
  • JavaScript 中的 i18n 是什么?
  • 如何处理日历、货币、日期等?

I've already read Internationalization inside JavaScript.

我已经阅读了JavaScript 中的国际化

回答by Andy E

Localisation support in legacy browsers is poor. Originally, this was due to phrases in the ECMAScript language spec that look like this:

传统浏览器中的本地化支持很差。最初,这是由于 ECMAScript 语言规范中的短语如下所示:

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.

Number.prototype.toLocaleString()
生成一个字符串值,该值表示根据主机环境当前语言环境的约定格式化的 Number 值。这个函数是依赖于实现的,允许但不鼓励它返回与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.

规范中定义的每个本地化方法都被定义为“依赖于实现”,这导致了很多不一致。在这种情况下,Chrome Opera 和 Safari 将返回与.toString(). Firefox 和 IE 将返回区域设置格式的字符串,IE 甚至包含千位分隔符(非常适合货币字符串)。Chrome 最近更新为返回以千位分隔的字符串,但没有固定的小数点。

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:

对于现代环境ECMAScript 国际化 API 规范是补充 ECMAScript 语言规范的新标准,为字符串比较、数字格式和日期和时间格式提供了更好的支持;它还修复了语言规范中的相应功能。可以在此处找到介绍。实现可用于:

  • Chrome 24
  • Firefox 29
  • Internet Explorer 11
  • Opera 15
  • 铬 24
  • 火狐 29
  • 浏览器 11
  • 歌剧15

There is also a compatibility implementation, Intl.js, which will provide the API in environments where it doesn't already exist.

还有一个兼容性实现Intl.js,它将在尚不存在的环境中提供 API。

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.

一个很好的解决方法是将 Accept-Language 标头从服务器转储到客户端。如果格式化为 JavaScript,则可以将其传递给 Internationalization API 构造函数,该构造函数将自动选择最佳(或第一个支持的)语言环境。

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:

用于本地化的各种库和插件:

Feel free to add/edit.

随意添加/编辑。

回答by ashwoods

Mozilla recently released the awesome L20n or localization 2.0. In their own words L20n is

Mozilla 最近发布了很棒的 L20n 或本地化 2.0。用他们自己的话说 L20n 是

an open source, localization-specific scripting language used to process gender, plurals, conjugations, and most of the other quirky elements of natural language.

一种开源的、特定于本地化的脚本语言,用于处理性别、复数、变形和自然语言的大多数其他古怪元素。

Their js implementation is on the github L20n repository.

他们的 js 实现位于github L20n 存储库中。

回答by Luca Matteis

Some of it is native, the rest is available through libraries.

其中一些是本地的,其余的可以通过库获得。

For example Datejsis a good international date library.

例如Datejs是一个很好的国际日期库。

For the rest, it's just about language translation, and JavaScript is natively Unicode compatible (as well as all major browsers).

其余的只是语言翻译,而 JavaScript 本身就与 Unicode 兼容(以及所有主要浏览器)。

回答by Amir E. Aharoni

You can also try another library - https://github.com/wikimedia/jquery.i18n.

您还可以尝试另一个库 - https://github.com/wikimedia/jquery.i18n

In addition to parameter replacement and multiple plural forms, it has support for gender a rather unique feature of custom grammar rules that some languages need.

除了参数替换和多个复数形式外,它还支持性别,这是某些语言需要的自定义语法规则的一个相当独特的功能。