javascript - 将默认语言环境设置为国际化 API?

标签 javascript localization internationalization

ECMAScript 国际化 API 使货币、日期、时间和其他值的本地化成为可能。这里有一个小例子:

var date = new Date();

alert(new Intl.DateTimeFormat('en-US').format(date));
alert(new Intl.DateTimeFormat('de-CH').format(date));

如何可以覆盖浏览器的默认区域设置,从而可以在站点范围内设置区域设置?目前我有帮助程序,它包装了 Intl API 并设置了本地。有更简单的解决方案吗?

最佳答案

由于潜在的安全风险和对嵌入式应用程序的潜在干扰,您可以使用自己的 API 包装 API 的正确方法,并且没有设置默认区域设置的标准方法。

引用 this comprehensive article在国际化 API 上,关于公开允许设置默认语言环境的 API,作者有这样的话:

Two issues prevent this: First, a settable default locale list would create a global communication channel between different scripts running within the same environment, which is considered a security risk. Second, an application may include different components, such as embedded apps, that need different default locales. ECMAScript has no knowledge of these components and no way to manage appropriate contexts for them. We decided therefore that a default locale is better left to higher-level systems. For example, the YUI library already includes an Intl module which manages a list of requested locales that is scoped to the containing YUI object and used for loading resource bundles. This module could easily be modified to keep a locale list object so that it can be used as a default within the scope of the containing YUI object.

此外,如果您查看 official specification ,默认语言环境是特定于实现的。目前,还没有浏览器公开 API 来设置默认语言环境。

关于javascript - 将默认语言环境设置为国际化 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47734486/

相关文章:

javascript - AngularJS 指令创建 OnTheFly 文本框和验证

asp.net - 从 asp.net 4 中的资源文件获取文本

python - Django 模型翻译 - 无法导入名称 'Constraint'

javascript - 在 javascript 文件中处理 i18n

ruby-on-rails-3 - Rails 如何翻译表单输入 file_field ( i18n ) 上的标题

javascript - 在html文本框中显示JS变量

javascript - VB.NET - WebBrowser 不显示 html 源代码中由 javascript 创建的 html 元素

javascript - 如何使用jQuery获得 parent parent child 的值(value)?

xcode - 在 xcode 3.2.3 中为 iPhone 本地化图像时出现问题

iphone - 可以使用 NSLocalizedString 作为字典中的键吗?