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

2022-01-18 00:00:00 internationalization javascript

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

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

  • JavaScript 是否具有对国际化的原生支持?
  • 什么是 JavaScript 中的 i18n?
  • 如何处理日历、货币、日期等?

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

推荐答案

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

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

Number.prototype.toLocaleString()
生成一个字符串值,该值表示根据格式化的 Number 的值宿主环境当前语言环境的约定.这个函数是依赖于实现的,并且允许但不鼓励它返回与 toString 相同的内容.

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

Every localization 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 thousand separator (perfect for currency strings). Chrome was recently updated to return a thousands-separated string, though with no fixed decimal.

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

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 the 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
  • 火狐 29
  • Internet Explorer 11
  • 歌剧 15

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

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;

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

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.

简而言之,您必须自己投入大量工作,或者使用 framework/library,因为你不能依赖浏览器为你做这件事.

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 localization:

  • 由开放社区维护(无顺序):
  • Mantained by an open community (no order):
  • Polyglot.js - AirBnb 的国际化库
  • Intl.js - 国际化 API 的兼容性实现
  • i18next (home) 用于 i18n(包括 jquery 插件、翻译 ui、...)
  • moment.js (home) 日期
  • numbro.js (home) (原为 numeral.js (home)) 用于数字和货币
  • l10n.js (主页)
  • L10ns (home) 用于 i18n 工作流程和复杂字符串格式化的工具
  • jQuery 本地化(插件) (主页)
  • YUI 国际化支持
  • jquery.i18Now 日期
  • browser-i18n 支持复数形式
  • counterpart 的灵感来自于 Ruby 著名的 I18n gem
  • jQuery Globalize jQuery 自己的 i18n 库
  • js-lingui - JS (ES2016) 和 React 的 MessageFormat 实现
  • Polyglot.js - AirBnb's internationalization library
  • 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
  • 其他:
  • jQuery 全球化 (插件)
  • requirejs-i18n 使用 RequireJS 定义 I18N 包.
  • jQuery Globalization (plugin)
  • requirejs-i18n Define an I18N Bundle with RequireJS.

随意添加/编辑.

相关文章