gt-i18n 0.9.3 → 0.9.5
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 +15 -0
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/internal-types.d.cts +2 -2
- package/dist/internal-types.d.mts +2 -2
- package/dist/internal.cjs +7 -7
- package/dist/internal.cjs.map +1 -1
- package/dist/internal.d.cts +19 -16
- package/dist/internal.d.mts +19 -16
- package/dist/internal.mjs +7 -7
- package/dist/internal.mjs.map +1 -1
- package/dist/{localeResolver-CYvrEtm0.d.cts → localeResolver-BSGZjvxg.d.cts} +3 -9
- package/dist/{localeResolver-Ci80ksxO.d.mts → localeResolver-DjL1m1_w.d.mts} +3 -9
- package/dist/{TranslationsCache-DKw8slPx.d.cts → types-BtGFavAy.d.cts} +14 -11
- package/dist/{TranslationsCache-C12Vb-dZ.d.mts → types-PZn6hXxt.d.mts} +14 -11
- package/dist/types.d.cts +1 -1
- package/dist/types.d.mts +1 -1
- package/dist/{versionId-C4WMCH8U.cjs → versionId-7sHbIIgr.cjs} +473 -652
- package/dist/versionId-7sHbIIgr.cjs.map +1 -0
- package/dist/{versionId-BkJZGHXr.mjs → versionId-BTjLA0FZ.mjs} +472 -651
- package/dist/versionId-BTjLA0FZ.mjs.map +1 -0
- package/package.json +3 -3
- package/dist/versionId-BkJZGHXr.mjs.map +0 -1
- package/dist/versionId-C4WMCH8U.cjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# gt-i18n
|
|
2
2
|
|
|
3
|
+
## 0.9.5
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [[`e041312`](https://github.com/generaltranslation/gt/commit/e04131263dd61e469db977bcc196dc1283e773d0)]:
|
|
8
|
+
- generaltranslation@8.2.16
|
|
9
|
+
- @generaltranslation/supported-locales@2.1.1
|
|
10
|
+
|
|
11
|
+
## 0.9.4
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [[`cc4edc1`](https://github.com/generaltranslation/gt/commit/cc4edc1e40d9977125bf6d053fb7f8cdbdd40b05)]:
|
|
16
|
+
- @generaltranslation/supported-locales@2.1.0
|
|
17
|
+
|
|
3
18
|
## 0.9.3
|
|
4
19
|
|
|
5
20
|
### Patch 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-eqrZWcmP.cjs");
|
|
3
|
-
const require_versionId = require("./versionId-
|
|
3
|
+
const require_versionId = require("./versionId-7sHbIIgr.cjs");
|
|
4
4
|
const require_mFallback = require("./mFallback-DSBMUg3c.cjs");
|
|
5
5
|
let generaltranslation_internal = require("generaltranslation/internal");
|
|
6
6
|
let _generaltranslation_format = require("@generaltranslation/format");
|
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-BOwWa_-J.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { C 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-BTjLA0FZ.mjs";
|
|
3
3
|
import { n as gtFallback, r as decodeMsg, t as mFallback } from "./mFallback-pqVm9wDk.mjs";
|
|
4
4
|
import { VAR_IDENTIFIER, declareStatic, declareVar, decodeVars, derive, encode, libraryDefaultLocale } from "generaltranslation/internal";
|
|
5
5
|
import { formatMessage } from "@generaltranslation/format";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { a as EncodedTranslationOptions, c as JsxTranslationOptions, d as ResolutionOptions, f as RuntimeTranslationOptions, i as DictionaryTranslationOptions, l as LookupOptions, n as DictionaryLookupOptions, o as InlineResolveOptions, r as DictionaryOptions, s as InlineTranslationOptions, t as BaseTranslationOptions, u as NormalizedLookupOptions } from "./options-D0Lnxv2X.cjs";
|
|
2
|
-
import { a as ConditionStore, c as I18nManagerConfig, d as WritableConditionStore, f as
|
|
3
|
-
import {
|
|
2
|
+
import { a as ConditionStore, c as I18nManagerConfig, d as WritableConditionStore, f as GTConfig, l as I18nManagerConstructorParams, o as ConditionStoreConfig, p as RenderMethod, s as DictionaryConfig, t as LocaleCandidates, u as ScopedConditionStore } from "./localeResolver-BSGZjvxg.cjs";
|
|
3
|
+
import { c as DictionaryEntry, d as DictionaryObject, f as DictionaryPath, i as LifecycleCallbacks, l as DictionaryKey, o as DictionaryLoader, p as DictionaryValue, s as Dictionary, t as TranslationsLoader, u as DictionaryLeaf } from "./types-BtGFavAy.cjs";
|
|
4
4
|
export { BaseTranslationOptions, type ConditionStore, type ConditionStoreConfig, type Dictionary, type DictionaryConfig, type DictionaryEntry, type DictionaryKey, type DictionaryLeaf, type DictionaryLoader, DictionaryLookupOptions, type DictionaryObject, type DictionaryOptions, type DictionaryPath, DictionaryTranslationOptions, type DictionaryValue, EncodedTranslationOptions, GTConfig, type I18nManagerConfig, type I18nManagerConstructorParams, InlineResolveOptions, InlineTranslationOptions, JsxTranslationOptions, type LifecycleCallbacks, type LocaleCandidates, LookupOptions, NormalizedLookupOptions, RenderMethod, ResolutionOptions, RuntimeTranslationOptions, type ScopedConditionStore, type TranslationsLoader, type WritableConditionStore };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { a as EncodedTranslationOptions, c as JsxTranslationOptions, d as ResolutionOptions, f as RuntimeTranslationOptions, i as DictionaryTranslationOptions, l as LookupOptions, n as DictionaryLookupOptions, o as InlineResolveOptions, r as DictionaryOptions, s as InlineTranslationOptions, t as BaseTranslationOptions, u as NormalizedLookupOptions } from "./options-CczghSzj.mjs";
|
|
2
|
-
import { a as ConditionStore, c as I18nManagerConfig, d as WritableConditionStore, f as
|
|
3
|
-
import {
|
|
2
|
+
import { a as ConditionStore, c as I18nManagerConfig, d as WritableConditionStore, f as GTConfig, l as I18nManagerConstructorParams, o as ConditionStoreConfig, p as RenderMethod, s as DictionaryConfig, t as LocaleCandidates, u as ScopedConditionStore } from "./localeResolver-DjL1m1_w.mjs";
|
|
3
|
+
import { c as DictionaryEntry, d as DictionaryObject, f as DictionaryPath, i as LifecycleCallbacks, l as DictionaryKey, o as DictionaryLoader, p as DictionaryValue, s as Dictionary, t as TranslationsLoader, u as DictionaryLeaf } from "./types-PZn6hXxt.mjs";
|
|
4
4
|
export { BaseTranslationOptions, type ConditionStore, type ConditionStoreConfig, type Dictionary, type DictionaryConfig, type DictionaryEntry, type DictionaryKey, type DictionaryLeaf, type DictionaryLoader, DictionaryLookupOptions, type DictionaryObject, type DictionaryOptions, type DictionaryPath, DictionaryTranslationOptions, type DictionaryValue, EncodedTranslationOptions, GTConfig, type I18nManagerConfig, type I18nManagerConstructorParams, InlineResolveOptions, InlineTranslationOptions, JsxTranslationOptions, type LifecycleCallbacks, type LocaleCandidates, LookupOptions, NormalizedLookupOptions, RenderMethod, ResolutionOptions, RuntimeTranslationOptions, type ScopedConditionStore, type TranslationsLoader, type WritableConditionStore };
|
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-eqrZWcmP.cjs");
|
|
3
|
-
const require_versionId = require("./versionId-
|
|
3
|
+
const require_versionId = require("./versionId-7sHbIIgr.cjs");
|
|
4
4
|
let generaltranslation_internal = require("generaltranslation/internal");
|
|
5
5
|
let _generaltranslation_format = require("@generaltranslation/format");
|
|
6
6
|
//#region src/translation-functions/internal/getGT.ts
|
|
@@ -152,8 +152,8 @@ async function getTranslations() {
|
|
|
152
152
|
function renderObject({ sourceObject, targetObject }) {
|
|
153
153
|
const targetEntry = require_versionId.getDictionaryEntry(targetObject);
|
|
154
154
|
if (targetEntry !== void 0) return targetEntry.entry;
|
|
155
|
-
if (require_versionId.
|
|
156
|
-
if (!require_versionId.
|
|
155
|
+
if (require_versionId.isDictionaryObject(targetObject)) {
|
|
156
|
+
if (!require_versionId.isDictionaryObject(sourceObject)) return renderObject({
|
|
157
157
|
sourceObject: targetObject,
|
|
158
158
|
targetObject: void 0
|
|
159
159
|
});
|
|
@@ -169,23 +169,23 @@ async function getTranslations() {
|
|
|
169
169
|
if (target !== void 0) return target;
|
|
170
170
|
return sourceEntry.entry;
|
|
171
171
|
}
|
|
172
|
-
if (require_versionId.
|
|
172
|
+
if (require_versionId.isDictionaryObject(sourceObject)) return renderDictionaryObject({
|
|
173
173
|
sourceObject,
|
|
174
174
|
targetObject: void 0
|
|
175
175
|
});
|
|
176
176
|
throw new Error("Dictionary object cannot be rendered");
|
|
177
177
|
}
|
|
178
178
|
function renderDictionaryObject({ sourceObject, targetObject }) {
|
|
179
|
-
if (!require_versionId.
|
|
179
|
+
if (!require_versionId.isDictionaryObject(sourceObject)) return renderObject({
|
|
180
180
|
sourceObject,
|
|
181
181
|
targetObject
|
|
182
182
|
});
|
|
183
183
|
const result = {};
|
|
184
|
-
const keys = new Set([...Object.keys(sourceObject), ...require_versionId.
|
|
184
|
+
const keys = new Set([...Object.keys(sourceObject), ...require_versionId.isDictionaryObject(targetObject) ? Object.keys(targetObject) : []]);
|
|
185
185
|
for (const key of Array.from(keys)) {
|
|
186
186
|
const renderedChild = renderObject({
|
|
187
187
|
sourceObject: sourceObject[key],
|
|
188
|
-
targetObject: require_versionId.
|
|
188
|
+
targetObject: require_versionId.isDictionaryObject(targetObject) ? targetObject[key] : void 0
|
|
189
189
|
});
|
|
190
190
|
if (renderedChild !== void 0) result[key] = renderedChild;
|
|
191
191
|
}
|
package/dist/internal.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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/format/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/format/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/format/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 =\n typeof options.$locale === 'string' ? 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 type { JsxChildren } from '@generaltranslation/format/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/format';\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;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BAAuB;AAC9B,MAAI,CAAC,eACH,OAAM,IAAI,MAAM,sBAAsB;AAExC,SAAO;;CAGT,SAASC,oBAAkB,oBAA6B;AACtD,mBAAiB;AACjB,oBAAA,kBAAyB,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,2BAAAA,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","isDictionaryObject","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/format/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/format/types';\nimport type {\n DictionaryEntry,\n DictionaryValue,\n} from '../../i18n-manager/translations-manager/DictionaryCache';\nimport {\n getDictionaryEntry,\n isDictionaryObject,\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 (isDictionaryObject(targetObject)) {\n if (!isDictionaryObject(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 (isDictionaryObject(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 (!isDictionaryObject(sourceObject)) {\n return renderObject({ sourceObject, targetObject });\n }\n const result: Record<string, DictionaryObjectTranslation> = {};\n const keys = new Set([\n ...Object.keys(sourceObject),\n ...(isDictionaryObject(targetObject) ? Object.keys(targetObject) : []),\n ]);\n\n for (const key of Array.from(keys)) {\n const renderedChild = renderObject({\n sourceObject: sourceObject[key],\n targetObject: isDictionaryObject(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/format/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 =\n typeof options.$locale === 'string' ? 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 type { JsxChildren } from '@generaltranslation/format/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/format';\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,S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AU,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,2BAAAA,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
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { c as JsxTranslationOptions, d as ResolutionOptions, f as RuntimeTranslationOptions, l as LookupOptions, s as InlineTranslationOptions, u as NormalizedLookupOptions } from "./options-D0Lnxv2X.cjs";
|
|
2
2
|
import { n as GTFunctionType, r as MFunctionType, s as TFunctionType } from "./functions-DI1QpQoC.cjs";
|
|
3
3
|
import { a as getLocales, i as getLocaleProperties, n as getDefaultLocale, r as getLocale, t as getVersionId } from "./index-UDlCzB02.cjs";
|
|
4
|
-
import { a as ConditionStore, c as I18nManagerConfig,
|
|
5
|
-
import { a as
|
|
4
|
+
import { a as ConditionStore, c as I18nManagerConfig, i as resolveSupportedLocale, l as I18nManagerConstructorParams, n as createLocaleResolver, r as determineSupportedLocale, t as LocaleCandidates } from "./localeResolver-BSGZjvxg.cjs";
|
|
5
|
+
import { a as Locale, c as DictionaryEntry, d as DictionaryObject, f as DictionaryPath, m as Translation, n as Hash, p as DictionaryValue, s as Dictionary, t as TranslationsLoader } from "./types-BtGFavAy.cjs";
|
|
6
6
|
import { CustomMapping, DataFormat, JsxChildren, StringContent, StringFormat } from "@generaltranslation/format/types";
|
|
7
7
|
import { GT } from "generaltranslation";
|
|
8
8
|
|
|
@@ -129,10 +129,15 @@ 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_HIT_EVENT_NAME = "locales-cache-hit";
|
|
132
133
|
declare const LOCALES_CACHE_MISS_EVENT_NAME = "locales-cache-miss";
|
|
134
|
+
declare const TRANSLATIONS_CACHE_HIT_EVENT_NAME = "translations-cache-hit";
|
|
133
135
|
declare const TRANSLATIONS_CACHE_MISS_EVENT_NAME = "translations-cache-miss";
|
|
136
|
+
declare const LOCALES_DICTIONARY_CACHE_HIT_EVENT_NAME = "locales-dictionary-cache-hit";
|
|
134
137
|
declare const LOCALES_DICTIONARY_CACHE_MISS_EVENT_NAME = "locales-dictionary-cache-miss";
|
|
138
|
+
declare const DICTIONARY_CACHE_HIT_EVENT_NAME = "dictionary-cache-hit";
|
|
135
139
|
declare const DICTIONARY_CACHE_MISS_EVENT_NAME = "dictionary-cache-miss";
|
|
140
|
+
declare const DICTIONARY_OBJECT_CACHE_HIT_EVENT_NAME = "dictionary-object-cache-hit";
|
|
136
141
|
/**
|
|
137
142
|
* A base event for the I18nManagers
|
|
138
143
|
* @prop {locales-cache-hit} - Emitted when a locale cache hit occurs
|
|
@@ -146,7 +151,7 @@ declare const DICTIONARY_CACHE_MISS_EVENT_NAME = "dictionary-cache-miss";
|
|
|
146
151
|
* @prop {dictionary-object-cache-hit} - Emitted when a dictionary object cache hit occurs
|
|
147
152
|
*/
|
|
148
153
|
type I18nEvents<TranslationValue extends Translation> = BaseEvent & {
|
|
149
|
-
|
|
154
|
+
[LOCALES_CACHE_HIT_EVENT_NAME]: {
|
|
150
155
|
locale: Locale;
|
|
151
156
|
translations: Record<Hash, TranslationValue>;
|
|
152
157
|
};
|
|
@@ -154,7 +159,7 @@ type I18nEvents<TranslationValue extends Translation> = BaseEvent & {
|
|
|
154
159
|
locale: Locale;
|
|
155
160
|
translations: Record<Hash, TranslationValue>;
|
|
156
161
|
};
|
|
157
|
-
|
|
162
|
+
[TRANSLATIONS_CACHE_HIT_EVENT_NAME]: {
|
|
158
163
|
locale: Locale;
|
|
159
164
|
hash: Hash;
|
|
160
165
|
translation: TranslationValue;
|
|
@@ -164,7 +169,7 @@ type I18nEvents<TranslationValue extends Translation> = BaseEvent & {
|
|
|
164
169
|
hash: Hash;
|
|
165
170
|
translation: TranslationValue;
|
|
166
171
|
};
|
|
167
|
-
|
|
172
|
+
[LOCALES_DICTIONARY_CACHE_HIT_EVENT_NAME]: {
|
|
168
173
|
locale: Locale;
|
|
169
174
|
dictionary: Dictionary;
|
|
170
175
|
};
|
|
@@ -172,7 +177,7 @@ type I18nEvents<TranslationValue extends Translation> = BaseEvent & {
|
|
|
172
177
|
locale: Locale;
|
|
173
178
|
dictionary: Dictionary;
|
|
174
179
|
};
|
|
175
|
-
|
|
180
|
+
[DICTIONARY_CACHE_HIT_EVENT_NAME]: {
|
|
176
181
|
locale: Locale;
|
|
177
182
|
id: DictionaryPath;
|
|
178
183
|
dictionaryEntry: DictionaryEntry;
|
|
@@ -182,7 +187,7 @@ type I18nEvents<TranslationValue extends Translation> = BaseEvent & {
|
|
|
182
187
|
id: DictionaryPath;
|
|
183
188
|
dictionaryEntry: DictionaryEntry;
|
|
184
189
|
};
|
|
185
|
-
|
|
190
|
+
[DICTIONARY_OBJECT_CACHE_HIT_EVENT_NAME]: {
|
|
186
191
|
locale: Locale;
|
|
187
192
|
id: DictionaryPath;
|
|
188
193
|
dictionaryValue: DictionaryValue;
|
|
@@ -207,6 +212,7 @@ declare class EventEmitter<Events extends BaseEvent> {
|
|
|
207
212
|
* Emit an event
|
|
208
213
|
*/
|
|
209
214
|
protected emit<EventName extends keyof Events>(eventName: EventName, event: Events[EventName]): void;
|
|
215
|
+
protected hasListeners<EventName extends keyof Events>(eventName: EventName): boolean;
|
|
210
216
|
}
|
|
211
217
|
//#endregion
|
|
212
218
|
//#region src/i18n-manager/I18nManager.d.ts
|
|
@@ -225,13 +231,9 @@ type TranslationResolver<U extends Translation = Translation> = <T extends U = U
|
|
|
225
231
|
declare class I18nManager<TranslationValue extends Translation = Translation> extends EventEmitter<I18nEvents<TranslationValue>> {
|
|
226
232
|
protected config: I18nManagerConfig;
|
|
227
233
|
/**
|
|
228
|
-
*
|
|
234
|
+
* Locale-scoped caches for translations and dictionaries
|
|
229
235
|
*/
|
|
230
236
|
private localesCache;
|
|
231
|
-
/**
|
|
232
|
-
* Cache for dictionaries
|
|
233
|
-
*/
|
|
234
|
-
private localesDictionaryCache;
|
|
235
237
|
/**
|
|
236
238
|
* Runtime-safe locale and formatting helpers
|
|
237
239
|
*/
|
|
@@ -312,6 +314,11 @@ declare class I18nManager<TranslationValue extends Translation = Translation> ex
|
|
|
312
314
|
* If it's not found, use the fallback (runtime translate)
|
|
313
315
|
*/
|
|
314
316
|
lookupDictionaryObjWithFallback(locale: string, id: string): Promise<DictionaryObject | undefined>;
|
|
317
|
+
private translateDictionaryEntry;
|
|
318
|
+
private getSourceDictionaryEntry;
|
|
319
|
+
private getSourceDictionaryObject;
|
|
320
|
+
private getDefaultDictionaryCache;
|
|
321
|
+
private resolveDictionaryCacheLocale;
|
|
315
322
|
/**
|
|
316
323
|
* Just lookup a translation
|
|
317
324
|
*/
|
|
@@ -384,10 +391,6 @@ declare class I18nManager<TranslationValue extends Translation = Translation> ex
|
|
|
384
391
|
private resolveLookupParams;
|
|
385
392
|
private resolveLookupOptions;
|
|
386
393
|
private lookupTranslationWithFallbackResolved;
|
|
387
|
-
/**
|
|
388
|
-
* Runtime lookup function for dictionaries
|
|
389
|
-
*/
|
|
390
|
-
private dictionaryRuntimeTranslate;
|
|
391
394
|
/**
|
|
392
395
|
* A helper function to create a gt class that is locale agnostic
|
|
393
396
|
* This is helpful for when our getLocale function is bound to a
|
package/dist/internal.d.mts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { c as JsxTranslationOptions, d as ResolutionOptions, f as RuntimeTranslationOptions, l as LookupOptions, s as InlineTranslationOptions, u as NormalizedLookupOptions } from "./options-CczghSzj.mjs";
|
|
2
2
|
import { n as GTFunctionType, r as MFunctionType, s as TFunctionType } from "./functions-Dojd5CfY.mjs";
|
|
3
3
|
import { a as getLocales, i as getLocaleProperties, n as getDefaultLocale, r as getLocale, t as getVersionId } from "./index-Bc5qKE7T.mjs";
|
|
4
|
-
import { a as ConditionStore, c as I18nManagerConfig,
|
|
5
|
-
import { a as
|
|
4
|
+
import { a as ConditionStore, c as I18nManagerConfig, i as resolveSupportedLocale, l as I18nManagerConstructorParams, n as createLocaleResolver, r as determineSupportedLocale, t as LocaleCandidates } from "./localeResolver-DjL1m1_w.mjs";
|
|
5
|
+
import { a as Locale, c as DictionaryEntry, d as DictionaryObject, f as DictionaryPath, m as Translation, n as Hash, p as DictionaryValue, s as Dictionary, t as TranslationsLoader } from "./types-PZn6hXxt.mjs";
|
|
6
6
|
import { GT } from "generaltranslation";
|
|
7
7
|
import { CustomMapping, DataFormat, JsxChildren, StringContent, StringFormat } from "@generaltranslation/format/types";
|
|
8
8
|
|
|
@@ -129,10 +129,15 @@ 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_HIT_EVENT_NAME = "locales-cache-hit";
|
|
132
133
|
declare const LOCALES_CACHE_MISS_EVENT_NAME = "locales-cache-miss";
|
|
134
|
+
declare const TRANSLATIONS_CACHE_HIT_EVENT_NAME = "translations-cache-hit";
|
|
133
135
|
declare const TRANSLATIONS_CACHE_MISS_EVENT_NAME = "translations-cache-miss";
|
|
136
|
+
declare const LOCALES_DICTIONARY_CACHE_HIT_EVENT_NAME = "locales-dictionary-cache-hit";
|
|
134
137
|
declare const LOCALES_DICTIONARY_CACHE_MISS_EVENT_NAME = "locales-dictionary-cache-miss";
|
|
138
|
+
declare const DICTIONARY_CACHE_HIT_EVENT_NAME = "dictionary-cache-hit";
|
|
135
139
|
declare const DICTIONARY_CACHE_MISS_EVENT_NAME = "dictionary-cache-miss";
|
|
140
|
+
declare const DICTIONARY_OBJECT_CACHE_HIT_EVENT_NAME = "dictionary-object-cache-hit";
|
|
136
141
|
/**
|
|
137
142
|
* A base event for the I18nManagers
|
|
138
143
|
* @prop {locales-cache-hit} - Emitted when a locale cache hit occurs
|
|
@@ -146,7 +151,7 @@ declare const DICTIONARY_CACHE_MISS_EVENT_NAME = "dictionary-cache-miss";
|
|
|
146
151
|
* @prop {dictionary-object-cache-hit} - Emitted when a dictionary object cache hit occurs
|
|
147
152
|
*/
|
|
148
153
|
type I18nEvents<TranslationValue extends Translation> = BaseEvent & {
|
|
149
|
-
|
|
154
|
+
[LOCALES_CACHE_HIT_EVENT_NAME]: {
|
|
150
155
|
locale: Locale;
|
|
151
156
|
translations: Record<Hash, TranslationValue>;
|
|
152
157
|
};
|
|
@@ -154,7 +159,7 @@ type I18nEvents<TranslationValue extends Translation> = BaseEvent & {
|
|
|
154
159
|
locale: Locale;
|
|
155
160
|
translations: Record<Hash, TranslationValue>;
|
|
156
161
|
};
|
|
157
|
-
|
|
162
|
+
[TRANSLATIONS_CACHE_HIT_EVENT_NAME]: {
|
|
158
163
|
locale: Locale;
|
|
159
164
|
hash: Hash;
|
|
160
165
|
translation: TranslationValue;
|
|
@@ -164,7 +169,7 @@ type I18nEvents<TranslationValue extends Translation> = BaseEvent & {
|
|
|
164
169
|
hash: Hash;
|
|
165
170
|
translation: TranslationValue;
|
|
166
171
|
};
|
|
167
|
-
|
|
172
|
+
[LOCALES_DICTIONARY_CACHE_HIT_EVENT_NAME]: {
|
|
168
173
|
locale: Locale;
|
|
169
174
|
dictionary: Dictionary;
|
|
170
175
|
};
|
|
@@ -172,7 +177,7 @@ type I18nEvents<TranslationValue extends Translation> = BaseEvent & {
|
|
|
172
177
|
locale: Locale;
|
|
173
178
|
dictionary: Dictionary;
|
|
174
179
|
};
|
|
175
|
-
|
|
180
|
+
[DICTIONARY_CACHE_HIT_EVENT_NAME]: {
|
|
176
181
|
locale: Locale;
|
|
177
182
|
id: DictionaryPath;
|
|
178
183
|
dictionaryEntry: DictionaryEntry;
|
|
@@ -182,7 +187,7 @@ type I18nEvents<TranslationValue extends Translation> = BaseEvent & {
|
|
|
182
187
|
id: DictionaryPath;
|
|
183
188
|
dictionaryEntry: DictionaryEntry;
|
|
184
189
|
};
|
|
185
|
-
|
|
190
|
+
[DICTIONARY_OBJECT_CACHE_HIT_EVENT_NAME]: {
|
|
186
191
|
locale: Locale;
|
|
187
192
|
id: DictionaryPath;
|
|
188
193
|
dictionaryValue: DictionaryValue;
|
|
@@ -207,6 +212,7 @@ declare class EventEmitter<Events extends BaseEvent> {
|
|
|
207
212
|
* Emit an event
|
|
208
213
|
*/
|
|
209
214
|
protected emit<EventName extends keyof Events>(eventName: EventName, event: Events[EventName]): void;
|
|
215
|
+
protected hasListeners<EventName extends keyof Events>(eventName: EventName): boolean;
|
|
210
216
|
}
|
|
211
217
|
//#endregion
|
|
212
218
|
//#region src/i18n-manager/I18nManager.d.ts
|
|
@@ -225,13 +231,9 @@ type TranslationResolver<U extends Translation = Translation> = <T extends U = U
|
|
|
225
231
|
declare class I18nManager<TranslationValue extends Translation = Translation> extends EventEmitter<I18nEvents<TranslationValue>> {
|
|
226
232
|
protected config: I18nManagerConfig;
|
|
227
233
|
/**
|
|
228
|
-
*
|
|
234
|
+
* Locale-scoped caches for translations and dictionaries
|
|
229
235
|
*/
|
|
230
236
|
private localesCache;
|
|
231
|
-
/**
|
|
232
|
-
* Cache for dictionaries
|
|
233
|
-
*/
|
|
234
|
-
private localesDictionaryCache;
|
|
235
237
|
/**
|
|
236
238
|
* Runtime-safe locale and formatting helpers
|
|
237
239
|
*/
|
|
@@ -312,6 +314,11 @@ declare class I18nManager<TranslationValue extends Translation = Translation> ex
|
|
|
312
314
|
* If it's not found, use the fallback (runtime translate)
|
|
313
315
|
*/
|
|
314
316
|
lookupDictionaryObjWithFallback(locale: string, id: string): Promise<DictionaryObject | undefined>;
|
|
317
|
+
private translateDictionaryEntry;
|
|
318
|
+
private getSourceDictionaryEntry;
|
|
319
|
+
private getSourceDictionaryObject;
|
|
320
|
+
private getDefaultDictionaryCache;
|
|
321
|
+
private resolveDictionaryCacheLocale;
|
|
315
322
|
/**
|
|
316
323
|
* Just lookup a translation
|
|
317
324
|
*/
|
|
@@ -384,10 +391,6 @@ declare class I18nManager<TranslationValue extends Translation = Translation> ex
|
|
|
384
391
|
private resolveLookupParams;
|
|
385
392
|
private resolveLookupOptions;
|
|
386
393
|
private lookupTranslationWithFallbackResolved;
|
|
387
|
-
/**
|
|
388
|
-
* Runtime lookup function for dictionaries
|
|
389
|
-
*/
|
|
390
|
-
private dictionaryRuntimeTranslate;
|
|
391
394
|
/**
|
|
392
395
|
* A helper function to create a gt class that is locale agnostic
|
|
393
396
|
* This is helpful for when our getLocale function is bound to a
|
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-BOwWa_-J.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { E as resolveDictionaryLookupOptions, S as TRANSLATIONS_CACHE_MISS_EVENT_NAME, T as isDictionaryObject, _ 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 getDictionaryEntry, x as LOCALES_DICTIONARY_CACHE_MISS_EVENT_NAME, y as DICTIONARY_CACHE_MISS_EVENT_NAME } from "./versionId-BTjLA0FZ.mjs";
|
|
3
3
|
import { libraryDefaultLocale } from "generaltranslation/internal";
|
|
4
4
|
import { LocaleConfig } from "@generaltranslation/format";
|
|
5
5
|
//#region src/translation-functions/internal/getGT.ts
|
|
@@ -151,8 +151,8 @@ async function getTranslations() {
|
|
|
151
151
|
function renderObject({ sourceObject, targetObject }) {
|
|
152
152
|
const targetEntry = getDictionaryEntry(targetObject);
|
|
153
153
|
if (targetEntry !== void 0) return targetEntry.entry;
|
|
154
|
-
if (
|
|
155
|
-
if (!
|
|
154
|
+
if (isDictionaryObject(targetObject)) {
|
|
155
|
+
if (!isDictionaryObject(sourceObject)) return renderObject({
|
|
156
156
|
sourceObject: targetObject,
|
|
157
157
|
targetObject: void 0
|
|
158
158
|
});
|
|
@@ -168,23 +168,23 @@ async function getTranslations() {
|
|
|
168
168
|
if (target !== void 0) return target;
|
|
169
169
|
return sourceEntry.entry;
|
|
170
170
|
}
|
|
171
|
-
if (
|
|
171
|
+
if (isDictionaryObject(sourceObject)) return renderDictionaryObject({
|
|
172
172
|
sourceObject,
|
|
173
173
|
targetObject: void 0
|
|
174
174
|
});
|
|
175
175
|
throw new Error("Dictionary object cannot be rendered");
|
|
176
176
|
}
|
|
177
177
|
function renderDictionaryObject({ sourceObject, targetObject }) {
|
|
178
|
-
if (!
|
|
178
|
+
if (!isDictionaryObject(sourceObject)) return renderObject({
|
|
179
179
|
sourceObject,
|
|
180
180
|
targetObject
|
|
181
181
|
});
|
|
182
182
|
const result = {};
|
|
183
|
-
const keys = new Set([...Object.keys(sourceObject), ...
|
|
183
|
+
const keys = new Set([...Object.keys(sourceObject), ...isDictionaryObject(targetObject) ? Object.keys(targetObject) : []]);
|
|
184
184
|
for (const key of Array.from(keys)) {
|
|
185
185
|
const renderedChild = renderObject({
|
|
186
186
|
sourceObject: sourceObject[key],
|
|
187
|
-
targetObject:
|
|
187
|
+
targetObject: isDictionaryObject(targetObject) ? targetObject[key] : void 0
|
|
188
188
|
});
|
|
189
189
|
if (renderedChild !== void 0) result[key] = renderedChild;
|
|
190
190
|
}
|
package/dist/internal.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"internal.mjs","names":["setConditionStore"],"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/format/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/format/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/format/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 =\n typeof options.$locale === 'string' ? 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 type { JsxChildren } from '@generaltranslation/format/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/format';\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 =>\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AACtD,mBAAiB;AACjB,oBAAyB,mBAAmB;;AAG9C,QAAO;EACL;EACA,mBAAA;EACD;;;;ACjBH,SAAS,8BAA8B,EACrC,eACA,SACA,kBACwB,EAAE,EAAE;CAC5B,MAAM,iBAAiB,iBAAiB;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,IADkB,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.mjs","names":["setConditionStore"],"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/format/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/format/types';\nimport type {\n DictionaryEntry,\n DictionaryValue,\n} from '../../i18n-manager/translations-manager/DictionaryCache';\nimport {\n getDictionaryEntry,\n isDictionaryObject,\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 (isDictionaryObject(targetObject)) {\n if (!isDictionaryObject(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 (isDictionaryObject(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 (!isDictionaryObject(sourceObject)) {\n return renderObject({ sourceObject, targetObject });\n }\n const result: Record<string, DictionaryObjectTranslation> = {};\n const keys = new Set([\n ...Object.keys(sourceObject),\n ...(isDictionaryObject(targetObject) ? Object.keys(targetObject) : []),\n ]);\n\n for (const key of Array.from(keys)) {\n const renderedChild = renderObject({\n sourceObject: sourceObject[key],\n targetObject: isDictionaryObject(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/format/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 =\n typeof options.$locale === 'string' ? 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 type { JsxChildren } from '@generaltranslation/format/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/format';\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,cAAc,gBAAgB;CACpC,MAAM,SAAS,kBAAkB;AACjC,OAAM,YAAY,iBAAiB,OAAO;CAC1C,MAAM,eAAe,YAAY,kBAAkB;;;;;;;;;;;;;;;;;CAkBnD,MAAM,MACJ,SACA,UAAoC,EAAE,KACnC;EAEH,MAAM,gBAAgB,oBADD,QAAQ,WAAW,QAGtC,SACA,MACD;AAUD,SAAO,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,iBAAiB,cAAc,WAAW,IAAI,EAAE;AAGtD,MAAI,4BAA4B,eAAe,CAC7C,QAAO,GAAG,eAAe,UAAU,eAAe;AAOpD,SAAO,GAAG,YAAY,QAAQ;;AAGhC,QAAO;;;;;;;;;;;;;AC9BT,eAAsB,kBAA0C;CAC9D,MAA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