使用areIntlLocalesSupported和Browerify / webpack添加本地的正确方法是什么?

时间:2015-12-15 09:49:08

标签: javascript internationalization polyfills

我正在尝试使用Browserify对React JS应用程序进行国际化,并遵循https://github.com/andyearnshaw/Intl.js/中的示例。当我尝试加载应用程序支持的语言环境时,当浏览器不支持Intl时,我尝试这样做:

// Intl polyfill
var areIntlLocalesSupported = require('intl-locales-supported');

var localesMyAppSupports = [
/* list locales here */
   'en-US',
   'bg-BG',
   'zh-Hans-CN',
   'ha-Arab',
   'fr-FR',
   'ru-RU',
   'de-DE',
   'eu-ES'
];

是否有其他方法可以添加支持的区域设置?文档中没有其他示例。事实证明,我不想支持我想要使用的语言环境。

以下是代码的其余部分:

if (global.Intl) {
// Determine if the built-in `Intl` has the locale data we need.
if (!areIntlLocalesSupported(localesMyAppSupports)) {
    // `Intl` exists, but it doesn't have the data we need, so load the
    // polyfill and replace the constructors with need with the polyfill's.
    global.Intl = require('intl');
    Intl.NumberFormat   = IntlPolyfill.NumberFormat;
    Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
   }
 } else {
// No `Intl`, so use and load the polyfill.
global.Intl = require('intl');

require('intl/locale-data/jsonp/en-US.js');
require('intl/locale-data/jsonp/bg-BG.js');
//"zh-Hans-CN": Chinese written in simplified characters as used in China.
require('intl/locale-data/jsonp/zh-Hans-CN.js');
require('intl/locale-data/jsonp/ha-Arab.js');
require('intl/locale-data/jsonp/fr-FR.js');
require('intl/locale-data/jsonp/ru-RU.js');
require('intl/locale-data/jsonp/de-DE.js');
require('intl/locale-data/jsonp/eu-ES.js');
}

1 个答案:

答案 0 :(得分:2)

我们发现我需要在内部if块中添加带语言环境的require语句,如下所示:

21st