@wordpress/i18n 6.16.0 → 6.17.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 +6 -6
- package/build/create-i18n.cjs.map +2 -2
- package/build/default-i18n.cjs +1 -1
- package/build/default-i18n.cjs.map +2 -2
- package/build/sprintf.cjs.map +2 -2
- package/build/types.cjs.map +1 -1
- package/build-module/create-i18n.mjs.map +2 -2
- package/build-module/default-i18n.mjs +1 -1
- package/build-module/default-i18n.mjs.map +2 -2
- package/build-module/sprintf.mjs.map +2 -2
- package/build-types/create-i18n.d.ts +4 -4
- package/build-types/create-i18n.d.ts.map +1 -1
- package/build-types/default-i18n.d.ts +9 -9
- package/build-types/default-i18n.d.ts.map +1 -1
- package/build-types/sprintf.d.ts +3 -3
- package/build-types/sprintf.d.ts.map +1 -1
- package/build-types/types.d.ts +13 -7
- package/build-types/types.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/create-i18n.ts +19 -17
- package/src/default-i18n.ts +9 -9
- package/src/sprintf.ts +8 -8
- package/src/test/sprintf.ts +14 -0
- package/src/types.ts +14 -7
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 6.17.0 (2026-04-15)
|
|
6
|
+
|
|
7
|
+
### Enhancement
|
|
8
|
+
|
|
9
|
+
- `sprintf` now returns `TransformedText<T>` instead of `string`, preserving the format string literal type for downstream type inference (e.g., `createInterpolateElement`). ([76974](https://github.com/WordPress/gutenberg/pull/76974))
|
|
10
|
+
|
|
5
11
|
## 6.16.0 (2026-04-01)
|
|
6
12
|
|
|
7
13
|
## 6.15.0 (2026-03-18)
|
package/README.md
CHANGED
|
@@ -121,12 +121,12 @@ _Related_
|
|
|
121
121
|
|
|
122
122
|
_Parameters_
|
|
123
123
|
|
|
124
|
-
- _format_ `T |
|
|
124
|
+
- _format_ `T | TransformedText< T >`: The format of the string to generate.
|
|
125
125
|
- _args_ `DistributeSprintfArgs< T >`: Arguments to apply to the format.
|
|
126
126
|
|
|
127
127
|
_Returns_
|
|
128
128
|
|
|
129
|
-
- `
|
|
129
|
+
- `TransformedText< T >`: The formatted string.
|
|
130
130
|
|
|
131
131
|
### subscribe
|
|
132
132
|
|
|
@@ -157,7 +157,7 @@ _Parameters_
|
|
|
157
157
|
|
|
158
158
|
_Returns_
|
|
159
159
|
|
|
160
|
-
- `
|
|
160
|
+
- `TransformedText<Single | Plural>`: The translated singular or plural form.
|
|
161
161
|
|
|
162
162
|
### \_nx
|
|
163
163
|
|
|
@@ -178,7 +178,7 @@ _Parameters_
|
|
|
178
178
|
|
|
179
179
|
_Returns_
|
|
180
180
|
|
|
181
|
-
- `
|
|
181
|
+
- `TransformedText<Single | Plural>`: The translated singular or plural form.
|
|
182
182
|
|
|
183
183
|
### \_x
|
|
184
184
|
|
|
@@ -196,7 +196,7 @@ _Parameters_
|
|
|
196
196
|
|
|
197
197
|
_Returns_
|
|
198
198
|
|
|
199
|
-
- `
|
|
199
|
+
- `TransformedText<Text>`: Translated context string without pipe.
|
|
200
200
|
|
|
201
201
|
### \_\_
|
|
202
202
|
|
|
@@ -213,7 +213,7 @@ _Parameters_
|
|
|
213
213
|
|
|
214
214
|
_Returns_
|
|
215
215
|
|
|
216
|
-
- `
|
|
216
|
+
- `TransformedText<Text>`: Translated text.
|
|
217
217
|
|
|
218
218
|
<!-- END TOKEN(Autogenerated API docs) -->
|
|
219
219
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/create-i18n.ts"],
|
|
4
|
-
"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"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,oBAAmB;
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { TanninLocaleDomain } from 'tannin';\nimport Tannin from 'tannin';\n\n/**\n * WordPress dependencies\n */\nimport type { Hooks } from '@wordpress/hooks';\n\n/**\n * Internal dependencies\n */\nimport type {\n\tgetFilterDomain,\n\tI18n,\n\tLocaleData,\n\tSubscribeCallback,\n\tTransformedText,\n\tUnsubscribeCallback,\n} from './types';\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 TransformedText< 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 TransformedText< 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 TransformedText< 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 TransformedText< 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 TransformedText< 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 TransformedText< 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 TransformedText<\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 TransformedText< 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 TransformedText< 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 TransformedText<\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 TransformedText< 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 TransformedText< 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"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,oBAAmB;AAsBnB,IAAM,sBAAkC;AAAA,EACvC,IAAI;AAAA,IACH,aAAc,GAAY;AACzB,aAAO,MAAM,IAAI,IAAI;AAAA,IACtB;AAAA,EACD;AACD;AAMA,IAAM,mBAAmB;AAWlB,IAAM,aAAa,CACzB,aACA,eACA,UACwB;AAIxB,QAAM,SAAS,IAAI,cAAAA,QAAQ,CAAC,CAAE;AAE9B,QAAM,YAAY,oBAAI,IAAkB;AAExC,QAAM,kBAAkB,MAAM;AAC7B,cAAU,QAAS,CAAE,aAAc,SAAS,CAAE;AAAA,EAC/C;AAQA,QAAM,YAAY,CAAE,aAAsD;AACzE,cAAU,IAAK,QAAS;AACxB,WAAO,MAAM,UAAU,OAAQ,QAAS;AAAA,EACzC;AAEA,QAAM,gBAAuD,CAC5D,SAAS,cACL,OAAO,KAAM,MAAO;AAMzB,QAAM,kBAAkB,CACvB,MACA,SA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ACZ,gCAAgC,gBAAiB,MAAO;AAAA,MACxD;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;AAEA,QAAM,QAAuC,MAAM;AAClD,WAAO,UAAU,GAAI,OAAO,gBAAiB;AAAA,EAC9C;AAEA,QAAM,iBAAyD,CAC9D,QACA,SACA,WACI;AACJ,UAAM,MAAM,UAAU,UAAU,MAAW,SAAS;AACpD,QAAI,SAAS,CAAC,CAAE,OAAO,OAAQ,UAAU,SAAU,IAAK,GAAI;AAC5D,QAAK,OAAQ;AASZ,eAAS,MAAM;AAAA,QACd;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACD;AAEA,eAAS,MAAM;AAAA,QACd,0BAA0B,gBAAiB,MAAO;AAAA,QAClD;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACD;AAAA,IACD;AACA,WAAO;AAAA,EACR;AAEA,MAAK,aAAc;AAClB,kBAAe,aAAa,aAAc;AAAA,EAC3C;AAEA,MAAK,OAAQ;AAIZ,UAAM,uBAAuB,CAAE,aAAsB;AACpD,UAAK,iBAAiB,KAAM,QAAS,GAAI;AACxC,wBAAgB;AAAA,MACjB;AAAA,IACD;AAEA,UAAM,UAAW,aAAa,aAAa,oBAAqB;AAChE,UAAM,UAAW,eAAe,aAAa,oBAAqB;AAAA,EACnE;AAEA,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;",
|
|
6
6
|
"names": ["Tannin"]
|
|
7
7
|
}
|
package/build/default-i18n.cjs
CHANGED
|
@@ -33,8 +33,8 @@ __export(default_i18n_exports, {
|
|
|
33
33
|
subscribe: () => subscribe
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(default_i18n_exports);
|
|
36
|
-
var import_create_i18n = require("./create-i18n.cjs");
|
|
37
36
|
var import_hooks = require("@wordpress/hooks");
|
|
37
|
+
var import_create_i18n = require("./create-i18n.cjs");
|
|
38
38
|
var i18n = (0, import_create_i18n.createI18n)(void 0, void 0, import_hooks.defaultHooks);
|
|
39
39
|
var default_i18n_default = i18n;
|
|
40
40
|
var getLocaleData = i18n.getLocaleData.bind(i18n);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/default-i18n.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n *
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { defaultHooks } from '@wordpress/hooks';\n\n/**\n * Internal dependencies\n */\nimport { createI18n } from './create-i18n';\nimport {\n\tLocaleData,\n\tSubscribeCallback,\n\tTransformedText,\n\tUnsubscribeCallback,\n} from './types';\n\nconst i18n = createI18n( undefined, undefined, defaultHooks );\n\n/**\n * Default, singleton instance of `I18n`.\n */\nexport default i18n;\n\n/*\n * Comments in this file are duplicated from ./i18n due to\n * https://github.com/WordPress/gutenberg/pull/20318#issuecomment-590837722\n */\n\n/**\n * Returns locale data by domain in a Jed-formatted JSON object shape.\n *\n * @see http://messageformat.github.io/Jed/\n *\n * @param { string | undefined } [domain] Domain for which to get the data.\n * @return { LocaleData } Locale data.\n */\nexport const getLocaleData = i18n.getLocaleData.bind( i18n );\n\n/**\n * Merges locale data into the Tannin instance by domain. Accepts data in a\n * Jed-formatted JSON object shape.\n *\n * @see http://messageformat.github.io/Jed/\n *\n * @param {LocaleData } [data] Locale data configuration.\n * @param {string | undefined} [domain] Domain for which configuration applies.\n */\nexport const setLocaleData = i18n.setLocaleData.bind( i18n );\n\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 * @param {LocaleData} [data] Locale data configuration.\n * @param {string | undefined} [domain] Domain for which configuration applies.\n */\nexport const resetLocaleData = i18n.resetLocaleData.bind( i18n );\n\n/**\n * Subscribes to changes of locale data\n *\n * @param {SubscribeCallback} callback Subscription callback\n * @return {UnsubscribeCallback} Unsubscribe callback\n */\nexport const subscribe = i18n.subscribe.bind( i18n );\n\n/**\n * Retrieve the translation of text.\n *\n * @see https://developer.wordpress.org/reference/functions/__/\n *\n * @template {string} Text\n *\n * @param {Text} text Text to translate.\n * @param {string | undefined} domain Domain to retrieve the translated text.\n *\n * @return {TransformedText<Text>} Translated text.\n */\nexport const __ = i18n.__.bind( i18n );\n\n/**\n * Retrieve translated string with gettext context.\n *\n * @see https://developer.wordpress.org/reference/functions/_x/\n *\n * @template {string} Text\n *\n * @param {Text} text Text to translate.\n * @param {string} context Context information for the translators.\n * @param {string | undefined} domain Domain to retrieve the translated text.\n *\n * @return {TransformedText<Text>} Translated context string without pipe.\n */\nexport const _x = i18n._x.bind( i18n );\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 * @template {string} Single\n * @template {string} Plural\n *\n * @param {Single} single The text to be used if the number is singular.\n * @param {Plural} plural The text to be used if the number is plural.\n * @param {number} number The number to compare against to use either the\n * singular or plural form.\n * @param {string | undefined} domain Domain to retrieve the translated text.\n *\n * @return {TransformedText<Single | Plural>} The translated singular or plural form.\n */\nexport const _n = i18n._n.bind( i18n );\n\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 * @template {string} Single\n * @template {string} Plural\n * @param {Single} single The text to be used if the number is singular.\n *\n * @param {Single} single The text to be used if the number is singular.\n * @param {Plural} plural The text to be used if the number is plural.\n * @param {number} number The number to compare against to use either the\n * singular or plural form.\n * @param {string} context Context information for the translators.\n * @param {string | undefined} [domain] Domain to retrieve the translated text.\n *\n * @return {TransformedText<Single | Plural>} The translated singular or plural form.\n */\nexport const _nx = i18n._nx.bind( i18n );\n\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 * @return {boolean} Whether locale is RTL.\n */\nexport const isRTL = i18n.isRTL.bind( i18n );\n\n/**\n * Check if there is a translation for a given string (in singular form).\n *\n * @param {string} single Singular form of the string to look up.\n * @param {string} context Context information for the translators.\n * @param {string} domain Domain to retrieve the translated text.\n *\n * @return {boolean} Whether the translation exists or not.\n */\nexport const hasTranslation = i18n.hasTranslation.bind( i18n );\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,mBAA6B;AAK7B,yBAA2B;AAQ3B,IAAM,WAAO,+BAAY,QAAW,QAAW,yBAAa;AAK5D,IAAO,uBAAQ;AAeR,IAAM,gBAAgB,KAAK,cAAc,KAAM,IAAK;AAWpD,IAAM,gBAAgB,KAAK,cAAc,KAAM,IAAK;AAWpD,IAAM,kBAAkB,KAAK,gBAAgB,KAAM,IAAK;AAQxD,IAAM,YAAY,KAAK,UAAU,KAAM,IAAK;AAc5C,IAAM,KAAK,KAAK,GAAG,KAAM,IAAK;AAe9B,IAAM,KAAK,KAAK,GAAG,KAAM,IAAK;AAmB9B,IAAM,KAAK,KAAK,GAAG,KAAM,IAAK;AAqB9B,IAAM,MAAM,KAAK,IAAI,KAAM,IAAK;AAYhC,IAAM,QAAQ,KAAK,MAAM,KAAM,IAAK;AAWpC,IAAM,iBAAiB,KAAK,eAAe,KAAM,IAAK;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/build/sprintf.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/sprintf.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport _sprintf from '@tannin/sprintf';\n\n/**\n * Internal dependencies\n */\nimport type { DistributeSprintfArgs,
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAAqB;AA0Bd,SAAS,QACf,WACG,
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport _sprintf from '@tannin/sprintf';\n\n/**\n * Internal dependencies\n */\nimport type { DistributeSprintfArgs, TransformedText } from './types';\n\nexport function sprintf< T extends string >(\n\tformat: T | TransformedText< T >,\n\t...args: DistributeSprintfArgs< T >\n): TransformedText< T >;\nexport function sprintf< T extends string >(\n\tformat: T | TransformedText< T >,\n\targs: DistributeSprintfArgs< T >\n): TransformedText< T >;\n\n/**\n * Returns a formatted string.\n *\n * @param format The format of the string to generate.\n * @param args Arguments to apply to the format.\n *\n * @see https://www.npmjs.com/package/@tannin/sprintf\n *\n * @return The formatted string.\n */\nexport function sprintf< T extends string >(\n\tformat: T | TransformedText< T >,\n\t...args: DistributeSprintfArgs< T >\n): TransformedText< T > {\n\treturn _sprintf( format, ...args ) as TransformedText< T >;\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAAqB;AA0Bd,SAAS,QACf,WACG,MACoB;AACvB,aAAO,eAAAA,SAAU,QAAQ,GAAG,IAAK;AAClC;",
|
|
6
6
|
"names": ["_sprintf"]
|
|
7
7
|
}
|
package/build/types.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/types.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport type sprintf from '@tannin/sprintf';\nimport { type TanninDomainMetadata } from 'tannin';\n\n/**\n * Return type for string translation
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport type sprintf from '@tannin/sprintf';\nimport { type TanninDomainMetadata } from 'tannin';\n\n/**\n * Return type for string transformation functions (translation, sprintf).\n * Preserves the original string literal type so that downstream consumers\n * can extract information (e.g., tag names) at the type level.\n *\n * This type should be treated as if it were `string`.\n */\nexport type TransformedText< T extends string > = string & {\n\t/**\n\t * DO NOT USE! This property _does not exist_.\n\t * @private\n\t */\n\treadonly __transformedText: T;\n};\n\n/**\n * @deprecated Use `TransformedText` instead.\n */\nexport type TranslatableText< T extends string > = TransformedText< T >;\n\n/**\n * Type to extends TanninDomainMetadata to support additional properties.\n */\nexport type I18nDomainMetadata< TextDomain extends string > =\n\tTanninDomainMetadata & {\n\t\tdomain?: TextDomain;\n\t\t[ key: string ]: unknown;\n\t};\n\n/**\n * Locale data is a record of domain names to their metadata or translations.\n */\nexport type LocaleData< TextDomain extends string = string > = Record<\n\tstring,\n\tI18nDomainMetadata< TextDomain > | string[]\n>;\n\nexport type SubscribeCallback = () => void;\nexport type UnsubscribeCallback = () => void;\n\n/**\n * Retrieve the domain to use when calling domain-specific filters.\n */\nexport type getFilterDomain = ( domain?: string ) => string;\n\n/**\n * An i18n instance\n */\nexport interface I18n< TextDomain extends string = string > {\n\t/**\n\t * Returns locale data by domain in a\n\t * Jed-formatted JSON object shape.\n\t *\n\t * @see http://messageformat.github.io/Jed/\n\t */\n\tgetLocaleData: ( domain?: TextDomain ) => LocaleData< TextDomain >;\n\n\t/**\n\t * Merges locale data into the Tannin instance by domain. Note that this\n\t * function will overwrite the domain configuration. Accepts data in a\n\t * Jed-formatted JSON object shape.\n\t *\n\t * @see http://messageformat.github.io/Jed/\n\t */\n\tsetLocaleData: (\n\t\tdata?: LocaleData< TextDomain >,\n\t\tdomain?: TextDomain\n\t) => void;\n\n\t/**\n\t * Merges locale data into the Tannin instance by domain. Note that this\n\t * function will also merge the domain configuration. Accepts data in a\n\t * Jed-formatted JSON object shape.\n\t *\n\t * @see http://messageformat.github.io/Jed/\n\t */\n\taddLocaleData: (\n\t\tdata?: LocaleData< TextDomain >,\n\t\tdomain?: TextDomain\n\t) => void;\n\n\t/**\n\t * Resets all current Tannin instance locale data and sets the specified\n\t * locale data for the domain. Accepts data in a Jed-formatted JSON object shape.\n\t *\n\t * @see http://messageformat.github.io/Jed/\n\t */\n\tresetLocaleData: (\n\t\tdata?: LocaleData< TextDomain >,\n\t\tdomain?: TextDomain\n\t) => void;\n\n\t/**\n\t * Subscribes to changes of locale data\n\t */\n\tsubscribe: ( callback: SubscribeCallback ) => UnsubscribeCallback;\n\n\t/**\n\t * Retrieve the translation of text.\n\t *\n\t * @see https://developer.wordpress.org/reference/functions/__/\n\t */\n\t__: < Text extends string >(\n\t\ttext: Text,\n\t\tdomain?: TextDomain\n\t) => TransformedText< Text >;\n\n\t/**\n\t * Retrieve translated string with gettext context.\n\t *\n\t * @see https://developer.wordpress.org/reference/functions/_x/\n\t */\n\t_x: < Text extends string >(\n\t\ttext: Text,\n\t\tcontext: string,\n\t\tdomain?: TextDomain\n\t) => TransformedText< Text >;\n\n\t/**\n\t * Translates and retrieves the singular or plural form based on the supplied\n\t * number.\n\t *\n\t * @see https://developer.wordpress.org/reference/functions/_n/\n\t */\n\t_n: < Single extends string, Plural extends string >(\n\t\tsingle: Single,\n\t\tplural: Plural,\n\t\tnumber: number,\n\t\tdomain?: TextDomain\n\t) => TransformedText< Single | Plural >;\n\n\t/**\n\t * Translates and retrieves the singular or plural form based on the supplied\n\t * number, with gettext context.\n\t *\n\t * @see https://developer.wordpress.org/reference/functions/_nx/\n\t */\n\t_nx: < Single extends string, Plural extends string >(\n\t\tsingle: Single,\n\t\tplural: Plural,\n\t\tnumber: number,\n\t\tcontext: string,\n\t\tdomain?: TextDomain\n\t) => TransformedText< Single | Plural >;\n\n\t/**\n\t * Check if current locale is RTL.\n\t *\n\t * **RTL (Right To Left)** is a locale property indicating that text is written from right to left.\n\t * For example, the `he` locale (for Hebrew) specifies right-to-left. Arabic (ar) is another common\n\t * language written RTL. The opposite of RTL, LTR (Left To Right) is used in other languages,\n\t * including English (`en`, `en-US`, `en-GB`, etc.), Spanish (`es`), and French (`fr`).\n\t */\n\tisRTL: () => boolean;\n\n\t/**\n\t * Check if there is a translation for a given string in singular form.\n\t */\n\thasTranslation: (\n\t\tsingle: string,\n\t\tcontext?: string,\n\t\tdomain?: TextDomain\n\t) => boolean;\n}\n\nexport type DistributeSprintfArgs< T extends string > = T extends any\n\t? Parameters< typeof sprintf< T > >[ 1 ]\n\t: never;\n"],
|
|
5
5
|
"mappings": ";;;;;;;;;;;;;;;;AAAA;AAAA;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/create-i18n.ts"],
|
|
4
|
-
"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"],
|
|
5
|
-
"mappings": ";AAIA,OAAO,YAAY;
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { TanninLocaleDomain } from 'tannin';\nimport Tannin from 'tannin';\n\n/**\n * WordPress dependencies\n */\nimport type { Hooks } from '@wordpress/hooks';\n\n/**\n * Internal dependencies\n */\nimport type {\n\tgetFilterDomain,\n\tI18n,\n\tLocaleData,\n\tSubscribeCallback,\n\tTransformedText,\n\tUnsubscribeCallback,\n} from './types';\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 TransformedText< 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 TransformedText< 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 TransformedText< 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 TransformedText< 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 TransformedText< 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 TransformedText< 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 TransformedText<\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 TransformedText< 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 TransformedText< 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 TransformedText<\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 TransformedText< 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 TransformedText< 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\tretur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,gBAAiB,MAAO;AAAA,QAClD;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACD;AAAA,IACD;AACA,WAAO;AAAA,EACR;AAEA,MAAK,aAAc;AAClB,kBAAe,aAAa,aAAc;AAAA,EAC3C;AAEA,MAAK,OAAQ;AAIZ,UAAM,uBAAuB,CAAE,aAAsB;AACpD,UAAK,iBAAiB,KAAM,QAAS,GAAI;AACxC,wBAAgB;AAAA,MACjB;AAAA,IACD;AAEA,UAAM,UAAW,aAAa,aAAa,oBAAqB;AAChE,UAAM,UAAW,eAAe,aAAa,oBAAqB;AAAA,EACnE;AAEA,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// packages/i18n/src/default-i18n.ts
|
|
2
|
-
import { createI18n } from "./create-i18n.mjs";
|
|
3
2
|
import { defaultHooks } from "@wordpress/hooks";
|
|
3
|
+
import { createI18n } from "./create-i18n.mjs";
|
|
4
4
|
var i18n = createI18n(void 0, void 0, defaultHooks);
|
|
5
5
|
var default_i18n_default = i18n;
|
|
6
6
|
var getLocaleData = i18n.getLocaleData.bind(i18n);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/default-i18n.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n *
|
|
5
|
-
"mappings": ";AAGA,SAAS,
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { defaultHooks } from '@wordpress/hooks';\n\n/**\n * Internal dependencies\n */\nimport { createI18n } from './create-i18n';\nimport {\n\tLocaleData,\n\tSubscribeCallback,\n\tTransformedText,\n\tUnsubscribeCallback,\n} from './types';\n\nconst i18n = createI18n( undefined, undefined, defaultHooks );\n\n/**\n * Default, singleton instance of `I18n`.\n */\nexport default i18n;\n\n/*\n * Comments in this file are duplicated from ./i18n due to\n * https://github.com/WordPress/gutenberg/pull/20318#issuecomment-590837722\n */\n\n/**\n * Returns locale data by domain in a Jed-formatted JSON object shape.\n *\n * @see http://messageformat.github.io/Jed/\n *\n * @param { string | undefined } [domain] Domain for which to get the data.\n * @return { LocaleData } Locale data.\n */\nexport const getLocaleData = i18n.getLocaleData.bind( i18n );\n\n/**\n * Merges locale data into the Tannin instance by domain. Accepts data in a\n * Jed-formatted JSON object shape.\n *\n * @see http://messageformat.github.io/Jed/\n *\n * @param {LocaleData } [data] Locale data configuration.\n * @param {string | undefined} [domain] Domain for which configuration applies.\n */\nexport const setLocaleData = i18n.setLocaleData.bind( i18n );\n\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 * @param {LocaleData} [data] Locale data configuration.\n * @param {string | undefined} [domain] Domain for which configuration applies.\n */\nexport const resetLocaleData = i18n.resetLocaleData.bind( i18n );\n\n/**\n * Subscribes to changes of locale data\n *\n * @param {SubscribeCallback} callback Subscription callback\n * @return {UnsubscribeCallback} Unsubscribe callback\n */\nexport const subscribe = i18n.subscribe.bind( i18n );\n\n/**\n * Retrieve the translation of text.\n *\n * @see https://developer.wordpress.org/reference/functions/__/\n *\n * @template {string} Text\n *\n * @param {Text} text Text to translate.\n * @param {string | undefined} domain Domain to retrieve the translated text.\n *\n * @return {TransformedText<Text>} Translated text.\n */\nexport const __ = i18n.__.bind( i18n );\n\n/**\n * Retrieve translated string with gettext context.\n *\n * @see https://developer.wordpress.org/reference/functions/_x/\n *\n * @template {string} Text\n *\n * @param {Text} text Text to translate.\n * @param {string} context Context information for the translators.\n * @param {string | undefined} domain Domain to retrieve the translated text.\n *\n * @return {TransformedText<Text>} Translated context string without pipe.\n */\nexport const _x = i18n._x.bind( i18n );\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 * @template {string} Single\n * @template {string} Plural\n *\n * @param {Single} single The text to be used if the number is singular.\n * @param {Plural} plural The text to be used if the number is plural.\n * @param {number} number The number to compare against to use either the\n * singular or plural form.\n * @param {string | undefined} domain Domain to retrieve the translated text.\n *\n * @return {TransformedText<Single | Plural>} The translated singular or plural form.\n */\nexport const _n = i18n._n.bind( i18n );\n\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 * @template {string} Single\n * @template {string} Plural\n * @param {Single} single The text to be used if the number is singular.\n *\n * @param {Single} single The text to be used if the number is singular.\n * @param {Plural} plural The text to be used if the number is plural.\n * @param {number} number The number to compare against to use either the\n * singular or plural form.\n * @param {string} context Context information for the translators.\n * @param {string | undefined} [domain] Domain to retrieve the translated text.\n *\n * @return {TransformedText<Single | Plural>} The translated singular or plural form.\n */\nexport const _nx = i18n._nx.bind( i18n );\n\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 * @return {boolean} Whether locale is RTL.\n */\nexport const isRTL = i18n.isRTL.bind( i18n );\n\n/**\n * Check if there is a translation for a given string (in singular form).\n *\n * @param {string} single Singular form of the string to look up.\n * @param {string} context Context information for the translators.\n * @param {string} domain Domain to retrieve the translated text.\n *\n * @return {boolean} Whether the translation exists or not.\n */\nexport const hasTranslation = i18n.hasTranslation.bind( i18n );\n"],
|
|
5
|
+
"mappings": ";AAGA,SAAS,oBAAoB;AAK7B,SAAS,kBAAkB;AAQ3B,IAAM,OAAO,WAAY,QAAW,QAAW,YAAa;AAK5D,IAAO,uBAAQ;AAeR,IAAM,gBAAgB,KAAK,cAAc,KAAM,IAAK;AAWpD,IAAM,gBAAgB,KAAK,cAAc,KAAM,IAAK;AAWpD,IAAM,kBAAkB,KAAK,gBAAgB,KAAM,IAAK;AAQxD,IAAM,YAAY,KAAK,UAAU,KAAM,IAAK;AAc5C,IAAM,KAAK,KAAK,GAAG,KAAM,IAAK;AAe9B,IAAM,KAAK,KAAK,GAAG,KAAM,IAAK;AAmB9B,IAAM,KAAK,KAAK,GAAG,KAAM,IAAK;AAqB9B,IAAM,MAAM,KAAK,IAAI,KAAM,IAAK;AAYhC,IAAM,QAAQ,KAAK,MAAM,KAAM,IAAK;AAWpC,IAAM,iBAAiB,KAAK,eAAe,KAAM,IAAK;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/sprintf.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport _sprintf from '@tannin/sprintf';\n\n/**\n * Internal dependencies\n */\nimport type { DistributeSprintfArgs,
|
|
5
|
-
"mappings": ";AAGA,OAAO,cAAc;AA0Bd,SAAS,QACf,WACG,
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport _sprintf from '@tannin/sprintf';\n\n/**\n * Internal dependencies\n */\nimport type { DistributeSprintfArgs, TransformedText } from './types';\n\nexport function sprintf< T extends string >(\n\tformat: T | TransformedText< T >,\n\t...args: DistributeSprintfArgs< T >\n): TransformedText< T >;\nexport function sprintf< T extends string >(\n\tformat: T | TransformedText< T >,\n\targs: DistributeSprintfArgs< T >\n): TransformedText< T >;\n\n/**\n * Returns a formatted string.\n *\n * @param format The format of the string to generate.\n * @param args Arguments to apply to the format.\n *\n * @see https://www.npmjs.com/package/@tannin/sprintf\n *\n * @return The formatted string.\n */\nexport function sprintf< T extends string >(\n\tformat: T | TransformedText< T >,\n\t...args: DistributeSprintfArgs< T >\n): TransformedText< T > {\n\treturn _sprintf( format, ...args ) as TransformedText< T >;\n}\n"],
|
|
5
|
+
"mappings": ";AAGA,OAAO,cAAc;AA0Bd,SAAS,QACf,WACG,MACoB;AACvB,SAAO,SAAU,QAAQ,GAAG,IAAK;AAClC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Internal dependencies
|
|
3
|
-
*/
|
|
4
|
-
import type { I18n, LocaleData } from './types';
|
|
5
1
|
/**
|
|
6
2
|
* WordPress dependencies
|
|
7
3
|
*/
|
|
8
4
|
import type { Hooks } from '@wordpress/hooks';
|
|
5
|
+
/**
|
|
6
|
+
* Internal dependencies
|
|
7
|
+
*/
|
|
8
|
+
import type { I18n, LocaleData } from './types';
|
|
9
9
|
/**
|
|
10
10
|
* Create an i18n instance
|
|
11
11
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-i18n.d.ts","sourceRoot":"","sources":["../src/create-i18n.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"create-i18n.d.ts","sourceRoot":"","sources":["../src/create-i18n.ts"],"names":[],"mappings":"AAMA;;GAEG;AACH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAE9C;;GAEG;AACH,OAAO,KAAK,EAEX,IAAI,EACJ,UAAU,EAIV,MAAM,SAAS,CAAC;AAmBjB;;;;;;;;GAQG;AACH,eAAO,MAAM,UAAU,GAAK,UAAU,SAAS,MAAM,EACpD,cAAc,UAAU,CAAE,UAAU,CAAE,EACtC,gBAAgB,UAAU,EAC1B,QAAQ,KAAK,KACX,IAAI,CAAE,UAAU,CAiWlB,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LocaleData, SubscribeCallback,
|
|
1
|
+
import { LocaleData, SubscribeCallback, TransformedText, UnsubscribeCallback } from './types';
|
|
2
2
|
declare const i18n: import("./types").I18n<string>;
|
|
3
3
|
/**
|
|
4
4
|
* Default, singleton instance of `I18n`.
|
|
@@ -50,9 +50,9 @@ export declare const subscribe: (callback: SubscribeCallback) => UnsubscribeCall
|
|
|
50
50
|
* @param {Text} text Text to translate.
|
|
51
51
|
* @param {string | undefined} domain Domain to retrieve the translated text.
|
|
52
52
|
*
|
|
53
|
-
* @return {
|
|
53
|
+
* @return {TransformedText<Text>} Translated text.
|
|
54
54
|
*/
|
|
55
|
-
export declare const __: <Text extends string>(text: Text, domain?: string | undefined) =>
|
|
55
|
+
export declare const __: <Text extends string>(text: Text, domain?: string | undefined) => TransformedText<Text>;
|
|
56
56
|
/**
|
|
57
57
|
* Retrieve translated string with gettext context.
|
|
58
58
|
*
|
|
@@ -64,9 +64,9 @@ export declare const __: <Text extends string>(text: Text, domain?: string | und
|
|
|
64
64
|
* @param {string} context Context information for the translators.
|
|
65
65
|
* @param {string | undefined} domain Domain to retrieve the translated text.
|
|
66
66
|
*
|
|
67
|
-
* @return {
|
|
67
|
+
* @return {TransformedText<Text>} Translated context string without pipe.
|
|
68
68
|
*/
|
|
69
|
-
export declare const _x: <Text extends string>(text: Text, context: string, domain?: string | undefined) =>
|
|
69
|
+
export declare const _x: <Text extends string>(text: Text, context: string, domain?: string | undefined) => TransformedText<Text>;
|
|
70
70
|
/**
|
|
71
71
|
* Translates and retrieves the singular or plural form based on the supplied
|
|
72
72
|
* number.
|
|
@@ -82,9 +82,9 @@ export declare const _x: <Text extends string>(text: Text, context: string, doma
|
|
|
82
82
|
* singular or plural form.
|
|
83
83
|
* @param {string | undefined} domain Domain to retrieve the translated text.
|
|
84
84
|
*
|
|
85
|
-
* @return {
|
|
85
|
+
* @return {TransformedText<Single | Plural>} The translated singular or plural form.
|
|
86
86
|
*/
|
|
87
|
-
export declare const _n: <Single extends string, Plural extends string>(single: Single, plural: Plural, number: number, domain?: string | undefined) =>
|
|
87
|
+
export declare const _n: <Single extends string, Plural extends string>(single: Single, plural: Plural, number: number, domain?: string | undefined) => TransformedText<Single | Plural>;
|
|
88
88
|
/**
|
|
89
89
|
* Translates and retrieves the singular or plural form based on the supplied
|
|
90
90
|
* number, with gettext context.
|
|
@@ -102,9 +102,9 @@ export declare const _n: <Single extends string, Plural extends string>(single:
|
|
|
102
102
|
* @param {string} context Context information for the translators.
|
|
103
103
|
* @param {string | undefined} [domain] Domain to retrieve the translated text.
|
|
104
104
|
*
|
|
105
|
-
* @return {
|
|
105
|
+
* @return {TransformedText<Single | Plural>} The translated singular or plural form.
|
|
106
106
|
*/
|
|
107
|
-
export declare const _nx: <Single extends string, Plural extends string>(single: Single, plural: Plural, number: number, context: string, domain?: string | undefined) =>
|
|
107
|
+
export declare const _nx: <Single extends string, Plural extends string>(single: Single, plural: Plural, number: number, context: string, domain?: string | undefined) => TransformedText<Single | Plural>;
|
|
108
108
|
/**
|
|
109
109
|
* Check if current locale is RTL.
|
|
110
110
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"default-i18n.d.ts","sourceRoot":"","sources":["../src/default-i18n.ts"],"names":[],"mappings":"AASA,OAAO,EACN,UAAU,EACV,iBAAiB,EACjB,
|
|
1
|
+
{"version":3,"file":"default-i18n.d.ts","sourceRoot":"","sources":["../src/default-i18n.ts"],"names":[],"mappings":"AASA,OAAO,EACN,UAAU,EACV,iBAAiB,EACjB,eAAe,EACf,mBAAmB,EACnB,MAAM,SAAS,CAAC;AAEjB,QAAA,MAAM,IAAI,gCAAmD,CAAC;AAE9D;;GAEG;AACH,eAAe,IAAI,CAAC;AAOpB;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa,qDAAkC,CAAC;AAE7D;;;;;;;;GAQG;AACH,eAAO,MAAM,aAAa,8EAAkC,CAAC;AAE7D;;;;;;;;GAQG;AACH,eAAO,MAAM,eAAe,8EAAoC,CAAC;AAEjE;;;;;GAKG;AACH,eAAO,MAAM,SAAS,sDAA8B,CAAC;AAErD;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,EAAE,yFAAuB,CAAC;AAEvC;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,EAAE,0GAAuB,CAAC;AAEvC;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,EAAE,iKAAuB,CAAC;AAEvC;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,GAAG,kLAAwB,CAAC;AAEzC;;;;;;;;;GASG;AACH,eAAO,MAAM,KAAK,eAA0B,CAAC;AAE7C;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,4EAAmC,CAAC"}
|
package/build-types/sprintf.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
-
import type { DistributeSprintfArgs,
|
|
5
|
-
export declare function sprintf<T extends string>(format: T |
|
|
6
|
-
export declare function sprintf<T extends string>(format: T |
|
|
4
|
+
import type { DistributeSprintfArgs, TransformedText } from './types';
|
|
5
|
+
export declare function sprintf<T extends string>(format: T | TransformedText<T>, ...args: DistributeSprintfArgs<T>): TransformedText<T>;
|
|
6
|
+
export declare function sprintf<T extends string>(format: T | TransformedText<T>, args: DistributeSprintfArgs<T>): TransformedText<T>;
|
|
7
7
|
//# sourceMappingURL=sprintf.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sprintf.d.ts","sourceRoot":"","sources":["../src/sprintf.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,OAAO,KAAK,EAAE,qBAAqB,EAAE,
|
|
1
|
+
{"version":3,"file":"sprintf.d.ts","sourceRoot":"","sources":["../src/sprintf.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,OAAO,KAAK,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAEtE,wBAAgB,OAAO,CAAE,CAAC,SAAS,MAAM,EACxC,MAAM,EAAE,CAAC,GAAG,eAAe,CAAE,CAAC,CAAE,EAChC,GAAG,IAAI,EAAE,qBAAqB,CAAE,CAAC,CAAE,GACjC,eAAe,CAAE,CAAC,CAAE,CAAC;AACxB,wBAAgB,OAAO,CAAE,CAAC,SAAS,MAAM,EACxC,MAAM,EAAE,CAAC,GAAG,eAAe,CAAE,CAAC,CAAE,EAChC,IAAI,EAAE,qBAAqB,CAAE,CAAC,CAAE,GAC9B,eAAe,CAAE,CAAC,CAAE,CAAC"}
|
package/build-types/types.d.ts
CHANGED
|
@@ -4,17 +4,23 @@
|
|
|
4
4
|
import type sprintf from '@tannin/sprintf';
|
|
5
5
|
import { type TanninDomainMetadata } from 'tannin';
|
|
6
6
|
/**
|
|
7
|
-
* Return type for string translation
|
|
7
|
+
* Return type for string transformation functions (translation, sprintf).
|
|
8
|
+
* Preserves the original string literal type so that downstream consumers
|
|
9
|
+
* can extract information (e.g., tag names) at the type level.
|
|
8
10
|
*
|
|
9
11
|
* This type should be treated as if it were `string`.
|
|
10
12
|
*/
|
|
11
|
-
export type
|
|
13
|
+
export type TransformedText<T extends string> = string & {
|
|
12
14
|
/**
|
|
13
15
|
* DO NOT USE! This property _does not exist_.
|
|
14
16
|
* @private
|
|
15
17
|
*/
|
|
16
|
-
readonly
|
|
18
|
+
readonly __transformedText: T;
|
|
17
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* @deprecated Use `TransformedText` instead.
|
|
22
|
+
*/
|
|
23
|
+
export type TranslatableText<T extends string> = TransformedText<T>;
|
|
18
24
|
/**
|
|
19
25
|
* Type to extends TanninDomainMetadata to support additional properties.
|
|
20
26
|
*/
|
|
@@ -75,27 +81,27 @@ export interface I18n<TextDomain extends string = string> {
|
|
|
75
81
|
*
|
|
76
82
|
* @see https://developer.wordpress.org/reference/functions/__/
|
|
77
83
|
*/
|
|
78
|
-
__: <Text extends string>(text: Text, domain?: TextDomain) =>
|
|
84
|
+
__: <Text extends string>(text: Text, domain?: TextDomain) => TransformedText<Text>;
|
|
79
85
|
/**
|
|
80
86
|
* Retrieve translated string with gettext context.
|
|
81
87
|
*
|
|
82
88
|
* @see https://developer.wordpress.org/reference/functions/_x/
|
|
83
89
|
*/
|
|
84
|
-
_x: <Text extends string>(text: Text, context: string, domain?: TextDomain) =>
|
|
90
|
+
_x: <Text extends string>(text: Text, context: string, domain?: TextDomain) => TransformedText<Text>;
|
|
85
91
|
/**
|
|
86
92
|
* Translates and retrieves the singular or plural form based on the supplied
|
|
87
93
|
* number.
|
|
88
94
|
*
|
|
89
95
|
* @see https://developer.wordpress.org/reference/functions/_n/
|
|
90
96
|
*/
|
|
91
|
-
_n: <Single extends string, Plural extends string>(single: Single, plural: Plural, number: number, domain?: TextDomain) =>
|
|
97
|
+
_n: <Single extends string, Plural extends string>(single: Single, plural: Plural, number: number, domain?: TextDomain) => TransformedText<Single | Plural>;
|
|
92
98
|
/**
|
|
93
99
|
* Translates and retrieves the singular or plural form based on the supplied
|
|
94
100
|
* number, with gettext context.
|
|
95
101
|
*
|
|
96
102
|
* @see https://developer.wordpress.org/reference/functions/_nx/
|
|
97
103
|
*/
|
|
98
|
-
_nx: <Single extends string, Plural extends string>(single: Single, plural: Plural, number: number, context: string, domain?: TextDomain) =>
|
|
104
|
+
_nx: <Single extends string, Plural extends string>(single: Single, plural: Plural, number: number, context: string, domain?: TextDomain) => TransformedText<Single | Plural>;
|
|
99
105
|
/**
|
|
100
106
|
* Check if current locale is RTL.
|
|
101
107
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,OAAO,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,KAAK,oBAAoB,EAAE,MAAM,QAAQ,CAAC;AAEnD
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,OAAO,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,KAAK,oBAAoB,EAAE,MAAM,QAAQ,CAAC;AAE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AC,SAAS,GAAG,GAClE,UAAU,CAAE,OAAO,OAAO,CAAE,CAAC,CAAE,CAAE,CAAE,CAAC,CAAE,GACtC,KAAK,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/i18n",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.17.0",
|
|
4
4
|
"description": "WordPress internationalization (i18n) library.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"@tannin/sprintf": "^1.3.2",
|
|
50
|
-
"@wordpress/hooks": "^4.
|
|
50
|
+
"@wordpress/hooks": "^4.44.0",
|
|
51
51
|
"gettext-parser": "^1.3.1",
|
|
52
52
|
"memize": "^2.1.0",
|
|
53
53
|
"tannin": "^1.2.0"
|
|
@@ -58,5 +58,5 @@
|
|
|
58
58
|
"publishConfig": {
|
|
59
59
|
"access": "public"
|
|
60
60
|
},
|
|
61
|
-
"gitHead": "
|
|
61
|
+
"gitHead": "b862d8c84121a47bbeff882f6c87e61681ce2e0d"
|
|
62
62
|
}
|
package/src/create-i18n.ts
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import type { TanninLocaleDomain } from 'tannin';
|
|
5
5
|
import Tannin from 'tannin';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* WordPress dependencies
|
|
9
|
+
*/
|
|
10
|
+
import type { Hooks } from '@wordpress/hooks';
|
|
11
|
+
|
|
6
12
|
/**
|
|
7
13
|
* Internal dependencies
|
|
8
14
|
*/
|
|
@@ -11,13 +17,9 @@ import type {
|
|
|
11
17
|
I18n,
|
|
12
18
|
LocaleData,
|
|
13
19
|
SubscribeCallback,
|
|
14
|
-
|
|
20
|
+
TransformedText,
|
|
15
21
|
UnsubscribeCallback,
|
|
16
22
|
} from './types';
|
|
17
|
-
/**
|
|
18
|
-
* WordPress dependencies
|
|
19
|
-
*/
|
|
20
|
-
import type { Hooks } from '@wordpress/hooks';
|
|
21
23
|
/**
|
|
22
24
|
* Default locale data to use for Tannin domain when not otherwise provided.
|
|
23
25
|
* Assumes an English plural forms expression.
|
|
@@ -178,7 +180,7 @@ export const createI18n = < TextDomain extends string >(
|
|
|
178
180
|
const __: I18n< TextDomain >[ '__' ] = ( text, domain ) => {
|
|
179
181
|
let translation = dcnpgettext( domain, undefined, text );
|
|
180
182
|
if ( ! hooks ) {
|
|
181
|
-
return translation as
|
|
183
|
+
return translation as TransformedText< typeof text >;
|
|
182
184
|
}
|
|
183
185
|
|
|
184
186
|
/**
|
|
@@ -193,20 +195,20 @@ export const createI18n = < TextDomain extends string >(
|
|
|
193
195
|
translation,
|
|
194
196
|
text,
|
|
195
197
|
domain
|
|
196
|
-
) as
|
|
198
|
+
) as TransformedText< typeof text >;
|
|
197
199
|
|
|
198
200
|
return hooks.applyFilters(
|
|
199
201
|
'i18n.gettext_' + getFilterDomain( domain ),
|
|
200
202
|
translation,
|
|
201
203
|
text,
|
|
202
204
|
domain
|
|
203
|
-
) as
|
|
205
|
+
) as TransformedText< typeof text >;
|
|
204
206
|
};
|
|
205
207
|
|
|
206
208
|
const _x: I18n< TextDomain >[ '_x' ] = ( text, context, domain ) => {
|
|
207
209
|
let translation = dcnpgettext( domain, context, text );
|
|
208
210
|
if ( ! hooks ) {
|
|
209
|
-
return translation as
|
|
211
|
+
return translation as TransformedText< typeof text >;
|
|
210
212
|
}
|
|
211
213
|
|
|
212
214
|
/**
|
|
@@ -223,7 +225,7 @@ export const createI18n = < TextDomain extends string >(
|
|
|
223
225
|
text,
|
|
224
226
|
context,
|
|
225
227
|
domain
|
|
226
|
-
) as
|
|
228
|
+
) as TransformedText< typeof text >;
|
|
227
229
|
|
|
228
230
|
return hooks.applyFilters(
|
|
229
231
|
'i18n.gettext_with_context_' + getFilterDomain( domain ),
|
|
@@ -231,7 +233,7 @@ export const createI18n = < TextDomain extends string >(
|
|
|
231
233
|
text,
|
|
232
234
|
context,
|
|
233
235
|
domain
|
|
234
|
-
) as
|
|
236
|
+
) as TransformedText< typeof text >;
|
|
235
237
|
};
|
|
236
238
|
|
|
237
239
|
const _n: I18n< TextDomain >[ '_n' ] = (
|
|
@@ -248,7 +250,7 @@ export const createI18n = < TextDomain extends string >(
|
|
|
248
250
|
number
|
|
249
251
|
);
|
|
250
252
|
if ( ! hooks ) {
|
|
251
|
-
return translation as
|
|
253
|
+
return translation as TransformedText<
|
|
252
254
|
typeof single | typeof plural
|
|
253
255
|
>;
|
|
254
256
|
}
|
|
@@ -269,7 +271,7 @@ export const createI18n = < TextDomain extends string >(
|
|
|
269
271
|
plural,
|
|
270
272
|
number,
|
|
271
273
|
domain
|
|
272
|
-
) as
|
|
274
|
+
) as TransformedText< typeof single | typeof plural >;
|
|
273
275
|
|
|
274
276
|
return hooks.applyFilters(
|
|
275
277
|
'i18n.ngettext_' + getFilterDomain( domain ),
|
|
@@ -278,7 +280,7 @@ export const createI18n = < TextDomain extends string >(
|
|
|
278
280
|
plural,
|
|
279
281
|
number,
|
|
280
282
|
domain
|
|
281
|
-
) as
|
|
283
|
+
) as TransformedText< typeof single | typeof plural >;
|
|
282
284
|
};
|
|
283
285
|
|
|
284
286
|
const _nx: I18n< TextDomain >[ '_nx' ] = (
|
|
@@ -296,7 +298,7 @@ export const createI18n = < TextDomain extends string >(
|
|
|
296
298
|
number
|
|
297
299
|
);
|
|
298
300
|
if ( ! hooks ) {
|
|
299
|
-
return translation as
|
|
301
|
+
return translation as TransformedText<
|
|
300
302
|
typeof single | typeof plural
|
|
301
303
|
>;
|
|
302
304
|
}
|
|
@@ -319,7 +321,7 @@ export const createI18n = < TextDomain extends string >(
|
|
|
319
321
|
number,
|
|
320
322
|
context,
|
|
321
323
|
domain
|
|
322
|
-
) as
|
|
324
|
+
) as TransformedText< typeof single | typeof plural >;
|
|
323
325
|
|
|
324
326
|
return hooks.applyFilters(
|
|
325
327
|
'i18n.ngettext_with_context_' + getFilterDomain( domain ),
|
|
@@ -329,7 +331,7 @@ export const createI18n = < TextDomain extends string >(
|
|
|
329
331
|
number,
|
|
330
332
|
context,
|
|
331
333
|
domain
|
|
332
|
-
) as
|
|
334
|
+
) as TransformedText< typeof single | typeof plural >;
|
|
333
335
|
};
|
|
334
336
|
|
|
335
337
|
const isRTL: I18n< TextDomain >[ 'isRTL' ] = () => {
|
package/src/default-i18n.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import { defaultHooks } from '@wordpress/hooks';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
7
|
+
* Internal dependencies
|
|
8
8
|
*/
|
|
9
|
-
import {
|
|
9
|
+
import { createI18n } from './create-i18n';
|
|
10
10
|
import {
|
|
11
11
|
LocaleData,
|
|
12
12
|
SubscribeCallback,
|
|
13
|
-
|
|
13
|
+
TransformedText,
|
|
14
14
|
UnsubscribeCallback,
|
|
15
15
|
} from './types';
|
|
16
16
|
|
|
@@ -76,7 +76,7 @@ export const subscribe = i18n.subscribe.bind( i18n );
|
|
|
76
76
|
* @param {Text} text Text to translate.
|
|
77
77
|
* @param {string | undefined} domain Domain to retrieve the translated text.
|
|
78
78
|
*
|
|
79
|
-
* @return {
|
|
79
|
+
* @return {TransformedText<Text>} Translated text.
|
|
80
80
|
*/
|
|
81
81
|
export const __ = i18n.__.bind( i18n );
|
|
82
82
|
|
|
@@ -91,7 +91,7 @@ export const __ = i18n.__.bind( i18n );
|
|
|
91
91
|
* @param {string} context Context information for the translators.
|
|
92
92
|
* @param {string | undefined} domain Domain to retrieve the translated text.
|
|
93
93
|
*
|
|
94
|
-
* @return {
|
|
94
|
+
* @return {TransformedText<Text>} Translated context string without pipe.
|
|
95
95
|
*/
|
|
96
96
|
export const _x = i18n._x.bind( i18n );
|
|
97
97
|
|
|
@@ -110,7 +110,7 @@ export const _x = i18n._x.bind( i18n );
|
|
|
110
110
|
* singular or plural form.
|
|
111
111
|
* @param {string | undefined} domain Domain to retrieve the translated text.
|
|
112
112
|
*
|
|
113
|
-
* @return {
|
|
113
|
+
* @return {TransformedText<Single | Plural>} The translated singular or plural form.
|
|
114
114
|
*/
|
|
115
115
|
export const _n = i18n._n.bind( i18n );
|
|
116
116
|
|
|
@@ -131,7 +131,7 @@ export const _n = i18n._n.bind( i18n );
|
|
|
131
131
|
* @param {string} context Context information for the translators.
|
|
132
132
|
* @param {string | undefined} [domain] Domain to retrieve the translated text.
|
|
133
133
|
*
|
|
134
|
-
* @return {
|
|
134
|
+
* @return {TransformedText<Single | Plural>} The translated singular or plural form.
|
|
135
135
|
*/
|
|
136
136
|
export const _nx = i18n._nx.bind( i18n );
|
|
137
137
|
|
package/src/sprintf.ts
CHANGED
|
@@ -6,16 +6,16 @@ import _sprintf from '@tannin/sprintf';
|
|
|
6
6
|
/**
|
|
7
7
|
* Internal dependencies
|
|
8
8
|
*/
|
|
9
|
-
import type { DistributeSprintfArgs,
|
|
9
|
+
import type { DistributeSprintfArgs, TransformedText } from './types';
|
|
10
10
|
|
|
11
11
|
export function sprintf< T extends string >(
|
|
12
|
-
format: T |
|
|
12
|
+
format: T | TransformedText< T >,
|
|
13
13
|
...args: DistributeSprintfArgs< T >
|
|
14
|
-
):
|
|
14
|
+
): TransformedText< T >;
|
|
15
15
|
export function sprintf< T extends string >(
|
|
16
|
-
format: T |
|
|
16
|
+
format: T | TransformedText< T >,
|
|
17
17
|
args: DistributeSprintfArgs< T >
|
|
18
|
-
):
|
|
18
|
+
): TransformedText< T >;
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* Returns a formatted string.
|
|
@@ -28,8 +28,8 @@ export function sprintf< T extends string >(
|
|
|
28
28
|
* @return The formatted string.
|
|
29
29
|
*/
|
|
30
30
|
export function sprintf< T extends string >(
|
|
31
|
-
format: T |
|
|
31
|
+
format: T | TransformedText< T >,
|
|
32
32
|
...args: DistributeSprintfArgs< T >
|
|
33
|
-
):
|
|
34
|
-
return _sprintf( format
|
|
33
|
+
): TransformedText< T > {
|
|
34
|
+
return _sprintf( format, ...args ) as TransformedText< T >;
|
|
35
35
|
}
|
package/src/test/sprintf.ts
CHANGED
|
@@ -10,6 +10,7 @@ jest.mock( 'memize', (): MemizeMock => ( fn ) => fn );
|
|
|
10
10
|
* Internal dependencies
|
|
11
11
|
*/
|
|
12
12
|
import { sprintf } from '../sprintf';
|
|
13
|
+
import type { TransformedText } from '../types';
|
|
13
14
|
|
|
14
15
|
describe( 'i18n', () => {
|
|
15
16
|
describe( 'sprintf', () => {
|
|
@@ -24,5 +25,18 @@ describe( 'i18n', () => {
|
|
|
24
25
|
|
|
25
26
|
expect( result ).toBe( 'bonjour Riad' );
|
|
26
27
|
} );
|
|
28
|
+
|
|
29
|
+
it( 'preserves format string literal in return type', () => {
|
|
30
|
+
const result = sprintf( '<Name>%s</Name>', 'Riad' );
|
|
31
|
+
|
|
32
|
+
// Type-level test: sprintf return type should be TransformedText
|
|
33
|
+
// preserving the format string literal.
|
|
34
|
+
const _check: TransformedText< '<Name>%s</Name>' > = result;
|
|
35
|
+
void _check;
|
|
36
|
+
|
|
37
|
+
// At runtime it's still a regular string.
|
|
38
|
+
expect( typeof result ).toBe( 'string' );
|
|
39
|
+
expect( result ).toBe( '<Name>Riad</Name>' );
|
|
40
|
+
} );
|
|
27
41
|
} );
|
|
28
42
|
} );
|
package/src/types.ts
CHANGED
|
@@ -5,18 +5,25 @@ import type sprintf from '@tannin/sprintf';
|
|
|
5
5
|
import { type TanninDomainMetadata } from 'tannin';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
* Return type for string translation
|
|
8
|
+
* Return type for string transformation functions (translation, sprintf).
|
|
9
|
+
* Preserves the original string literal type so that downstream consumers
|
|
10
|
+
* can extract information (e.g., tag names) at the type level.
|
|
9
11
|
*
|
|
10
12
|
* This type should be treated as if it were `string`.
|
|
11
13
|
*/
|
|
12
|
-
export type
|
|
14
|
+
export type TransformedText< T extends string > = string & {
|
|
13
15
|
/**
|
|
14
16
|
* DO NOT USE! This property _does not exist_.
|
|
15
17
|
* @private
|
|
16
18
|
*/
|
|
17
|
-
readonly
|
|
19
|
+
readonly __transformedText: T;
|
|
18
20
|
};
|
|
19
21
|
|
|
22
|
+
/**
|
|
23
|
+
* @deprecated Use `TransformedText` instead.
|
|
24
|
+
*/
|
|
25
|
+
export type TranslatableText< T extends string > = TransformedText< T >;
|
|
26
|
+
|
|
20
27
|
/**
|
|
21
28
|
* Type to extends TanninDomainMetadata to support additional properties.
|
|
22
29
|
*/
|
|
@@ -102,7 +109,7 @@ export interface I18n< TextDomain extends string = string > {
|
|
|
102
109
|
__: < Text extends string >(
|
|
103
110
|
text: Text,
|
|
104
111
|
domain?: TextDomain
|
|
105
|
-
) =>
|
|
112
|
+
) => TransformedText< Text >;
|
|
106
113
|
|
|
107
114
|
/**
|
|
108
115
|
* Retrieve translated string with gettext context.
|
|
@@ -113,7 +120,7 @@ export interface I18n< TextDomain extends string = string > {
|
|
|
113
120
|
text: Text,
|
|
114
121
|
context: string,
|
|
115
122
|
domain?: TextDomain
|
|
116
|
-
) =>
|
|
123
|
+
) => TransformedText< Text >;
|
|
117
124
|
|
|
118
125
|
/**
|
|
119
126
|
* Translates and retrieves the singular or plural form based on the supplied
|
|
@@ -126,7 +133,7 @@ export interface I18n< TextDomain extends string = string > {
|
|
|
126
133
|
plural: Plural,
|
|
127
134
|
number: number,
|
|
128
135
|
domain?: TextDomain
|
|
129
|
-
) =>
|
|
136
|
+
) => TransformedText< Single | Plural >;
|
|
130
137
|
|
|
131
138
|
/**
|
|
132
139
|
* Translates and retrieves the singular or plural form based on the supplied
|
|
@@ -140,7 +147,7 @@ export interface I18n< TextDomain extends string = string > {
|
|
|
140
147
|
number: number,
|
|
141
148
|
context: string,
|
|
142
149
|
domain?: TextDomain
|
|
143
|
-
) =>
|
|
150
|
+
) => TransformedText< Single | Plural >;
|
|
144
151
|
|
|
145
152
|
/**
|
|
146
153
|
* Check if current locale is RTL.
|