使用areIntlLocalesSupported和Browserify/webpack添加本地程序的正确方法是什么

What is the correct way to add locals using areIntlLocalesSupported and Browerify/webpack?

本文关键字:程序 是什么 方法 areIntlLocalesSupported Browserify 添加 webpack 使用      更新时间:2023-09-26

我正在尝试使用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');
}

我们刚刚发现,我需要在内部if块中添加带有区域设置的require语句,如下所示:

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;
   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');
 }