@dcloudio/uni-i18n 0.0.1-nvue3.3030820220125001
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/LICENSE +202 -0
- package/dist/uni-i18n.cjs.js +476 -0
- package/dist/uni-i18n.d.ts +89 -0
- package/dist/uni-i18n.es.js +458 -0
- package/package.json +22 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const isArray = Array.isArray;
|
|
6
|
+
const isObject = (val) => val !== null && typeof val === 'object';
|
|
7
|
+
const defaultDelimiters = ['{', '}'];
|
|
8
|
+
class BaseFormatter {
|
|
9
|
+
constructor() {
|
|
10
|
+
this._caches = Object.create(null);
|
|
11
|
+
}
|
|
12
|
+
interpolate(message, values, delimiters = defaultDelimiters) {
|
|
13
|
+
if (!values) {
|
|
14
|
+
return [message];
|
|
15
|
+
}
|
|
16
|
+
let tokens = this._caches[message];
|
|
17
|
+
if (!tokens) {
|
|
18
|
+
tokens = parse(message, delimiters);
|
|
19
|
+
this._caches[message] = tokens;
|
|
20
|
+
}
|
|
21
|
+
return compile(tokens, values);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const RE_TOKEN_LIST_VALUE = /^(?:\d)+/;
|
|
25
|
+
const RE_TOKEN_NAMED_VALUE = /^(?:\w)+/;
|
|
26
|
+
function parse(format, [startDelimiter, endDelimiter]) {
|
|
27
|
+
const tokens = [];
|
|
28
|
+
let position = 0;
|
|
29
|
+
let text = '';
|
|
30
|
+
while (position < format.length) {
|
|
31
|
+
let char = format[position++];
|
|
32
|
+
if (char === startDelimiter) {
|
|
33
|
+
if (text) {
|
|
34
|
+
tokens.push({ type: 'text', value: text });
|
|
35
|
+
}
|
|
36
|
+
text = '';
|
|
37
|
+
let sub = '';
|
|
38
|
+
char = format[position++];
|
|
39
|
+
while (char !== undefined && char !== endDelimiter) {
|
|
40
|
+
sub += char;
|
|
41
|
+
char = format[position++];
|
|
42
|
+
}
|
|
43
|
+
const isClosed = char === endDelimiter;
|
|
44
|
+
const type = RE_TOKEN_LIST_VALUE.test(sub)
|
|
45
|
+
? 'list'
|
|
46
|
+
: isClosed && RE_TOKEN_NAMED_VALUE.test(sub)
|
|
47
|
+
? 'named'
|
|
48
|
+
: 'unknown';
|
|
49
|
+
tokens.push({ value: sub, type });
|
|
50
|
+
}
|
|
51
|
+
// else if (char === '%') {
|
|
52
|
+
// // when found rails i18n syntax, skip text capture
|
|
53
|
+
// if (format[position] !== '{') {
|
|
54
|
+
// text += char
|
|
55
|
+
// }
|
|
56
|
+
// }
|
|
57
|
+
else {
|
|
58
|
+
text += char;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
text && tokens.push({ type: 'text', value: text });
|
|
62
|
+
return tokens;
|
|
63
|
+
}
|
|
64
|
+
function compile(tokens, values) {
|
|
65
|
+
const compiled = [];
|
|
66
|
+
let index = 0;
|
|
67
|
+
const mode = isArray(values)
|
|
68
|
+
? 'list'
|
|
69
|
+
: isObject(values)
|
|
70
|
+
? 'named'
|
|
71
|
+
: 'unknown';
|
|
72
|
+
if (mode === 'unknown') {
|
|
73
|
+
return compiled;
|
|
74
|
+
}
|
|
75
|
+
while (index < tokens.length) {
|
|
76
|
+
const token = tokens[index];
|
|
77
|
+
switch (token.type) {
|
|
78
|
+
case 'text':
|
|
79
|
+
compiled.push(token.value);
|
|
80
|
+
break;
|
|
81
|
+
case 'list':
|
|
82
|
+
compiled.push(values[parseInt(token.value, 10)]);
|
|
83
|
+
break;
|
|
84
|
+
case 'named':
|
|
85
|
+
if (mode === 'named') {
|
|
86
|
+
compiled.push(values[token.value]);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
90
|
+
console.warn(`Type of token '${token.type}' and format of value '${mode}' don't match!`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
break;
|
|
94
|
+
case 'unknown':
|
|
95
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
96
|
+
console.warn(`Detect 'unknown' type of token!`);
|
|
97
|
+
}
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
index++;
|
|
101
|
+
}
|
|
102
|
+
return compiled;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const LOCALE_ZH_HANS = 'zh-Hans';
|
|
106
|
+
const LOCALE_ZH_HANT = 'zh-Hant';
|
|
107
|
+
const LOCALE_EN = 'en';
|
|
108
|
+
const LOCALE_FR = 'fr';
|
|
109
|
+
const LOCALE_ES = 'es';
|
|
110
|
+
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
111
|
+
const hasOwn = (val, key) => hasOwnProperty.call(val, key);
|
|
112
|
+
const defaultFormatter = new BaseFormatter();
|
|
113
|
+
function include(str, parts) {
|
|
114
|
+
return !!parts.find((part) => str.indexOf(part) !== -1);
|
|
115
|
+
}
|
|
116
|
+
function startsWith(str, parts) {
|
|
117
|
+
return parts.find((part) => str.indexOf(part) === 0);
|
|
118
|
+
}
|
|
119
|
+
function normalizeLocale(locale, messages) {
|
|
120
|
+
if (!locale) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
locale = locale.trim().replace(/_/g, '-');
|
|
124
|
+
if (messages && messages[locale]) {
|
|
125
|
+
return locale;
|
|
126
|
+
}
|
|
127
|
+
locale = locale.toLowerCase();
|
|
128
|
+
if (locale === 'chinese') {
|
|
129
|
+
// 支付宝
|
|
130
|
+
return LOCALE_ZH_HANS;
|
|
131
|
+
}
|
|
132
|
+
if (locale.indexOf('zh') === 0) {
|
|
133
|
+
if (locale.indexOf('-hans') > -1) {
|
|
134
|
+
return LOCALE_ZH_HANS;
|
|
135
|
+
}
|
|
136
|
+
if (locale.indexOf('-hant') > -1) {
|
|
137
|
+
return LOCALE_ZH_HANT;
|
|
138
|
+
}
|
|
139
|
+
if (include(locale, ['-tw', '-hk', '-mo', '-cht'])) {
|
|
140
|
+
return LOCALE_ZH_HANT;
|
|
141
|
+
}
|
|
142
|
+
return LOCALE_ZH_HANS;
|
|
143
|
+
}
|
|
144
|
+
const lang = startsWith(locale, [LOCALE_EN, LOCALE_FR, LOCALE_ES]);
|
|
145
|
+
if (lang) {
|
|
146
|
+
return lang;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
class I18n {
|
|
150
|
+
constructor({ locale, fallbackLocale, messages, watcher, formater, }) {
|
|
151
|
+
this.locale = LOCALE_EN;
|
|
152
|
+
this.fallbackLocale = LOCALE_EN;
|
|
153
|
+
this.message = {};
|
|
154
|
+
this.messages = {};
|
|
155
|
+
this.watchers = [];
|
|
156
|
+
if (fallbackLocale) {
|
|
157
|
+
this.fallbackLocale = fallbackLocale;
|
|
158
|
+
}
|
|
159
|
+
this.formater = formater || defaultFormatter;
|
|
160
|
+
this.messages = messages || {};
|
|
161
|
+
this.setLocale(locale || LOCALE_EN);
|
|
162
|
+
if (watcher) {
|
|
163
|
+
this.watchLocale(watcher);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
setLocale(locale) {
|
|
167
|
+
const oldLocale = this.locale;
|
|
168
|
+
this.locale = normalizeLocale(locale, this.messages) || this.fallbackLocale;
|
|
169
|
+
if (!this.messages[this.locale]) {
|
|
170
|
+
// 可能初始化时不存在
|
|
171
|
+
this.messages[this.locale] = {};
|
|
172
|
+
}
|
|
173
|
+
this.message = this.messages[this.locale];
|
|
174
|
+
// 仅发生变化时,通知
|
|
175
|
+
if (oldLocale !== this.locale) {
|
|
176
|
+
this.watchers.forEach((watcher) => {
|
|
177
|
+
watcher(this.locale, oldLocale);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
getLocale() {
|
|
182
|
+
return this.locale;
|
|
183
|
+
}
|
|
184
|
+
watchLocale(fn) {
|
|
185
|
+
const index = this.watchers.push(fn) - 1;
|
|
186
|
+
return () => {
|
|
187
|
+
this.watchers.splice(index, 1);
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
add(locale, message, override = true) {
|
|
191
|
+
const curMessages = this.messages[locale];
|
|
192
|
+
if (curMessages) {
|
|
193
|
+
if (override) {
|
|
194
|
+
Object.assign(curMessages, message);
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
Object.keys(message).forEach((key) => {
|
|
198
|
+
if (!hasOwn(curMessages, key)) {
|
|
199
|
+
curMessages[key] = message[key];
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
this.messages[locale] = message;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
f(message, values, delimiters) {
|
|
209
|
+
return this.formater.interpolate(message, values, delimiters).join('');
|
|
210
|
+
}
|
|
211
|
+
t(key, locale, values) {
|
|
212
|
+
let message = this.message;
|
|
213
|
+
if (typeof locale === 'string') {
|
|
214
|
+
locale = normalizeLocale(locale, this.messages);
|
|
215
|
+
locale && (message = this.messages[locale]);
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
values = locale;
|
|
219
|
+
}
|
|
220
|
+
if (!hasOwn(message, key)) {
|
|
221
|
+
console.warn(`Cannot translate the value of keypath ${key}. Use the value of keypath as default.`);
|
|
222
|
+
return key;
|
|
223
|
+
}
|
|
224
|
+
return this.formater.interpolate(message[key], values).join('');
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function watchAppLocale(appVm, i18n) {
|
|
229
|
+
// 需要保证 watch 的触发在组件渲染之前
|
|
230
|
+
if (appVm.$watchLocale) {
|
|
231
|
+
// vue2
|
|
232
|
+
appVm.$watchLocale((newLocale) => {
|
|
233
|
+
i18n.setLocale(newLocale);
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
appVm.$watch(() => appVm.$locale, (newLocale) => {
|
|
238
|
+
i18n.setLocale(newLocale);
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
function getDefaultLocale() {
|
|
243
|
+
if (typeof uni !== 'undefined' && uni.getLocale) {
|
|
244
|
+
return uni.getLocale();
|
|
245
|
+
}
|
|
246
|
+
// 小程序平台,uni 和 uni-i18n 互相引用,导致访问不到 uni,故在 global 上挂了 getLocale
|
|
247
|
+
if (typeof global !== 'undefined' && global.getLocale) {
|
|
248
|
+
return global.getLocale();
|
|
249
|
+
}
|
|
250
|
+
return LOCALE_EN;
|
|
251
|
+
}
|
|
252
|
+
function initVueI18n(locale, messages = {}, fallbackLocale, watcher) {
|
|
253
|
+
// 兼容旧版本入参
|
|
254
|
+
if (typeof locale !== 'string') {
|
|
255
|
+
[locale, messages] = [
|
|
256
|
+
messages,
|
|
257
|
+
locale,
|
|
258
|
+
];
|
|
259
|
+
}
|
|
260
|
+
if (typeof locale !== 'string') {
|
|
261
|
+
// 因为小程序平台,uni-i18n 和 uni 互相引用,导致此时访问 uni 时,为 undefined
|
|
262
|
+
locale = getDefaultLocale();
|
|
263
|
+
}
|
|
264
|
+
if (typeof fallbackLocale !== 'string') {
|
|
265
|
+
fallbackLocale =
|
|
266
|
+
(typeof __uniConfig !== 'undefined' && __uniConfig.fallbackLocale) ||
|
|
267
|
+
LOCALE_EN;
|
|
268
|
+
}
|
|
269
|
+
const i18n = new I18n({
|
|
270
|
+
locale,
|
|
271
|
+
fallbackLocale,
|
|
272
|
+
messages,
|
|
273
|
+
watcher,
|
|
274
|
+
});
|
|
275
|
+
let t = (key, values) => {
|
|
276
|
+
if (typeof getApp !== 'function') {
|
|
277
|
+
// app view
|
|
278
|
+
/* eslint-disable no-func-assign */
|
|
279
|
+
t = function (key, values) {
|
|
280
|
+
return i18n.t(key, values);
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
let isWatchedAppLocale = false;
|
|
285
|
+
t = function (key, values) {
|
|
286
|
+
const appVm = getApp().$vm;
|
|
287
|
+
// 可能$vm还不存在,比如在支付宝小程序中,组件定义较早,在props的default里使用了t()函数(如uni-goods-nav),此时app还未初始化
|
|
288
|
+
// options: {
|
|
289
|
+
// type: Array,
|
|
290
|
+
// default () {
|
|
291
|
+
// return [{
|
|
292
|
+
// icon: 'shop',
|
|
293
|
+
// text: t("uni-goods-nav.options.shop"),
|
|
294
|
+
// }, {
|
|
295
|
+
// icon: 'cart',
|
|
296
|
+
// text: t("uni-goods-nav.options.cart")
|
|
297
|
+
// }]
|
|
298
|
+
// }
|
|
299
|
+
// },
|
|
300
|
+
if (appVm) {
|
|
301
|
+
// 触发响应式
|
|
302
|
+
appVm.$locale;
|
|
303
|
+
if (!isWatchedAppLocale) {
|
|
304
|
+
isWatchedAppLocale = true;
|
|
305
|
+
watchAppLocale(appVm, i18n);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return i18n.t(key, values);
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
return t(key, values);
|
|
312
|
+
};
|
|
313
|
+
return {
|
|
314
|
+
i18n,
|
|
315
|
+
f(message, values, delimiters) {
|
|
316
|
+
return i18n.f(message, values, delimiters);
|
|
317
|
+
},
|
|
318
|
+
t(key, values) {
|
|
319
|
+
return t(key, values);
|
|
320
|
+
},
|
|
321
|
+
add(locale, message, override = true) {
|
|
322
|
+
return i18n.add(locale, message, override);
|
|
323
|
+
},
|
|
324
|
+
watch(fn) {
|
|
325
|
+
return i18n.watchLocale(fn);
|
|
326
|
+
},
|
|
327
|
+
getLocale() {
|
|
328
|
+
return i18n.getLocale();
|
|
329
|
+
},
|
|
330
|
+
setLocale(newLocale) {
|
|
331
|
+
return i18n.setLocale(newLocale);
|
|
332
|
+
},
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const isString = (val) => typeof val === 'string';
|
|
337
|
+
let formater;
|
|
338
|
+
function hasI18nJson(jsonObj, delimiters) {
|
|
339
|
+
if (!formater) {
|
|
340
|
+
formater = new BaseFormatter();
|
|
341
|
+
}
|
|
342
|
+
return walkJsonObj(jsonObj, (jsonObj, key) => {
|
|
343
|
+
const value = jsonObj[key];
|
|
344
|
+
if (isString(value)) {
|
|
345
|
+
if (isI18nStr(value, delimiters)) {
|
|
346
|
+
return true;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
return hasI18nJson(value, delimiters);
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
function parseI18nJson(jsonObj, values, delimiters) {
|
|
355
|
+
if (!formater) {
|
|
356
|
+
formater = new BaseFormatter();
|
|
357
|
+
}
|
|
358
|
+
walkJsonObj(jsonObj, (jsonObj, key) => {
|
|
359
|
+
const value = jsonObj[key];
|
|
360
|
+
if (isString(value)) {
|
|
361
|
+
if (isI18nStr(value, delimiters)) {
|
|
362
|
+
jsonObj[key] = compileStr(value, values, delimiters);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
parseI18nJson(value, values, delimiters);
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
return jsonObj;
|
|
370
|
+
}
|
|
371
|
+
function compileI18nJsonStr(jsonStr, { locale, locales, delimiters, }) {
|
|
372
|
+
if (!isI18nStr(jsonStr, delimiters)) {
|
|
373
|
+
return jsonStr;
|
|
374
|
+
}
|
|
375
|
+
if (!formater) {
|
|
376
|
+
formater = new BaseFormatter();
|
|
377
|
+
}
|
|
378
|
+
const localeValues = [];
|
|
379
|
+
Object.keys(locales).forEach((name) => {
|
|
380
|
+
if (name !== locale) {
|
|
381
|
+
localeValues.push({
|
|
382
|
+
locale: name,
|
|
383
|
+
values: locales[name],
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
localeValues.unshift({ locale, values: locales[locale] });
|
|
388
|
+
try {
|
|
389
|
+
return JSON.stringify(compileJsonObj(JSON.parse(jsonStr), localeValues, delimiters), null, 2);
|
|
390
|
+
}
|
|
391
|
+
catch (e) { }
|
|
392
|
+
return jsonStr;
|
|
393
|
+
}
|
|
394
|
+
function isI18nStr(value, delimiters) {
|
|
395
|
+
return value.indexOf(delimiters[0]) > -1;
|
|
396
|
+
}
|
|
397
|
+
function compileStr(value, values, delimiters) {
|
|
398
|
+
return formater.interpolate(value, values, delimiters).join('');
|
|
399
|
+
}
|
|
400
|
+
function compileValue(jsonObj, key, localeValues, delimiters) {
|
|
401
|
+
const value = jsonObj[key];
|
|
402
|
+
if (isString(value)) {
|
|
403
|
+
// 存在国际化
|
|
404
|
+
if (isI18nStr(value, delimiters)) {
|
|
405
|
+
jsonObj[key] = compileStr(value, localeValues[0].values, delimiters);
|
|
406
|
+
if (localeValues.length > 1) {
|
|
407
|
+
// 格式化国际化语言
|
|
408
|
+
const valueLocales = (jsonObj[key + 'Locales'] = {});
|
|
409
|
+
localeValues.forEach((localValue) => {
|
|
410
|
+
valueLocales[localValue.locale] = compileStr(value, localValue.values, delimiters);
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
compileJsonObj(value, localeValues, delimiters);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
function compileJsonObj(jsonObj, localeValues, delimiters) {
|
|
420
|
+
walkJsonObj(jsonObj, (jsonObj, key) => {
|
|
421
|
+
compileValue(jsonObj, key, localeValues, delimiters);
|
|
422
|
+
});
|
|
423
|
+
return jsonObj;
|
|
424
|
+
}
|
|
425
|
+
function walkJsonObj(jsonObj, walk) {
|
|
426
|
+
if (isArray(jsonObj)) {
|
|
427
|
+
for (let i = 0; i < jsonObj.length; i++) {
|
|
428
|
+
if (walk(jsonObj, i)) {
|
|
429
|
+
return true;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
else if (isObject(jsonObj)) {
|
|
434
|
+
for (const key in jsonObj) {
|
|
435
|
+
if (walk(jsonObj, key)) {
|
|
436
|
+
return true;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
return false;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function resolveLocale(locales) {
|
|
444
|
+
return (locale) => {
|
|
445
|
+
if (!locale) {
|
|
446
|
+
return locale;
|
|
447
|
+
}
|
|
448
|
+
locale = normalizeLocale(locale) || locale;
|
|
449
|
+
return resolveLocaleChain(locale).find((locale) => locales.indexOf(locale) > -1);
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
function resolveLocaleChain(locale) {
|
|
453
|
+
const chain = [];
|
|
454
|
+
const tokens = locale.split('-');
|
|
455
|
+
while (tokens.length) {
|
|
456
|
+
chain.push(tokens.join('-'));
|
|
457
|
+
tokens.pop();
|
|
458
|
+
}
|
|
459
|
+
return chain;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
exports.Formatter = BaseFormatter;
|
|
463
|
+
exports.I18n = I18n;
|
|
464
|
+
exports.LOCALE_EN = LOCALE_EN;
|
|
465
|
+
exports.LOCALE_ES = LOCALE_ES;
|
|
466
|
+
exports.LOCALE_FR = LOCALE_FR;
|
|
467
|
+
exports.LOCALE_ZH_HANS = LOCALE_ZH_HANS;
|
|
468
|
+
exports.LOCALE_ZH_HANT = LOCALE_ZH_HANT;
|
|
469
|
+
exports.compileI18nJsonStr = compileI18nJsonStr;
|
|
470
|
+
exports.hasI18nJson = hasI18nJson;
|
|
471
|
+
exports.initVueI18n = initVueI18n;
|
|
472
|
+
exports.isI18nStr = isI18nStr;
|
|
473
|
+
exports.isString = isString;
|
|
474
|
+
exports.normalizeLocale = normalizeLocale;
|
|
475
|
+
exports.parseI18nJson = parseI18nJson;
|
|
476
|
+
exports.resolveLocale = resolveLocale;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
export declare type BuiltInLocale = typeof LOCALE_ZH_HANS | typeof LOCALE_ZH_HANT | typeof LOCALE_EN | typeof LOCALE_FR | typeof LOCALE_ES;
|
|
2
|
+
|
|
3
|
+
export declare function compileI18nJsonStr(jsonStr: string, { locale, locales, delimiters, }: {
|
|
4
|
+
locale: string;
|
|
5
|
+
locales: Record<string, Record<string, string>>;
|
|
6
|
+
delimiters: [string, string];
|
|
7
|
+
}): string;
|
|
8
|
+
|
|
9
|
+
export declare class Formatter {
|
|
10
|
+
_caches: {
|
|
11
|
+
[key: string]: Array<Token>;
|
|
12
|
+
};
|
|
13
|
+
constructor();
|
|
14
|
+
interpolate(message: string, values?: Record<string, unknown> | Array<unknown>, delimiters?: [string, string]): Array<unknown>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
declare interface Formatter_2 {
|
|
18
|
+
interpolate: (message: string, values?: Record<string, unknown> | Array<unknown>, delimiters?: [string, string]) => Array<unknown>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export declare function hasI18nJson(jsonObj: unknown, delimiters: [string, string]): boolean;
|
|
22
|
+
|
|
23
|
+
export declare class I18n {
|
|
24
|
+
private locale;
|
|
25
|
+
private fallbackLocale;
|
|
26
|
+
private message;
|
|
27
|
+
private messages;
|
|
28
|
+
private watchers;
|
|
29
|
+
private formater;
|
|
30
|
+
constructor({ locale, fallbackLocale, messages, watcher, formater, }: I18nOptions);
|
|
31
|
+
setLocale(locale: string): void;
|
|
32
|
+
getLocale(): string;
|
|
33
|
+
watchLocale(fn: LocaleWatcher): () => void;
|
|
34
|
+
add(locale: BuiltInLocale, message: Record<string, string>, override?: boolean): void;
|
|
35
|
+
f(message: string, values?: Record<string, unknown> | Array<unknown>, delimiters?: [string, string]): string;
|
|
36
|
+
t(key: string, values?: Record<string, unknown> | Array<unknown> | BuiltInLocale): string;
|
|
37
|
+
t(key: string, locale?: BuiltInLocale, values?: Record<string, unknown> | Array<unknown>): string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export declare interface I18nOptions {
|
|
41
|
+
locale: string;
|
|
42
|
+
fallbackLocale?: string;
|
|
43
|
+
messages?: LocaleMessages;
|
|
44
|
+
formater?: Formatter_2;
|
|
45
|
+
watcher?: LocaleWatcher;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export declare function initVueI18n(locale?: string, messages?: LocaleMessages, fallbackLocale?: string, watcher?: (locale: string) => void): {
|
|
49
|
+
i18n: I18n;
|
|
50
|
+
f(message: string, values?: Record<string, unknown> | unknown[] | undefined, delimiters?: [string, string] | undefined): string;
|
|
51
|
+
t(key: string, values?: Record<string, unknown> | unknown[] | undefined): string;
|
|
52
|
+
add(locale: BuiltInLocale, message: Record<string, string>, override?: boolean): void;
|
|
53
|
+
watch(fn: LocaleWatcher): () => void;
|
|
54
|
+
getLocale(): string;
|
|
55
|
+
setLocale(newLocale: string): void;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export declare function isI18nStr(value: string, delimiters: [string, string]): boolean;
|
|
59
|
+
|
|
60
|
+
export declare const isString: (val: unknown) => val is string;
|
|
61
|
+
|
|
62
|
+
declare type Locale = string;
|
|
63
|
+
|
|
64
|
+
export declare const LOCALE_EN = "en";
|
|
65
|
+
|
|
66
|
+
export declare const LOCALE_ES = "es";
|
|
67
|
+
|
|
68
|
+
export declare const LOCALE_FR = "fr";
|
|
69
|
+
|
|
70
|
+
export declare const LOCALE_ZH_HANS = "zh-Hans";
|
|
71
|
+
|
|
72
|
+
export declare const LOCALE_ZH_HANT = "zh-Hant";
|
|
73
|
+
|
|
74
|
+
export declare type LocaleMessages = Record<string, Record<string, string>>;
|
|
75
|
+
|
|
76
|
+
export declare type LocaleWatcher = (newLocale: string, oldLocale: string) => void;
|
|
77
|
+
|
|
78
|
+
export declare function normalizeLocale(locale: string, messages?: LocaleMessages): BuiltInLocale | undefined;
|
|
79
|
+
|
|
80
|
+
export declare function parseI18nJson(jsonObj: unknown, values: Record<string, string>, delimiters: [string, string]): unknown;
|
|
81
|
+
|
|
82
|
+
export declare function resolveLocale(locales: Locale[]): (locale: Locale) => string | undefined;
|
|
83
|
+
|
|
84
|
+
declare type Token = {
|
|
85
|
+
type: 'text' | 'named' | 'list' | 'unknown';
|
|
86
|
+
value: string;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export { }
|
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
const isArray = Array.isArray;
|
|
2
|
+
const isObject = (val) => val !== null && typeof val === 'object';
|
|
3
|
+
const defaultDelimiters = ['{', '}'];
|
|
4
|
+
class BaseFormatter {
|
|
5
|
+
constructor() {
|
|
6
|
+
this._caches = Object.create(null);
|
|
7
|
+
}
|
|
8
|
+
interpolate(message, values, delimiters = defaultDelimiters) {
|
|
9
|
+
if (!values) {
|
|
10
|
+
return [message];
|
|
11
|
+
}
|
|
12
|
+
let tokens = this._caches[message];
|
|
13
|
+
if (!tokens) {
|
|
14
|
+
tokens = parse(message, delimiters);
|
|
15
|
+
this._caches[message] = tokens;
|
|
16
|
+
}
|
|
17
|
+
return compile(tokens, values);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const RE_TOKEN_LIST_VALUE = /^(?:\d)+/;
|
|
21
|
+
const RE_TOKEN_NAMED_VALUE = /^(?:\w)+/;
|
|
22
|
+
function parse(format, [startDelimiter, endDelimiter]) {
|
|
23
|
+
const tokens = [];
|
|
24
|
+
let position = 0;
|
|
25
|
+
let text = '';
|
|
26
|
+
while (position < format.length) {
|
|
27
|
+
let char = format[position++];
|
|
28
|
+
if (char === startDelimiter) {
|
|
29
|
+
if (text) {
|
|
30
|
+
tokens.push({ type: 'text', value: text });
|
|
31
|
+
}
|
|
32
|
+
text = '';
|
|
33
|
+
let sub = '';
|
|
34
|
+
char = format[position++];
|
|
35
|
+
while (char !== undefined && char !== endDelimiter) {
|
|
36
|
+
sub += char;
|
|
37
|
+
char = format[position++];
|
|
38
|
+
}
|
|
39
|
+
const isClosed = char === endDelimiter;
|
|
40
|
+
const type = RE_TOKEN_LIST_VALUE.test(sub)
|
|
41
|
+
? 'list'
|
|
42
|
+
: isClosed && RE_TOKEN_NAMED_VALUE.test(sub)
|
|
43
|
+
? 'named'
|
|
44
|
+
: 'unknown';
|
|
45
|
+
tokens.push({ value: sub, type });
|
|
46
|
+
}
|
|
47
|
+
// else if (char === '%') {
|
|
48
|
+
// // when found rails i18n syntax, skip text capture
|
|
49
|
+
// if (format[position] !== '{') {
|
|
50
|
+
// text += char
|
|
51
|
+
// }
|
|
52
|
+
// }
|
|
53
|
+
else {
|
|
54
|
+
text += char;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
text && tokens.push({ type: 'text', value: text });
|
|
58
|
+
return tokens;
|
|
59
|
+
}
|
|
60
|
+
function compile(tokens, values) {
|
|
61
|
+
const compiled = [];
|
|
62
|
+
let index = 0;
|
|
63
|
+
const mode = isArray(values)
|
|
64
|
+
? 'list'
|
|
65
|
+
: isObject(values)
|
|
66
|
+
? 'named'
|
|
67
|
+
: 'unknown';
|
|
68
|
+
if (mode === 'unknown') {
|
|
69
|
+
return compiled;
|
|
70
|
+
}
|
|
71
|
+
while (index < tokens.length) {
|
|
72
|
+
const token = tokens[index];
|
|
73
|
+
switch (token.type) {
|
|
74
|
+
case 'text':
|
|
75
|
+
compiled.push(token.value);
|
|
76
|
+
break;
|
|
77
|
+
case 'list':
|
|
78
|
+
compiled.push(values[parseInt(token.value, 10)]);
|
|
79
|
+
break;
|
|
80
|
+
case 'named':
|
|
81
|
+
if (mode === 'named') {
|
|
82
|
+
compiled.push(values[token.value]);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
86
|
+
console.warn(`Type of token '${token.type}' and format of value '${mode}' don't match!`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
break;
|
|
90
|
+
case 'unknown':
|
|
91
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
92
|
+
console.warn(`Detect 'unknown' type of token!`);
|
|
93
|
+
}
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
index++;
|
|
97
|
+
}
|
|
98
|
+
return compiled;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const LOCALE_ZH_HANS = 'zh-Hans';
|
|
102
|
+
const LOCALE_ZH_HANT = 'zh-Hant';
|
|
103
|
+
const LOCALE_EN = 'en';
|
|
104
|
+
const LOCALE_FR = 'fr';
|
|
105
|
+
const LOCALE_ES = 'es';
|
|
106
|
+
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
107
|
+
const hasOwn = (val, key) => hasOwnProperty.call(val, key);
|
|
108
|
+
const defaultFormatter = new BaseFormatter();
|
|
109
|
+
function include(str, parts) {
|
|
110
|
+
return !!parts.find((part) => str.indexOf(part) !== -1);
|
|
111
|
+
}
|
|
112
|
+
function startsWith(str, parts) {
|
|
113
|
+
return parts.find((part) => str.indexOf(part) === 0);
|
|
114
|
+
}
|
|
115
|
+
function normalizeLocale(locale, messages) {
|
|
116
|
+
if (!locale) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
locale = locale.trim().replace(/_/g, '-');
|
|
120
|
+
if (messages && messages[locale]) {
|
|
121
|
+
return locale;
|
|
122
|
+
}
|
|
123
|
+
locale = locale.toLowerCase();
|
|
124
|
+
if (locale === 'chinese') {
|
|
125
|
+
// 支付宝
|
|
126
|
+
return LOCALE_ZH_HANS;
|
|
127
|
+
}
|
|
128
|
+
if (locale.indexOf('zh') === 0) {
|
|
129
|
+
if (locale.indexOf('-hans') > -1) {
|
|
130
|
+
return LOCALE_ZH_HANS;
|
|
131
|
+
}
|
|
132
|
+
if (locale.indexOf('-hant') > -1) {
|
|
133
|
+
return LOCALE_ZH_HANT;
|
|
134
|
+
}
|
|
135
|
+
if (include(locale, ['-tw', '-hk', '-mo', '-cht'])) {
|
|
136
|
+
return LOCALE_ZH_HANT;
|
|
137
|
+
}
|
|
138
|
+
return LOCALE_ZH_HANS;
|
|
139
|
+
}
|
|
140
|
+
const lang = startsWith(locale, [LOCALE_EN, LOCALE_FR, LOCALE_ES]);
|
|
141
|
+
if (lang) {
|
|
142
|
+
return lang;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
class I18n {
|
|
146
|
+
constructor({ locale, fallbackLocale, messages, watcher, formater, }) {
|
|
147
|
+
this.locale = LOCALE_EN;
|
|
148
|
+
this.fallbackLocale = LOCALE_EN;
|
|
149
|
+
this.message = {};
|
|
150
|
+
this.messages = {};
|
|
151
|
+
this.watchers = [];
|
|
152
|
+
if (fallbackLocale) {
|
|
153
|
+
this.fallbackLocale = fallbackLocale;
|
|
154
|
+
}
|
|
155
|
+
this.formater = formater || defaultFormatter;
|
|
156
|
+
this.messages = messages || {};
|
|
157
|
+
this.setLocale(locale || LOCALE_EN);
|
|
158
|
+
if (watcher) {
|
|
159
|
+
this.watchLocale(watcher);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
setLocale(locale) {
|
|
163
|
+
const oldLocale = this.locale;
|
|
164
|
+
this.locale = normalizeLocale(locale, this.messages) || this.fallbackLocale;
|
|
165
|
+
if (!this.messages[this.locale]) {
|
|
166
|
+
// 可能初始化时不存在
|
|
167
|
+
this.messages[this.locale] = {};
|
|
168
|
+
}
|
|
169
|
+
this.message = this.messages[this.locale];
|
|
170
|
+
// 仅发生变化时,通知
|
|
171
|
+
if (oldLocale !== this.locale) {
|
|
172
|
+
this.watchers.forEach((watcher) => {
|
|
173
|
+
watcher(this.locale, oldLocale);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
getLocale() {
|
|
178
|
+
return this.locale;
|
|
179
|
+
}
|
|
180
|
+
watchLocale(fn) {
|
|
181
|
+
const index = this.watchers.push(fn) - 1;
|
|
182
|
+
return () => {
|
|
183
|
+
this.watchers.splice(index, 1);
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
add(locale, message, override = true) {
|
|
187
|
+
const curMessages = this.messages[locale];
|
|
188
|
+
if (curMessages) {
|
|
189
|
+
if (override) {
|
|
190
|
+
Object.assign(curMessages, message);
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
Object.keys(message).forEach((key) => {
|
|
194
|
+
if (!hasOwn(curMessages, key)) {
|
|
195
|
+
curMessages[key] = message[key];
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
this.messages[locale] = message;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
f(message, values, delimiters) {
|
|
205
|
+
return this.formater.interpolate(message, values, delimiters).join('');
|
|
206
|
+
}
|
|
207
|
+
t(key, locale, values) {
|
|
208
|
+
let message = this.message;
|
|
209
|
+
if (typeof locale === 'string') {
|
|
210
|
+
locale = normalizeLocale(locale, this.messages);
|
|
211
|
+
locale && (message = this.messages[locale]);
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
values = locale;
|
|
215
|
+
}
|
|
216
|
+
if (!hasOwn(message, key)) {
|
|
217
|
+
console.warn(`Cannot translate the value of keypath ${key}. Use the value of keypath as default.`);
|
|
218
|
+
return key;
|
|
219
|
+
}
|
|
220
|
+
return this.formater.interpolate(message[key], values).join('');
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function watchAppLocale(appVm, i18n) {
|
|
225
|
+
// 需要保证 watch 的触发在组件渲染之前
|
|
226
|
+
if (appVm.$watchLocale) {
|
|
227
|
+
// vue2
|
|
228
|
+
appVm.$watchLocale((newLocale) => {
|
|
229
|
+
i18n.setLocale(newLocale);
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
appVm.$watch(() => appVm.$locale, (newLocale) => {
|
|
234
|
+
i18n.setLocale(newLocale);
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
function getDefaultLocale() {
|
|
239
|
+
if (typeof uni !== 'undefined' && uni.getLocale) {
|
|
240
|
+
return uni.getLocale();
|
|
241
|
+
}
|
|
242
|
+
// 小程序平台,uni 和 uni-i18n 互相引用,导致访问不到 uni,故在 global 上挂了 getLocale
|
|
243
|
+
if (typeof global !== 'undefined' && global.getLocale) {
|
|
244
|
+
return global.getLocale();
|
|
245
|
+
}
|
|
246
|
+
return LOCALE_EN;
|
|
247
|
+
}
|
|
248
|
+
function initVueI18n(locale, messages = {}, fallbackLocale, watcher) {
|
|
249
|
+
// 兼容旧版本入参
|
|
250
|
+
if (typeof locale !== 'string') {
|
|
251
|
+
[locale, messages] = [
|
|
252
|
+
messages,
|
|
253
|
+
locale,
|
|
254
|
+
];
|
|
255
|
+
}
|
|
256
|
+
if (typeof locale !== 'string') {
|
|
257
|
+
// 因为小程序平台,uni-i18n 和 uni 互相引用,导致此时访问 uni 时,为 undefined
|
|
258
|
+
locale = getDefaultLocale();
|
|
259
|
+
}
|
|
260
|
+
if (typeof fallbackLocale !== 'string') {
|
|
261
|
+
fallbackLocale =
|
|
262
|
+
(typeof __uniConfig !== 'undefined' && __uniConfig.fallbackLocale) ||
|
|
263
|
+
LOCALE_EN;
|
|
264
|
+
}
|
|
265
|
+
const i18n = new I18n({
|
|
266
|
+
locale,
|
|
267
|
+
fallbackLocale,
|
|
268
|
+
messages,
|
|
269
|
+
watcher,
|
|
270
|
+
});
|
|
271
|
+
let t = (key, values) => {
|
|
272
|
+
if (typeof getApp !== 'function') {
|
|
273
|
+
// app view
|
|
274
|
+
/* eslint-disable no-func-assign */
|
|
275
|
+
t = function (key, values) {
|
|
276
|
+
return i18n.t(key, values);
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
let isWatchedAppLocale = false;
|
|
281
|
+
t = function (key, values) {
|
|
282
|
+
const appVm = getApp().$vm;
|
|
283
|
+
// 可能$vm还不存在,比如在支付宝小程序中,组件定义较早,在props的default里使用了t()函数(如uni-goods-nav),此时app还未初始化
|
|
284
|
+
// options: {
|
|
285
|
+
// type: Array,
|
|
286
|
+
// default () {
|
|
287
|
+
// return [{
|
|
288
|
+
// icon: 'shop',
|
|
289
|
+
// text: t("uni-goods-nav.options.shop"),
|
|
290
|
+
// }, {
|
|
291
|
+
// icon: 'cart',
|
|
292
|
+
// text: t("uni-goods-nav.options.cart")
|
|
293
|
+
// }]
|
|
294
|
+
// }
|
|
295
|
+
// },
|
|
296
|
+
if (appVm) {
|
|
297
|
+
// 触发响应式
|
|
298
|
+
appVm.$locale;
|
|
299
|
+
if (!isWatchedAppLocale) {
|
|
300
|
+
isWatchedAppLocale = true;
|
|
301
|
+
watchAppLocale(appVm, i18n);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return i18n.t(key, values);
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
return t(key, values);
|
|
308
|
+
};
|
|
309
|
+
return {
|
|
310
|
+
i18n,
|
|
311
|
+
f(message, values, delimiters) {
|
|
312
|
+
return i18n.f(message, values, delimiters);
|
|
313
|
+
},
|
|
314
|
+
t(key, values) {
|
|
315
|
+
return t(key, values);
|
|
316
|
+
},
|
|
317
|
+
add(locale, message, override = true) {
|
|
318
|
+
return i18n.add(locale, message, override);
|
|
319
|
+
},
|
|
320
|
+
watch(fn) {
|
|
321
|
+
return i18n.watchLocale(fn);
|
|
322
|
+
},
|
|
323
|
+
getLocale() {
|
|
324
|
+
return i18n.getLocale();
|
|
325
|
+
},
|
|
326
|
+
setLocale(newLocale) {
|
|
327
|
+
return i18n.setLocale(newLocale);
|
|
328
|
+
},
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const isString = (val) => typeof val === 'string';
|
|
333
|
+
let formater;
|
|
334
|
+
function hasI18nJson(jsonObj, delimiters) {
|
|
335
|
+
if (!formater) {
|
|
336
|
+
formater = new BaseFormatter();
|
|
337
|
+
}
|
|
338
|
+
return walkJsonObj(jsonObj, (jsonObj, key) => {
|
|
339
|
+
const value = jsonObj[key];
|
|
340
|
+
if (isString(value)) {
|
|
341
|
+
if (isI18nStr(value, delimiters)) {
|
|
342
|
+
return true;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
return hasI18nJson(value, delimiters);
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
function parseI18nJson(jsonObj, values, delimiters) {
|
|
351
|
+
if (!formater) {
|
|
352
|
+
formater = new BaseFormatter();
|
|
353
|
+
}
|
|
354
|
+
walkJsonObj(jsonObj, (jsonObj, key) => {
|
|
355
|
+
const value = jsonObj[key];
|
|
356
|
+
if (isString(value)) {
|
|
357
|
+
if (isI18nStr(value, delimiters)) {
|
|
358
|
+
jsonObj[key] = compileStr(value, values, delimiters);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
parseI18nJson(value, values, delimiters);
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
return jsonObj;
|
|
366
|
+
}
|
|
367
|
+
function compileI18nJsonStr(jsonStr, { locale, locales, delimiters, }) {
|
|
368
|
+
if (!isI18nStr(jsonStr, delimiters)) {
|
|
369
|
+
return jsonStr;
|
|
370
|
+
}
|
|
371
|
+
if (!formater) {
|
|
372
|
+
formater = new BaseFormatter();
|
|
373
|
+
}
|
|
374
|
+
const localeValues = [];
|
|
375
|
+
Object.keys(locales).forEach((name) => {
|
|
376
|
+
if (name !== locale) {
|
|
377
|
+
localeValues.push({
|
|
378
|
+
locale: name,
|
|
379
|
+
values: locales[name],
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
localeValues.unshift({ locale, values: locales[locale] });
|
|
384
|
+
try {
|
|
385
|
+
return JSON.stringify(compileJsonObj(JSON.parse(jsonStr), localeValues, delimiters), null, 2);
|
|
386
|
+
}
|
|
387
|
+
catch (e) { }
|
|
388
|
+
return jsonStr;
|
|
389
|
+
}
|
|
390
|
+
function isI18nStr(value, delimiters) {
|
|
391
|
+
return value.indexOf(delimiters[0]) > -1;
|
|
392
|
+
}
|
|
393
|
+
function compileStr(value, values, delimiters) {
|
|
394
|
+
return formater.interpolate(value, values, delimiters).join('');
|
|
395
|
+
}
|
|
396
|
+
function compileValue(jsonObj, key, localeValues, delimiters) {
|
|
397
|
+
const value = jsonObj[key];
|
|
398
|
+
if (isString(value)) {
|
|
399
|
+
// 存在国际化
|
|
400
|
+
if (isI18nStr(value, delimiters)) {
|
|
401
|
+
jsonObj[key] = compileStr(value, localeValues[0].values, delimiters);
|
|
402
|
+
if (localeValues.length > 1) {
|
|
403
|
+
// 格式化国际化语言
|
|
404
|
+
const valueLocales = (jsonObj[key + 'Locales'] = {});
|
|
405
|
+
localeValues.forEach((localValue) => {
|
|
406
|
+
valueLocales[localValue.locale] = compileStr(value, localValue.values, delimiters);
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
else {
|
|
412
|
+
compileJsonObj(value, localeValues, delimiters);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
function compileJsonObj(jsonObj, localeValues, delimiters) {
|
|
416
|
+
walkJsonObj(jsonObj, (jsonObj, key) => {
|
|
417
|
+
compileValue(jsonObj, key, localeValues, delimiters);
|
|
418
|
+
});
|
|
419
|
+
return jsonObj;
|
|
420
|
+
}
|
|
421
|
+
function walkJsonObj(jsonObj, walk) {
|
|
422
|
+
if (isArray(jsonObj)) {
|
|
423
|
+
for (let i = 0; i < jsonObj.length; i++) {
|
|
424
|
+
if (walk(jsonObj, i)) {
|
|
425
|
+
return true;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
else if (isObject(jsonObj)) {
|
|
430
|
+
for (const key in jsonObj) {
|
|
431
|
+
if (walk(jsonObj, key)) {
|
|
432
|
+
return true;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
return false;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function resolveLocale(locales) {
|
|
440
|
+
return (locale) => {
|
|
441
|
+
if (!locale) {
|
|
442
|
+
return locale;
|
|
443
|
+
}
|
|
444
|
+
locale = normalizeLocale(locale) || locale;
|
|
445
|
+
return resolveLocaleChain(locale).find((locale) => locales.indexOf(locale) > -1);
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
function resolveLocaleChain(locale) {
|
|
449
|
+
const chain = [];
|
|
450
|
+
const tokens = locale.split('-');
|
|
451
|
+
while (tokens.length) {
|
|
452
|
+
chain.push(tokens.join('-'));
|
|
453
|
+
tokens.pop();
|
|
454
|
+
}
|
|
455
|
+
return chain;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
export { BaseFormatter as Formatter, I18n, LOCALE_EN, LOCALE_ES, LOCALE_FR, LOCALE_ZH_HANS, LOCALE_ZH_HANT, compileI18nJsonStr, hasI18nJson, initVueI18n, isI18nStr, isString, normalizeLocale, parseI18nJson, resolveLocale };
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dcloudio/uni-i18n",
|
|
3
|
+
"version": "0.0.1-nvue3.3030820220125001",
|
|
4
|
+
"description": "@dcloudio/uni-i18n",
|
|
5
|
+
"main": "./dist/uni-i18n.cjs.js",
|
|
6
|
+
"module": "./dist/uni-i18n.es.js",
|
|
7
|
+
"types": "./dist/uni-i18n.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/dcloudio/uni-app.git",
|
|
15
|
+
"directory": "packages/uni-i18n"
|
|
16
|
+
},
|
|
17
|
+
"license": "Apache-2.0",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/dcloudio/uni-app/issues"
|
|
20
|
+
},
|
|
21
|
+
"gitHead": "33e807d66e1fe47e2ee08ad9c59247e37b8884da"
|
|
22
|
+
}
|