@wordpress/i18n 6.0.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.
Files changed (39) hide show
  1. package/README.md +20 -19
  2. package/build/create-i18n.js +60 -205
  3. package/build/create-i18n.js.map +1 -1
  4. package/build/default-i18n.js +31 -34
  5. package/build/default-i18n.js.map +1 -1
  6. package/build/index.js.map +1 -1
  7. package/build/sprintf.js +3 -4
  8. package/build/sprintf.js.map +1 -1
  9. package/build/types.js.map +1 -1
  10. package/build-module/create-i18n.js +60 -205
  11. package/build-module/create-i18n.js.map +1 -1
  12. package/build-module/default-i18n.js +31 -35
  13. package/build-module/default-i18n.js.map +1 -1
  14. package/build-module/index.js.map +1 -1
  15. package/build-module/sprintf.js +3 -4
  16. package/build-module/sprintf.js.map +1 -1
  17. package/build-module/types.js.map +1 -1
  18. package/build-types/create-i18n.d.ts +11 -115
  19. package/build-types/create-i18n.d.ts.map +1 -1
  20. package/build-types/default-i18n.d.ts +45 -47
  21. package/build-types/default-i18n.d.ts.map +1 -1
  22. package/build-types/index.d.ts +4 -3
  23. package/build-types/index.d.ts.map +1 -1
  24. package/build-types/sprintf.d.ts +1 -4
  25. package/build-types/sprintf.d.ts.map +1 -1
  26. package/build-types/types.d.ts +101 -0
  27. package/build-types/types.d.ts.map +1 -1
  28. package/package.json +3 -3
  29. package/src/create-i18n.ts +405 -0
  30. package/src/{default-i18n.js → default-i18n.ts} +37 -35
  31. package/src/index.ts +16 -0
  32. package/src/sprintf.ts +4 -10
  33. package/src/test/{create-i18n.js → create-i18n.ts} +63 -30
  34. package/src/test/{sprintf.js → sprintf.ts} +5 -1
  35. package/src/test/{subscribe-i18n.js → subscribe-i18n.ts} +3 -3
  36. package/src/types.ts +155 -0
  37. package/tsconfig.tsbuildinfo +1 -1
  38. package/src/create-i18n.js +0 -514
  39. /package/src/test/{default-i18n.js → default-i18n.ts} +0 -0
