gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:55:27 25 4
gpt4 key购买 nike

我正在尝试使用 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 block 中添加带有语言环境的 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');
}

关于javascript - 使用 areIntlLocalesSupported 和 Browerify/webpack 添加本地人的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34285852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com