@wordpress/i18n 5.26.0 → 6.0.1-next.46f643fa0.0
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/CHANGELOG.md +6 -0
- package/README.md +28 -27
- package/build/create-i18n.js +56 -201
- package/build/create-i18n.js.map +1 -1
- package/build/default-i18n.js +41 -34
- package/build/default-i18n.js.map +1 -1
- package/build/index.js.map +1 -1
- package/build/sprintf.js +10 -22
- package/build/sprintf.js.map +1 -1
- package/build/types.js +6 -0
- package/build/types.js.map +1 -0
- package/build-module/create-i18n.js +56 -201
- package/build-module/create-i18n.js.map +1 -1
- package/build-module/default-i18n.js +41 -35
- package/build-module/default-i18n.js.map +1 -1
- package/build-module/index.js.map +1 -1
- package/build-module/sprintf.js +10 -22
- package/build-module/sprintf.js.map +1 -1
- package/build-module/types.js +2 -0
- package/build-module/types.js.map +1 -0
- package/build-types/create-i18n.d.ts +11 -115
- package/build-types/create-i18n.d.ts.map +1 -1
- package/build-types/default-i18n.d.ts +55 -47
- package/build-types/default-i18n.d.ts.map +1 -1
- package/build-types/index.d.ts +4 -3
- package/build-types/index.d.ts.map +1 -1
- package/build-types/sprintf.d.ts +4 -10
- package/build-types/sprintf.d.ts.map +1 -1
- package/build-types/types.d.ts +114 -0
- package/build-types/types.d.ts.map +1 -0
- package/package.json +4 -4
- package/src/create-i18n.ts +405 -0
- package/src/{default-i18n.js → default-i18n.ts} +47 -35
- package/src/index.ts +16 -0
- package/src/sprintf.ts +37 -0
- package/src/test/{create-i18n.js → create-i18n.ts} +63 -30
- package/src/test/{sprintf.js → sprintf.ts} +5 -10
- package/src/test/{subscribe-i18n.js → subscribe-i18n.ts} +3 -3
- package/src/types.ts +167 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/src/create-i18n.js +0 -506
- package/src/sprintf.js +0 -36
- /package/src/test/{default-i18n.js → default-i18n.ts} +0 -0
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* External dependencies
|
|
3
3
|
*/
|
|
4
|
+
|
|
4
5
|
import Tannin from 'tannin';
|
|
6
|
+
/**
|
|
7
|
+
* Internal dependencies
|
|
8
|
+
*/
|
|
5
9
|
|
|
6
10
|
/**
|
|
7
|
-
*
|
|
11
|
+
* WordPress dependencies
|
|
8
12
|
*/
|
|
9
13
|
|
|
10
14
|
/**
|
|
11
15
|
* Default locale data to use for Tannin domain when not otherwise provided.
|
|
12
16
|
* Assumes an English plural forms expression.
|
|
13
|
-
*
|
|
14
|
-
* @type {LocaleData}
|
|
15
17
|
*/
|
|
16
18
|
const DEFAULT_LOCALE_DATA = {
|
|
17
19
|
'': {
|
|
18
|
-
/** @param {number} n */
|
|
19
20
|
plural_forms(n) {
|
|
20
21
|
return n === 1 ? 0 : 1;
|
|
21
22
|
}
|
|
@@ -28,136 +29,18 @@ const DEFAULT_LOCALE_DATA = {
|
|
|
28
29
|
*/
|
|
29
30
|
const I18N_HOOK_REGEXP = /^i18n\.(n?gettext|has_translation)(_|$)/;
|
|
30
31
|
|
|
31
|
-
/**
|
|
32
|
-
* @typedef {(domain?: string) => LocaleData} GetLocaleData
|
|
33
|
-
*
|
|
34
|
-
* Returns locale data by domain in a
|
|
35
|
-
* Jed-formatted JSON object shape.
|
|
36
|
-
*
|
|
37
|
-
* @see http://messageformat.github.io/Jed/
|
|
38
|
-
*/
|
|
39
|
-
/**
|
|
40
|
-
* @typedef {(data?: LocaleData, domain?: string) => void} SetLocaleData
|
|
41
|
-
*
|
|
42
|
-
* Merges locale data into the Tannin instance by domain. Note that this
|
|
43
|
-
* function will overwrite the domain configuration. Accepts data in a
|
|
44
|
-
* Jed-formatted JSON object shape.
|
|
45
|
-
*
|
|
46
|
-
* @see http://messageformat.github.io/Jed/
|
|
47
|
-
*/
|
|
48
|
-
/**
|
|
49
|
-
* @typedef {(data?: LocaleData, domain?: string) => void} AddLocaleData
|
|
50
|
-
*
|
|
51
|
-
* Merges locale data into the Tannin instance by domain. Note that this
|
|
52
|
-
* function will also merge the domain configuration. Accepts data in a
|
|
53
|
-
* Jed-formatted JSON object shape.
|
|
54
|
-
*
|
|
55
|
-
* @see http://messageformat.github.io/Jed/
|
|
56
|
-
*/
|
|
57
|
-
/**
|
|
58
|
-
* @typedef {(data?: LocaleData, domain?: string) => void} ResetLocaleData
|
|
59
|
-
*
|
|
60
|
-
* Resets all current Tannin instance locale data and sets the specified
|
|
61
|
-
* locale data for the domain. Accepts data in a Jed-formatted JSON object shape.
|
|
62
|
-
*
|
|
63
|
-
* @see http://messageformat.github.io/Jed/
|
|
64
|
-
*/
|
|
65
|
-
/** @typedef {() => void} SubscribeCallback */
|
|
66
|
-
/** @typedef {() => void} UnsubscribeCallback */
|
|
67
|
-
/**
|
|
68
|
-
* @typedef {(callback: SubscribeCallback) => UnsubscribeCallback} Subscribe
|
|
69
|
-
*
|
|
70
|
-
* Subscribes to changes of locale data
|
|
71
|
-
*/
|
|
72
|
-
/**
|
|
73
|
-
* @typedef {(domain?: string) => string} GetFilterDomain
|
|
74
|
-
* Retrieve the domain to use when calling domain-specific filters.
|
|
75
|
-
*/
|
|
76
|
-
/**
|
|
77
|
-
* @typedef {(text: string, domain?: string) => string} __
|
|
78
|
-
*
|
|
79
|
-
* Retrieve the translation of text.
|
|
80
|
-
*
|
|
81
|
-
* @see https://developer.wordpress.org/reference/functions/__/
|
|
82
|
-
*/
|
|
83
|
-
/**
|
|
84
|
-
* @typedef {(text: string, context: string, domain?: string) => string} _x
|
|
85
|
-
*
|
|
86
|
-
* Retrieve translated string with gettext context.
|
|
87
|
-
*
|
|
88
|
-
* @see https://developer.wordpress.org/reference/functions/_x/
|
|
89
|
-
*/
|
|
90
|
-
/**
|
|
91
|
-
* @typedef {(single: string, plural: string, number: number, domain?: string) => string} _n
|
|
92
|
-
*
|
|
93
|
-
* Translates and retrieves the singular or plural form based on the supplied
|
|
94
|
-
* number.
|
|
95
|
-
*
|
|
96
|
-
* @see https://developer.wordpress.org/reference/functions/_n/
|
|
97
|
-
*/
|
|
98
|
-
/**
|
|
99
|
-
* @typedef {(single: string, plural: string, number: number, context: string, domain?: string) => string} _nx
|
|
100
|
-
*
|
|
101
|
-
* Translates and retrieves the singular or plural form based on the supplied
|
|
102
|
-
* number, with gettext context.
|
|
103
|
-
*
|
|
104
|
-
* @see https://developer.wordpress.org/reference/functions/_nx/
|
|
105
|
-
*/
|
|
106
|
-
/**
|
|
107
|
-
* @typedef {() => boolean} IsRtl
|
|
108
|
-
*
|
|
109
|
-
* Check if current locale is RTL.
|
|
110
|
-
*
|
|
111
|
-
* **RTL (Right To Left)** is a locale property indicating that text is written from right to left.
|
|
112
|
-
* For example, the `he` locale (for Hebrew) specifies right-to-left. Arabic (ar) is another common
|
|
113
|
-
* language written RTL. The opposite of RTL, LTR (Left To Right) is used in other languages,
|
|
114
|
-
* including English (`en`, `en-US`, `en-GB`, etc.), Spanish (`es`), and French (`fr`).
|
|
115
|
-
*/
|
|
116
|
-
/**
|
|
117
|
-
* @typedef {(single: string, context?: string, domain?: string) => boolean} HasTranslation
|
|
118
|
-
*
|
|
119
|
-
* Check if there is a translation for a given string in singular form.
|
|
120
|
-
*/
|
|
121
|
-
/** @typedef {import('@wordpress/hooks').Hooks} Hooks */
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* An i18n instance
|
|
125
|
-
*
|
|
126
|
-
* @typedef I18n
|
|
127
|
-
* @property {GetLocaleData} getLocaleData Returns locale data by domain in a Jed-formatted JSON object shape.
|
|
128
|
-
* @property {SetLocaleData} setLocaleData Merges locale data into the Tannin instance by domain. Note that this
|
|
129
|
-
* function will overwrite the domain configuration. Accepts data in a
|
|
130
|
-
* Jed-formatted JSON object shape.
|
|
131
|
-
* @property {AddLocaleData} addLocaleData Merges locale data into the Tannin instance by domain. Note that this
|
|
132
|
-
* function will also merge the domain configuration. Accepts data in a
|
|
133
|
-
* Jed-formatted JSON object shape.
|
|
134
|
-
* @property {ResetLocaleData} resetLocaleData Resets all current Tannin instance locale data and sets the specified
|
|
135
|
-
* locale data for the domain. Accepts data in a Jed-formatted JSON object shape.
|
|
136
|
-
* @property {Subscribe} subscribe Subscribes to changes of Tannin locale data.
|
|
137
|
-
* @property {__} __ Retrieve the translation of text.
|
|
138
|
-
* @property {_x} _x Retrieve translated string with gettext context.
|
|
139
|
-
* @property {_n} _n Translates and retrieves the singular or plural form based on the supplied
|
|
140
|
-
* number.
|
|
141
|
-
* @property {_nx} _nx Translates and retrieves the singular or plural form based on the supplied
|
|
142
|
-
* number, with gettext context.
|
|
143
|
-
* @property {IsRtl} isRTL Check if current locale is RTL.
|
|
144
|
-
* @property {HasTranslation} hasTranslation Check if there is a translation for a given string.
|
|
145
|
-
*/
|
|
146
|
-
|
|
147
32
|
/**
|
|
148
33
|
* Create an i18n instance
|
|
149
34
|
*
|
|
150
|
-
* @param
|
|
151
|
-
* @param
|
|
152
|
-
* @param
|
|
35
|
+
* @param [initialData] Locale data configuration.
|
|
36
|
+
* @param [initialDomain] Domain for which configuration applies.
|
|
37
|
+
* @param [hooks] Hooks implementation.
|
|
153
38
|
*
|
|
154
|
-
* @return
|
|
39
|
+
* @return I18n instance.
|
|
155
40
|
*/
|
|
156
41
|
export const createI18n = (initialData, initialDomain, hooks) => {
|
|
157
42
|
/**
|
|
158
43
|
* The underlying instance of Tannin to which exported functions interface.
|
|
159
|
-
*
|
|
160
|
-
* @type {Tannin}
|
|
161
44
|
*/
|
|
162
45
|
const tannin = new Tannin({});
|
|
163
46
|
const listeners = new Set();
|
|
@@ -168,20 +51,18 @@ export const createI18n = (initialData, initialDomain, hooks) => {
|
|
|
168
51
|
/**
|
|
169
52
|
* Subscribe to changes of locale data.
|
|
170
53
|
*
|
|
171
|
-
* @param
|
|
172
|
-
* @return
|
|
54
|
+
* @param callback Subscription callback.
|
|
55
|
+
* @return Unsubscribe callback.
|
|
173
56
|
*/
|
|
174
57
|
const subscribe = callback => {
|
|
175
58
|
listeners.add(callback);
|
|
176
59
|
return () => listeners.delete(callback);
|
|
177
60
|
};
|
|
178
|
-
|
|
179
|
-
/** @type {GetLocaleData} */
|
|
180
61
|
const getLocaleData = (domain = 'default') => tannin.data[domain];
|
|
181
62
|
|
|
182
63
|
/**
|
|
183
|
-
* @param
|
|
184
|
-
* @param
|
|
64
|
+
* @param [data]
|
|
65
|
+
* @param [domain]
|
|
185
66
|
*/
|
|
186
67
|
const doSetLocaleData = (data, domain = 'default') => {
|
|
187
68
|
tannin.data[domain] = {
|
|
@@ -199,14 +80,10 @@ export const createI18n = (initialData, initialDomain, hooks) => {
|
|
|
199
80
|
// Clean up cached plural forms functions cache as it might be updated.
|
|
200
81
|
delete tannin.pluralForms[domain];
|
|
201
82
|
};
|
|
202
|
-
|
|
203
|
-
/** @type {SetLocaleData} */
|
|
204
83
|
const setLocaleData = (data, domain) => {
|
|
205
84
|
doSetLocaleData(data, domain);
|
|
206
85
|
notifyListeners();
|
|
207
86
|
};
|
|
208
|
-
|
|
209
|
-
/** @type {AddLocaleData} */
|
|
210
87
|
const addLocaleData = (data, domain = 'default') => {
|
|
211
88
|
tannin.data[domain] = {
|
|
212
89
|
...tannin.data[domain],
|
|
@@ -224,8 +101,6 @@ export const createI18n = (initialData, initialDomain, hooks) => {
|
|
|
224
101
|
delete tannin.pluralForms[domain];
|
|
225
102
|
notifyListeners();
|
|
226
103
|
};
|
|
227
|
-
|
|
228
|
-
/** @type {ResetLocaleData} */
|
|
229
104
|
const resetLocaleData = (data, domain) => {
|
|
230
105
|
// Reset all current Tannin locale data.
|
|
231
106
|
tannin.data = {};
|
|
@@ -239,16 +114,16 @@ export const createI18n = (initialData, initialDomain, hooks) => {
|
|
|
239
114
|
* Wrapper for Tannin's `dcnpgettext`. Populates default locale data if not
|
|
240
115
|
* otherwise previously assigned.
|
|
241
116
|
*
|
|
242
|
-
* @param
|
|
243
|
-
* @param
|
|
244
|
-
* @param
|
|
245
|
-
*
|
|
246
|
-
* @param
|
|
247
|
-
*
|
|
248
|
-
* @param
|
|
249
|
-
*
|
|
117
|
+
* @param domain Domain to retrieve the translated text.
|
|
118
|
+
* @param context Context information for the translators.
|
|
119
|
+
* @param single Text to translate if non-plural. Used as
|
|
120
|
+
* fallback return value on a caught error.
|
|
121
|
+
* @param [plural] The text to be used if the number is
|
|
122
|
+
* plural.
|
|
123
|
+
* @param [number] The number to compare against to use
|
|
124
|
+
* either the singular or plural form.
|
|
250
125
|
*
|
|
251
|
-
* @return
|
|
126
|
+
* @return The translated string.
|
|
252
127
|
*/
|
|
253
128
|
const dcnpgettext = (domain = 'default', context, single, plural, number) => {
|
|
254
129
|
if (!tannin.data[domain]) {
|
|
@@ -257,11 +132,7 @@ export const createI18n = (initialData, initialDomain, hooks) => {
|
|
|
257
132
|
}
|
|
258
133
|
return tannin.dcnpgettext(domain, context, single, plural, number);
|
|
259
134
|
};
|
|
260
|
-
|
|
261
|
-
/** @type {GetFilterDomain} */
|
|
262
|
-
const getFilterDomain = (domain = 'default') => domain;
|
|
263
|
-
|
|
264
|
-
/** @type {__} */
|
|
135
|
+
const getFilterDomain = domain => domain || 'default';
|
|
265
136
|
const __ = (text, domain) => {
|
|
266
137
|
let translation = dcnpgettext(domain, undefined, text);
|
|
267
138
|
if (!hooks) {
|
|
@@ -271,16 +142,13 @@ export const createI18n = (initialData, initialDomain, hooks) => {
|
|
|
271
142
|
/**
|
|
272
143
|
* Filters text with its translation.
|
|
273
144
|
*
|
|
274
|
-
* @param
|
|
275
|
-
* @param
|
|
276
|
-
* @param
|
|
145
|
+
* @param translation Translated text.
|
|
146
|
+
* @param text Text to translate.
|
|
147
|
+
* @param domain Text domain. Unique identifier for retrieving translated strings.
|
|
277
148
|
*/
|
|
278
|
-
translation =
|
|
279
|
-
|
|
280
|
-
return /** @type {string} */ /** @type {*} */hooks.applyFilters('i18n.gettext_' + getFilterDomain(domain), translation, text, domain);
|
|
149
|
+
translation = hooks.applyFilters('i18n.gettext', translation, text, domain);
|
|
150
|
+
return hooks.applyFilters('i18n.gettext_' + getFilterDomain(domain), translation, text, domain);
|
|
281
151
|
};
|
|
282
|
-
|
|
283
|
-
/** @type {_x} */
|
|
284
152
|
const _x = (text, context, domain) => {
|
|
285
153
|
let translation = dcnpgettext(domain, context, text);
|
|
286
154
|
if (!hooks) {
|
|
@@ -290,17 +158,14 @@ export const createI18n = (initialData, initialDomain, hooks) => {
|
|
|
290
158
|
/**
|
|
291
159
|
* Filters text with its translation based on context information.
|
|
292
160
|
*
|
|
293
|
-
* @param
|
|
294
|
-
* @param
|
|
295
|
-
* @param
|
|
296
|
-
* @param
|
|
161
|
+
* @param translation Translated text.
|
|
162
|
+
* @param text Text to translate.
|
|
163
|
+
* @param context Context information for the translators.
|
|
164
|
+
* @param domain Text domain. Unique identifier for retrieving translated strings.
|
|
297
165
|
*/
|
|
298
|
-
translation =
|
|
299
|
-
|
|
300
|
-
return /** @type {string} */ /** @type {*} */hooks.applyFilters('i18n.gettext_with_context_' + getFilterDomain(domain), translation, text, context, domain);
|
|
166
|
+
translation = hooks.applyFilters('i18n.gettext_with_context', translation, text, context, domain);
|
|
167
|
+
return hooks.applyFilters('i18n.gettext_with_context_' + getFilterDomain(domain), translation, text, context, domain);
|
|
301
168
|
};
|
|
302
|
-
|
|
303
|
-
/** @type {_n} */
|
|
304
169
|
const _n = (single, plural, number, domain) => {
|
|
305
170
|
let translation = dcnpgettext(domain, undefined, single, plural, number);
|
|
306
171
|
if (!hooks) {
|
|
@@ -310,18 +175,15 @@ export const createI18n = (initialData, initialDomain, hooks) => {
|
|
|
310
175
|
/**
|
|
311
176
|
* Filters the singular or plural form of a string.
|
|
312
177
|
*
|
|
313
|
-
* @param
|
|
314
|
-
* @param
|
|
315
|
-
* @param
|
|
316
|
-
* @param
|
|
317
|
-
* @param
|
|
178
|
+
* @param translation Translated text.
|
|
179
|
+
* @param single The text to be used if the number is singular.
|
|
180
|
+
* @param plural The text to be used if the number is plural.
|
|
181
|
+
* @param number The number to compare against to use either the singular or plural form.
|
|
182
|
+
* @param domain Text domain. Unique identifier for retrieving translated strings.
|
|
318
183
|
*/
|
|
319
|
-
translation =
|
|
320
|
-
|
|
321
|
-
return /** @type {string} */ /** @type {*} */hooks.applyFilters('i18n.ngettext_' + getFilterDomain(domain), translation, single, plural, number, domain);
|
|
184
|
+
translation = hooks.applyFilters('i18n.ngettext', translation, single, plural, number, domain);
|
|
185
|
+
return hooks.applyFilters('i18n.ngettext_' + getFilterDomain(domain), translation, single, plural, number, domain);
|
|
322
186
|
};
|
|
323
|
-
|
|
324
|
-
/** @type {_nx} */
|
|
325
187
|
const _nx = (single, plural, number, context, domain) => {
|
|
326
188
|
let translation = dcnpgettext(domain, context, single, plural, number);
|
|
327
189
|
if (!hooks) {
|
|
@@ -331,24 +193,19 @@ export const createI18n = (initialData, initialDomain, hooks) => {
|
|
|
331
193
|
/**
|
|
332
194
|
* Filters the singular or plural form of a string with gettext context.
|
|
333
195
|
*
|
|
334
|
-
* @param
|
|
335
|
-
* @param
|
|
336
|
-
* @param
|
|
337
|
-
* @param
|
|
338
|
-
* @param
|
|
339
|
-
* @param
|
|
196
|
+
* @param translation Translated text.
|
|
197
|
+
* @param single The text to be used if the number is singular.
|
|
198
|
+
* @param plural The text to be used if the number is plural.
|
|
199
|
+
* @param number The number to compare against to use either the singular or plural form.
|
|
200
|
+
* @param context Context information for the translators.
|
|
201
|
+
* @param domain Text domain. Unique identifier for retrieving translated strings.
|
|
340
202
|
*/
|
|
341
|
-
translation =
|
|
342
|
-
|
|
343
|
-
return /** @type {string} */ /** @type {*} */hooks.applyFilters('i18n.ngettext_with_context_' + getFilterDomain(domain), translation, single, plural, number, context, domain);
|
|
203
|
+
translation = hooks.applyFilters('i18n.ngettext_with_context', translation, single, plural, number, context, domain);
|
|
204
|
+
return hooks.applyFilters('i18n.ngettext_with_context_' + getFilterDomain(domain), translation, single, plural, number, context, domain);
|
|
344
205
|
};
|
|
345
|
-
|
|
346
|
-
/** @type {IsRtl} */
|
|
347
206
|
const isRTL = () => {
|
|
348
207
|
return 'rtl' === _x('ltr', 'text direction');
|
|
349
208
|
};
|
|
350
|
-
|
|
351
|
-
/** @type {HasTranslation} */
|
|
352
209
|
const hasTranslation = (single, context, domain) => {
|
|
353
210
|
const key = context ? context + '\u0004' + single : single;
|
|
354
211
|
let result = !!tannin.data?.[domain !== null && domain !== void 0 ? domain : 'default']?.[key];
|
|
@@ -356,15 +213,13 @@ export const createI18n = (initialData, initialDomain, hooks) => {
|
|
|
356
213
|
/**
|
|
357
214
|
* Filters the presence of a translation in the locale data.
|
|
358
215
|
*
|
|
359
|
-
* @param
|
|
360
|
-
* @param
|
|
361
|
-
* @param
|
|
362
|
-
* @param
|
|
216
|
+
* @param hasTranslation Whether the translation is present or not..
|
|
217
|
+
* @param single The singular form of the translated text (used as key in locale data)
|
|
218
|
+
* @param context Context information for the translators.
|
|
219
|
+
* @param domain Text domain. Unique identifier for retrieving translated strings.
|
|
363
220
|
*/
|
|
364
|
-
result =
|
|
365
|
-
|
|
366
|
-
result = /** @type { boolean } */
|
|
367
|
-
/** @type {*} */hooks.applyFilters('i18n.has_translation_' + getFilterDomain(domain), result, single, context, domain);
|
|
221
|
+
result = hooks.applyFilters('i18n.has_translation', result, single, context, domain);
|
|
222
|
+
result = hooks.applyFilters('i18n.has_translation_' + getFilterDomain(domain), result, single, context, domain);
|
|
368
223
|
}
|
|
369
224
|
return result;
|
|
370
225
|
};
|
|
@@ -373,7 +228,7 @@ export const createI18n = (initialData, initialDomain, hooks) => {
|
|
|
373
228
|
}
|
|
374
229
|
if (hooks) {
|
|
375
230
|
/**
|
|
376
|
-
* @param
|
|
231
|
+
* @param hookName
|
|
377
232
|
*/
|
|
378
233
|
const onHookAddedOrRemoved = hookName => {
|
|
379
234
|
if (I18N_HOOK_REGEXP.test(hookName)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["Tannin","DEFAULT_LOCALE_DATA","plural_forms","n","I18N_HOOK_REGEXP","createI18n","initialData","initialDomain","hooks","tannin","listeners","Set","notifyListeners","forEach","listener","subscribe","callback","add","delete","getLocaleData","domain","data","doSetLocaleData","pluralForms","setLocaleData","addLocaleData","resetLocaleData","dcnpgettext","context","single","plural","number","undefined","getFilterDomain","__","text","translation","applyFilters","_x","_n","_nx","isRTL","hasTranslation","key","result","onHookAddedOrRemoved","hookName","test","addAction"],"sources":["@wordpress/i18n/src/create-i18n.js"],"sourcesContent":["/**\n * External dependencies\n */\nimport Tannin from 'tannin';\n\n/**\n * @typedef {Record<string,any>} LocaleData\n */\n\n/**\n * Default locale data to use for Tannin domain when not otherwise provided.\n * Assumes an English plural forms expression.\n *\n * @type {LocaleData}\n */\nconst DEFAULT_LOCALE_DATA = {\n\t'': {\n\t\t/** @param {number} n */\n\t\tplural_forms( n ) {\n\t\t\treturn n === 1 ? 0 : 1;\n\t\t},\n\t},\n};\n\n/*\n * Regular expression that matches i18n hooks like `i18n.gettext`, `i18n.ngettext`,\n * `i18n.gettext_domain` or `i18n.ngettext_with_context` or `i18n.has_translation`.\n */\nconst I18N_HOOK_REGEXP = /^i18n\\.(n?gettext|has_translation)(_|$)/;\n\n/**\n * @typedef {(domain?: string) => LocaleData} GetLocaleData\n *\n * Returns locale data by domain in a\n * Jed-formatted JSON object shape.\n *\n * @see http://messageformat.github.io/Jed/\n */\n/**\n * @typedef {(data?: LocaleData, domain?: string) => void} SetLocaleData\n *\n * Merges locale data into the Tannin instance by domain. Note that this\n * function will overwrite the domain configuration. Accepts data in a\n * Jed-formatted JSON object shape.\n *\n * @see http://messageformat.github.io/Jed/\n */\n/**\n * @typedef {(data?: LocaleData, domain?: string) => void} AddLocaleData\n *\n * Merges locale data into the Tannin instance by domain. Note that this\n * function will also merge the domain configuration. Accepts data in a\n * Jed-formatted JSON object shape.\n *\n * @see http://messageformat.github.io/Jed/\n */\n/**\n * @typedef {(data?: LocaleData, domain?: string) => void} ResetLocaleData\n *\n * Resets all current Tannin instance locale data and sets the specified\n * locale data for the domain. Accepts data in a Jed-formatted JSON object shape.\n *\n * @see http://messageformat.github.io/Jed/\n */\n/** @typedef {() => void} SubscribeCallback */\n/** @typedef {() => void} UnsubscribeCallback */\n/**\n * @typedef {(callback: SubscribeCallback) => UnsubscribeCallback} Subscribe\n *\n * Subscribes to changes of locale data\n */\n/**\n * @typedef {(domain?: string) => string} GetFilterDomain\n * Retrieve the domain to use when calling domain-specific filters.\n */\n/**\n * @typedef {(text: string, domain?: string) => string} __\n *\n * Retrieve the translation of text.\n *\n * @see https://developer.wordpress.org/reference/functions/__/\n */\n/**\n * @typedef {(text: string, context: string, domain?: string) => string} _x\n *\n * Retrieve translated string with gettext context.\n *\n * @see https://developer.wordpress.org/reference/functions/_x/\n */\n/**\n * @typedef {(single: string, plural: string, number: number, domain?: string) => string} _n\n *\n * Translates and retrieves the singular or plural form based on the supplied\n * number.\n *\n * @see https://developer.wordpress.org/reference/functions/_n/\n */\n/**\n * @typedef {(single: string, plural: string, number: number, context: string, domain?: string) => string} _nx\n *\n * Translates and retrieves the singular or plural form based on the supplied\n * number, with gettext context.\n *\n * @see https://developer.wordpress.org/reference/functions/_nx/\n */\n/**\n * @typedef {() => boolean} IsRtl\n *\n * Check if current locale is RTL.\n *\n * **RTL (Right To Left)** is a locale property indicating that text is written from right to left.\n * For example, the `he` locale (for Hebrew) specifies right-to-left. Arabic (ar) is another common\n * language written RTL. The opposite of RTL, LTR (Left To Right) is used in other languages,\n * including English (`en`, `en-US`, `en-GB`, etc.), Spanish (`es`), and French (`fr`).\n */\n/**\n * @typedef {(single: string, context?: string, domain?: string) => boolean} HasTranslation\n *\n * Check if there is a translation for a given string in singular form.\n */\n/** @typedef {import('@wordpress/hooks').Hooks} Hooks */\n\n/**\n * An i18n instance\n *\n * @typedef I18n\n * @property {GetLocaleData} getLocaleData Returns locale data by domain in a Jed-formatted JSON object shape.\n * @property {SetLocaleData} setLocaleData Merges locale data into the Tannin instance by domain. Note that this\n * function will overwrite the domain configuration. Accepts data in a\n * Jed-formatted JSON object shape.\n * @property {AddLocaleData} addLocaleData Merges locale data into the Tannin instance by domain. Note that this\n * function will also merge the domain configuration. Accepts data in a\n * Jed-formatted JSON object shape.\n * @property {ResetLocaleData} resetLocaleData Resets all current Tannin instance locale data and sets the specified\n * locale data for the domain. Accepts data in a Jed-formatted JSON object shape.\n * @property {Subscribe} subscribe Subscribes to changes of Tannin locale data.\n * @property {__} __ Retrieve the translation of text.\n * @property {_x} _x Retrieve translated string with gettext context.\n * @property {_n} _n Translates and retrieves the singular or plural form based on the supplied\n * number.\n * @property {_nx} _nx Translates and retrieves the singular or plural form based on the supplied\n * number, with gettext context.\n * @property {IsRtl} isRTL Check if current locale is RTL.\n * @property {HasTranslation} hasTranslation Check if there is a translation for a given string.\n */\n\n/**\n * Create an i18n instance\n *\n * @param {LocaleData} [initialData] Locale data configuration.\n * @param {string} [initialDomain] Domain for which configuration applies.\n * @param {Hooks} [hooks] Hooks implementation.\n *\n * @return {I18n} I18n instance.\n */\nexport const createI18n = ( initialData, initialDomain, hooks ) => {\n\t/**\n\t * The underlying instance of Tannin to which exported functions interface.\n\t *\n\t * @type {Tannin}\n\t */\n\tconst tannin = new Tannin( {} );\n\n\tconst listeners = new Set();\n\n\tconst notifyListeners = () => {\n\t\tlisteners.forEach( ( listener ) => listener() );\n\t};\n\n\t/**\n\t * Subscribe to changes of locale data.\n\t *\n\t * @param {SubscribeCallback} callback Subscription callback.\n\t * @return {UnsubscribeCallback} Unsubscribe callback.\n\t */\n\tconst subscribe = ( callback ) => {\n\t\tlisteners.add( callback );\n\t\treturn () => listeners.delete( callback );\n\t};\n\n\t/** @type {GetLocaleData} */\n\tconst getLocaleData = ( domain = 'default' ) => tannin.data[ domain ];\n\n\t/**\n\t * @param {LocaleData} [data]\n\t * @param {string} [domain]\n\t */\n\tconst doSetLocaleData = ( data, domain = 'default' ) => {\n\t\ttannin.data[ domain ] = {\n\t\t\t...tannin.data[ domain ],\n\t\t\t...data,\n\t\t};\n\n\t\t// Populate default domain configuration (supported locale date which omits\n\t\t// a plural forms expression).\n\t\ttannin.data[ domain ][ '' ] = {\n\t\t\t...DEFAULT_LOCALE_DATA[ '' ],\n\t\t\t...tannin.data[ domain ]?.[ '' ],\n\t\t};\n\n\t\t// Clean up cached plural forms functions cache as it might be updated.\n\t\tdelete tannin.pluralForms[ domain ];\n\t};\n\n\t/** @type {SetLocaleData} */\n\tconst setLocaleData = ( data, domain ) => {\n\t\tdoSetLocaleData( data, domain );\n\t\tnotifyListeners();\n\t};\n\n\t/** @type {AddLocaleData} */\n\tconst addLocaleData = ( data, domain = 'default' ) => {\n\t\ttannin.data[ domain ] = {\n\t\t\t...tannin.data[ domain ],\n\t\t\t...data,\n\t\t\t// Populate default domain configuration (supported locale date which omits\n\t\t\t// a plural forms expression).\n\t\t\t'': {\n\t\t\t\t...DEFAULT_LOCALE_DATA[ '' ],\n\t\t\t\t...tannin.data[ domain ]?.[ '' ],\n\t\t\t\t...data?.[ '' ],\n\t\t\t},\n\t\t};\n\n\t\t// Clean up cached plural forms functions cache as it might be updated.\n\t\tdelete tannin.pluralForms[ domain ];\n\n\t\tnotifyListeners();\n\t};\n\n\t/** @type {ResetLocaleData} */\n\tconst resetLocaleData = ( data, domain ) => {\n\t\t// Reset all current Tannin locale data.\n\t\ttannin.data = {};\n\n\t\t// Reset cached plural forms functions cache.\n\t\ttannin.pluralForms = {};\n\n\t\tsetLocaleData( data, domain );\n\t};\n\n\t/**\n\t * Wrapper for Tannin's `dcnpgettext`. Populates default locale data if not\n\t * otherwise previously assigned.\n\t *\n\t * @param {string|undefined} domain Domain to retrieve the translated text.\n\t * @param {string|undefined} context Context information for the translators.\n\t * @param {string} single Text to translate if non-plural. Used as\n\t * fallback return value on a caught error.\n\t * @param {string} [plural] The text to be used if the number is\n\t * plural.\n\t * @param {number} [number] The number to compare against to use\n\t * either the singular or plural form.\n\t *\n\t * @return {string} The translated string.\n\t */\n\tconst dcnpgettext = (\n\t\tdomain = 'default',\n\t\tcontext,\n\t\tsingle,\n\t\tplural,\n\t\tnumber\n\t) => {\n\t\tif ( ! tannin.data[ domain ] ) {\n\t\t\t// Use `doSetLocaleData` to set silently, without notifying listeners.\n\t\t\tdoSetLocaleData( undefined, domain );\n\t\t}\n\n\t\treturn tannin.dcnpgettext( domain, context, single, plural, number );\n\t};\n\n\t/** @type {GetFilterDomain} */\n\tconst getFilterDomain = ( domain = 'default' ) => domain;\n\n\t/** @type {__} */\n\tconst __ = ( text, domain ) => {\n\t\tlet translation = dcnpgettext( domain, undefined, text );\n\t\tif ( ! hooks ) {\n\t\t\treturn translation;\n\t\t}\n\n\t\t/**\n\t\t * Filters text with its translation.\n\t\t *\n\t\t * @param {string} translation Translated text.\n\t\t * @param {string} text Text to translate.\n\t\t * @param {string} domain Text domain. Unique identifier for retrieving translated strings.\n\t\t */\n\t\ttranslation = /** @type {string} */ (\n\t\t\t/** @type {*} */ hooks.applyFilters(\n\t\t\t\t'i18n.gettext',\n\t\t\t\ttranslation,\n\t\t\t\ttext,\n\t\t\t\tdomain\n\t\t\t)\n\t\t);\n\t\treturn /** @type {string} */ (\n\t\t\t/** @type {*} */ hooks.applyFilters(\n\t\t\t\t'i18n.gettext_' + getFilterDomain( domain ),\n\t\t\t\ttranslation,\n\t\t\t\ttext,\n\t\t\t\tdomain\n\t\t\t)\n\t\t);\n\t};\n\n\t/** @type {_x} */\n\tconst _x = ( text, context, domain ) => {\n\t\tlet translation = dcnpgettext( domain, context, text );\n\t\tif ( ! hooks ) {\n\t\t\treturn translation;\n\t\t}\n\n\t\t/**\n\t\t * Filters text with its translation based on context information.\n\t\t *\n\t\t * @param {string} translation Translated text.\n\t\t * @param {string} text Text to translate.\n\t\t * @param {string} context Context information for the translators.\n\t\t * @param {string} domain Text domain. Unique identifier for retrieving translated strings.\n\t\t */\n\t\ttranslation = /** @type {string} */ (\n\t\t\t/** @type {*} */ hooks.applyFilters(\n\t\t\t\t'i18n.gettext_with_context',\n\t\t\t\ttranslation,\n\t\t\t\ttext,\n\t\t\t\tcontext,\n\t\t\t\tdomain\n\t\t\t)\n\t\t);\n\t\treturn /** @type {string} */ (\n\t\t\t/** @type {*} */ hooks.applyFilters(\n\t\t\t\t'i18n.gettext_with_context_' + getFilterDomain( domain ),\n\t\t\t\ttranslation,\n\t\t\t\ttext,\n\t\t\t\tcontext,\n\t\t\t\tdomain\n\t\t\t)\n\t\t);\n\t};\n\n\t/** @type {_n} */\n\tconst _n = ( single, plural, number, domain ) => {\n\t\tlet translation = dcnpgettext(\n\t\t\tdomain,\n\t\t\tundefined,\n\t\t\tsingle,\n\t\t\tplural,\n\t\t\tnumber\n\t\t);\n\t\tif ( ! hooks ) {\n\t\t\treturn translation;\n\t\t}\n\n\t\t/**\n\t\t * Filters the singular or plural form of a string.\n\t\t *\n\t\t * @param {string} translation Translated text.\n\t\t * @param {string} single The text to be used if the number is singular.\n\t\t * @param {string} plural The text to be used if the number is plural.\n\t\t * @param {string} number The number to compare against to use either the singular or plural form.\n\t\t * @param {string} domain Text domain. Unique identifier for retrieving translated strings.\n\t\t */\n\t\ttranslation = /** @type {string} */ (\n\t\t\t/** @type {*} */ hooks.applyFilters(\n\t\t\t\t'i18n.ngettext',\n\t\t\t\ttranslation,\n\t\t\t\tsingle,\n\t\t\t\tplural,\n\t\t\t\tnumber,\n\t\t\t\tdomain\n\t\t\t)\n\t\t);\n\t\treturn /** @type {string} */ (\n\t\t\t/** @type {*} */ hooks.applyFilters(\n\t\t\t\t'i18n.ngettext_' + getFilterDomain( domain ),\n\t\t\t\ttranslation,\n\t\t\t\tsingle,\n\t\t\t\tplural,\n\t\t\t\tnumber,\n\t\t\t\tdomain\n\t\t\t)\n\t\t);\n\t};\n\n\t/** @type {_nx} */\n\tconst _nx = ( single, plural, number, context, domain ) => {\n\t\tlet translation = dcnpgettext(\n\t\t\tdomain,\n\t\t\tcontext,\n\t\t\tsingle,\n\t\t\tplural,\n\t\t\tnumber\n\t\t);\n\t\tif ( ! hooks ) {\n\t\t\treturn translation;\n\t\t}\n\n\t\t/**\n\t\t * Filters the singular or plural form of a string with gettext context.\n\t\t *\n\t\t * @param {string} translation Translated text.\n\t\t * @param {string} single The text to be used if the number is singular.\n\t\t * @param {string} plural The text to be used if the number is plural.\n\t\t * @param {string} number The number to compare against to use either the singular or plural form.\n\t\t * @param {string} context Context information for the translators.\n\t\t * @param {string} domain Text domain. Unique identifier for retrieving translated strings.\n\t\t */\n\t\ttranslation = /** @type {string} */ (\n\t\t\t/** @type {*} */ hooks.applyFilters(\n\t\t\t\t'i18n.ngettext_with_context',\n\t\t\t\ttranslation,\n\t\t\t\tsingle,\n\t\t\t\tplural,\n\t\t\t\tnumber,\n\t\t\t\tcontext,\n\t\t\t\tdomain\n\t\t\t)\n\t\t);\n\n\t\treturn /** @type {string} */ (\n\t\t\t/** @type {*} */ hooks.applyFilters(\n\t\t\t\t'i18n.ngettext_with_context_' + getFilterDomain( domain ),\n\t\t\t\ttranslation,\n\t\t\t\tsingle,\n\t\t\t\tplural,\n\t\t\t\tnumber,\n\t\t\t\tcontext,\n\t\t\t\tdomain\n\t\t\t)\n\t\t);\n\t};\n\n\t/** @type {IsRtl} */\n\tconst isRTL = () => {\n\t\treturn 'rtl' === _x( 'ltr', 'text direction' );\n\t};\n\n\t/** @type {HasTranslation} */\n\tconst hasTranslation = ( single, context, domain ) => {\n\t\tconst key = context ? context + '\\u0004' + single : single;\n\t\tlet result = !! tannin.data?.[ domain ?? 'default' ]?.[ key ];\n\t\tif ( hooks ) {\n\t\t\t/**\n\t\t\t * Filters the presence of a translation in the locale data.\n\t\t\t *\n\t\t\t * @param {boolean} hasTranslation Whether the translation is present or not..\n\t\t\t * @param {string} single The singular form of the translated text (used as key in locale data)\n\t\t\t * @param {string} context Context information for the translators.\n\t\t\t * @param {string} domain Text domain. Unique identifier for retrieving translated strings.\n\t\t\t */\n\t\t\tresult = /** @type { boolean } */ (\n\t\t\t\t/** @type {*} */ hooks.applyFilters(\n\t\t\t\t\t'i18n.has_translation',\n\t\t\t\t\tresult,\n\t\t\t\t\tsingle,\n\t\t\t\t\tcontext,\n\t\t\t\t\tdomain\n\t\t\t\t)\n\t\t\t);\n\n\t\t\tresult = /** @type { boolean } */ (\n\t\t\t\t/** @type {*} */ hooks.applyFilters(\n\t\t\t\t\t'i18n.has_translation_' + getFilterDomain( domain ),\n\t\t\t\t\tresult,\n\t\t\t\t\tsingle,\n\t\t\t\t\tcontext,\n\t\t\t\t\tdomain\n\t\t\t\t)\n\t\t\t);\n\t\t}\n\t\treturn result;\n\t};\n\n\tif ( initialData ) {\n\t\tsetLocaleData( initialData, initialDomain );\n\t}\n\n\tif ( hooks ) {\n\t\t/**\n\t\t * @param {string} hookName\n\t\t */\n\t\tconst onHookAddedOrRemoved = ( hookName ) => {\n\t\t\tif ( I18N_HOOK_REGEXP.test( hookName ) ) {\n\t\t\t\tnotifyListeners();\n\t\t\t}\n\t\t};\n\n\t\thooks.addAction( 'hookAdded', 'core/i18n', onHookAddedOrRemoved );\n\t\thooks.addAction( 'hookRemoved', 'core/i18n', onHookAddedOrRemoved );\n\t}\n\n\treturn {\n\t\tgetLocaleData,\n\t\tsetLocaleData,\n\t\taddLocaleData,\n\t\tresetLocaleData,\n\t\tsubscribe,\n\t\t__,\n\t\t_x,\n\t\t_n,\n\t\t_nx,\n\t\tisRTL,\n\t\thasTranslation,\n\t};\n};\n"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,MAAM,MAAM,QAAQ;;AAE3B;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,mBAAmB,GAAG;EAC3B,EAAE,EAAE;IACH;IACAC,YAAYA,CAAEC,CAAC,EAAG;MACjB,OAAOA,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC;IACvB;EACD;AACD,CAAC;;AAED;AACA;AACA;AACA;AACA,MAAMC,gBAAgB,GAAG,yCAAyC;;AAElE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,UAAU,GAAGA,CAAEC,WAAW,EAAEC,aAAa,EAAEC,KAAK,KAAM;EAClE;AACD;AACA;AACA;AACA;EACC,MAAMC,MAAM,GAAG,IAAIT,MAAM,CAAE,CAAC,CAAE,CAAC;EAE/B,MAAMU,SAAS,GAAG,IAAIC,GAAG,CAAC,CAAC;EAE3B,MAAMC,eAAe,GAAGA,CAAA,KAAM;IAC7BF,SAAS,CAACG,OAAO,CAAIC,QAAQ,IAAMA,QAAQ,CAAC,CAAE,CAAC;EAChD,CAAC;;EAED;AACD;AACA;AACA;AACA;AACA;EACC,MAAMC,SAAS,GAAKC,QAAQ,IAAM;IACjCN,SAAS,CAACO,GAAG,CAAED,QAAS,CAAC;IACzB,OAAO,MAAMN,SAAS,CAACQ,MAAM,CAAEF,QAAS,CAAC;EAC1C,CAAC;;EAED;EACA,MAAMG,aAAa,GAAGA,CAAEC,MAAM,GAAG,SAAS,KAAMX,MAAM,CAACY,IAAI,CAAED,MAAM,CAAE;;EAErE;AACD;AACA;AACA;EACC,MAAME,eAAe,GAAGA,CAAED,IAAI,EAAED,MAAM,GAAG,SAAS,KAAM;IACvDX,MAAM,CAACY,IAAI,CAAED,MAAM,CAAE,GAAG;MACvB,GAAG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AAC2C,IAAI,CAAED,QAAS,CAAC,EAAG;QACxClC,eAAe,CAAC,CAAC;MAClB;IACD,CAAC;IAEDJ,KAAK,CAACwC,SAAS,CAAE,WAAW,EAAE,WAAW,EAAEH,oBAAqB,CAAC;IACjErC,KAAK,CAACwC,SAAS,CAAE,aAAa,EAAE,WAAW,EAAEH,oBAAqB,CAAC;EACpE;EAEA,OAAO;IACN1B,aAAa;IACbK,aAAa;IACbC,aAAa;IACbC,eAAe;IACfX,SAAS;IACTmB,EAAE;IACFI,EAAE;IACFC,EAAE;IACFC,GAAG;IACHC,KAAK;IACLC;EACD,CAAC;AACF,CAAC","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["Tannin","DEFAULT_LOCALE_DATA","plural_forms","n","I18N_HOOK_REGEXP","createI18n","initialData","initialDomain","hooks","tannin","listeners","Set","notifyListeners","forEach","listener","subscribe","callback","add","delete","getLocaleData","domain","data","doSetLocaleData","pluralForms","setLocaleData","addLocaleData","resetLocaleData","dcnpgettext","context","single","plural","number","undefined","getFilterDomain","__","text","translation","applyFilters","_x","_n","_nx","isRTL","hasTranslation","key","result","onHookAddedOrRemoved","hookName","test","addAction"],"sources":["@wordpress/i18n/src/create-i18n.ts"],"sourcesContent":["/**\n * External dependencies\n */\nimport type { TanninLocaleDomain } from 'tannin';\nimport Tannin from 'tannin';\n/**\n * Internal dependencies\n */\nimport type {\n\tgetFilterDomain,\n\tI18n,\n\tLocaleData,\n\tSubscribeCallback,\n\tTranslatableText,\n\tUnsubscribeCallback,\n} from './types';\n/**\n * WordPress dependencies\n */\nimport type { Hooks } from '@wordpress/hooks';\n/**\n * Default locale data to use for Tannin domain when not otherwise provided.\n * Assumes an English plural forms expression.\n */\nconst DEFAULT_LOCALE_DATA: LocaleData = {\n\t'': {\n\t\tplural_forms( n: number ) {\n\t\t\treturn n === 1 ? 0 : 1;\n\t\t},\n\t},\n};\n\n/*\n * Regular expression that matches i18n hooks like `i18n.gettext`, `i18n.ngettext`,\n * `i18n.gettext_domain` or `i18n.ngettext_with_context` or `i18n.has_translation`.\n */\nconst I18N_HOOK_REGEXP = /^i18n\\.(n?gettext|has_translation)(_|$)/;\n\n/**\n * Create an i18n instance\n *\n * @param [initialData] Locale data configuration.\n * @param [initialDomain] Domain for which configuration applies.\n * @param [hooks] Hooks implementation.\n *\n * @return I18n instance.\n */\nexport const createI18n = < TextDomain extends string >(\n\tinitialData?: LocaleData< TextDomain >,\n\tinitialDomain?: TextDomain,\n\thooks?: Hooks\n): I18n< TextDomain > => {\n\t/**\n\t * The underlying instance of Tannin to which exported functions interface.\n\t */\n\tconst tannin = new Tannin( {} );\n\n\tconst listeners = new Set< () => void >();\n\n\tconst notifyListeners = () => {\n\t\tlisteners.forEach( ( listener ) => listener() );\n\t};\n\n\t/**\n\t * Subscribe to changes of locale data.\n\t *\n\t * @param callback Subscription callback.\n\t * @return Unsubscribe callback.\n\t */\n\tconst subscribe = ( callback: SubscribeCallback ): UnsubscribeCallback => {\n\t\tlisteners.add( callback );\n\t\treturn () => listeners.delete( callback );\n\t};\n\n\tconst getLocaleData: I18n< TextDomain >[ 'getLocaleData' ] = (\n\t\tdomain = 'default' as TextDomain\n\t) => tannin.data[ domain ] as LocaleData< TextDomain >;\n\n\t/**\n\t * @param [data]\n\t * @param [domain]\n\t */\n\tconst doSetLocaleData = (\n\t\tdata?: LocaleData,\n\t\tdomain: TextDomain = 'default' as TextDomain\n\t) => {\n\t\ttannin.data[ domain ] = {\n\t\t\t...tannin.data[ domain ],\n\t\t\t...data,\n\t\t} as TanninLocaleDomain;\n\n\t\t// Populate default domain configuration (supported locale date which omits\n\t\t// a plural forms expression).\n\t\ttannin.data[ domain ][ '' ] = {\n\t\t\t...DEFAULT_LOCALE_DATA[ '' ],\n\t\t\t...tannin.data[ domain ]?.[ '' ],\n\t\t};\n\n\t\t// Clean up cached plural forms functions cache as it might be updated.\n\t\tdelete tannin.pluralForms[ domain ];\n\t};\n\n\tconst setLocaleData: I18n< TextDomain >[ 'setLocaleData' ] = (\n\t\tdata,\n\t\tdomain\n\t) => {\n\t\tdoSetLocaleData( data, domain );\n\t\tnotifyListeners();\n\t};\n\n\tconst addLocaleData: I18n< TextDomain >[ 'addLocaleData' ] = (\n\t\tdata,\n\t\tdomain = 'default' as TextDomain\n\t) => {\n\t\ttannin.data[ domain ] = {\n\t\t\t...tannin.data[ domain ],\n\t\t\t...data,\n\t\t\t// Populate default domain configuration (supported locale date which omits\n\t\t\t// a plural forms expression).\n\t\t\t'': {\n\t\t\t\t...DEFAULT_LOCALE_DATA[ '' ],\n\t\t\t\t...tannin.data[ domain ]?.[ '' ],\n\t\t\t\t...data?.[ '' ],\n\t\t\t},\n\t\t} as TanninLocaleDomain;\n\n\t\t// Clean up cached plural forms functions cache as it might be updated.\n\t\tdelete tannin.pluralForms[ domain ];\n\n\t\tnotifyListeners();\n\t};\n\n\tconst resetLocaleData: I18n< TextDomain >[ 'resetLocaleData' ] = (\n\t\tdata,\n\t\tdomain\n\t) => {\n\t\t// Reset all current Tannin locale data.\n\t\ttannin.data = {};\n\n\t\t// Reset cached plural forms functions cache.\n\t\ttannin.pluralForms = {};\n\n\t\tsetLocaleData( data, domain );\n\t};\n\n\t/**\n\t * Wrapper for Tannin's `dcnpgettext`. Populates default locale data if not\n\t * otherwise previously assigned.\n\t *\n\t * @param domain Domain to retrieve the translated text.\n\t * @param context Context information for the translators.\n\t * @param single Text to translate if non-plural. Used as\n\t * fallback return value on a caught error.\n\t * @param [plural] The text to be used if the number is\n\t * plural.\n\t * @param [number] The number to compare against to use\n\t * either the singular or plural form.\n\t *\n\t * @return The translated string.\n\t */\n\tconst dcnpgettext = (\n\t\tdomain = 'default' as TextDomain,\n\t\tcontext: string | void,\n\t\tsingle: string,\n\t\tplural?: string,\n\t\tnumber?: number\n\t): string => {\n\t\tif ( ! tannin.data[ domain ] ) {\n\t\t\t// Use `doSetLocaleData` to set silently, without notifying listeners.\n\t\t\tdoSetLocaleData( undefined, domain );\n\t\t}\n\n\t\treturn tannin.dcnpgettext( domain, context, single, plural, number );\n\t};\n\n\tconst getFilterDomain: getFilterDomain = ( domain ) => domain || 'default';\n\n\tconst __: I18n< TextDomain >[ '__' ] = ( text, domain ) => {\n\t\tlet translation = dcnpgettext( domain, undefined, text );\n\t\tif ( ! hooks ) {\n\t\t\treturn translation as TranslatableText< typeof text >;\n\t\t}\n\n\t\t/**\n\t\t * Filters text with its translation.\n\t\t *\n\t\t * @param translation Translated text.\n\t\t * @param text Text to translate.\n\t\t * @param domain Text domain. Unique identifier for retrieving translated strings.\n\t\t */\n\t\ttranslation = hooks.applyFilters(\n\t\t\t'i18n.gettext',\n\t\t\ttranslation,\n\t\t\ttext,\n\t\t\tdomain\n\t\t) as TranslatableText< typeof text >;\n\n\t\treturn hooks.applyFilters(\n\t\t\t'i18n.gettext_' + getFilterDomain( domain ),\n\t\t\ttranslation,\n\t\t\ttext,\n\t\t\tdomain\n\t\t) as TranslatableText< typeof text >;\n\t};\n\n\tconst _x: I18n< TextDomain >[ '_x' ] = ( text, context, domain ) => {\n\t\tlet translation = dcnpgettext( domain, context, text );\n\t\tif ( ! hooks ) {\n\t\t\treturn translation as TranslatableText< typeof text >;\n\t\t}\n\n\t\t/**\n\t\t * Filters text with its translation based on context information.\n\t\t *\n\t\t * @param translation Translated text.\n\t\t * @param text Text to translate.\n\t\t * @param context Context information for the translators.\n\t\t * @param domain Text domain. Unique identifier for retrieving translated strings.\n\t\t */\n\t\ttranslation = hooks.applyFilters(\n\t\t\t'i18n.gettext_with_context',\n\t\t\ttranslation,\n\t\t\ttext,\n\t\t\tcontext,\n\t\t\tdomain\n\t\t) as TranslatableText< typeof text >;\n\n\t\treturn hooks.applyFilters(\n\t\t\t'i18n.gettext_with_context_' + getFilterDomain( domain ),\n\t\t\ttranslation,\n\t\t\ttext,\n\t\t\tcontext,\n\t\t\tdomain\n\t\t) as TranslatableText< typeof text >;\n\t};\n\n\tconst _n: I18n< TextDomain >[ '_n' ] = (\n\t\tsingle,\n\t\tplural,\n\t\tnumber,\n\t\tdomain\n\t) => {\n\t\tlet translation = dcnpgettext(\n\t\t\tdomain,\n\t\t\tundefined,\n\t\t\tsingle,\n\t\t\tplural,\n\t\t\tnumber\n\t\t);\n\t\tif ( ! hooks ) {\n\t\t\treturn translation as TranslatableText<\n\t\t\t\ttypeof single | typeof plural\n\t\t\t>;\n\t\t}\n\n\t\t/**\n\t\t * Filters the singular or plural form of a string.\n\t\t *\n\t\t * @param translation Translated text.\n\t\t * @param single The text to be used if the number is singular.\n\t\t * @param plural The text to be used if the number is plural.\n\t\t * @param number The number to compare against to use either the singular or plural form.\n\t\t * @param domain Text domain. Unique identifier for retrieving translated strings.\n\t\t */\n\t\ttranslation = hooks.applyFilters(\n\t\t\t'i18n.ngettext',\n\t\t\ttranslation,\n\t\t\tsingle,\n\t\t\tplural,\n\t\t\tnumber,\n\t\t\tdomain\n\t\t) as TranslatableText< typeof single | typeof plural >;\n\n\t\treturn hooks.applyFilters(\n\t\t\t'i18n.ngettext_' + getFilterDomain( domain ),\n\t\t\ttranslation,\n\t\t\tsingle,\n\t\t\tplural,\n\t\t\tnumber,\n\t\t\tdomain\n\t\t) as TranslatableText< typeof single | typeof plural >;\n\t};\n\n\tconst _nx: I18n< TextDomain >[ '_nx' ] = (\n\t\tsingle,\n\t\tplural,\n\t\tnumber,\n\t\tcontext,\n\t\tdomain\n\t) => {\n\t\tlet translation = dcnpgettext(\n\t\t\tdomain,\n\t\t\tcontext,\n\t\t\tsingle,\n\t\t\tplural,\n\t\t\tnumber\n\t\t);\n\t\tif ( ! hooks ) {\n\t\t\treturn translation as TranslatableText<\n\t\t\t\ttypeof single | typeof plural\n\t\t\t>;\n\t\t}\n\n\t\t/**\n\t\t * Filters the singular or plural form of a string with gettext context.\n\t\t *\n\t\t * @param translation Translated text.\n\t\t * @param single The text to be used if the number is singular.\n\t\t * @param plural The text to be used if the number is plural.\n\t\t * @param number The number to compare against to use either the singular or plural form.\n\t\t * @param context Context information for the translators.\n\t\t * @param domain Text domain. Unique identifier for retrieving translated strings.\n\t\t */\n\t\ttranslation = hooks.applyFilters(\n\t\t\t'i18n.ngettext_with_context',\n\t\t\ttranslation,\n\t\t\tsingle,\n\t\t\tplural,\n\t\t\tnumber,\n\t\t\tcontext,\n\t\t\tdomain\n\t\t) as TranslatableText< typeof single | typeof plural >;\n\n\t\treturn hooks.applyFilters(\n\t\t\t'i18n.ngettext_with_context_' + getFilterDomain( domain ),\n\t\t\ttranslation,\n\t\t\tsingle,\n\t\t\tplural,\n\t\t\tnumber,\n\t\t\tcontext,\n\t\t\tdomain\n\t\t) as TranslatableText< typeof single | typeof plural >;\n\t};\n\n\tconst isRTL: I18n< TextDomain >[ 'isRTL' ] = () => {\n\t\treturn 'rtl' === _x( 'ltr', 'text direction' );\n\t};\n\n\tconst hasTranslation: I18n< TextDomain >[ 'hasTranslation' ] = (\n\t\tsingle,\n\t\tcontext,\n\t\tdomain\n\t) => {\n\t\tconst key = context ? context + '\\u0004' + single : single;\n\t\tlet result = !! tannin.data?.[ domain ?? 'default' ]?.[ key ];\n\t\tif ( hooks ) {\n\t\t\t/**\n\t\t\t * Filters the presence of a translation in the locale data.\n\t\t\t *\n\t\t\t * @param hasTranslation Whether the translation is present or not..\n\t\t\t * @param single The singular form of the translated text (used as key in locale data)\n\t\t\t * @param context Context information for the translators.\n\t\t\t * @param domain Text domain. Unique identifier for retrieving translated strings.\n\t\t\t */\n\t\t\tresult = hooks.applyFilters(\n\t\t\t\t'i18n.has_translation',\n\t\t\t\tresult,\n\t\t\t\tsingle,\n\t\t\t\tcontext,\n\t\t\t\tdomain\n\t\t\t) as boolean;\n\n\t\t\tresult = hooks.applyFilters(\n\t\t\t\t'i18n.has_translation_' + getFilterDomain( domain ),\n\t\t\t\tresult,\n\t\t\t\tsingle,\n\t\t\t\tcontext,\n\t\t\t\tdomain\n\t\t\t) as boolean;\n\t\t}\n\t\treturn result;\n\t};\n\n\tif ( initialData ) {\n\t\tsetLocaleData( initialData, initialDomain );\n\t}\n\n\tif ( hooks ) {\n\t\t/**\n\t\t * @param hookName\n\t\t */\n\t\tconst onHookAddedOrRemoved = ( hookName: string ) => {\n\t\t\tif ( I18N_HOOK_REGEXP.test( hookName ) ) {\n\t\t\t\tnotifyListeners();\n\t\t\t}\n\t\t};\n\n\t\thooks.addAction( 'hookAdded', 'core/i18n', onHookAddedOrRemoved );\n\t\thooks.addAction( 'hookRemoved', 'core/i18n', onHookAddedOrRemoved );\n\t}\n\n\treturn {\n\t\tgetLocaleData,\n\t\tsetLocaleData,\n\t\taddLocaleData,\n\t\tresetLocaleData,\n\t\tsubscribe,\n\t\t__,\n\t\t_x,\n\t\t_n,\n\t\t_nx,\n\t\tisRTL,\n\t\thasTranslation,\n\t};\n};\n"],"mappings":"AAAA;AACA;AACA;;AAEA,OAAOA,MAAM,MAAM,QAAQ;AAC3B;AACA;AACA;;AASA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA,MAAMC,mBAA+B,GAAG;EACvC,EAAE,EAAE;IACHC,YAAYA,CAAEC,CAAS,EAAG;MACzB,OAAOA,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC;IACvB;EACD;AACD,CAAC;;AAED;AACA;AACA;AACA;AACA,MAAMC,gBAAgB,GAAG,yCAAyC;;AAElE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,UAAU,GAAGA,CACzBC,WAAsC,EACtCC,aAA0B,EAC1BC,KAAa,KACW;EACxB;AACD;AACA;EACC,MAAMC,MAAM,GAAG,IAAIT,MAAM,CAAE,CAAC,CAAE,CAAC;EAE/B,MAAMU,SAAS,GAAG,IAAIC,GAAG,CAAe,CAAC;EAEzC,MAAMC,eAAe,GAAGA,CAAA,KAAM;IAC7BF,SAAS,CAACG,OAAO,CAAIC,QAAQ,IAAMA,QAAQ,CAAC,CAAE,CAAC;EAChD,CAAC;;EAED;AACD;AACA;AACA;AACA;AACA;EACC,MAAMC,SAAS,GAAKC,QAA2B,IAA2B;IAC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EACd,CAAC;EAED,IAAKtC,WAAW,EAAG;IAClBkB,aAAa,CAAElB,WAAW,EAAEC,aAAc,CAAC;EAC5C;EAEA,IAAKC,KAAK,EAAG;IACZ;AACF;AACA;IACE,MAAMqC,oBAAoB,GAAKC,QAAgB,IAAM;MACpD,IAAK1C,gBAAgB,CAAC2C,IAAI,CAAED,QAAS,CAAC,EAAG;QACxClC,eAAe,CAAC,CAAC;MAClB;IACD,CAAC;IAEDJ,KAAK,CAACwC,SAAS,CAAE,WAAW,EAAE,WAAW,EAAEH,oBAAqB,CAAC;IACjErC,KAAK,CAACwC,SAAS,CAAE,aAAa,EAAE,WAAW,EAAEH,oBAAqB,CAAC;EACpE;EAEA,OAAO;IACN1B,aAAa;IACbK,aAAa;IACbC,aAAa;IACbC,eAAe;IACfX,SAAS;IACTmB,EAAE;IACFI,EAAE;IACFC,EAAE;IACFC,GAAG;IACHC,KAAK;IACLC;EACD,CAAC;AACF,CAAC","ignoreList":[]}
|
|
@@ -19,19 +19,13 @@ export default i18n;
|
|
|
19
19
|
* https://github.com/WordPress/gutenberg/pull/20318#issuecomment-590837722
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
-
/**
|
|
23
|
-
* @typedef {import('./create-i18n').LocaleData} LocaleData
|
|
24
|
-
* @typedef {import('./create-i18n').SubscribeCallback} SubscribeCallback
|
|
25
|
-
* @typedef {import('./create-i18n').UnsubscribeCallback} UnsubscribeCallback
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
22
|
/**
|
|
29
23
|
* Returns locale data by domain in a Jed-formatted JSON object shape.
|
|
30
24
|
*
|
|
31
25
|
* @see http://messageformat.github.io/Jed/
|
|
32
26
|
*
|
|
33
|
-
* @param {string} [domain] Domain for which to get the data.
|
|
34
|
-
* @return {LocaleData} Locale data.
|
|
27
|
+
* @param { string | undefined } [domain] Domain for which to get the data.
|
|
28
|
+
* @return { LocaleData } Locale data.
|
|
35
29
|
*/
|
|
36
30
|
export const getLocaleData = i18n.getLocaleData.bind(i18n);
|
|
37
31
|
|
|
@@ -41,8 +35,8 @@ export const getLocaleData = i18n.getLocaleData.bind(i18n);
|
|
|
41
35
|
*
|
|
42
36
|
* @see http://messageformat.github.io/Jed/
|
|
43
37
|
*
|
|
44
|
-
* @param {LocaleData}
|
|
45
|
-
* @param {string}
|
|
38
|
+
* @param {LocaleData } [data] Locale data configuration.
|
|
39
|
+
* @param {string | undefined} [domain] Domain for which configuration applies.
|
|
46
40
|
*/
|
|
47
41
|
export const setLocaleData = i18n.setLocaleData.bind(i18n);
|
|
48
42
|
|
|
@@ -52,8 +46,8 @@ export const setLocaleData = i18n.setLocaleData.bind(i18n);
|
|
|
52
46
|
*
|
|
53
47
|
* @see http://messageformat.github.io/Jed/
|
|
54
48
|
*
|
|
55
|
-
* @param {LocaleData}
|
|
56
|
-
* @param {string}
|
|
49
|
+
* @param {LocaleData} [data] Locale data configuration.
|
|
50
|
+
* @param {string | undefined} [domain] Domain for which configuration applies.
|
|
57
51
|
*/
|
|
58
52
|
export const resetLocaleData = i18n.resetLocaleData.bind(i18n);
|
|
59
53
|
|
|
@@ -70,10 +64,12 @@ export const subscribe = i18n.subscribe.bind(i18n);
|
|
|
70
64
|
*
|
|
71
65
|
* @see https://developer.wordpress.org/reference/functions/__/
|
|
72
66
|
*
|
|
73
|
-
* @
|
|
74
|
-
*
|
|
67
|
+
* @template {string} Text
|
|
68
|
+
*
|
|
69
|
+
* @param {Text} text Text to translate.
|
|
70
|
+
* @param {string | undefined} domain Domain to retrieve the translated text.
|
|
75
71
|
*
|
|
76
|
-
* @return {
|
|
72
|
+
* @return {TranslatableText<Text>} Translated text.
|
|
77
73
|
*/
|
|
78
74
|
export const __ = i18n.__.bind(i18n);
|
|
79
75
|
|
|
@@ -82,11 +78,13 @@ export const __ = i18n.__.bind(i18n);
|
|
|
82
78
|
*
|
|
83
79
|
* @see https://developer.wordpress.org/reference/functions/_x/
|
|
84
80
|
*
|
|
85
|
-
* @
|
|
86
|
-
* @param {string} context Context information for the translators.
|
|
87
|
-
* @param {string} [domain] Domain to retrieve the translated text.
|
|
81
|
+
* @template {string} Text
|
|
88
82
|
*
|
|
89
|
-
* @
|
|
83
|
+
* @param {Text} text Text to translate.
|
|
84
|
+
* @param {string} context Context information for the translators.
|
|
85
|
+
* @param {string | undefined} domain Domain to retrieve the translated text.
|
|
86
|
+
*
|
|
87
|
+
* @return {TranslatableText<Text>} Translated context string without pipe.
|
|
90
88
|
*/
|
|
91
89
|
export const _x = i18n._x.bind(i18n);
|
|
92
90
|
|
|
@@ -96,13 +94,16 @@ export const _x = i18n._x.bind(i18n);
|
|
|
96
94
|
*
|
|
97
95
|
* @see https://developer.wordpress.org/reference/functions/_n/
|
|
98
96
|
*
|
|
99
|
-
* @
|
|
100
|
-
* @
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
* @param {
|
|
97
|
+
* @template {string} Single
|
|
98
|
+
* @template {string} Plural
|
|
99
|
+
*
|
|
100
|
+
* @param {Single} single The text to be used if the number is singular.
|
|
101
|
+
* @param {Plural} plural The text to be used if the number is plural.
|
|
102
|
+
* @param {number} number The number to compare against to use either the
|
|
103
|
+
* singular or plural form.
|
|
104
|
+
* @param {string | undefined} domain Domain to retrieve the translated text.
|
|
104
105
|
*
|
|
105
|
-
* @return {
|
|
106
|
+
* @return {TranslatableText<Single | Plural>} The translated singular or plural form.
|
|
106
107
|
*/
|
|
107
108
|
export const _n = i18n._n.bind(i18n);
|
|
108
109
|
|
|
@@ -112,14 +113,18 @@ export const _n = i18n._n.bind(i18n);
|
|
|
112
113
|
*
|
|
113
114
|
* @see https://developer.wordpress.org/reference/functions/_nx/
|
|
114
115
|
*
|
|
115
|
-
* @
|
|
116
|
-
* @
|
|
117
|
-
* @param {
|
|
118
|
-
* singular or plural form.
|
|
119
|
-
* @param {string} context Context information for the translators.
|
|
120
|
-
* @param {string} [domain] Domain to retrieve the translated text.
|
|
116
|
+
* @template {string} Single
|
|
117
|
+
* @template {string} Plural
|
|
118
|
+
* @param {Single} single The text to be used if the number is singular.
|
|
121
119
|
*
|
|
122
|
-
* @
|
|
120
|
+
* @param {Single} single The text to be used if the number is singular.
|
|
121
|
+
* @param {Plural} plural The text to be used if the number is plural.
|
|
122
|
+
* @param {number} number The number to compare against to use either the
|
|
123
|
+
* singular or plural form.
|
|
124
|
+
* @param {string} context Context information for the translators.
|
|
125
|
+
* @param {string | undefined} [domain] Domain to retrieve the translated text.
|
|
126
|
+
*
|
|
127
|
+
* @return {TranslatableText<Single | Plural>} The translated singular or plural form.
|
|
123
128
|
*/
|
|
124
129
|
export const _nx = i18n._nx.bind(i18n);
|
|
125
130
|
|
|
@@ -138,9 +143,10 @@ export const isRTL = i18n.isRTL.bind(i18n);
|
|
|
138
143
|
/**
|
|
139
144
|
* Check if there is a translation for a given string (in singular form).
|
|
140
145
|
*
|
|
141
|
-
* @param {string} single
|
|
142
|
-
* @param {string}
|
|
143
|
-
* @param {string}
|
|
146
|
+
* @param {string} single Singular form of the string to look up.
|
|
147
|
+
* @param {string} context Context information for the translators.
|
|
148
|
+
* @param {string} domain Domain to retrieve the translated text.
|
|
149
|
+
*
|
|
144
150
|
* @return {boolean} Whether the translation exists or not.
|
|
145
151
|
*/
|
|
146
152
|
export const hasTranslation = i18n.hasTranslation.bind(i18n);
|