@chaoswise/intl 2.1.2 → 2.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/scripts/addLocale.js +21 -6
- package/bin/scripts/util/makeVisitorUpdate.js +46 -19
- package/bin/scripts/util/transformAst.js +3 -0
- package/lib/index.js +1 -1
- package/lib/react-intl-universal/ReactIntlUniversal.js +329 -0
- package/lib/react-intl-universal/constants.js +22 -0
- package/lib/react-intl-universal/index.js +35 -0
- package/lib/useIntl/index.js +1 -1
- package/package.json +2 -2
package/bin/scripts/addLocale.js
CHANGED
|
@@ -4,6 +4,7 @@ const transformAst = require('./util/transformAst');
|
|
|
4
4
|
const getWordsByIds = require('./util/getWordsByIds');
|
|
5
5
|
const log = require('./util/log');
|
|
6
6
|
const genXlsx = require('./util/genXlsx');
|
|
7
|
+
const service = require('./service');
|
|
7
8
|
|
|
8
9
|
async function update() {
|
|
9
10
|
log.info('收集全部词条...');
|
|
@@ -15,19 +16,33 @@ async function update() {
|
|
|
15
16
|
// 获取全部词条
|
|
16
17
|
const allWords = await getWordsByIds(_downloadIds, conf);
|
|
17
18
|
|
|
19
|
+
// 获取所有语种
|
|
20
|
+
const languageRes = await service.getLanguage();
|
|
21
|
+
if (languageRes.code !== 10000) {
|
|
22
|
+
log.error('请求数据出错:' + languageRes.msg);
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
const languages = languageRes.data;
|
|
26
|
+
const mainLanguage = languages.filter((l) => l.isMain)[0];
|
|
27
|
+
|
|
18
28
|
const localeKeys = Object.keys(allWords);
|
|
19
|
-
const xlsxData = _downloadIds.map(id => {
|
|
29
|
+
const xlsxData = _downloadIds.map((id) => {
|
|
20
30
|
const localeObj = {};
|
|
21
|
-
localeKeys.forEach(key => {
|
|
22
|
-
|
|
31
|
+
localeKeys.forEach((key) => {
|
|
32
|
+
if (mainLanguage && key === mainLanguage.key) {
|
|
33
|
+
localeObj[key] =
|
|
34
|
+
allWords[key][id] || info.defaultKeyWordMap[id] || '待翻译';
|
|
35
|
+
} else {
|
|
36
|
+
localeObj[key] = allWords[key][id] || '待翻译';
|
|
37
|
+
}
|
|
23
38
|
});
|
|
24
39
|
return {
|
|
25
40
|
id,
|
|
26
|
-
...localeObj
|
|
27
|
-
}
|
|
41
|
+
...localeObj,
|
|
42
|
+
};
|
|
28
43
|
});
|
|
29
44
|
|
|
30
|
-
await genXlsx(xlsxData)
|
|
45
|
+
await genXlsx(xlsxData);
|
|
31
46
|
}
|
|
32
47
|
|
|
33
48
|
module.exports = update;
|
|
@@ -2,21 +2,14 @@ const t = require('@babel/types');
|
|
|
2
2
|
const specialMatch = require('./specialMatch');
|
|
3
3
|
|
|
4
4
|
module.exports = function f(
|
|
5
|
-
{
|
|
6
|
-
i18nObject,
|
|
7
|
-
i18nMethod,
|
|
8
|
-
importCode,
|
|
9
|
-
ignoreLines,
|
|
10
|
-
i18nDefaultFunctionKey,
|
|
11
|
-
},
|
|
5
|
+
{ i18nObject, i18nMethod, importCode, ignoreLines, i18nDefaultFunctionKey },
|
|
12
6
|
returns,
|
|
13
|
-
{ special }
|
|
7
|
+
{ special }
|
|
14
8
|
) {
|
|
15
|
-
const { replaceWords, downloadIds } = returns;
|
|
9
|
+
const { replaceWords, downloadIds, defaultKeyWordMap } = returns;
|
|
16
10
|
|
|
17
11
|
// XXX: [TRICKY] 防止中文转码为 unicode
|
|
18
12
|
function hackValue(value, id) {
|
|
19
|
-
|
|
20
13
|
return Object.assign(t.StringLiteral(value), {
|
|
21
14
|
extra: {
|
|
22
15
|
raw: `'${id}'`, // id
|
|
@@ -64,7 +57,6 @@ module.exports = function f(
|
|
|
64
57
|
|
|
65
58
|
// 替换字符串为我们使用的国际化API
|
|
66
59
|
function makeReplace(value, variables) {
|
|
67
|
-
|
|
68
60
|
// 存在临时id替换
|
|
69
61
|
returns.hasTouch = true;
|
|
70
62
|
|
|
@@ -90,25 +82,54 @@ module.exports = function f(
|
|
|
90
82
|
|
|
91
83
|
// 函数或方法调用表达式
|
|
92
84
|
function dealCallExpressionNode(node) {
|
|
85
|
+
// 记录当前key对应的默认词条
|
|
86
|
+
if (
|
|
87
|
+
node.callee.type === 'MemberExpression' &&
|
|
88
|
+
node.callee.object.type === 'CallExpression' &&
|
|
89
|
+
node.callee.property.name === i18nDefaultFunctionKey &&
|
|
90
|
+
((node.callee.object.callee.type === 'MemberExpression' &&
|
|
91
|
+
node.callee.object.callee.object.name === i18nObject &&
|
|
92
|
+
node.callee.object.callee.property.name === i18nMethod) ||
|
|
93
|
+
(!i18nObject &&
|
|
94
|
+
node.callee.object.callee.type === 'Identifier' &&
|
|
95
|
+
node.callee.object.callee.name === i18nMethod))
|
|
96
|
+
) {
|
|
97
|
+
if (
|
|
98
|
+
node.arguments.length > 0 &&
|
|
99
|
+
node.callee.object.arguments.length > 0
|
|
100
|
+
) {
|
|
101
|
+
const k = node.callee.object.arguments[0].value;
|
|
102
|
+
const v = node.arguments[0].value;
|
|
103
|
+
|
|
104
|
+
if (k) {
|
|
105
|
+
defaultKeyWordMap[k] = v;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
93
110
|
const res = {
|
|
94
111
|
needReplace: false,
|
|
95
112
|
value: null,
|
|
96
113
|
skip: false,
|
|
97
|
-
|
|
114
|
+
otherArgs: [],
|
|
115
|
+
};
|
|
98
116
|
|
|
99
117
|
if (
|
|
100
118
|
(node.callee.type === 'MemberExpression' &&
|
|
101
119
|
node.callee.object.name === i18nObject &&
|
|
102
120
|
node.callee.property.name === i18nMethod) ||
|
|
103
|
-
(!i18nObject &&
|
|
121
|
+
(!i18nObject &&
|
|
122
|
+
node.callee.type === 'Identifier' &&
|
|
123
|
+
node.callee.name === i18nMethod)
|
|
104
124
|
) {
|
|
105
125
|
// 收集现有的 key
|
|
106
126
|
const args = node.arguments;
|
|
107
|
-
if(!args.length) {
|
|
127
|
+
if (!args.length) {
|
|
108
128
|
return res;
|
|
109
129
|
}
|
|
110
130
|
|
|
111
131
|
let key = args[0].value;
|
|
132
|
+
res.otherArgs = args.slice(1);
|
|
112
133
|
|
|
113
134
|
// intl.t(key)
|
|
114
135
|
// replaceWords { key: id }
|
|
@@ -137,16 +158,21 @@ module.exports = function f(
|
|
|
137
158
|
|
|
138
159
|
if (!shouldIgnore(node) && special) {
|
|
139
160
|
// intl.get('id').d(中文) 获取id和中文
|
|
161
|
+
// TODO: 当前get()方法不支持处理有参数的场景[get('id',{a:1})],如需处理需要同时修改脚手架中的react-router-config-loader
|
|
140
162
|
const { id, defaultValue } = specialMatch(value) || {};
|
|
141
163
|
if (!id) return;
|
|
142
164
|
|
|
165
|
+
defaultKeyWordMap[id] = defaultValue;
|
|
166
|
+
|
|
143
167
|
const finalyId = replaceWords[id];
|
|
144
168
|
if (finalyId) {
|
|
145
169
|
returns.hasTouch = true;
|
|
146
170
|
downloadIds.push(finalyId);
|
|
147
171
|
|
|
148
|
-
const str = `${
|
|
149
|
-
|
|
172
|
+
const str = `${
|
|
173
|
+
i18nObject ? `${i18nObject}.` : ''
|
|
174
|
+
}${i18nMethod}("${finalyId}").${i18nDefaultFunctionKey}("${defaultValue}")`;
|
|
175
|
+
const newStr = hackValue(str, str);
|
|
150
176
|
path.replaceWith(newStr);
|
|
151
177
|
path.skip();
|
|
152
178
|
} else {
|
|
@@ -169,12 +195,13 @@ module.exports = function f(
|
|
|
169
195
|
// 函数或方法调用表达式,比如:a('中午')
|
|
170
196
|
CallExpression(path) {
|
|
171
197
|
const { node } = path;
|
|
172
|
-
|
|
173
198
|
if (!node.ignore) {
|
|
174
|
-
const { needReplace, value, skip } =
|
|
199
|
+
const { needReplace, value, skip, otherArgs } =
|
|
200
|
+
dealCallExpressionNode(node);
|
|
175
201
|
|
|
176
202
|
if (needReplace) {
|
|
177
203
|
const res = makeReplace(value);
|
|
204
|
+
res.arguments = res.arguments.concat(otherArgs);
|
|
178
205
|
path.replaceWith(res);
|
|
179
206
|
|
|
180
207
|
path.node.ignore = true;
|
|
@@ -183,4 +210,4 @@ module.exports = function f(
|
|
|
183
210
|
}
|
|
184
211
|
},
|
|
185
212
|
};
|
|
186
|
-
}
|
|
213
|
+
};
|
|
@@ -82,6 +82,7 @@ module.exports = function (type, files = [], conf = {}, replaceWords) {
|
|
|
82
82
|
const allWords = []; // 收集到的所有词条
|
|
83
83
|
const downloadIds = []; // 需要从平台下载的词条id
|
|
84
84
|
const specialMethod = []; // 替换的国际化特殊方法
|
|
85
|
+
const defaultKeyWordMap = {}; // addLocal指令中使用:记录当前key对应的默认词条
|
|
85
86
|
|
|
86
87
|
// babel配置
|
|
87
88
|
const transformOptions = {
|
|
@@ -129,6 +130,7 @@ module.exports = function (type, files = [], conf = {}, replaceWords) {
|
|
|
129
130
|
replaceWords: replaceWords || {},
|
|
130
131
|
downloadIds,
|
|
131
132
|
specialMethod,
|
|
133
|
+
defaultKeyWordMap,
|
|
132
134
|
hasImport: false, // 是否已经引入通用国际化API,import {init} from ...;
|
|
133
135
|
hasTouch: false, // 是否存在需要替换的词条
|
|
134
136
|
};
|
|
@@ -200,5 +202,6 @@ module.exports = function (type, files = [], conf = {}, replaceWords) {
|
|
|
200
202
|
allWords,
|
|
201
203
|
downloadIds,
|
|
202
204
|
specialMethod,
|
|
205
|
+
defaultKeyWordMap
|
|
203
206
|
};
|
|
204
207
|
};
|
package/lib/index.js
CHANGED
|
@@ -25,5 +25,5 @@ _Object$defineProperty(exports, "useIntl", {
|
|
|
25
25
|
});
|
|
26
26
|
var _useIntl = _interopRequireDefault(require("./useIntl"));
|
|
27
27
|
var _context = _interopRequireDefault(require("./useIntl/context"));
|
|
28
|
-
var _reactIntlUniversal = _interopRequireDefault(require("react-intl-universal"));
|
|
28
|
+
var _reactIntlUniversal = _interopRequireDefault(require("./react-intl-universal"));
|
|
29
29
|
_reactIntlUniversal["default"].options.escapeHtml = false;
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _typeof = require("@babel/runtime-corejs3/helpers/typeof");
|
|
4
|
+
var _WeakMap = require("@babel/runtime-corejs3/core-js-stable/weak-map");
|
|
5
|
+
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
|
|
6
|
+
var _Object$getOwnPropertyDescriptor = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor");
|
|
7
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
|
|
8
|
+
_Object$defineProperty(exports, "__esModule", {
|
|
9
|
+
value: true
|
|
10
|
+
});
|
|
11
|
+
exports["default"] = void 0;
|
|
12
|
+
var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
|
|
13
|
+
var _assign = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/assign"));
|
|
14
|
+
var _indexOf = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/index-of"));
|
|
15
|
+
var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));
|
|
16
|
+
var _urlSearchParams = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/url-search-params"));
|
|
17
|
+
var _reduce = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/reduce"));
|
|
18
|
+
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
|
|
19
|
+
var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
|
|
20
|
+
var _react = _interopRequireDefault(require("react"));
|
|
21
|
+
var _intlMessageformat = _interopRequireDefault(require("intl-messageformat"));
|
|
22
|
+
var _escapeHtml = _interopRequireDefault(require("escape-html"));
|
|
23
|
+
var _cookie = _interopRequireDefault(require("cookie"));
|
|
24
|
+
var _invariant = _interopRequireDefault(require("invariant"));
|
|
25
|
+
var constants = _interopRequireWildcard(require("./constants"));
|
|
26
|
+
var _lodash = _interopRequireDefault(require("lodash.merge"));
|
|
27
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof _WeakMap) return null; var r = new _WeakMap(), t = new _WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
|
|
28
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = _Object$defineProperty && _Object$getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? _Object$getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? _Object$defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
|
|
29
|
+
String.prototype.defaultMessage = String.prototype.d = function (msg) {
|
|
30
|
+
return this || msg || "";
|
|
31
|
+
};
|
|
32
|
+
var ReactIntlUniversal = /*#__PURE__*/function () {
|
|
33
|
+
function ReactIntlUniversal() {
|
|
34
|
+
(0, _classCallCheck2["default"])(this, ReactIntlUniversal);
|
|
35
|
+
this.options = {
|
|
36
|
+
currentLocale: null,
|
|
37
|
+
// Current locale such as 'en-US'
|
|
38
|
+
locales: {},
|
|
39
|
+
// app locale data like {"en-US":{"key1":"value1"},"zh-CN":{"key1":"值1"}}
|
|
40
|
+
warningHandler: function warn() {
|
|
41
|
+
var _console;
|
|
42
|
+
(_console = console).warn.apply(_console, arguments);
|
|
43
|
+
},
|
|
44
|
+
// ability to accumulate missing messages using third party services
|
|
45
|
+
escapeHtml: true,
|
|
46
|
+
// disable escape html in variable mode
|
|
47
|
+
fallbackLocale: null,
|
|
48
|
+
// Locale to use if a key is not found in the current locale
|
|
49
|
+
debug: false,
|
|
50
|
+
// If debugger mode is on, the message will be wrapped by a span
|
|
51
|
+
dataKey: 'data-i18n-key' // If debugger mode is on, the message will be wrapped by a span with this data key
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Get the formatted message by key
|
|
57
|
+
* @param {string} key The string representing key in locale data file
|
|
58
|
+
* @param {Object} variables Variables in message
|
|
59
|
+
* @returns {string} message
|
|
60
|
+
*/
|
|
61
|
+
(0, _createClass2["default"])(ReactIntlUniversal, [{
|
|
62
|
+
key: "_getFormattedMessage",
|
|
63
|
+
value: function _getFormattedMessage(key, variables) {
|
|
64
|
+
if (this.options.intlGetHook) {
|
|
65
|
+
try {
|
|
66
|
+
this.options.intlGetHook(key, this.options.currentLocale);
|
|
67
|
+
} catch (e) {
|
|
68
|
+
console.log('intl get hook error: ', e);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
(0, _invariant["default"])(key, "key is required");
|
|
72
|
+
var _this$options = this.options,
|
|
73
|
+
locales = _this$options.locales,
|
|
74
|
+
currentLocale = _this$options.currentLocale,
|
|
75
|
+
formats = _this$options.formats;
|
|
76
|
+
|
|
77
|
+
// 1. check if the locale data and key exists
|
|
78
|
+
if (!locales || !locales[currentLocale]) {
|
|
79
|
+
var errorMsg = "react-intl-universal locales data \"".concat(currentLocale, "\" not exists.");
|
|
80
|
+
if (!currentLocale) {
|
|
81
|
+
errorMsg += ' More info: https://github.com/alibaba/react-intl-universal/issues/144#issuecomment-1345193138';
|
|
82
|
+
}
|
|
83
|
+
this.options.warningHandler(errorMsg);
|
|
84
|
+
return "";
|
|
85
|
+
}
|
|
86
|
+
var msg = this.getDescendantProp(locales[currentLocale], key);
|
|
87
|
+
if (msg == null) {
|
|
88
|
+
if (this.options.fallbackLocale) {
|
|
89
|
+
msg = this.getDescendantProp(locales[this.options.fallbackLocale], key);
|
|
90
|
+
if (msg == null) {
|
|
91
|
+
var _context, _context2;
|
|
92
|
+
this.options.warningHandler((0, _concat["default"])(_context = (0, _concat["default"])(_context2 = "react-intl-universal key \"".concat(key, "\" not defined in ")).call(_context2, currentLocale, " or the fallback locale, ")).call(_context, this.options.fallbackLocale));
|
|
93
|
+
return "";
|
|
94
|
+
}
|
|
95
|
+
} else {
|
|
96
|
+
var _context3;
|
|
97
|
+
this.options.warningHandler((0, _concat["default"])(_context3 = "react-intl-universal key \"".concat(key, "\" not defined in ")).call(_context3, currentLocale));
|
|
98
|
+
return "";
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// Fixed 处理翻译为空串的时候显示.d()中默认翻译的情况
|
|
102
|
+
if (msg == "") {
|
|
103
|
+
var nullStr = new String("");
|
|
104
|
+
nullStr.defaultMessage = nullStr.d = function () {
|
|
105
|
+
return "";
|
|
106
|
+
};
|
|
107
|
+
return nullStr;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 2. handle security issue for variables
|
|
111
|
+
if (variables) {
|
|
112
|
+
variables = (0, _assign["default"])({}, variables);
|
|
113
|
+
// HTML message with variables. Escape it to avoid XSS attack.
|
|
114
|
+
for (var i in variables) {
|
|
115
|
+
var value = variables[i];
|
|
116
|
+
if (this.options.escapeHtml === true && (typeof value === "string" || value instanceof String) && (0, _indexOf["default"])(value).call(value, "<") >= 0) {
|
|
117
|
+
value = (0, _escapeHtml["default"])(value);
|
|
118
|
+
}
|
|
119
|
+
variables[i] = value;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 3. resolve variables
|
|
124
|
+
try {
|
|
125
|
+
var finalMsg;
|
|
126
|
+
if (variables) {
|
|
127
|
+
// format message with variables
|
|
128
|
+
var msgFormatter = new _intlMessageformat["default"](msg, currentLocale, formats);
|
|
129
|
+
finalMsg = msgFormatter.format(variables);
|
|
130
|
+
} else {
|
|
131
|
+
// no variables, just return the message
|
|
132
|
+
finalMsg = msg;
|
|
133
|
+
}
|
|
134
|
+
return finalMsg;
|
|
135
|
+
} catch (err) {
|
|
136
|
+
this.options.warningHandler("react-intl-universal format message failed for key='".concat(key, "'."), err.message);
|
|
137
|
+
return msg;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Get the formatted message by key
|
|
143
|
+
* @param {string} key The string representing key in locale data file
|
|
144
|
+
* @param {Object} [variables] Variables in message
|
|
145
|
+
* @returns {string} message
|
|
146
|
+
*/
|
|
147
|
+
}, {
|
|
148
|
+
key: "get",
|
|
149
|
+
value: function get(key, variables) {
|
|
150
|
+
var msg = this._getFormattedMessage(key, variables);
|
|
151
|
+
return this.options.debug ? this._getSpanElementMessage(key, msg) : msg;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Get the formatted html message by key.
|
|
156
|
+
* @param {string} key The string representing key in locale data file
|
|
157
|
+
* @param {Object} [variables] Variables in message
|
|
158
|
+
* @returns {React.ReactElement} html message
|
|
159
|
+
*/
|
|
160
|
+
}, {
|
|
161
|
+
key: "getHTML",
|
|
162
|
+
value: function getHTML(key, variables) {
|
|
163
|
+
var msg = this._getFormattedMessage(key, variables);
|
|
164
|
+
if (msg) {
|
|
165
|
+
return this._getSpanElementMessage(key, msg);
|
|
166
|
+
}
|
|
167
|
+
return "";
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* As same as get(...) API
|
|
172
|
+
* @param {Object} options
|
|
173
|
+
* @param {string} options.id
|
|
174
|
+
* @param {string} options.defaultMessage
|
|
175
|
+
* @param {Object} variables Variables in message
|
|
176
|
+
* @returns {string} message
|
|
177
|
+
*/
|
|
178
|
+
}, {
|
|
179
|
+
key: "formatMessage",
|
|
180
|
+
value: function formatMessage(messageDescriptor, variables) {
|
|
181
|
+
var id = messageDescriptor.id,
|
|
182
|
+
defaultMessage = messageDescriptor.defaultMessage;
|
|
183
|
+
return this.get(id, variables).defaultMessage(defaultMessage);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* As same as getHTML(...) API
|
|
188
|
+
* @param {Object} options
|
|
189
|
+
* @param {string} options.id
|
|
190
|
+
* @param {React.Element} options.defaultMessage
|
|
191
|
+
* @param {Object} variables Variables in message
|
|
192
|
+
* @returns {React.Element} message
|
|
193
|
+
*/
|
|
194
|
+
}, {
|
|
195
|
+
key: "formatHTMLMessage",
|
|
196
|
+
value: function formatHTMLMessage(messageDescriptor, variables) {
|
|
197
|
+
var id = messageDescriptor.id,
|
|
198
|
+
defaultMessage = messageDescriptor.defaultMessage;
|
|
199
|
+
return this.getHTML(id, variables).defaultMessage(defaultMessage);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Helper: determine user's locale via URL, cookie, localStorage, and browser's language.
|
|
204
|
+
* You may not need this API, if you have other rules to determine user's locale.
|
|
205
|
+
* @param {string} options.urlLocaleKey URL's query Key to determine locale. Example: if URL=http://localhost?lang=en-US, then set it 'lang'
|
|
206
|
+
* @param {string} options.cookieLocaleKey Cookie's Key to determine locale. Example: if cookie=lang:en-US, then set it 'lang'
|
|
207
|
+
* @param {string} options.localStorageLocaleKey LocalStorage's Key to determine locale such as 'lang'
|
|
208
|
+
* @returns {string} determined locale such as 'en-US'
|
|
209
|
+
*/
|
|
210
|
+
}, {
|
|
211
|
+
key: "determineLocale",
|
|
212
|
+
value: function determineLocale() {
|
|
213
|
+
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
214
|
+
return this.getLocaleFromURL(options) || this.getLocaleFromCookie(options) || this.getLocaleFromLocalStorage(options) || this.getLocaleFromBrowser();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Initialize properties and load CLDR locale data according to currentLocale
|
|
219
|
+
* @param {Object} options
|
|
220
|
+
* @param {string} options.currentLocale Current locale such as 'en-US'
|
|
221
|
+
* @param {any} options.locales App locale data like {"en-US":{"key1":"value1"},"zh-CN":{"key1":"值1"}}
|
|
222
|
+
* @param {boolean} [options.debug] debug mode
|
|
223
|
+
* @returns {Promise}
|
|
224
|
+
*/
|
|
225
|
+
}, {
|
|
226
|
+
key: "init",
|
|
227
|
+
value: function init() {
|
|
228
|
+
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
229
|
+
(0, _invariant["default"])(options.currentLocale, "options.currentLocale is required");
|
|
230
|
+
(0, _invariant["default"])(options.locales, "options.locales is required");
|
|
231
|
+
(0, _assign["default"])(this.options, options);
|
|
232
|
+
this.options.formats = (0, _assign["default"])({}, this.options.formats, constants.defaultFormats);
|
|
233
|
+
return new _promise["default"](function (resolve, reject) {
|
|
234
|
+
// init() will not load external common locale data anymore.
|
|
235
|
+
// But, it still return a Promise for backward compatibility.
|
|
236
|
+
resolve();
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Get the inital options
|
|
242
|
+
*/
|
|
243
|
+
}, {
|
|
244
|
+
key: "getInitOptions",
|
|
245
|
+
value: function getInitOptions() {
|
|
246
|
+
return this.options;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Load more locales after init
|
|
251
|
+
*/
|
|
252
|
+
}, {
|
|
253
|
+
key: "load",
|
|
254
|
+
value: function load(locales) {
|
|
255
|
+
(0, _lodash["default"])(this.options.locales, locales);
|
|
256
|
+
}
|
|
257
|
+
}, {
|
|
258
|
+
key: "getLocaleFromCookie",
|
|
259
|
+
value: function getLocaleFromCookie(options) {
|
|
260
|
+
var cookieLocaleKey = options.cookieLocaleKey;
|
|
261
|
+
if (cookieLocaleKey) {
|
|
262
|
+
var params = _cookie["default"].parse(document.cookie);
|
|
263
|
+
return params && params[cookieLocaleKey];
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}, {
|
|
267
|
+
key: "getLocaleFromLocalStorage",
|
|
268
|
+
value: function getLocaleFromLocalStorage(options) {
|
|
269
|
+
var localStorageLocaleKey = options.localStorageLocaleKey;
|
|
270
|
+
if (localStorageLocaleKey && window.localStorage) {
|
|
271
|
+
return localStorage.getItem(localStorageLocaleKey);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}, {
|
|
275
|
+
key: "getLocaleFromURL",
|
|
276
|
+
value: function getLocaleFromURL(options) {
|
|
277
|
+
var urlLocaleKey = options.urlLocaleKey;
|
|
278
|
+
if (urlLocaleKey) {
|
|
279
|
+
var query = location.search.split("?");
|
|
280
|
+
if (query.length >= 2) {
|
|
281
|
+
var params = new _urlSearchParams["default"](query[1]);
|
|
282
|
+
if (params.has(urlLocaleKey)) {
|
|
283
|
+
return params.get(urlLocaleKey);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}, {
|
|
289
|
+
key: "getDescendantProp",
|
|
290
|
+
value: function getDescendantProp(locale, key) {
|
|
291
|
+
var _context4;
|
|
292
|
+
if (locale[key]) {
|
|
293
|
+
return locale[key];
|
|
294
|
+
}
|
|
295
|
+
var msg = (0, _reduce["default"])(_context4 = key.split(".")).call(_context4, function (a, b) {
|
|
296
|
+
return a != undefined ? a[b] : a;
|
|
297
|
+
}, locale);
|
|
298
|
+
return msg;
|
|
299
|
+
}
|
|
300
|
+
}, {
|
|
301
|
+
key: "getLocaleFromBrowser",
|
|
302
|
+
value: function getLocaleFromBrowser() {
|
|
303
|
+
return navigator.language || navigator.userLanguage;
|
|
304
|
+
}
|
|
305
|
+
}, {
|
|
306
|
+
key: "_getSpanElementMessage",
|
|
307
|
+
value: function _getSpanElementMessage(key, msg) {
|
|
308
|
+
var options = {
|
|
309
|
+
dangerouslySetInnerHTML: {
|
|
310
|
+
__html: msg
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
if (this.options.debug) {
|
|
314
|
+
options[this.options.dataKey] = key;
|
|
315
|
+
}
|
|
316
|
+
var el = _react["default"].createElement('span', options);
|
|
317
|
+
// when key exists, it should still return element if there's defaultMessage() after getHTML()
|
|
318
|
+
var defaultMessage = function defaultMessage() {
|
|
319
|
+
return el;
|
|
320
|
+
};
|
|
321
|
+
return (0, _assign["default"])({
|
|
322
|
+
defaultMessage: defaultMessage,
|
|
323
|
+
d: defaultMessage
|
|
324
|
+
}, el);
|
|
325
|
+
}
|
|
326
|
+
}]);
|
|
327
|
+
return ReactIntlUniversal;
|
|
328
|
+
}();
|
|
329
|
+
var _default = exports["default"] = ReactIntlUniversal;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
|
|
4
|
+
_Object$defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.defaultFormats = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* Currency code list
|
|
10
|
+
* https://www.currency-iso.org/en/home/tables/table-a1.html
|
|
11
|
+
*/
|
|
12
|
+
var currency = ["AFN", "EUR", "ALL", "DZD", "USD", "AOA", "XCD", "ARS", "AMD", "AWG", "AUD", "AZN", "BSD", "BHD", "BDT", "BBD", "BYN", "BZD", "XOF", "BMD", "INR", "BTN", "BOB", "BOV", "BAM", "BWP", "NOK", "BRL", "BND", "BGN", "BIF", "CVE", "KHR", "XAF", "CAD", "KYD", "CLP", "CLF", "CNY", "COP", "COU", "KMF", "CDF", "NZD", "CRC", "HRK", "CUP", "CUC", "ANG", "CZK", "DKK", "DJF", "DOP", "EGP", "SVC", "ERN", "ETB", "FKP", "FJD", "XPF", "GMD", "GEL", "GHS", "GIP", "GTQ", "GBP", "GNF", "GYD", "HTG", "HNL", "HKD", "HUF", "ISK", "IDR", "XDR", "IRR", "IQD", "ILS", "JMD", "JPY", "JOD", "KZT", "KES", "KPW", "KRW", "KWD", "KGS", "LAK", "LBP", "LSL", "ZAR", "LRD", "LYD", "CHF", "MOP", "MKD", "MGA", "MWK", "MYR", "MVR", "MRO", "MUR", "XUA", "MXN", "MXV", "MDL", "MNT", "MAD", "MZN", "MMK", "NAD", "NPR", "NIO", "NGN", "OMR", "PKR", "PAB", "PGK", "PYG", "PEN", "PHP", "PLN", "QAR", "RON", "RUB", "RWF", "SHP", "WST", "STD", "SAR", "RSD", "SCR", "SLL", "SGD", "XSU", "SBD", "SOS", "SSP", "LKR", "SDG", "SRD", "SZL", "SEK", "CHE", "CHW", "SYP", "TWD", "TJS", "TZS", "THB", "TOP", "TTD", "TND", "TRY", "TMT", "UGX", "UAH", "AED", "USN", "UYU", "UYI", "UZS", "VUV", "VEF", "VND", "YER", "ZMW", "ZWL", "XBA", "XBB", "XBC", "XBD", "XTS", "XXX", "XAU", "XPD", "XPT", "XAG"];
|
|
13
|
+
var numberFormat = {};
|
|
14
|
+
for (var i = 0; i < currency.length; i++) {
|
|
15
|
+
numberFormat[currency[i]] = {
|
|
16
|
+
style: 'currency',
|
|
17
|
+
currency: currency[i]
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
var defaultFormats = exports.defaultFormats = {
|
|
21
|
+
number: numberFormat
|
|
22
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
|
|
4
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
|
|
5
|
+
_Object$defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
_Object$defineProperty(exports, "ReactIntlUniversal", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function get() {
|
|
11
|
+
return _ReactIntlUniversal["default"];
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
exports.load = exports.init = exports.getLocaleFromURL = exports.getLocaleFromLocalStorage = exports.getLocaleFromCookie = exports.getLocaleFromBrowser = exports.getInitOptions = exports.getHTML = exports.getDescendantProp = exports.get = exports.formatMessage = exports.formatHTMLMessage = exports.determineLocale = exports["default"] = void 0;
|
|
15
|
+
var _bind = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/bind"));
|
|
16
|
+
var _ReactIntlUniversal = _interopRequireDefault(require("./ReactIntlUniversal"));
|
|
17
|
+
var _context, _context2, _context3, _context4, _context5, _context6, _context7, _context8, _context9, _context10, _context11, _context12, _context13;
|
|
18
|
+
var defaultInstance = exports["default"] = new _ReactIntlUniversal["default"]();
|
|
19
|
+
// resolved by CommonJS module loader
|
|
20
|
+
defaultInstance.ReactIntlUniversal = _ReactIntlUniversal["default"];
|
|
21
|
+
// react pattern: https://github.com/facebook/react/blob/main/packages/react/src/React.js
|
|
22
|
+
var get = exports.get = (0, _bind["default"])(_context = defaultInstance.get).call(_context, defaultInstance);
|
|
23
|
+
var getHTML = exports.getHTML = (0, _bind["default"])(_context2 = defaultInstance.getHTML).call(_context2, defaultInstance);
|
|
24
|
+
var formatMessage = exports.formatMessage = (0, _bind["default"])(_context3 = defaultInstance.formatMessage).call(_context3, defaultInstance);
|
|
25
|
+
var formatHTMLMessage = exports.formatHTMLMessage = (0, _bind["default"])(_context4 = defaultInstance.formatHTMLMessage).call(_context4, defaultInstance);
|
|
26
|
+
var determineLocale = exports.determineLocale = (0, _bind["default"])(_context5 = defaultInstance.determineLocale).call(_context5, defaultInstance);
|
|
27
|
+
var init = exports.init = (0, _bind["default"])(_context6 = defaultInstance.init).call(_context6, defaultInstance);
|
|
28
|
+
var getInitOptions = exports.getInitOptions = (0, _bind["default"])(_context7 = defaultInstance.getInitOptions).call(_context7, defaultInstance);
|
|
29
|
+
var load = exports.load = (0, _bind["default"])(_context8 = defaultInstance.load).call(_context8, defaultInstance);
|
|
30
|
+
var getLocaleFromCookie = exports.getLocaleFromCookie = (0, _bind["default"])(_context9 = defaultInstance.getLocaleFromCookie).call(_context9, defaultInstance);
|
|
31
|
+
var getLocaleFromLocalStorage = exports.getLocaleFromLocalStorage = (0, _bind["default"])(_context10 = defaultInstance.getLocaleFromLocalStorage).call(_context10, defaultInstance);
|
|
32
|
+
var getLocaleFromURL = exports.getLocaleFromURL = (0, _bind["default"])(_context11 = defaultInstance.getLocaleFromURL).call(_context11, defaultInstance);
|
|
33
|
+
var getDescendantProp = exports.getDescendantProp = (0, _bind["default"])(_context12 = defaultInstance.getDescendantProp).call(_context12, defaultInstance);
|
|
34
|
+
var getLocaleFromBrowser = exports.getLocaleFromBrowser = (0, _bind["default"])(_context13 = defaultInstance.getLocaleFromBrowser).call(_context13, defaultInstance);
|
|
35
|
+
// resolved by ECMAScript module loader
|
package/lib/useIntl/index.js
CHANGED
|
@@ -8,7 +8,7 @@ _Object$defineProperty(exports, "__esModule", {
|
|
|
8
8
|
exports["default"] = void 0;
|
|
9
9
|
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/slicedToArray"));
|
|
10
10
|
var _react = require("react");
|
|
11
|
-
var _reactIntlUniversal = _interopRequireDefault(require("react-intl-universal"));
|
|
11
|
+
var _reactIntlUniversal = _interopRequireDefault(require("../react-intl-universal"));
|
|
12
12
|
var _useForceUpdate = _interopRequireDefault(require("./use-force-update"));
|
|
13
13
|
var _context = _interopRequireDefault(require("./context"));
|
|
14
14
|
var useIntl = function useIntl() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chaoswise/intl",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.4",
|
|
4
4
|
"author": "cloudwiser",
|
|
5
5
|
"description": "intl",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -84,5 +84,5 @@
|
|
|
84
84
|
"react-dom": "^16.13.1"
|
|
85
85
|
},
|
|
86
86
|
"license": "MIT",
|
|
87
|
-
"gitHead": "
|
|
87
|
+
"gitHead": "d62d0e15a28dad6c4a531ed72724210d3bacc453"
|
|
88
88
|
}
|