@@ -1,514 +0,0 @@
1
- /**
2
- * External dependencies
3
- */
4
- import Tannin from 'tannin';
5
-
6
- /**
7
- * @typedef {Record<string,any>} LocaleData
8
- */
9
-
10
- /**
11
- * Default locale data to use for Tannin domain when not otherwise provided.
12
- * Assumes an English plural forms expression.
13
- *
14
- * @type {LocaleData}
15
- */
16
- const DEFAULT_LOCALE_DATA = {
17
- '': {
18
- /** @param {number} n */
19
- plural_forms( n ) {
20
- return n === 1 ? 0 : 1;
21
- },
22
- },
23
- };
24
-
25
- /*
26
- * Regular expression that matches i18n hooks like `i18n.gettext`, `i18n.ngettext`,
27
- * `i18n.gettext_domain` or `i18n.ngettext_with_context` or `i18n.has_translation`.
28
- */
29
- const I18N_HOOK_REGEXP = /^i18n\.(n?gettext|has_translation)(_|$)/;
30
-
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 extends string>(text: Text, domain?: string) => import('./types').TranslatableText< Text >} __
78
- *
79
- * Retrieve the translation of text.
80
- *
81
- * @see https://developer.wordpress.org/reference/functions/__/
82
- */
83
- /**
84
- * @typedef {<Text extends string>(text: Text, context: string, domain?: string) => import('./types').TranslatableText< Text >} _x
85
- *
86
- * Retrieve translated string with gettext context.
87
- *
88
- * @see https://developer.wordpress.org/reference/functions/_x/
89
- */
90
- /**
91
- * @typedef {<Single extends string, Plural extends string>(single: Single, plural: Plural, number: number, domain?: string) => import('./types').TranslatableText< Single | Plural >} _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 extends string, Plural extends string>(single: Single, plural: Plural, number: number, context: string, domain?: string) => import('./types').TranslatableText< Single | Plural >} _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
- /**
148
- * Create an i18n instance
149
- *
150
- * @param {LocaleData} [initialData] Locale data configuration.
151
- * @param {string} [initialDomain] Domain for which configuration applies.
152
- * @param {Hooks} [hooks] Hooks implementation.
153
- *
154
- * @return {I18n} I18n instance.
155
- */
156
- export const createI18n = ( initialData, initialDomain, hooks ) => {
157
- /**
158
- * The underlying instance of Tannin to which exported functions interface.
159
- *
160
- * @type {Tannin}
161
- */
162
- const tannin = new Tannin( {} );
163
-
164
- const listeners = new Set();
165
-
166
- const notifyListeners = () => {
167
- listeners.forEach( ( listener ) => listener() );
168
- };
169
-
170
- /**
171
- * Subscribe to changes of locale data.
172
- *
173
- * @param {SubscribeCallback} callback Subscription callback.
174
- * @return {UnsubscribeCallback} Unsubscribe callback.
175
- */
176
- const subscribe = ( callback ) => {
177
- listeners.add( callback );
178
- return () => listeners.delete( callback );
179
- };
180
-
181
- /** @type {GetLocaleData} */
182
- const getLocaleData = ( domain = 'default' ) => tannin.data[ domain ];
183
-
184
- /**
185
- * @param {LocaleData} [data]
186
- * @param {string} [domain]
187
- */
188
- const doSetLocaleData = ( data, domain = 'default' ) => {
189
- tannin.data[ domain ] = {
190
- ...tannin.data[ domain ],
191
- ...data,
192
- };
193
-
194
- // Populate default domain configuration (supported locale date which omits
195
- // a plural forms expression).
196
- tannin.data[ domain ][ '' ] = {
197
- ...DEFAULT_LOCALE_DATA[ '' ],
198
- ...tannin.data[ domain ]?.[ '' ],
199
- };
200
-
201
- // Clean up cached plural forms functions cache as it might be updated.
202
- delete tannin.pluralForms[ domain ];
203
- };
204
-
205
- /** @type {SetLocaleData} */
206
- const setLocaleData = ( data, domain ) => {
207
- doSetLocaleData( data, domain );
208
- notifyListeners();
209
- };
210
-
211
- /** @type {AddLocaleData} */
212
- const addLocaleData = ( data, domain = 'default' ) => {
213
- tannin.data[ domain ] = {
214
- ...tannin.data[ domain ],
215
- ...data,
216
- // Populate default domain configuration (supported locale date which omits
217
- // a plural forms expression).
218
- '': {
219
- ...DEFAULT_LOCALE_DATA[ '' ],
220
- ...tannin.data[ domain ]?.[ '' ],
221
- ...data?.[ '' ],
222
- },
223
- };
224
-
225
- // Clean up cached plural forms functions cache as it might be updated.
226
- delete tannin.pluralForms[ domain ];
227
-
228
- notifyListeners();
229
- };
230
-
231
- /** @type {ResetLocaleData} */
232
- const resetLocaleData = ( data, domain ) => {
233
- // Reset all current Tannin locale data.
234
- tannin.data = {};
235
-
236
- // Reset cached plural forms functions cache.
237
- tannin.pluralForms = {};
238
-
239
- setLocaleData( data, domain );
240
- };
241
-
242
- /**
243
- * Wrapper for Tannin's `dcnpgettext`. Populates default locale data if not
244
- * otherwise previously assigned.
245
- *
246
- * @param {string|undefined} domain Domain to retrieve the translated text.
247
- * @param {string|undefined} context Context information for the translators.
248
- * @param {string} single Text to translate if non-plural. Used as
249
- * fallback return value on a caught error.
250
- * @param {string} [plural] The text to be used if the number is
251
- * plural.
252
- * @param {number} [number] The number to compare against to use
253
- * either the singular or plural form.
254
- *
255
- * @return {string} The translated string.
256
- */
257
- const dcnpgettext = (
258
- domain = 'default',
259
- context,
260
- single,
261
- plural,
262
- number
263
- ) => {
264
- if ( ! tannin.data[ domain ] ) {
265
- // Use `doSetLocaleData` to set silently, without notifying listeners.
266
- doSetLocaleData( undefined, domain );
267
- }
268
-
269
- return tannin.dcnpgettext( domain, context, single, plural, number );
270
- };
271
-
272
- /** @type {GetFilterDomain} */
273
- const getFilterDomain = ( domain = 'default' ) => domain;
274
-
275
- /** @type {__} */
276
- const __ = ( text, domain ) => {
277
- let translation = dcnpgettext( domain, undefined, text );
278
- if ( ! hooks ) {
279
- return /** @type {import('./types').TranslatableText<typeof text>} */ (
280
- translation
281
- );
282
- }
283
-
284
- /**
285
- * Filters text with its translation.
286
- *
287
- * @param {string} translation Translated text.
288
- * @param {string} text Text to translate.
289
- * @param {string} domain Text domain. Unique identifier for retrieving translated strings.
290
- */
291
- translation = /** @type {string} */ (
292
- /** @type {*} */ hooks.applyFilters(
293
- 'i18n.gettext',
294
- translation,
295
- text,
296
- domain
297
- )
298
- );
299
- return /** @type {import('./types').TranslatableText<typeof text>} */ (
300
- /** @type {*} */ hooks.applyFilters(
301
- 'i18n.gettext_' + getFilterDomain( domain ),
302
- translation,
303
- text,
304
- domain
305
- )
306
- );
307
- };
308
-
309
- /** @type {_x} */
310
- const _x = ( text, context, domain ) => {
311
- let translation = dcnpgettext( domain, context, text );
312
- if ( ! hooks ) {
313
- return /** @type {import('./types').TranslatableText<typeof text>} */ (
314
- translation
315
- );
316
- }
317
-
318
- /**
319
- * Filters text with its translation based on context information.
320
- *
321
- * @param {string} translation Translated text.
322
- * @param {string} text Text to translate.
323
- * @param {string} context Context information for the translators.
324
- * @param {string} domain Text domain. Unique identifier for retrieving translated strings.
325
- */
326
- translation = /** @type {string} */ (
327
- /** @type {*} */ hooks.applyFilters(
328
- 'i18n.gettext_with_context',
329
- translation,
330
- text,
331
- context,
332
- domain
333
- )
334
- );
335
- return /** @type {import('./types').TranslatableText<typeof text>} */ (
336
- /** @type {*} */ hooks.applyFilters(
337
- 'i18n.gettext_with_context_' + getFilterDomain( domain ),
338
- translation,
339
- text,
340
- context,
341
- domain
342
- )
343
- );
344
- };
345
-
346
- /** @type {_n} */
347
- const _n = ( single, plural, number, domain ) => {
348
- let translation = dcnpgettext(
349
- domain,
350
- undefined,
351
- single,
352
- plural,
353
- number
354
- );
355
- if ( ! hooks ) {
356
- return /** @type {import('./types').TranslatableText<typeof single | typeof plural>} */ (
357
- translation
358
- );
359
- }
360
-
361
- /**
362
- * Filters the singular or plural form of a string.
363
- *
364
- * @param {string} translation Translated text.
365
- * @param {string} single The text to be used if the number is singular.
366
- * @param {string} plural The text to be used if the number is plural.
367
- * @param {string} number The number to compare against to use either the singular or plural form.
368
- * @param {string} domain Text domain. Unique identifier for retrieving translated strings.
369
- */
370
- translation = /** @type {string} */ (
371
- /** @type {*} */ hooks.applyFilters(
372
- 'i18n.ngettext',
373
- translation,
374
- single,
375
- plural,
376
- number,
377
- domain
378
- )
379
- );
380
- return /** @type {import('./types').TranslatableText<typeof single | typeof plural>} */ (
381
- /** @type {*} */ hooks.applyFilters(
382
- 'i18n.ngettext_' + getFilterDomain( domain ),
383
- translation,
384
- single,
385
- plural,
386
- number,
387
- domain
388
- )
389
- );
390
- };
391
-
392
- /** @type {_nx} */
393
- const _nx = ( single, plural, number, context, domain ) => {
394
- let translation = dcnpgettext(
395
- domain,
396
- context,
397
- single,
398
- plural,
399
- number
400
- );
401
- if ( ! hooks ) {
402
- return /** @type {import('./types').TranslatableText<typeof single | typeof plural>} */ (
403
- translation
404
- );
405
- }
406
-
407
- /**
408
- * Filters the singular or plural form of a string with gettext context.
409
- *
410
- * @param {string} translation Translated text.
411
- * @param {string} single The text to be used if the number is singular.
412
- * @param {string} plural The text to be used if the number is plural.
413
- * @param {string} number The number to compare against to use either the singular or plural form.
414
- * @param {string} context Context information for the translators.
415
- * @param {string} domain Text domain. Unique identifier for retrieving translated strings.
416
- */
417
- translation = /** @type {string} */ (
418
- /** @type {*} */ hooks.applyFilters(
419
- 'i18n.ngettext_with_context',
420
- translation,
421
- single,
422
- plural,
423
- number,
424
- context,
425
- domain
426
- )
427
- );
428
-
429
- return /** @type {import('./types').TranslatableText<typeof single | typeof plural>} */ (
430
- /** @type {*} */ hooks.applyFilters(
431
- 'i18n.ngettext_with_context_' + getFilterDomain( domain ),
432
- translation,
433
- single,
434
- plural,
435
- number,
436
- context,
437
- domain
438
- )
439
- );
440
- };
441
-
442
- /** @type {IsRtl} */
443
- const isRTL = () => {
444
- return 'rtl' === _x( 'ltr', 'text direction' );
445
- };
446
-
447
- /** @type {HasTranslation} */
448
- const hasTranslation = ( single, context, domain ) => {
449
- const key = context ? context + '\u0004' + single : single;
450
- let result = !! tannin.data?.[ domain ?? 'default' ]?.[ key ];
451
- if ( hooks ) {
452
- /**
453
- * Filters the presence of a translation in the locale data.
454
- *
455
- * @param {boolean} hasTranslation Whether the translation is present or not..
456
- * @param {string} single The singular form of the translated text (used as key in locale data)
457
- * @param {string} context Context information for the translators.
458
- * @param {string} domain Text domain. Unique identifier for retrieving translated strings.
459
- */
460
- result = /** @type { boolean } */ (
461
- /** @type {*} */ hooks.applyFilters(
462
- 'i18n.has_translation',
463
- result,
464
- single,
465
- context,
466
- domain
467
- )
468
- );
469
-
470
- result = /** @type { boolean } */ (
471
- /** @type {*} */ hooks.applyFilters(
472
- 'i18n.has_translation_' + getFilterDomain( domain ),
473
- result,
474
- single,
475
- context,
476
- domain
477
- )
478
- );
479
- }
480
- return result;
481
- };
482
-
483
- if ( initialData ) {
484
- setLocaleData( initialData, initialDomain );
485
- }
486
-
487
- if ( hooks ) {
488
- /**
489
- * @param {string} hookName
490
- */
491
- const onHookAddedOrRemoved = ( hookName ) => {
492
- if ( I18N_HOOK_REGEXP.test( hookName ) ) {
493
- notifyListeners();
494
- }
495
- };
496
-
497
- hooks.addAction( 'hookAdded', 'core/i18n', onHookAddedOrRemoved );
498
- hooks.addAction( 'hookRemoved', 'core/i18n', onHookAddedOrRemoved );
499
- }
500
-
501
- return {
502
- getLocaleData,
503
- setLocaleData,
504
- addLocaleData,
505
- resetLocaleData,
506
- subscribe,
507
- __,
508
- _x,
509
- _n,
510
- _nx,
511
- isRTL,
512
- hasTranslation,
513
- };
514
- };
File without changes