gt-i18n 0.9.0 → 0.9.1
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 +14 -0
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/internal.cjs +65 -43
- package/dist/internal.cjs.map +1 -1
- package/dist/internal.d.cts +10 -6
- package/dist/internal.d.mts +10 -6
- package/dist/internal.mjs +62 -44
- package/dist/internal.mjs.map +1 -1
- package/dist/{versionId-qtCCdgKr.cjs → versionId-Bh53lhdJ.cjs} +40 -10
- package/dist/versionId-Bh53lhdJ.cjs.map +1 -0
- package/dist/{versionId-D0t6HuUM.mjs → versionId-CB81YFd9.mjs} +17 -11
- package/dist/versionId-CB81YFd9.mjs.map +1 -0
- package/package.json +5 -5
- package/dist/versionId-D0t6HuUM.mjs.map +0 -1
- package/dist/versionId-qtCCdgKr.cjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# gt-i18n
|
|
2
2
|
|
|
3
|
+
## 0.9.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#1374](https://github.com/generaltranslation/gt/pull/1374) [`4d77edf`](https://github.com/generaltranslation/gt/commit/4d77edf7cb2bca5c20911c20c58f702803c9acc9) Thanks [@ErnestM1234](https://github.com/ErnestM1234)! - chore(gt-i18n): add comment documentation for t.obj()
|
|
8
|
+
|
|
9
|
+
- [#1380](https://github.com/generaltranslation/gt/pull/1380) [`feffb35`](https://github.com/generaltranslation/gt/commit/feffb35f75b3deee12e29878792461b8d32fad3e) Thanks [@ErnestM1234](https://github.com/ErnestM1234)! - Export i18n manager cache-miss event-name constants from `gt-i18n/internal` so downstream packages such as `@generaltranslation/react-core` can consume one shared source of truth.
|
|
10
|
+
|
|
11
|
+
- [#1375](https://github.com/generaltranslation/gt/pull/1375) [`86263b3`](https://github.com/generaltranslation/gt/commit/86263b3aa8f2d283200515d609d69f570b97a84f) Thanks [@ErnestM1234](https://github.com/ErnestM1234)! - Fix dictionary getTranslations fallback to use loaded translations when a target dictionary entry is missing.
|
|
12
|
+
|
|
13
|
+
- Updated dependencies [[`e88fd39`](https://github.com/generaltranslation/gt/commit/e88fd399683868d5af1fe2b0ba2974fe5b17d7a7), [`95f852a`](https://github.com/generaltranslation/gt/commit/95f852ae086ac79d2c446f4d3072d8fd18688796)]:
|
|
14
|
+
- generaltranslation@8.2.13
|
|
15
|
+
- @generaltranslation/supported-locales@2.0.71
|
|
16
|
+
|
|
3
17
|
## 0.9.0
|
|
4
18
|
|
|
5
19
|
### Minor Changes
|
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_isEncodedTranslationOptions = require("./isEncodedTranslationOptions-D7ltjc4f.cjs");
|
|
3
|
-
const require_versionId = require("./versionId-
|
|
3
|
+
const require_versionId = require("./versionId-Bh53lhdJ.cjs");
|
|
4
4
|
const require_mFallback = require("./mFallback-CoPRtKyL.cjs");
|
|
5
5
|
let generaltranslation_internal = require("generaltranslation/internal");
|
|
6
6
|
let generaltranslation_core = require("generaltranslation/core");
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { a as extractVariables, i as createInterpolationFailureMessage, n as decodeOptions, o as logger_default } from "./isEncodedTranslationOptions-DdHEzDDM.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { E as hashMessage, a as getLocales, d as resolveStringContentWithFallback, i as getLocaleProperties, m as getCurrentLocale, n as getDefaultLocale, r as getLocale, t as getVersionId } from "./versionId-CB81YFd9.mjs";
|
|
3
3
|
import { n as gtFallback, r as decodeMsg, t as mFallback } from "./mFallback-BQX_yiE-.mjs";
|
|
4
4
|
import { VAR_IDENTIFIER, declareStatic, declareVar, decodeVars, derive, encode, libraryDefaultLocale } from "generaltranslation/internal";
|
|
5
5
|
import { formatMessage } from "generaltranslation/core";
|
package/dist/internal.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_isEncodedTranslationOptions = require("./isEncodedTranslationOptions-D7ltjc4f.cjs");
|
|
3
|
-
const require_versionId = require("./versionId-
|
|
3
|
+
const require_versionId = require("./versionId-Bh53lhdJ.cjs");
|
|
4
4
|
let generaltranslation_internal = require("generaltranslation/internal");
|
|
5
5
|
let generaltranslation_core = require("generaltranslation/core");
|
|
6
6
|
//#region src/translation-functions/internal/getGT.ts
|
|
@@ -98,7 +98,7 @@ async function getMessages() {
|
|
|
98
98
|
async function getTranslations() {
|
|
99
99
|
const i18nManager = require_versionId.getI18nManager();
|
|
100
100
|
const locale = require_versionId.getCurrentLocale();
|
|
101
|
-
await i18nManager.loadDictionary(locale);
|
|
101
|
+
await Promise.all([i18nManager.loadDictionary(locale), i18nManager.loadTranslations(locale)]);
|
|
102
102
|
const sourceLocale = i18nManager.getDefaultLocale();
|
|
103
103
|
/**
|
|
104
104
|
* Dictionary resolution
|
|
@@ -119,14 +119,28 @@ async function getTranslations() {
|
|
|
119
119
|
const t = ((id, options = {}) => {
|
|
120
120
|
const sourceEntry = i18nManager.lookupDictionary(sourceLocale, id);
|
|
121
121
|
if (sourceEntry === void 0) throw new Error(`Dictionary entry ${id} cannot be found`);
|
|
122
|
+
const targetEntry = i18nManager.lookupDictionary(locale, id);
|
|
123
|
+
const dictionaryOptions = require_versionId.resolveDictionaryLookupOptions(sourceEntry.options);
|
|
122
124
|
return renderEntry({
|
|
123
125
|
sourceLocale,
|
|
124
126
|
targetLocale: locale,
|
|
125
127
|
sourceEntry,
|
|
126
|
-
|
|
128
|
+
target: targetEntry?.entry ?? i18nManager.lookupTranslation(locale, sourceEntry.entry, dictionaryOptions),
|
|
129
|
+
dictionaryOptions,
|
|
127
130
|
options
|
|
128
131
|
});
|
|
129
132
|
});
|
|
133
|
+
/**
|
|
134
|
+
* Returns a subtree of the dictionary object translation based on its id.
|
|
135
|
+
* @param {string} id - The id of the translation to translate.
|
|
136
|
+
* @returns The translated object.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* const t = await getTranslations();
|
|
140
|
+
* const greetings = t.obj('greetings');
|
|
141
|
+
* console.log(greetings);
|
|
142
|
+
* // { greeting1: 'Hello', greeting2: 'Hi' }
|
|
143
|
+
*/
|
|
130
144
|
t.obj = (id) => {
|
|
131
145
|
const sourceObject = i18nManager.lookupDictionaryObj(sourceLocale, id);
|
|
132
146
|
if (sourceObject === void 0) throw new Error(`Dictionary entry ${id} cannot be found`);
|
|
@@ -135,10 +149,51 @@ async function getTranslations() {
|
|
|
135
149
|
targetObject: i18nManager.lookupDictionaryObj(locale, id)
|
|
136
150
|
});
|
|
137
151
|
};
|
|
152
|
+
function renderObject({ sourceObject, targetObject }) {
|
|
153
|
+
const targetEntry = require_versionId.getDictionaryEntry(targetObject);
|
|
154
|
+
if (targetEntry !== void 0) return targetEntry.entry;
|
|
155
|
+
if (require_versionId.isDictionaryValue(targetObject)) {
|
|
156
|
+
if (!require_versionId.isDictionaryValue(sourceObject)) return renderObject({
|
|
157
|
+
sourceObject: targetObject,
|
|
158
|
+
targetObject: void 0
|
|
159
|
+
});
|
|
160
|
+
return renderDictionaryObject({
|
|
161
|
+
sourceObject,
|
|
162
|
+
targetObject
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
const sourceEntry = require_versionId.getDictionaryEntry(sourceObject);
|
|
166
|
+
if (sourceEntry !== void 0) {
|
|
167
|
+
const dictionaryOptions = require_versionId.resolveDictionaryLookupOptions(sourceEntry.options);
|
|
168
|
+
const target = i18nManager.lookupTranslation(locale, sourceEntry.entry, dictionaryOptions);
|
|
169
|
+
if (target !== void 0) return target;
|
|
170
|
+
return sourceEntry.entry;
|
|
171
|
+
}
|
|
172
|
+
if (require_versionId.isDictionaryValue(sourceObject)) return renderDictionaryObject({
|
|
173
|
+
sourceObject,
|
|
174
|
+
targetObject: void 0
|
|
175
|
+
});
|
|
176
|
+
throw new Error("Dictionary object cannot be rendered");
|
|
177
|
+
}
|
|
178
|
+
function renderDictionaryObject({ sourceObject, targetObject }) {
|
|
179
|
+
if (!require_versionId.isDictionaryValue(sourceObject)) return renderObject({
|
|
180
|
+
sourceObject,
|
|
181
|
+
targetObject
|
|
182
|
+
});
|
|
183
|
+
const result = {};
|
|
184
|
+
const keys = new Set([...Object.keys(sourceObject), ...require_versionId.isDictionaryValue(targetObject) ? Object.keys(targetObject) : []]);
|
|
185
|
+
for (const key of Array.from(keys)) {
|
|
186
|
+
const renderedChild = renderObject({
|
|
187
|
+
sourceObject: sourceObject[key],
|
|
188
|
+
targetObject: require_versionId.isDictionaryValue(targetObject) ? targetObject[key] : void 0
|
|
189
|
+
});
|
|
190
|
+
if (renderedChild !== void 0) result[key] = renderedChild;
|
|
191
|
+
}
|
|
192
|
+
return result;
|
|
193
|
+
}
|
|
138
194
|
return t;
|
|
139
195
|
}
|
|
140
|
-
function renderEntry({ sourceLocale, targetLocale, sourceEntry,
|
|
141
|
-
const dictionaryOptions = require_versionId.resolveDictionaryLookupOptions(sourceEntry.options);
|
|
196
|
+
function renderEntry({ sourceLocale, targetLocale, sourceEntry, target, dictionaryOptions, options = {} }) {
|
|
142
197
|
const interpolationOptions = require_isEncodedTranslationOptions.extractVariables(options);
|
|
143
198
|
const lookupOptions = require_versionId.createLookupOptions(targetLocale, {
|
|
144
199
|
...dictionaryOptions,
|
|
@@ -146,48 +201,11 @@ function renderEntry({ sourceLocale, targetLocale, sourceEntry, targetEntry, opt
|
|
|
146
201
|
}, dictionaryOptions.$format ?? "ICU");
|
|
147
202
|
return require_versionId.interpolateMessage({
|
|
148
203
|
source: sourceEntry.entry,
|
|
149
|
-
target
|
|
204
|
+
target,
|
|
150
205
|
options: lookupOptions,
|
|
151
206
|
sourceLocale
|
|
152
207
|
});
|
|
153
208
|
}
|
|
154
|
-
function renderObject({ sourceObject, targetObject }) {
|
|
155
|
-
const targetEntry = require_versionId.getDictionaryEntry(targetObject);
|
|
156
|
-
if (targetEntry !== void 0) return targetEntry.entry;
|
|
157
|
-
if (require_versionId.isDictionaryValue(targetObject)) {
|
|
158
|
-
if (!require_versionId.isDictionaryValue(sourceObject)) return renderObject({
|
|
159
|
-
sourceObject: targetObject,
|
|
160
|
-
targetObject: void 0
|
|
161
|
-
});
|
|
162
|
-
return renderDictionaryObject({
|
|
163
|
-
sourceObject,
|
|
164
|
-
targetObject
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
const sourceEntry = require_versionId.getDictionaryEntry(sourceObject);
|
|
168
|
-
if (sourceEntry !== void 0) return sourceEntry.entry;
|
|
169
|
-
if (require_versionId.isDictionaryValue(sourceObject)) return renderDictionaryObject({
|
|
170
|
-
sourceObject,
|
|
171
|
-
targetObject: void 0
|
|
172
|
-
});
|
|
173
|
-
throw new Error("Dictionary object cannot be rendered");
|
|
174
|
-
}
|
|
175
|
-
function renderDictionaryObject({ sourceObject, targetObject }) {
|
|
176
|
-
if (!require_versionId.isDictionaryValue(sourceObject)) return renderObject({
|
|
177
|
-
sourceObject,
|
|
178
|
-
targetObject
|
|
179
|
-
});
|
|
180
|
-
const result = {};
|
|
181
|
-
const keys = new Set([...Object.keys(sourceObject), ...require_versionId.isDictionaryValue(targetObject) ? Object.keys(targetObject) : []]);
|
|
182
|
-
for (const key of keys) {
|
|
183
|
-
const renderedChild = renderObject({
|
|
184
|
-
sourceObject: sourceObject[key],
|
|
185
|
-
targetObject: require_versionId.isDictionaryValue(targetObject) ? targetObject[key] : void 0
|
|
186
|
-
});
|
|
187
|
-
if (renderedChild !== void 0) result[key] = renderedChild;
|
|
188
|
-
}
|
|
189
|
-
return result;
|
|
190
|
-
}
|
|
191
209
|
//#endregion
|
|
192
210
|
//#region src/translation-functions/internal/tx.ts
|
|
193
211
|
/**
|
|
@@ -296,7 +314,11 @@ function createLocaleResolver(config = {}) {
|
|
|
296
314
|
return (candidates) => determineSupportedLocaleWithConfig(candidates, normalizedConfig) || normalizedConfig.defaultLocale;
|
|
297
315
|
}
|
|
298
316
|
//#endregion
|
|
317
|
+
exports.DICTIONARY_CACHE_MISS_EVENT_NAME = require_versionId.DICTIONARY_CACHE_MISS_EVENT_NAME;
|
|
299
318
|
exports.I18nManager = require_versionId.I18nManager;
|
|
319
|
+
exports.LOCALES_CACHE_MISS_EVENT_NAME = require_versionId.LOCALES_CACHE_MISS_EVENT_NAME;
|
|
320
|
+
exports.LOCALES_DICTIONARY_CACHE_MISS_EVENT_NAME = require_versionId.LOCALES_DICTIONARY_CACHE_MISS_EVENT_NAME;
|
|
321
|
+
exports.TRANSLATIONS_CACHE_MISS_EVENT_NAME = require_versionId.TRANSLATIONS_CACHE_MISS_EVENT_NAME;
|
|
300
322
|
exports.createConditionStoreSingleton = createConditionStoreSingleton;
|
|
301
323
|
exports.createLocaleResolver = createLocaleResolver;
|
|
302
324
|
exports.createLookupOptions = require_versionId.createLookupOptions;
|
package/dist/internal.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"internal.cjs","names":["getI18nManager","getCurrentLocale","createLookupOptions","interpolateMessage","decodeOptions","isEncodedTranslationOptions","getI18nManager","getCurrentLocale","resolveDictionaryLookupOptions","extractVariables","createLookupOptions","interpolateMessage","getDictionaryEntry","isDictionaryValue","resolveStringContentWithRuntimeFallback","getCurrentLocale","resolveStringContent","resolveStringContentWithFallback","resolveJsx","setConditionStore","libraryDefaultLocale","LocaleConfig"],"sources":["../src/translation-functions/internal/getGT.ts","../src/translation-functions/internal/getMessages.ts","../src/translation-functions/internal/getTranslations.ts","../src/translation-functions/internal/tx.ts","../src/translation-functions/internal/sync-translation-resolution.ts","../src/translation-functions/internal/jsx-resolution.ts","../src/i18n-manager/condition-store/createConditionStoreSingleton.ts","../src/i18n-manager/condition-store/localeResolver.ts"],"sourcesContent":["import {\n getCurrentLocale,\n getI18nManager,\n} from '../../i18n-manager/singleton-operations';\nimport { InlineTranslationOptions } from '../types/options';\nimport { GTFunctionType } from '../types/functions';\nimport { interpolateMessage } from '../utils/interpolation/interpolateMessage';\nimport { createLookupOptions } from './helpers';\nimport type { StringFormat } from 'generaltranslation/types';\n\n/**\n * Returns the gt function that registers a string at build time and resolves its translation at runtime.\n * @returns A promise of the gt function\n *\n * @example\n * const gt = await getGT();\n * const greeting = gt('Hello, world!');\n */\nexport async function getGT(): Promise<GTFunctionType> {\n // Get the translation resolver\n const i18nManager = getI18nManager();\n const locale = getCurrentLocale();\n await i18nManager.loadTranslations(locale);\n const sourceLocale = i18nManager.getDefaultLocale();\n\n /**\n * Registers a message at build time and resolves its translation at runtime.\n * @param {string} message - The message to translate\n * @param {InlineTranslationOptions} options - The options for the translation\n * @returns The translated message\n *\n * @example\n * // Simple translation without interpolation\n * const gt = await getGT();\n * const greeting = gt('Hello, world!');\n *\n * @example\n * // Translation with interpolation\n * const gt = await getGT();\n * const welcome = gt('Hello, {name}!', { name: 'Alice' });\n */\n const gt: GTFunctionType = (\n message: string,\n options: InlineTranslationOptions = {}\n ) => {\n const targetLocale = options.$locale ?? locale;\n const lookupOptions = createLookupOptions<StringFormat>(\n targetLocale,\n options,\n 'ICU'\n );\n\n // Lookup translation\n const translation = i18nManager.lookupTranslation(\n lookupOptions.$locale,\n message,\n lookupOptions\n );\n\n // Format result\n return interpolateMessage({\n source: message,\n target: translation,\n options: lookupOptions,\n sourceLocale,\n });\n };\n\n return gt;\n}\n","import { InlineResolveOptions } from '../types/options';\nimport { decodeOptions } from '../msg/decodeOptions';\nimport { isEncodedTranslationOptions } from '../utils/isEncodedTranslationOptions';\nimport { getGT } from './getGT';\nimport { MFunctionType } from '../types/functions';\n\n/**\n * Returns the m function that resolves a registered message to its translation.\n * @returns A promise of the m function\n *\n * @example\n * // Registration\n * const registeredMessage = msg('Hello, world!');\n *\n * // Resolution\n * const m = await getMessages();\n * const greeting = m(registeredMessage);\n */\nexport async function getMessages(): Promise<MFunctionType> {\n // Get the gt function\n const gt = await getGT();\n\n /**\n * Resolves a registered message to its translation.\n * @param {string | null | undefined} encodedMsg - The encoded message to decode and interpolate.\n * @param {InlineTranslationOptions} options - The options to interpolate.\n * @returns - The decoded and interpolated message.\n *\n * @example\n * // Simple message without interpolation\n * const m = await getMessages();\n * const greeting = m(msg('Hello, world!'));\n *\n * @example\n * // Message with interpolation\n * const m = await getMessages();\n * const welcome = m(msg('Welcome, {user}!'), { user: 'Alice' });\n */\n const m: MFunctionType = <T extends string | null | undefined>(\n encodedMsg: T,\n options: InlineResolveOptions = {}\n ): T extends string ? string : T => {\n // Return if the encoded message is null or undefined\n if (encodedMsg == null) return encodedMsg as T extends string ? string : T;\n\n // Get any encoded options\n const decodedOptions = decodeOptions(encodedMsg) ?? {};\n\n // Return early if string already interpolated eg: mFallback(msg('Hello, {name}!', { name: 'Brian' }))\n if (isEncodedTranslationOptions(decodedOptions)) {\n return gt(decodedOptions.$_source, decodedOptions) as T extends string\n ? string\n : T;\n }\n\n // Use gt to interpolate\n // Separate from decoded options to match behavior in @gt/react-core\n return gt(encodedMsg, options) as T extends string ? string : T;\n };\n\n return m;\n}\n","import {\n getCurrentLocale,\n getI18nManager,\n} from '../../i18n-manager/singleton-operations';\nimport { DictionaryTranslationOptions } from '../types/options';\nimport { TFunctionType } from '../types/functions';\nimport { interpolateMessage } from '../utils/interpolation/interpolateMessage';\nimport { createLookupOptions } from './helpers';\nimport { extractVariables } from '../../utils/extractVariables';\nimport type { StringFormat } from 'generaltranslation/types';\nimport type {\n DictionaryEntry,\n DictionaryValue,\n} from '../../i18n-manager/translations-manager/DictionaryCache';\nimport {\n getDictionaryEntry,\n isDictionaryValue,\n resolveDictionaryLookupOptions,\n} from '../../i18n-manager/translations-manager/utils/dictionary-helpers';\nimport type { DictionaryObjectTranslation } from '../types/functions';\n\n/**\n * Returns the t function that translates a dictionary entry based on its id and options.\n * @returns A promise of the t function\n * @important Must be used inside of a request context\n *\n * @example\n * const t = await getTranslations();\n * const title = await t('page.title');\n */\nexport async function getTranslations(): Promise<TFunctionType> {\n const i18nManager = getI18nManager();\n const locale = getCurrentLocale();\n await i18nManager.loadDictionary(locale);\n const sourceLocale = i18nManager.getDefaultLocale();\n\n /**\n * Dictionary resolution\n * @param {string} id - The id of the translation to translate.\n * @param {DictionaryTranslationOptions} options - The options for interpolation.\n * @returns {string} The translated message.\n *\n * @example\n * // Simple dictionary lookup without interpolation\n * const t = await getTranslations();\n * const title = t('page.title');\n *\n * @example\n * // Dictionary lookup with interpolation\n * const t = await getTranslations();\n * const greeting = t('user.greeting', { name: 'Bob' });\n */\n const t = ((\n id: string,\n options: DictionaryTranslationOptions = {}\n ): string => {\n const sourceEntry = i18nManager.lookupDictionary(sourceLocale, id);\n if (sourceEntry === undefined) {\n throw new Error(`Dictionary entry ${id} cannot be found`);\n }\n const targetEntry = i18nManager.lookupDictionary(locale, id);\n return renderEntry({\n sourceLocale,\n targetLocale: locale,\n sourceEntry,\n targetEntry,\n options,\n });\n }) as TFunctionType;\n\n t.obj = (id: string): DictionaryObjectTranslation => {\n const sourceObject = i18nManager.lookupDictionaryObj(sourceLocale, id);\n if (sourceObject === undefined) {\n throw new Error(`Dictionary entry ${id} cannot be found`);\n }\n const targetObject = i18nManager.lookupDictionaryObj(locale, id);\n return renderObject({ sourceObject, targetObject });\n };\n\n return t;\n}\n\nfunction renderEntry({\n sourceLocale,\n targetLocale,\n sourceEntry,\n targetEntry,\n options = {},\n}: {\n sourceLocale: string;\n targetLocale: string;\n sourceEntry: DictionaryEntry;\n targetEntry: DictionaryEntry | undefined;\n options?: DictionaryTranslationOptions;\n}): string {\n const dictionaryOptions = resolveDictionaryLookupOptions(sourceEntry.options);\n const interpolationOptions = extractVariables(options);\n const lookupOptions = createLookupOptions<StringFormat>(\n targetLocale,\n {\n ...dictionaryOptions,\n ...interpolationOptions,\n },\n dictionaryOptions.$format ?? 'ICU'\n );\n\n return interpolateMessage({\n source: sourceEntry.entry,\n target: targetEntry?.entry,\n options: lookupOptions,\n sourceLocale,\n });\n}\n\nfunction renderObject({\n sourceObject,\n targetObject,\n}: {\n sourceObject: DictionaryValue | undefined;\n targetObject: DictionaryValue | undefined;\n}): DictionaryObjectTranslation {\n const targetEntry = getDictionaryEntry(targetObject);\n if (targetEntry !== undefined) {\n return targetEntry.entry;\n }\n\n if (isDictionaryValue(targetObject)) {\n if (!isDictionaryValue(sourceObject)) {\n return renderObject({\n sourceObject: targetObject,\n targetObject: undefined,\n });\n }\n\n return renderDictionaryObject({\n sourceObject,\n targetObject,\n });\n }\n\n const sourceEntry = getDictionaryEntry(sourceObject);\n if (sourceEntry !== undefined) {\n return sourceEntry.entry;\n }\n\n if (isDictionaryValue(sourceObject)) {\n return renderDictionaryObject({\n sourceObject,\n targetObject: undefined,\n });\n }\n\n throw new Error('Dictionary object cannot be rendered');\n}\n\nfunction renderDictionaryObject({\n sourceObject,\n targetObject,\n}: {\n sourceObject: DictionaryValue;\n targetObject: DictionaryValue | undefined;\n}): DictionaryObjectTranslation {\n if (!isDictionaryValue(sourceObject)) {\n return renderObject({ sourceObject, targetObject });\n }\n const result: Record<string, DictionaryObjectTranslation> = {};\n const keys = new Set([\n ...Object.keys(sourceObject),\n ...(isDictionaryValue(targetObject) ? Object.keys(targetObject) : []),\n ]);\n\n for (const key of keys) {\n const renderedChild = renderObject({\n sourceObject: sourceObject[key],\n targetObject: isDictionaryValue(targetObject)\n ? targetObject[key]\n : undefined,\n });\n if (renderedChild !== undefined) {\n result[key] = renderedChild;\n }\n }\n\n return result;\n}\n","import { RuntimeTranslationOptions } from '../types/options';\nimport type { StringFormat } from 'generaltranslation/types';\nimport { resolveStringContentWithRuntimeFallback } from './helpers';\nimport { getCurrentLocale } from '../../i18n-manager/singleton-operations';\n\ntype RuntimeTranslationOptionsWithFormat = Omit<\n RuntimeTranslationOptions,\n '$format'\n> & {\n $format?: StringFormat;\n};\n\n/**\n * Translates a message at runtime.\n * @param {string} message - The message to translate.\n * @param {RuntimeTranslationOptions} options - The options for the translation.\n * @returns {Promise<string>} The translated message.\n *\n * @example\n * // Simple runtime translation without interpolation\n * const status = await tx('Processing complete', { $locale: 'es-MX' });\n *\n * @example\n * // Runtime translation with interpolation\n * const progress = await tx(`Processing ${status}`, { $locale: 'es-MX' });\n */\nexport async function tx(\n content: string,\n options: RuntimeTranslationOptionsWithFormat = {}\n): Promise<string> {\n const locale = options.$locale ?? getCurrentLocale();\n return resolveStringContentWithRuntimeFallback(locale, content, {\n $format: 'STRING',\n ...options,\n });\n}\n","import { InlineTranslationOptions } from '../types/options';\nimport {\n resolveStringContent,\n resolveStringContentWithFallback,\n} from './helpers';\n\n/**\n * Synchronously resolve a translation for a given message and options\n * @param {string} message - The message to translate.\n * @param {InlineTranslationOptions} [options] - The options for the translation.\n * @returns {string | undefined} The translated message or undefined if the message is not found.\n * @deprecated use resolveTranslation instead\n */\nexport function resolveTranslationSync(\n locale: string,\n message: string,\n options: InlineTranslationOptions = {}\n) {\n return resolveStringContent(locale, message, { $format: 'ICU', ...options });\n}\n\n/**\n * Translate a message\n * @param {string} message - The message to translate.\n * @param {InlineTranslationOptions} [options] - The options for the translation.\n * @returns {string} The translated message or the original message interpolated if the translation is not found.\n * @deprecated use resolveTranslationWithFallback instead\n */\nexport function resolveTranslationSyncWithFallback(\n locale: string,\n message: string,\n options: InlineTranslationOptions = {}\n) {\n return resolveStringContentWithFallback(locale, message, {\n $format: 'ICU',\n ...options,\n });\n}\n","import { JsxChildren } from 'generaltranslation/types';\nimport { JsxTranslationOptions } from '../types/options';\nimport { resolveJsx } from './helpers';\n\n/**\n * Given a JsxChildren and any associated metadata needed for a hash calculation, resolve the associated translation.\n * @deprecated use resolveJsx instead\n */\nexport function resolveJsxTranslation(\n locale: string,\n children: JsxChildren,\n options: JsxTranslationOptions = {}\n) {\n return resolveJsx(locale, children, options);\n}\n","import type { ConditionStore } from '../types';\nimport { setConditionStore as setCurrentConditionStore } from '../singleton-operations';\n\nexport function createConditionStoreSingleton<T extends ConditionStore>(\n notInitializedMessage: string\n) {\n let conditionStore: T | undefined;\n\n function getConditionStore(): T {\n if (!conditionStore) {\n throw new Error(notInitializedMessage);\n }\n return conditionStore;\n }\n\n function setConditionStore(nextConditionStore: T): void {\n conditionStore = nextConditionStore;\n setCurrentConditionStore(nextConditionStore);\n }\n\n return {\n getConditionStore,\n setConditionStore,\n };\n}\n","import { LocaleConfig } from 'generaltranslation/core';\nimport { libraryDefaultLocale } from 'generaltranslation/internal';\nimport type { ConditionStoreConfig } from '../types';\n\nexport type LocaleCandidates = string | string[] | undefined;\n\nfunction normalizeConditionStoreConfig({\n defaultLocale,\n locales,\n customMapping,\n}: ConditionStoreConfig = {}) {\n const fallbackLocale = defaultLocale || libraryDefaultLocale;\n return {\n defaultLocale: fallbackLocale,\n locales: locales?.length ? locales : [fallbackLocale],\n customMapping,\n };\n}\n\ntype NormalizedConditionStoreConfig = ReturnType<\n typeof normalizeConditionStoreConfig\n>;\n\n/**\n * Return the best supported locale for the candidates, or undefined when none match.\n */\nexport function determineSupportedLocale(\n candidates: LocaleCandidates,\n config: ConditionStoreConfig = {}\n): string | undefined {\n return determineSupportedLocaleWithConfig(\n candidates,\n normalizeConditionStoreConfig(config)\n );\n}\n\nfunction determineSupportedLocaleWithConfig(\n candidates: LocaleCandidates,\n config: NormalizedConditionStoreConfig\n): string | undefined {\n if (\n candidates == null ||\n (Array.isArray(candidates) && candidates.length === 0)\n ) {\n return undefined;\n }\n\n const localeConfig = new LocaleConfig(config);\n return localeConfig.determineLocale(candidates) || undefined;\n}\n\n/**\n * Return the best supported locale for the candidates, falling back to the default locale.\n */\nexport function resolveSupportedLocale(\n candidates: LocaleCandidates,\n config: ConditionStoreConfig = {}\n): string {\n const normalizedConfig = normalizeConditionStoreConfig(config);\n return (\n determineSupportedLocaleWithConfig(candidates, normalizedConfig) ||\n normalizedConfig.defaultLocale\n );\n}\n\nexport function createLocaleResolver(config: ConditionStoreConfig = {}) {\n const normalizedConfig = normalizeConditionStoreConfig(config);\n return (candidates?: LocaleCandidates): string =>\n determineSupportedLocaleWithConfig(candidates, normalizedConfig) ||\n normalizedConfig.defaultLocale;\n}\n"],"mappings":";;;;;;;;;;;;;;AAkBA,eAAsB,QAAiC;CAErD,MAAM,cAAcA,kBAAAA,gBAAgB;CACpC,MAAM,SAASC,kBAAAA,kBAAkB;AACjC,OAAM,YAAY,iBAAiB,OAAO;CAC1C,MAAM,eAAe,YAAY,kBAAkB;;;;;;;;;;;;;;;;;CAkBnD,MAAM,MACJ,SACA,UAAoC,EAAE,KACnC;EAEH,MAAM,gBAAgBC,kBAAAA,oBADD,QAAQ,WAAW,QAGtC,SACA,MACD;AAUD,SAAOC,kBAAAA,mBAAmB;GACxB,QAAQ;GACR,QATkB,YAAY,kBAC9B,cAAc,SACd,SACA,cAMmB;GACnB,SAAS;GACT;GACD,CAAC;;AAGJ,QAAO;;;;;;;;;;;;;;;;AClDT,eAAsB,cAAsC;CAE1D,MAAM,KAAK,MAAM,OAAO;;;;;;;;;;;;;;;;;CAkBxB,MAAM,KACJ,YACA,UAAgC,EAAE,KACA;AAElC,MAAI,cAAc,KAAM,QAAO;EAG/B,MAAM,iBAAiBC,oCAAAA,cAAc,WAAW,IAAI,EAAE;AAGtD,MAAIC,oCAAAA,4BAA4B,eAAe,CAC7C,QAAO,GAAG,eAAe,UAAU,eAAe;AAOpD,SAAO,GAAG,YAAY,QAAQ;;AAGhC,QAAO;;;;;;;;;;;;;AC9BT,eAAsB,kBAA0C;CAC9D,MAAM,cAAcC,kBAAAA,gBAAgB;CACpC,MAAM,SAASC,kBAAAA,kBAAkB;AACjC,OAAM,YAAY,eAAe,OAAO;CACxC,MAAM,eAAe,YAAY,kBAAkB;;;;;;;;;;;;;;;;;CAkBnD,MAAM,MACJ,IACA,UAAwC,EAAE,KAC/B;EACX,MAAM,cAAc,YAAY,iBAAiB,cAAc,GAAG;AAClE,MAAI,gBAAgB,KAAA,EAClB,OAAM,IAAI,MAAM,oBAAoB,GAAG,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AAyB,mBAAmB;;AAG9C,QAAO;EACL;EACA,mBAAA;EACD;;;;ACjBH,SAAS,8BAA8B,EACrC,eACA,SACA,kBACwB,EAAE,EAAE;CAC5B,MAAM,iBAAiB,iBAAiBC,4BAAAA;AACxC,QAAO;EACL,eAAe;EACf,SAAS,SAAS,SAAS,UAAU,CAAC,eAAe;EACrD;EACD;;;;;AAUH,SAAgB,yBACd,YACA,SAA+B,EAAE,EACb;AACpB,QAAO,mCACL,YACA,8BAA8B,OAAO,CACtC;;AAGH,SAAS,mCACP,YACA,QACoB;AACpB,KACE,cAAc,QACb,MAAM,QAAQ,WAAW,IAAI,WAAW,WAAW,EAEpD;AAIF,QAAO,IADkBC,wBAAAA,aAAa,OACnB,CAAC,gBAAgB,WAAW,IAAI,KAAA;;;;;AAMrD,SAAgB,uBACd,YACA,SAA+B,EAAE,EACzB;CACR,MAAM,mBAAmB,8BAA8B,OAAO;AAC9D,QACE,mCAAmC,YAAY,iBAAiB,IAChE,iBAAiB;;AAIrB,SAAgB,qBAAqB,SAA+B,EAAE,EAAE;CACtE,MAAM,mBAAmB,8BAA8B,OAAO;AAC9D,SAAQ,eACN,mCAAmC,YAAY,iBAAiB,IAChE,iBAAiB"}
|
|
1
|
+
{"version":3,"file":"internal.cjs","names":["getI18nManager","getCurrentLocale","createLookupOptions","interpolateMessage","decodeOptions","isEncodedTranslationOptions","getI18nManager","getCurrentLocale","resolveDictionaryLookupOptions","getDictionaryEntry","isDictionaryValue","extractVariables","createLookupOptions","interpolateMessage","resolveStringContentWithRuntimeFallback","getCurrentLocale","resolveStringContent","resolveStringContentWithFallback","resolveJsx","setConditionStore","libraryDefaultLocale","LocaleConfig"],"sources":["../src/translation-functions/internal/getGT.ts","../src/translation-functions/internal/getMessages.ts","../src/translation-functions/internal/getTranslations.ts","../src/translation-functions/internal/tx.ts","../src/translation-functions/internal/sync-translation-resolution.ts","../src/translation-functions/internal/jsx-resolution.ts","../src/i18n-manager/condition-store/createConditionStoreSingleton.ts","../src/i18n-manager/condition-store/localeResolver.ts"],"sourcesContent":["import {\n getCurrentLocale,\n getI18nManager,\n} from '../../i18n-manager/singleton-operations';\nimport { InlineTranslationOptions } from '../types/options';\nimport { GTFunctionType } from '../types/functions';\nimport { interpolateMessage } from '../utils/interpolation/interpolateMessage';\nimport { createLookupOptions } from './helpers';\nimport type { StringFormat } from 'generaltranslation/types';\n\n/**\n * Returns the gt function that registers a string at build time and resolves its translation at runtime.\n * @returns A promise of the gt function\n *\n * @example\n * const gt = await getGT();\n * const greeting = gt('Hello, world!');\n */\nexport async function getGT(): Promise<GTFunctionType> {\n // Get the translation resolver\n const i18nManager = getI18nManager();\n const locale = getCurrentLocale();\n await i18nManager.loadTranslations(locale);\n const sourceLocale = i18nManager.getDefaultLocale();\n\n /**\n * Registers a message at build time and resolves its translation at runtime.\n * @param {string} message - The message to translate\n * @param {InlineTranslationOptions} options - The options for the translation\n * @returns The translated message\n *\n * @example\n * // Simple translation without interpolation\n * const gt = await getGT();\n * const greeting = gt('Hello, world!');\n *\n * @example\n * // Translation with interpolation\n * const gt = await getGT();\n * const welcome = gt('Hello, {name}!', { name: 'Alice' });\n */\n const gt: GTFunctionType = (\n message: string,\n options: InlineTranslationOptions = {}\n ) => {\n const targetLocale = options.$locale ?? locale;\n const lookupOptions = createLookupOptions<StringFormat>(\n targetLocale,\n options,\n 'ICU'\n );\n\n // Lookup translation\n const translation = i18nManager.lookupTranslation(\n lookupOptions.$locale,\n message,\n lookupOptions\n );\n\n // Format result\n return interpolateMessage({\n source: message,\n target: translation,\n options: lookupOptions,\n sourceLocale,\n });\n };\n\n return gt;\n}\n","import { InlineResolveOptions } from '../types/options';\nimport { decodeOptions } from '../msg/decodeOptions';\nimport { isEncodedTranslationOptions } from '../utils/isEncodedTranslationOptions';\nimport { getGT } from './getGT';\nimport { MFunctionType } from '../types/functions';\n\n/**\n * Returns the m function that resolves a registered message to its translation.\n * @returns A promise of the m function\n *\n * @example\n * // Registration\n * const registeredMessage = msg('Hello, world!');\n *\n * // Resolution\n * const m = await getMessages();\n * const greeting = m(registeredMessage);\n */\nexport async function getMessages(): Promise<MFunctionType> {\n // Get the gt function\n const gt = await getGT();\n\n /**\n * Resolves a registered message to its translation.\n * @param {string | null | undefined} encodedMsg - The encoded message to decode and interpolate.\n * @param {InlineTranslationOptions} options - The options to interpolate.\n * @returns - The decoded and interpolated message.\n *\n * @example\n * // Simple message without interpolation\n * const m = await getMessages();\n * const greeting = m(msg('Hello, world!'));\n *\n * @example\n * // Message with interpolation\n * const m = await getMessages();\n * const welcome = m(msg('Welcome, {user}!'), { user: 'Alice' });\n */\n const m: MFunctionType = <T extends string | null | undefined>(\n encodedMsg: T,\n options: InlineResolveOptions = {}\n ): T extends string ? string : T => {\n // Return if the encoded message is null or undefined\n if (encodedMsg == null) return encodedMsg as T extends string ? string : T;\n\n // Get any encoded options\n const decodedOptions = decodeOptions(encodedMsg) ?? {};\n\n // Return early if string already interpolated eg: mFallback(msg('Hello, {name}!', { name: 'Brian' }))\n if (isEncodedTranslationOptions(decodedOptions)) {\n return gt(decodedOptions.$_source, decodedOptions) as T extends string\n ? string\n : T;\n }\n\n // Use gt to interpolate\n // Separate from decoded options to match behavior in @gt/react-core\n return gt(encodedMsg, options) as T extends string ? string : T;\n };\n\n return m;\n}\n","import {\n getCurrentLocale,\n getI18nManager,\n} from '../../i18n-manager/singleton-operations';\nimport { DictionaryTranslationOptions } from '../types/options';\nimport { TFunctionType } from '../types/functions';\nimport { interpolateMessage } from '../utils/interpolation/interpolateMessage';\nimport { createLookupOptions } from './helpers';\nimport { extractVariables } from '../../utils/extractVariables';\nimport type { StringFormat } from 'generaltranslation/types';\nimport type {\n DictionaryEntry,\n DictionaryValue,\n} from '../../i18n-manager/translations-manager/DictionaryCache';\nimport {\n getDictionaryEntry,\n isDictionaryValue,\n resolveDictionaryLookupOptions,\n} from '../../i18n-manager/translations-manager/utils/dictionary-helpers';\nimport type { DictionaryObjectTranslation } from '../types/functions';\n\n/**\n * Returns the t function that translates a dictionary entry based on its id and options.\n * @returns A promise of the t function\n * @important Must be used inside of a request context\n *\n * @example\n * const t = await getTranslations();\n * const title = await t('page.title');\n */\nexport async function getTranslations(): Promise<TFunctionType> {\n const i18nManager = getI18nManager();\n const locale = getCurrentLocale();\n await Promise.all([\n i18nManager.loadDictionary(locale),\n i18nManager.loadTranslations(locale),\n ]);\n const sourceLocale = i18nManager.getDefaultLocale();\n\n /**\n * Dictionary resolution\n * @param {string} id - The id of the translation to translate.\n * @param {DictionaryTranslationOptions} options - The options for interpolation.\n * @returns {string} The translated message.\n *\n * @example\n * // Simple dictionary lookup without interpolation\n * const t = await getTranslations();\n * const title = t('page.title');\n *\n * @example\n * // Dictionary lookup with interpolation\n * const t = await getTranslations();\n * const greeting = t('user.greeting', { name: 'Bob' });\n */\n const t = ((\n id: string,\n options: DictionaryTranslationOptions = {}\n ): string => {\n const sourceEntry = i18nManager.lookupDictionary(sourceLocale, id);\n if (sourceEntry === undefined) {\n throw new Error(`Dictionary entry ${id} cannot be found`);\n }\n const targetEntry = i18nManager.lookupDictionary(locale, id);\n const dictionaryOptions = resolveDictionaryLookupOptions(\n sourceEntry.options\n );\n const target =\n targetEntry?.entry ??\n i18nManager.lookupTranslation(\n locale,\n sourceEntry.entry,\n dictionaryOptions\n );\n return renderEntry({\n sourceLocale,\n targetLocale: locale,\n sourceEntry,\n target,\n dictionaryOptions,\n options,\n });\n }) as TFunctionType;\n\n /**\n * Returns a subtree of the dictionary object translation based on its id.\n * @param {string} id - The id of the translation to translate.\n * @returns The translated object.\n *\n * @example\n * const t = await getTranslations();\n * const greetings = t.obj('greetings');\n * console.log(greetings);\n * // { greeting1: 'Hello', greeting2: 'Hi' }\n */\n t.obj = (id: string): DictionaryObjectTranslation => {\n const sourceObject = i18nManager.lookupDictionaryObj(sourceLocale, id);\n if (sourceObject === undefined) {\n throw new Error(`Dictionary entry ${id} cannot be found`);\n }\n const targetObject = i18nManager.lookupDictionaryObj(locale, id);\n return renderObject({ sourceObject, targetObject });\n };\n\n function renderObject({\n sourceObject,\n targetObject,\n }: {\n sourceObject: DictionaryValue | undefined;\n targetObject: DictionaryValue | undefined;\n }): DictionaryObjectTranslation {\n const targetEntry = getDictionaryEntry(targetObject);\n if (targetEntry !== undefined) {\n return targetEntry.entry;\n }\n\n if (isDictionaryValue(targetObject)) {\n if (!isDictionaryValue(sourceObject)) {\n return renderObject({\n sourceObject: targetObject,\n targetObject: undefined,\n });\n }\n\n return renderDictionaryObject({\n sourceObject,\n targetObject,\n });\n }\n\n const sourceEntry = getDictionaryEntry(sourceObject);\n if (sourceEntry !== undefined) {\n // Fallback to translations cache\n const dictionaryOptions = resolveDictionaryLookupOptions(\n sourceEntry.options\n );\n\n const target = i18nManager.lookupTranslation(\n locale,\n sourceEntry.entry,\n dictionaryOptions\n );\n if (target !== undefined) {\n return target;\n }\n\n // Fallback to source entry\n return sourceEntry.entry;\n }\n\n if (isDictionaryValue(sourceObject)) {\n return renderDictionaryObject({\n sourceObject,\n targetObject: undefined,\n });\n }\n\n throw new Error('Dictionary object cannot be rendered');\n }\n\n function renderDictionaryObject({\n sourceObject,\n targetObject,\n }: {\n sourceObject: DictionaryValue;\n targetObject: DictionaryValue | undefined;\n }): DictionaryObjectTranslation {\n if (!isDictionaryValue(sourceObject)) {\n return renderObject({ sourceObject, targetObject });\n }\n const result: Record<string, DictionaryObjectTranslation> = {};\n const keys = new Set([\n ...Object.keys(sourceObject),\n ...(isDictionaryValue(targetObject) ? Object.keys(targetObject) : []),\n ]);\n\n for (const key of Array.from(keys)) {\n const renderedChild = renderObject({\n sourceObject: sourceObject[key],\n targetObject: isDictionaryValue(targetObject)\n ? targetObject[key]\n : undefined,\n });\n if (renderedChild !== undefined) {\n result[key] = renderedChild;\n }\n }\n\n return result;\n }\n\n return t;\n}\n\nfunction renderEntry({\n sourceLocale,\n targetLocale,\n sourceEntry,\n target,\n dictionaryOptions,\n options = {},\n}: {\n sourceLocale: string;\n targetLocale: string;\n sourceEntry: DictionaryEntry;\n target: string | undefined;\n dictionaryOptions: ReturnType<typeof resolveDictionaryLookupOptions>;\n options?: DictionaryTranslationOptions;\n}): string {\n const interpolationOptions = extractVariables(options);\n const lookupOptions = createLookupOptions<StringFormat>(\n targetLocale,\n {\n ...dictionaryOptions,\n ...interpolationOptions,\n },\n dictionaryOptions.$format ?? 'ICU'\n );\n\n return interpolateMessage({\n source: sourceEntry.entry,\n target,\n options: lookupOptions,\n sourceLocale,\n });\n}\n","import { RuntimeTranslationOptions } from '../types/options';\nimport type { StringFormat } from 'generaltranslation/types';\nimport { resolveStringContentWithRuntimeFallback } from './helpers';\nimport { getCurrentLocale } from '../../i18n-manager/singleton-operations';\n\ntype RuntimeTranslationOptionsWithFormat = Omit<\n RuntimeTranslationOptions,\n '$format'\n> & {\n $format?: StringFormat;\n};\n\n/**\n * Translates a message at runtime.\n * @param {string} message - The message to translate.\n * @param {RuntimeTranslationOptions} options - The options for the translation.\n * @returns {Promise<string>} The translated message.\n *\n * @example\n * // Simple runtime translation without interpolation\n * const status = await tx('Processing complete', { $locale: 'es-MX' });\n *\n * @example\n * // Runtime translation with interpolation\n * const progress = await tx(`Processing ${status}`, { $locale: 'es-MX' });\n */\nexport async function tx(\n content: string,\n options: RuntimeTranslationOptionsWithFormat = {}\n): Promise<string> {\n const locale = options.$locale ?? getCurrentLocale();\n return resolveStringContentWithRuntimeFallback(locale, content, {\n $format: 'STRING',\n ...options,\n });\n}\n","import { InlineTranslationOptions } from '../types/options';\nimport {\n resolveStringContent,\n resolveStringContentWithFallback,\n} from './helpers';\n\n/**\n * Synchronously resolve a translation for a given message and options\n * @param {string} message - The message to translate.\n * @param {InlineTranslationOptions} [options] - The options for the translation.\n * @returns {string | undefined} The translated message or undefined if the message is not found.\n * @deprecated use resolveTranslation instead\n */\nexport function resolveTranslationSync(\n locale: string,\n message: string,\n options: InlineTranslationOptions = {}\n) {\n return resolveStringContent(locale, message, { $format: 'ICU', ...options });\n}\n\n/**\n * Translate a message\n * @param {string} message - The message to translate.\n * @param {InlineTranslationOptions} [options] - The options for the translation.\n * @returns {string} The translated message or the original message interpolated if the translation is not found.\n * @deprecated use resolveTranslationWithFallback instead\n */\nexport function resolveTranslationSyncWithFallback(\n locale: string,\n message: string,\n options: InlineTranslationOptions = {}\n) {\n return resolveStringContentWithFallback(locale, message, {\n $format: 'ICU',\n ...options,\n });\n}\n","import { JsxChildren } from 'generaltranslation/types';\nimport { JsxTranslationOptions } from '../types/options';\nimport { resolveJsx } from './helpers';\n\n/**\n * Given a JsxChildren and any associated metadata needed for a hash calculation, resolve the associated translation.\n * @deprecated use resolveJsx instead\n */\nexport function resolveJsxTranslation(\n locale: string,\n children: JsxChildren,\n options: JsxTranslationOptions = {}\n) {\n return resolveJsx(locale, children, options);\n}\n","import type { ConditionStore } from '../types';\nimport { setConditionStore as setCurrentConditionStore } from '../singleton-operations';\n\nexport function createConditionStoreSingleton<T extends ConditionStore>(\n notInitializedMessage: string\n) {\n let conditionStore: T | undefined;\n\n function getConditionStore(): T {\n if (!conditionStore) {\n throw new Error(notInitializedMessage);\n }\n return conditionStore;\n }\n\n function setConditionStore(nextConditionStore: T): void {\n conditionStore = nextConditionStore;\n setCurrentConditionStore(nextConditionStore);\n }\n\n return {\n getConditionStore,\n setConditionStore,\n };\n}\n","import { LocaleConfig } from 'generaltranslation/core';\nimport { libraryDefaultLocale } from 'generaltranslation/internal';\nimport type { ConditionStoreConfig } from '../types';\n\nexport type LocaleCandidates = string | string[] | undefined;\n\nfunction normalizeConditionStoreConfig({\n defaultLocale,\n locales,\n customMapping,\n}: ConditionStoreConfig = {}) {\n const fallbackLocale = defaultLocale || libraryDefaultLocale;\n return {\n defaultLocale: fallbackLocale,\n locales: locales?.length ? locales : [fallbackLocale],\n customMapping,\n };\n}\n\ntype NormalizedConditionStoreConfig = ReturnType<\n typeof normalizeConditionStoreConfig\n>;\n\n/**\n * Return the best supported locale for the candidates, or undefined when none match.\n */\nexport function determineSupportedLocale(\n candidates: LocaleCandidates,\n config: ConditionStoreConfig = {}\n): string | undefined {\n return determineSupportedLocaleWithConfig(\n candidates,\n normalizeConditionStoreConfig(config)\n );\n}\n\nfunction determineSupportedLocaleWithConfig(\n candidates: LocaleCandidates,\n config: NormalizedConditionStoreConfig\n): string | undefined {\n if (\n candidates == null ||\n (Array.isArray(candidates) && candidates.length === 0)\n ) {\n return undefined;\n }\n\n const localeConfig = new LocaleConfig(config);\n return localeConfig.determineLocale(candidates) || undefined;\n}\n\n/**\n * Return the best supported locale for the candidates, falling back to the default locale.\n */\nexport function resolveSupportedLocale(\n candidates: LocaleCandidates,\n config: ConditionStoreConfig = {}\n): string {\n const normalizedConfig = normalizeConditionStoreConfig(config);\n return (\n determineSupportedLocaleWithConfig(candidates, normalizedConfig) ||\n normalizedConfig.defaultLocale\n );\n}\n\nexport function createLocaleResolver(config: ConditionStoreConfig = {}) {\n const normalizedConfig = normalizeConditionStoreConfig(config);\n return (candidates?: LocaleCandidates): string =>\n determineSupportedLocaleWithConfig(candidates, normalizedConfig) ||\n normalizedConfig.defaultLocale;\n}\n"],"mappings":";;;;;;;;;;;;;;AAkBA,eAAsB,QAAiC;CAErD,MAAM,cAAcA,kBAAAA,gBAAgB;CACpC,MAAM,SAASC,kBAAAA,kBAAkB;AACjC,OAAM,YAAY,iBAAiB,OAAO;CAC1C,MAAM,eAAe,YAAY,kBAAkB;;;;;;;;;;;;;;;;;CAkBnD,MAAM,MACJ,SACA,UAAoC,EAAE,KACnC;EAEH,MAAM,gBAAgBC,kBAAAA,oBADD,QAAQ,WAAW,QAGtC,SACA,MACD;AAUD,SAAOC,kBAAAA,mBAAmB;GACxB,QAAQ;GACR,QATkB,YAAY,kBAC9B,cAAc,SACd,SACA,cAMmB;GACnB,SAAS;GACT;GACD,CAAC;;AAGJ,QAAO;;;;;;;;;;;;;;;;AClDT,eAAsB,cAAsC;CAE1D,MAAM,KAAK,MAAM,OAAO;;;;;;;;;;;;;;;;;CAkBxB,MAAM,KACJ,YACA,UAAgC,EA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W,IAAI,WAAW,WAAW,EAEpD;AAIF,QAAO,IADkBC,wBAAAA,aAAa,OACnB,CAAC,gBAAgB,WAAW,IAAI,KAAA;;;;;AAMrD,SAAgB,uBACd,YACA,SAA+B,EAAE,EACzB;CACR,MAAM,mBAAmB,8BAA8B,OAAO;AAC9D,QACE,mCAAmC,YAAY,iBAAiB,IAChE,iBAAiB;;AAIrB,SAAgB,qBAAqB,SAA+B,EAAE,EAAE;CACtE,MAAM,mBAAmB,8BAA8B,OAAO;AAC9D,SAAQ,eACN,mCAAmC,YAAY,iBAAiB,IAChE,iBAAiB"}
|
package/dist/internal.d.cts
CHANGED
|
@@ -129,6 +129,10 @@ type EventName = string;
|
|
|
129
129
|
type BaseEvent = Record<EventName, unknown>;
|
|
130
130
|
type Listener<Events extends BaseEvent, EventName extends keyof Events = keyof Events> = (event: Events[EventName]) => void;
|
|
131
131
|
type ListenerStore<Events extends BaseEvent> = Partial<{ [EventName in keyof Events]: Set<Listener<Events, EventName>> }>;
|
|
132
|
+
declare const LOCALES_CACHE_MISS_EVENT_NAME = "locales-cache-miss";
|
|
133
|
+
declare const TRANSLATIONS_CACHE_MISS_EVENT_NAME = "translations-cache-miss";
|
|
134
|
+
declare const LOCALES_DICTIONARY_CACHE_MISS_EVENT_NAME = "locales-dictionary-cache-miss";
|
|
135
|
+
declare const DICTIONARY_CACHE_MISS_EVENT_NAME = "dictionary-cache-miss";
|
|
132
136
|
/**
|
|
133
137
|
* A base event for the I18nManagers
|
|
134
138
|
* @prop {locales-cache-hit} - Emitted when a locale cache hit occurs
|
|
@@ -146,7 +150,7 @@ type I18nEvents<TranslationValue extends Translation> = BaseEvent & {
|
|
|
146
150
|
locale: Locale;
|
|
147
151
|
translations: Record<Hash, TranslationValue>;
|
|
148
152
|
};
|
|
149
|
-
|
|
153
|
+
[LOCALES_CACHE_MISS_EVENT_NAME]: {
|
|
150
154
|
locale: Locale;
|
|
151
155
|
translations: Record<Hash, TranslationValue>;
|
|
152
156
|
};
|
|
@@ -155,7 +159,7 @@ type I18nEvents<TranslationValue extends Translation> = BaseEvent & {
|
|
|
155
159
|
hash: Hash;
|
|
156
160
|
translation: TranslationValue;
|
|
157
161
|
};
|
|
158
|
-
|
|
162
|
+
[TRANSLATIONS_CACHE_MISS_EVENT_NAME]: {
|
|
159
163
|
locale: Locale;
|
|
160
164
|
hash: Hash;
|
|
161
165
|
translation: TranslationValue;
|
|
@@ -164,7 +168,7 @@ type I18nEvents<TranslationValue extends Translation> = BaseEvent & {
|
|
|
164
168
|
locale: Locale;
|
|
165
169
|
dictionary: Dictionary;
|
|
166
170
|
};
|
|
167
|
-
|
|
171
|
+
[LOCALES_DICTIONARY_CACHE_MISS_EVENT_NAME]: {
|
|
168
172
|
locale: Locale;
|
|
169
173
|
dictionary: Dictionary;
|
|
170
174
|
};
|
|
@@ -173,7 +177,7 @@ type I18nEvents<TranslationValue extends Translation> = BaseEvent & {
|
|
|
173
177
|
id: DictionaryPath;
|
|
174
178
|
dictionaryEntry: DictionaryEntry;
|
|
175
179
|
};
|
|
176
|
-
|
|
180
|
+
[DICTIONARY_CACHE_MISS_EVENT_NAME]: {
|
|
177
181
|
locale: Locale;
|
|
178
182
|
id: DictionaryPath;
|
|
179
183
|
dictionaryEntry: DictionaryEntry;
|
|
@@ -248,7 +252,7 @@ declare class I18nManager<TranslationValue extends Translation = Translation> ex
|
|
|
248
252
|
*
|
|
249
253
|
* Pair this with {@link lookupTranslation} to get the translation entry
|
|
250
254
|
*/
|
|
251
|
-
subscribeToTranslationsCacheMiss(listener: (event: I18nEvents<TranslationValue>[
|
|
255
|
+
subscribeToTranslationsCacheMiss(listener: (event: I18nEvents<TranslationValue>[typeof TRANSLATIONS_CACHE_MISS_EVENT_NAME]) => void, locale: Locale, hash: Hash): () => void;
|
|
252
256
|
/**
|
|
253
257
|
* Get the default locale
|
|
254
258
|
*/
|
|
@@ -430,5 +434,5 @@ declare function setI18nManager<TranslationValue extends Translation>(i18nManage
|
|
|
430
434
|
*/
|
|
431
435
|
declare function interpolateIcuMessage<T extends string | null | undefined>(encodedMsg: T, options: InlineTranslationOptions): T extends string ? string : T;
|
|
432
436
|
//#endregion
|
|
433
|
-
export { I18nManager, type LocaleCandidates, createConditionStoreSingleton, createLocaleResolver, createLookupOptions, determineSupportedLocale, getCurrentLocale, getDefaultLocale, getGT, getI18nManager, getLocale, getLocaleProperties, getLocales, getMessages, getTranslations, getVersionId, interpolateIcuMessage, resolveJsx, resolveJsxTranslation, resolveJsxWithFallback, resolveJsxWithRuntimeFallback, resolveStringContent, resolveStringContentWithFallback, resolveStringContentWithRuntimeFallback, resolveSupportedLocale, resolveTranslationSync, resolveTranslationSyncWithFallback, setConditionStore, setI18nManager, tx };
|
|
437
|
+
export { DICTIONARY_CACHE_MISS_EVENT_NAME, I18nManager, LOCALES_CACHE_MISS_EVENT_NAME, LOCALES_DICTIONARY_CACHE_MISS_EVENT_NAME, type LocaleCandidates, TRANSLATIONS_CACHE_MISS_EVENT_NAME, createConditionStoreSingleton, createLocaleResolver, createLookupOptions, determineSupportedLocale, getCurrentLocale, getDefaultLocale, getGT, getI18nManager, getLocale, getLocaleProperties, getLocales, getMessages, getTranslations, getVersionId, interpolateIcuMessage, resolveJsx, resolveJsxTranslation, resolveJsxWithFallback, resolveJsxWithRuntimeFallback, resolveStringContent, resolveStringContentWithFallback, resolveStringContentWithRuntimeFallback, resolveSupportedLocale, resolveTranslationSync, resolveTranslationSyncWithFallback, setConditionStore, setI18nManager, tx };
|
|
434
438
|
//# sourceMappingURL=internal.d.cts.map
|
package/dist/internal.d.mts
CHANGED
|
@@ -129,6 +129,10 @@ type EventName = string;
|
|
|
129
129
|
type BaseEvent = Record<EventName, unknown>;
|
|
130
130
|
type Listener<Events extends BaseEvent, EventName extends keyof Events = keyof Events> = (event: Events[EventName]) => void;
|
|
131
131
|
type ListenerStore<Events extends BaseEvent> = Partial<{ [EventName in keyof Events]: Set<Listener<Events, EventName>> }>;
|
|
132
|
+
declare const LOCALES_CACHE_MISS_EVENT_NAME = "locales-cache-miss";
|
|
133
|
+
declare const TRANSLATIONS_CACHE_MISS_EVENT_NAME = "translations-cache-miss";
|
|
134
|
+
declare const LOCALES_DICTIONARY_CACHE_MISS_EVENT_NAME = "locales-dictionary-cache-miss";
|
|
135
|
+
declare const DICTIONARY_CACHE_MISS_EVENT_NAME = "dictionary-cache-miss";
|
|
132
136
|
/**
|
|
133
137
|
* A base event for the I18nManagers
|
|
134
138
|
* @prop {locales-cache-hit} - Emitted when a locale cache hit occurs
|
|
@@ -146,7 +150,7 @@ type I18nEvents<TranslationValue extends Translation> = BaseEvent & {
|
|
|
146
150
|
locale: Locale;
|
|
147
151
|
translations: Record<Hash, TranslationValue>;
|
|
148
152
|
};
|
|
149
|
-
|
|
153
|
+
[LOCALES_CACHE_MISS_EVENT_NAME]: {
|
|
150
154
|
locale: Locale;
|
|
151
155
|
translations: Record<Hash, TranslationValue>;
|
|
152
156
|
};
|
|
@@ -155,7 +159,7 @@ type I18nEvents<TranslationValue extends Translation> = BaseEvent & {
|
|
|
155
159
|
hash: Hash;
|
|
156
160
|
translation: TranslationValue;
|
|
157
161
|
};
|
|
158
|
-
|
|
162
|
+
[TRANSLATIONS_CACHE_MISS_EVENT_NAME]: {
|
|
159
163
|
locale: Locale;
|
|
160
164
|
hash: Hash;
|
|
161
165
|
translation: TranslationValue;
|
|
@@ -164,7 +168,7 @@ type I18nEvents<TranslationValue extends Translation> = BaseEvent & {
|
|
|
164
168
|
locale: Locale;
|
|
165
169
|
dictionary: Dictionary;
|
|
166
170
|
};
|
|
167
|
-
|
|
171
|
+
[LOCALES_DICTIONARY_CACHE_MISS_EVENT_NAME]: {
|
|
168
172
|
locale: Locale;
|
|
169
173
|
dictionary: Dictionary;
|
|
170
174
|
};
|
|
@@ -173,7 +177,7 @@ type I18nEvents<TranslationValue extends Translation> = BaseEvent & {
|
|
|
173
177
|
id: DictionaryPath;
|
|
174
178
|
dictionaryEntry: DictionaryEntry;
|
|
175
179
|
};
|
|
176
|
-
|
|
180
|
+
[DICTIONARY_CACHE_MISS_EVENT_NAME]: {
|
|
177
181
|
locale: Locale;
|
|
178
182
|
id: DictionaryPath;
|
|
179
183
|
dictionaryEntry: DictionaryEntry;
|
|
@@ -248,7 +252,7 @@ declare class I18nManager<TranslationValue extends Translation = Translation> ex
|
|
|
248
252
|
*
|
|
249
253
|
* Pair this with {@link lookupTranslation} to get the translation entry
|
|
250
254
|
*/
|
|
251
|
-
subscribeToTranslationsCacheMiss(listener: (event: I18nEvents<TranslationValue>[
|
|
255
|
+
subscribeToTranslationsCacheMiss(listener: (event: I18nEvents<TranslationValue>[typeof TRANSLATIONS_CACHE_MISS_EVENT_NAME]) => void, locale: Locale, hash: Hash): () => void;
|
|
252
256
|
/**
|
|
253
257
|
* Get the default locale
|
|
254
258
|
*/
|
|
@@ -430,5 +434,5 @@ declare function setI18nManager<TranslationValue extends Translation>(i18nManage
|
|
|
430
434
|
*/
|
|
431
435
|
declare function interpolateIcuMessage<T extends string | null | undefined>(encodedMsg: T, options: InlineTranslationOptions): T extends string ? string : T;
|
|
432
436
|
//#endregion
|
|
433
|
-
export { I18nManager, type LocaleCandidates, createConditionStoreSingleton, createLocaleResolver, createLookupOptions, determineSupportedLocale, getCurrentLocale, getDefaultLocale, getGT, getI18nManager, getLocale, getLocaleProperties, getLocales, getMessages, getTranslations, getVersionId, interpolateIcuMessage, resolveJsx, resolveJsxTranslation, resolveJsxWithFallback, resolveJsxWithRuntimeFallback, resolveStringContent, resolveStringContentWithFallback, resolveStringContentWithRuntimeFallback, resolveSupportedLocale, resolveTranslationSync, resolveTranslationSyncWithFallback, setConditionStore, setI18nManager, tx };
|
|
437
|
+
export { DICTIONARY_CACHE_MISS_EVENT_NAME, I18nManager, LOCALES_CACHE_MISS_EVENT_NAME, LOCALES_DICTIONARY_CACHE_MISS_EVENT_NAME, type LocaleCandidates, TRANSLATIONS_CACHE_MISS_EVENT_NAME, createConditionStoreSingleton, createLocaleResolver, createLookupOptions, determineSupportedLocale, getCurrentLocale, getDefaultLocale, getGT, getI18nManager, getLocale, getLocaleProperties, getLocales, getMessages, getTranslations, getVersionId, interpolateIcuMessage, resolveJsx, resolveJsxTranslation, resolveJsxWithFallback, resolveJsxWithRuntimeFallback, resolveStringContent, resolveStringContentWithFallback, resolveStringContentWithRuntimeFallback, resolveSupportedLocale, resolveTranslationSync, resolveTranslationSyncWithFallback, setConditionStore, setI18nManager, tx };
|
|
434
438
|
//# sourceMappingURL=internal.d.mts.map
|
package/dist/internal.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { a as extractVariables, n as decodeOptions, r as interpolateIcuMessage, t as isEncodedTranslationOptions } from "./isEncodedTranslationOptions-DdHEzDDM.mjs";
|
|
2
|
-
import { _ as setI18nManager, a as getLocales, b as
|
|
2
|
+
import { C as getDictionaryEntry, S as TRANSLATIONS_CACHE_MISS_EVENT_NAME, T as resolveDictionaryLookupOptions, _ as setI18nManager, a as getLocales, b as LOCALES_CACHE_MISS_EVENT_NAME, c as resolveJsxWithFallback, d as resolveStringContentWithFallback, f as resolveStringContentWithRuntimeFallback, g as setConditionStore, h as getI18nManager, i as getLocaleProperties, l as resolveJsxWithRuntimeFallback, m as getCurrentLocale, n as getDefaultLocale, o as createLookupOptions, p as interpolateMessage, r as getLocale, s as resolveJsx, t as getVersionId, u as resolveStringContent, v as I18nManager, w as isDictionaryValue, x as LOCALES_DICTIONARY_CACHE_MISS_EVENT_NAME, y as DICTIONARY_CACHE_MISS_EVENT_NAME } from "./versionId-CB81YFd9.mjs";
|
|
3
3
|
import { libraryDefaultLocale } from "generaltranslation/internal";
|
|
4
4
|
import { LocaleConfig } from "generaltranslation/core";
|
|
5
5
|
//#region src/translation-functions/internal/getGT.ts
|
|
@@ -97,7 +97,7 @@ async function getMessages() {
|
|
|
97
97
|
async function getTranslations() {
|
|
98
98
|
const i18nManager = getI18nManager();
|
|
99
99
|
const locale = getCurrentLocale();
|
|
100
|
-
await i18nManager.loadDictionary(locale);
|
|
100
|
+
await Promise.all([i18nManager.loadDictionary(locale), i18nManager.loadTranslations(locale)]);
|
|
101
101
|
const sourceLocale = i18nManager.getDefaultLocale();
|
|
102
102
|
/**
|
|
103
103
|
* Dictionary resolution
|
|
@@ -118,14 +118,28 @@ async function getTranslations() {
|
|
|
118
118
|
const t = ((id, options = {}) => {
|
|
119
119
|
const sourceEntry = i18nManager.lookupDictionary(sourceLocale, id);
|
|
120
120
|
if (sourceEntry === void 0) throw new Error(`Dictionary entry ${id} cannot be found`);
|
|
121
|
+
const targetEntry = i18nManager.lookupDictionary(locale, id);
|
|
122
|
+
const dictionaryOptions = resolveDictionaryLookupOptions(sourceEntry.options);
|
|
121
123
|
return renderEntry({
|
|
122
124
|
sourceLocale,
|
|
123
125
|
targetLocale: locale,
|
|
124
126
|
sourceEntry,
|
|
125
|
-
|
|
127
|
+
target: targetEntry?.entry ?? i18nManager.lookupTranslation(locale, sourceEntry.entry, dictionaryOptions),
|
|
128
|
+
dictionaryOptions,
|
|
126
129
|
options
|
|
127
130
|
});
|
|
128
131
|
});
|
|
132
|
+
/**
|
|
133
|
+
* Returns a subtree of the dictionary object translation based on its id.
|
|
134
|
+
* @param {string} id - The id of the translation to translate.
|
|
135
|
+
* @returns The translated object.
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* const t = await getTranslations();
|
|
139
|
+
* const greetings = t.obj('greetings');
|
|
140
|
+
* console.log(greetings);
|
|
141
|
+
* // { greeting1: 'Hello', greeting2: 'Hi' }
|
|
142
|
+
*/
|
|
129
143
|
t.obj = (id) => {
|
|
130
144
|
const sourceObject = i18nManager.lookupDictionaryObj(sourceLocale, id);
|
|
131
145
|
if (sourceObject === void 0) throw new Error(`Dictionary entry ${id} cannot be found`);
|
|
@@ -134,10 +148,51 @@ async function getTranslations() {
|
|
|
134
148
|
targetObject: i18nManager.lookupDictionaryObj(locale, id)
|
|
135
149
|
});
|
|
136
150
|
};
|
|
151
|
+
function renderObject({ sourceObject, targetObject }) {
|
|
152
|
+
const targetEntry = getDictionaryEntry(targetObject);
|
|
153
|
+
if (targetEntry !== void 0) return targetEntry.entry;
|
|
154
|
+
if (isDictionaryValue(targetObject)) {
|
|
155
|
+
if (!isDictionaryValue(sourceObject)) return renderObject({
|
|
156
|
+
sourceObject: targetObject,
|
|
157
|
+
targetObject: void 0
|
|
158
|
+
});
|
|
159
|
+
return renderDictionaryObject({
|
|
160
|
+
sourceObject,
|
|
161
|
+
targetObject
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
const sourceEntry = getDictionaryEntry(sourceObject);
|
|
165
|
+
if (sourceEntry !== void 0) {
|
|
166
|
+
const dictionaryOptions = resolveDictionaryLookupOptions(sourceEntry.options);
|
|
167
|
+
const target = i18nManager.lookupTranslation(locale, sourceEntry.entry, dictionaryOptions);
|
|
168
|
+
if (target !== void 0) return target;
|
|
169
|
+
return sourceEntry.entry;
|
|
170
|
+
}
|
|
171
|
+
if (isDictionaryValue(sourceObject)) return renderDictionaryObject({
|
|
172
|
+
sourceObject,
|
|
173
|
+
targetObject: void 0
|
|
174
|
+
});
|
|
175
|
+
throw new Error("Dictionary object cannot be rendered");
|
|
176
|
+
}
|
|
177
|
+
function renderDictionaryObject({ sourceObject, targetObject }) {
|
|
178
|
+
if (!isDictionaryValue(sourceObject)) return renderObject({
|
|
179
|
+
sourceObject,
|
|
180
|
+
targetObject
|
|
181
|
+
});
|
|
182
|
+
const result = {};
|
|
183
|
+
const keys = new Set([...Object.keys(sourceObject), ...isDictionaryValue(targetObject) ? Object.keys(targetObject) : []]);
|
|
184
|
+
for (const key of Array.from(keys)) {
|
|
185
|
+
const renderedChild = renderObject({
|
|
186
|
+
sourceObject: sourceObject[key],
|
|
187
|
+
targetObject: isDictionaryValue(targetObject) ? targetObject[key] : void 0
|
|
188
|
+
});
|
|
189
|
+
if (renderedChild !== void 0) result[key] = renderedChild;
|
|
190
|
+
}
|
|
191
|
+
return result;
|
|
192
|
+
}
|
|
137
193
|
return t;
|
|
138
194
|
}
|
|
139
|
-
function renderEntry({ sourceLocale, targetLocale, sourceEntry,
|
|
140
|
-
const dictionaryOptions = resolveDictionaryLookupOptions(sourceEntry.options);
|
|
195
|
+
function renderEntry({ sourceLocale, targetLocale, sourceEntry, target, dictionaryOptions, options = {} }) {
|
|
141
196
|
const interpolationOptions = extractVariables(options);
|
|
142
197
|
const lookupOptions = createLookupOptions(targetLocale, {
|
|
143
198
|
...dictionaryOptions,
|
|
@@ -145,48 +200,11 @@ function renderEntry({ sourceLocale, targetLocale, sourceEntry, targetEntry, opt
|
|
|
145
200
|
}, dictionaryOptions.$format ?? "ICU");
|
|
146
201
|
return interpolateMessage({
|
|
147
202
|
source: sourceEntry.entry,
|
|
148
|
-
target
|
|
203
|
+
target,
|
|
149
204
|
options: lookupOptions,
|
|
150
205
|
sourceLocale
|
|
151
206
|
});
|
|
152
207
|
}
|
|
153
|
-
function renderObject({ sourceObject, targetObject }) {
|
|
154
|
-
const targetEntry = getDictionaryEntry(targetObject);
|
|
155
|
-
if (targetEntry !== void 0) return targetEntry.entry;
|
|
156
|
-
if (isDictionaryValue(targetObject)) {
|
|
157
|
-
if (!isDictionaryValue(sourceObject)) return renderObject({
|
|
158
|
-
sourceObject: targetObject,
|
|
159
|
-
targetObject: void 0
|
|
160
|
-
});
|
|
161
|
-
return renderDictionaryObject({
|
|
162
|
-
sourceObject,
|
|
163
|
-
targetObject
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
const sourceEntry = getDictionaryEntry(sourceObject);
|
|
167
|
-
if (sourceEntry !== void 0) return sourceEntry.entry;
|
|
168
|
-
if (isDictionaryValue(sourceObject)) return renderDictionaryObject({
|
|
169
|
-
sourceObject,
|
|
170
|
-
targetObject: void 0
|
|
171
|
-
});
|
|
172
|
-
throw new Error("Dictionary object cannot be rendered");
|
|
173
|
-
}
|
|
174
|
-
function renderDictionaryObject({ sourceObject, targetObject }) {
|
|
175
|
-
if (!isDictionaryValue(sourceObject)) return renderObject({
|
|
176
|
-
sourceObject,
|
|
177
|
-
targetObject
|
|
178
|
-
});
|
|
179
|
-
const result = {};
|
|
180
|
-
const keys = new Set([...Object.keys(sourceObject), ...isDictionaryValue(targetObject) ? Object.keys(targetObject) : []]);
|
|
181
|
-
for (const key of keys) {
|
|
182
|
-
const renderedChild = renderObject({
|
|
183
|
-
sourceObject: sourceObject[key],
|
|
184
|
-
targetObject: isDictionaryValue(targetObject) ? targetObject[key] : void 0
|
|
185
|
-
});
|
|
186
|
-
if (renderedChild !== void 0) result[key] = renderedChild;
|
|
187
|
-
}
|
|
188
|
-
return result;
|
|
189
|
-
}
|
|
190
208
|
//#endregion
|
|
191
209
|
//#region src/translation-functions/internal/tx.ts
|
|
192
210
|
/**
|
|
@@ -295,6 +313,6 @@ function createLocaleResolver(config = {}) {
|
|
|
295
313
|
return (candidates) => determineSupportedLocaleWithConfig(candidates, normalizedConfig) || normalizedConfig.defaultLocale;
|
|
296
314
|
}
|
|
297
315
|
//#endregion
|
|
298
|
-
export { I18nManager, createConditionStoreSingleton, createLocaleResolver, createLookupOptions, determineSupportedLocale, getCurrentLocale, getDefaultLocale, getGT, getI18nManager, getLocale, getLocaleProperties, getLocales, getMessages, getTranslations, getVersionId, interpolateIcuMessage, resolveJsx, resolveJsxTranslation, resolveJsxWithFallback, resolveJsxWithRuntimeFallback, resolveStringContent, resolveStringContentWithFallback, resolveStringContentWithRuntimeFallback, resolveSupportedLocale, resolveTranslationSync, resolveTranslationSyncWithFallback, setConditionStore, setI18nManager, tx };
|
|
316
|
+
export { DICTIONARY_CACHE_MISS_EVENT_NAME, I18nManager, LOCALES_CACHE_MISS_EVENT_NAME, LOCALES_DICTIONARY_CACHE_MISS_EVENT_NAME, TRANSLATIONS_CACHE_MISS_EVENT_NAME, createConditionStoreSingleton, createLocaleResolver, createLookupOptions, determineSupportedLocale, getCurrentLocale, getDefaultLocale, getGT, getI18nManager, getLocale, getLocaleProperties, getLocales, getMessages, getTranslations, getVersionId, interpolateIcuMessage, resolveJsx, resolveJsxTranslation, resolveJsxWithFallback, resolveJsxWithRuntimeFallback, resolveStringContent, resolveStringContentWithFallback, resolveStringContentWithRuntimeFallback, resolveSupportedLocale, resolveTranslationSync, resolveTranslationSyncWithFallback, setConditionStore, setI18nManager, tx };
|
|
299
317
|
|
|
300
318
|
//# sourceMappingURL=internal.mjs.map
|