@pyreon/i18n 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/analysis/index.js.html +1 -1
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/types/devtools.d.ts +21 -65
- package/lib/types/devtools.d.ts.map +1 -1
- package/lib/types/index.d.ts +227 -362
- package/lib/types/index.d.ts.map +1 -1
- package/package.json +3 -3
- package/lib/types/devtools2.d.ts +0 -30
- package/lib/types/devtools2.d.ts.map +0 -1
- package/lib/types/index2.d.ts +0 -244
- package/lib/types/index2.d.ts.map +0 -1
|
@@ -5386,7 +5386,7 @@ var drawChart = (function (exports) {
|
|
|
5386
5386
|
</script>
|
|
5387
5387
|
<script>
|
|
5388
5388
|
/*<!--*/
|
|
5389
|
-
const data = {"version":2,"tree":{"name":"root","children":[{"name":"index.js","children":[{"name":"src","children":[{"uid":"
|
|
5389
|
+
const data = {"version":2,"tree":{"name":"root","children":[{"name":"index.js","children":[{"name":"src","children":[{"uid":"551551b3-1","name":"context.ts"},{"uid":"551551b3-3","name":"interpolation.ts"},{"uid":"551551b3-5","name":"pluralization.ts"},{"uid":"551551b3-7","name":"create-i18n.ts"},{"uid":"551551b3-11","name":"trans.tsx"},{"uid":"551551b3-13","name":"index.ts"}]},{"name":"home/runner/work/fundamentals/fundamentals/node_modules/.bun/@pyreon+core@0.7.3/node_modules/@pyreon/core/lib/jsx-runtime.js","uid":"551551b3-9"}]}],"isRoot":true},"nodeParts":{"551551b3-1":{"renderedLength":902,"gzipLength":490,"brotliLength":0,"metaUid":"551551b3-0"},"551551b3-3":{"renderedLength":711,"gzipLength":437,"brotliLength":0,"metaUid":"551551b3-2"},"551551b3-5":{"renderedLength":560,"gzipLength":337,"brotliLength":0,"metaUid":"551551b3-4"},"551551b3-7":{"renderedLength":5789,"gzipLength":1812,"brotliLength":0,"metaUid":"551551b3-6"},"551551b3-9":{"renderedLength":1758,"gzipLength":885,"brotliLength":0,"metaUid":"551551b3-8"},"551551b3-11":{"renderedLength":2167,"gzipLength":1003,"brotliLength":0,"metaUid":"551551b3-10"},"551551b3-13":{"renderedLength":0,"gzipLength":0,"brotliLength":0,"metaUid":"551551b3-12"}},"nodeMetas":{"551551b3-0":{"id":"/src/context.ts","moduleParts":{"index.js":"551551b3-1"},"imported":[{"uid":"551551b3-14"}],"importedBy":[{"uid":"551551b3-12"}]},"551551b3-2":{"id":"/src/interpolation.ts","moduleParts":{"index.js":"551551b3-3"},"imported":[],"importedBy":[{"uid":"551551b3-12"},{"uid":"551551b3-6"}]},"551551b3-4":{"id":"/src/pluralization.ts","moduleParts":{"index.js":"551551b3-5"},"imported":[],"importedBy":[{"uid":"551551b3-12"},{"uid":"551551b3-6"}]},"551551b3-6":{"id":"/src/create-i18n.ts","moduleParts":{"index.js":"551551b3-7"},"imported":[{"uid":"551551b3-15"},{"uid":"551551b3-2"},{"uid":"551551b3-4"}],"importedBy":[{"uid":"551551b3-12"}]},"551551b3-8":{"id":"/home/runner/work/fundamentals/fundamentals/node_modules/.bun/@pyreon+core@0.7.3/node_modules/@pyreon/core/lib/jsx-runtime.js","moduleParts":{"index.js":"551551b3-9"},"imported":[],"importedBy":[{"uid":"551551b3-10"}]},"551551b3-10":{"id":"/src/trans.tsx","moduleParts":{"index.js":"551551b3-11"},"imported":[{"uid":"551551b3-8"}],"importedBy":[{"uid":"551551b3-12"}]},"551551b3-12":{"id":"/src/index.ts","moduleParts":{"index.js":"551551b3-13"},"imported":[{"uid":"551551b3-0"},{"uid":"551551b3-6"},{"uid":"551551b3-2"},{"uid":"551551b3-4"},{"uid":"551551b3-10"}],"importedBy":[],"isEntry":true},"551551b3-14":{"id":"@pyreon/core","moduleParts":{},"imported":[],"importedBy":[{"uid":"551551b3-0"}]},"551551b3-15":{"id":"@pyreon/reactivity","moduleParts":{},"imported":[],"importedBy":[{"uid":"551551b3-6"}]}},"env":{"rollup":"4.23.0"},"options":{"gzip":true,"brotli":false,"sourcemap":false}};
|
|
5390
5390
|
|
|
5391
5391
|
const run = () => {
|
|
5392
5392
|
const width = window.innerWidth;
|
package/lib/index.js
CHANGED
|
@@ -255,7 +255,7 @@ function createI18n(options) {
|
|
|
255
255
|
}
|
|
256
256
|
|
|
257
257
|
//#endregion
|
|
258
|
-
//#region ../../node_modules/.bun/@pyreon+core@0.
|
|
258
|
+
//#region ../../node_modules/.bun/@pyreon+core@0.7.3/node_modules/@pyreon/core/lib/jsx-runtime.js
|
|
259
259
|
/** Marker for fragment nodes — renders children without a wrapper element */
|
|
260
260
|
const Fragment = Symbol("Pyreon.Fragment");
|
|
261
261
|
/**
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/context.ts","../src/interpolation.ts","../src/pluralization.ts","../src/create-i18n.ts","../../../node_modules/.bun/@pyreon+core@0.6.0/node_modules/@pyreon/core/lib/jsx-runtime.js","../src/trans.tsx"],"sourcesContent":["import type { Props, VNode, VNodeChild } from '@pyreon/core'\nimport { createContext, provide, useContext } from '@pyreon/core'\nimport type { I18nInstance } from './types'\n\nexport const I18nContext = createContext<I18nInstance | null>(null)\n\nexport interface I18nProviderProps extends Props {\n instance: I18nInstance\n children?: VNodeChild\n}\n\n/**\n * Provide an i18n instance to the component tree.\n *\n * @example\n * const i18n = createI18n({ locale: 'en', messages: { en: { hello: 'Hello' } } })\n *\n * // In JSX:\n * <I18nProvider instance={i18n}>\n * <App />\n * </I18nProvider>\n */\nexport function I18nProvider(props: I18nProviderProps): VNode {\n provide(I18nContext, props.instance)\n\n const ch = props.children\n return (typeof ch === 'function' ? (ch as () => VNodeChild)() : ch) as VNode\n}\n\n/**\n * Access the i18n instance from the nearest I18nProvider.\n * Must be called within a component tree wrapped by I18nProvider.\n *\n * @example\n * function Greeting() {\n * const { t, locale } = useI18n()\n * return <h1>{t('greeting', { name: 'World' })}</h1>\n * }\n */\nexport function useI18n(): I18nInstance {\n const instance = useContext(I18nContext)\n if (!instance) {\n throw new Error(\n '[@pyreon/i18n] useI18n() must be used within an <I18nProvider>.',\n )\n }\n return instance\n}\n","import type { InterpolationValues } from './types'\n\nconst INTERPOLATION_RE = /\\{\\{(\\s*\\w+\\s*)\\}\\}/g\n\n/**\n * Replace `{{key}}` placeholders in a string with values from the given record.\n * Supports optional whitespace inside braces: `{{ name }}` works too.\n * Unmatched placeholders are left as-is.\n */\nexport function interpolate(\n template: string,\n values?: InterpolationValues,\n): string {\n if (!values || !template.includes('{{')) return template\n return template.replace(INTERPOLATION_RE, (_, key: string) => {\n const trimmed = key.trim()\n const value = values[trimmed]\n if (value === undefined) return `{{${trimmed}}}`\n // Safely coerce — guard against malicious toString/valueOf\n try {\n return typeof value === 'object' && value !== null\n ? JSON.stringify(value)\n : `${value}`\n } catch {\n return `{{${trimmed}}}`\n }\n })\n}\n","import type { PluralRules } from './types'\n\n/**\n * Resolve the plural category for a given count and locale.\n *\n * Uses custom rules if provided, otherwise falls back to `Intl.PluralRules`.\n * Returns CLDR plural categories: \"zero\", \"one\", \"two\", \"few\", \"many\", \"other\".\n */\nexport function resolvePluralCategory(\n locale: string,\n count: number,\n customRules?: PluralRules,\n): string {\n // Custom rules take priority\n if (customRules?.[locale]) {\n return customRules[locale](count)\n }\n\n // Use Intl.PluralRules if available\n if (typeof Intl !== 'undefined' && Intl.PluralRules) {\n try {\n const pr = new Intl.PluralRules(locale)\n return pr.select(count)\n } catch {\n // Invalid locale — fall through\n }\n }\n\n // Basic fallback\n return count === 1 ? 'one' : 'other'\n}\n","import { computed, signal } from '@pyreon/reactivity'\nimport { interpolate } from './interpolation'\nimport { resolvePluralCategory } from './pluralization'\nimport type {\n I18nInstance,\n I18nOptions,\n InterpolationValues,\n TranslationDictionary,\n} from './types'\n\n/**\n * Resolve a dot-separated key path in a nested dictionary.\n * E.g. \"user.greeting\" → dictionary.user.greeting\n */\nfunction resolveKey(\n dict: TranslationDictionary,\n keyPath: string,\n): string | undefined {\n const parts = keyPath.split('.')\n let current: TranslationDictionary | string = dict\n\n for (const part of parts) {\n if (current == null || typeof current === 'string') return undefined\n current = current[part] as TranslationDictionary | string\n }\n\n return typeof current === 'string' ? current : undefined\n}\n\n/**\n * Deep-merge source into target (mutates target).\n */\nfunction deepMerge(\n target: TranslationDictionary,\n source: TranslationDictionary,\n): void {\n for (const key of Object.keys(source)) {\n if (key === '__proto__' || key === 'constructor' || key === 'prototype')\n continue\n const sourceVal = source[key]\n const targetVal = target[key]\n if (\n typeof sourceVal === 'object' &&\n sourceVal !== null &&\n typeof targetVal === 'object' &&\n targetVal !== null\n ) {\n deepMerge(\n targetVal as TranslationDictionary,\n sourceVal as TranslationDictionary,\n )\n } else {\n target[key] = sourceVal!\n }\n }\n}\n\n/**\n * Create a reactive i18n instance.\n *\n * @example\n * const i18n = createI18n({\n * locale: 'en',\n * fallbackLocale: 'en',\n * messages: {\n * en: { greeting: 'Hello {{name}}!' },\n * de: { greeting: 'Hallo {{name}}!' },\n * },\n * })\n *\n * // Reactive translation — re-evaluates on locale change\n * i18n.t('greeting', { name: 'Alice' }) // \"Hello Alice!\"\n * i18n.locale.set('de')\n * i18n.t('greeting', { name: 'Alice' }) // \"Hallo Alice!\"\n *\n * @example\n * // Async namespace loading\n * const i18n = createI18n({\n * locale: 'en',\n * loader: async (locale, namespace) => {\n * const mod = await import(`./locales/${locale}/${namespace}.json`)\n * return mod.default\n * },\n * })\n * await i18n.loadNamespace('auth')\n * i18n.t('auth:errors.invalid') // looks up \"errors.invalid\" in \"auth\" namespace\n */\nexport function createI18n(options: I18nOptions): I18nInstance {\n const {\n fallbackLocale,\n loader,\n defaultNamespace = 'common',\n pluralRules,\n onMissingKey,\n } = options\n\n // ── Reactive state ──────────────────────────────────────────────────\n\n const locale = signal(options.locale)\n\n // Internal store: locale → namespace → dictionary\n // We use a version counter to trigger reactive updates when messages change,\n // since the store is mutated in place (Object.is would skip same-reference sets).\n const store = new Map<string, Map<string, TranslationDictionary>>()\n const storeVersion = signal(0)\n\n // Loading state\n const pendingLoads = signal(0)\n const loadedNsVersion = signal(0)\n\n // In-flight load promises — deduplicates concurrent loads for the same locale:namespace\n const pendingPromises = new Map<string, Promise<void>>()\n\n const isLoading = computed(() => pendingLoads() > 0)\n const loadedNamespaces = computed(() => {\n loadedNsVersion()\n const currentLocale = locale()\n const nsMap = store.get(currentLocale)\n return new Set(nsMap ? nsMap.keys() : [])\n })\n const availableLocales = computed(() => {\n storeVersion() // subscribe to store changes\n return [...store.keys()]\n })\n\n // ── Initialize static messages ──────────────────────────────────────\n\n if (options.messages) {\n for (const [loc, dict] of Object.entries(options.messages)) {\n const nsMap = new Map<string, TranslationDictionary>()\n nsMap.set(defaultNamespace, dict)\n store.set(loc, nsMap)\n }\n }\n\n // ── Internal helpers ────────────────────────────────────────────────\n\n function getNamespaceMap(loc: string): Map<string, TranslationDictionary> {\n let nsMap = store.get(loc)\n if (!nsMap) {\n nsMap = new Map()\n store.set(loc, nsMap)\n }\n return nsMap\n }\n\n function lookupKey(\n loc: string,\n namespace: string,\n keyPath: string,\n ): string | undefined {\n const nsMap = store.get(loc)\n if (!nsMap) return undefined\n const dict = nsMap.get(namespace)\n if (!dict) return undefined\n return resolveKey(dict, keyPath)\n }\n\n function resolveTranslation(\n key: string,\n values?: InterpolationValues,\n ): string {\n // Subscribe to reactive dependencies\n const currentLocale = locale()\n storeVersion()\n\n // Parse key: \"namespace:key.path\" or just \"key.path\"\n let namespace = defaultNamespace\n let keyPath = key\n\n const colonIndex = key.indexOf(':')\n if (colonIndex > 0) {\n namespace = key.slice(0, colonIndex)\n keyPath = key.slice(colonIndex + 1)\n }\n\n // Handle pluralization: if values contain `count`, try plural suffixes\n if (values && 'count' in values) {\n const count = Number(values.count)\n const category = resolvePluralCategory(currentLocale, count, pluralRules)\n\n // Try exact form first (e.g. \"items_one\"), then fall back to base key\n const pluralKey = `${keyPath}_${category}`\n const pluralResult =\n lookupKey(currentLocale, namespace, pluralKey) ??\n (fallbackLocale\n ? lookupKey(fallbackLocale, namespace, pluralKey)\n : undefined)\n\n if (pluralResult) {\n return interpolate(pluralResult, values)\n }\n }\n\n // Standard lookup: current locale → fallback locale\n const result =\n lookupKey(currentLocale, namespace, keyPath) ??\n (fallbackLocale\n ? lookupKey(fallbackLocale, namespace, keyPath)\n : undefined)\n\n if (result !== undefined) {\n return interpolate(result, values)\n }\n\n // Missing key handler\n if (onMissingKey) {\n const custom = onMissingKey(currentLocale, key, namespace)\n if (custom !== undefined) return custom!\n }\n\n // Return the key itself as a visual fallback\n return key\n }\n\n // ── Public API ──────────────────────────────────────────────────────\n\n const t = (key: string, values?: InterpolationValues): string => {\n return resolveTranslation(key, values)\n }\n\n const loadNamespace = async (\n namespace: string,\n loc?: string,\n ): Promise<void> => {\n if (!loader) return\n\n const targetLocale = loc ?? locale.peek()\n const cacheKey = `${targetLocale}:${namespace}`\n const nsMap = getNamespaceMap(targetLocale)\n\n // Skip if already loaded\n if (nsMap.has(namespace)) return\n\n // Deduplicate concurrent loads for the same locale:namespace\n const existing = pendingPromises.get(cacheKey)\n if (existing) return existing\n\n pendingLoads.update((n) => n + 1)\n\n const promise = loader(targetLocale, namespace)\n .then((dict) => {\n if (dict) {\n nsMap.set(namespace, dict)\n storeVersion.update((n) => n + 1)\n loadedNsVersion.update((n) => n + 1)\n }\n })\n .finally(() => {\n pendingPromises.delete(cacheKey)\n pendingLoads.update((n) => n - 1)\n })\n\n pendingPromises.set(cacheKey, promise)\n return promise\n }\n\n const exists = (key: string): boolean => {\n const currentLocale = locale.peek()\n\n let namespace = defaultNamespace\n let keyPath = key\n const colonIndex = key.indexOf(':')\n if (colonIndex > 0) {\n namespace = key.slice(0, colonIndex)\n keyPath = key.slice(colonIndex + 1)\n }\n\n return (\n lookupKey(currentLocale, namespace, keyPath) !== undefined ||\n (fallbackLocale\n ? lookupKey(fallbackLocale, namespace, keyPath) !== undefined\n : false)\n )\n }\n\n const addMessages = (\n loc: string,\n messages: TranslationDictionary,\n namespace?: string,\n ): void => {\n const ns = namespace ?? defaultNamespace\n const nsMap = getNamespaceMap(loc)\n const existing = nsMap.get(ns)\n\n if (existing) {\n deepMerge(existing, messages)\n } else {\n // Deep-clone to prevent external mutation from corrupting the store\n const cloned: TranslationDictionary = {}\n deepMerge(cloned, messages)\n nsMap.set(ns, cloned)\n }\n\n storeVersion.update((n) => n + 1)\n loadedNsVersion.update((n) => n + 1)\n }\n\n return {\n t,\n locale,\n loadNamespace,\n isLoading,\n loadedNamespaces,\n exists,\n addMessages,\n availableLocales,\n }\n}\n","//#region src/h.ts\n/** Marker for fragment nodes — renders children without a wrapper element */\nconst Fragment = Symbol(\"Pyreon.Fragment\");\n/**\n* Hyperscript function — the compiled output of JSX.\n* `<div class=\"x\">hello</div>` → `h(\"div\", { class: \"x\" }, \"hello\")`\n*\n* Generic on P so TypeScript validates props match the component's signature\n* at the call site, then stores the result in the loosely-typed VNode.\n*/\n/** Shared empty props sentinel — identity-checked in mountElement to skip applyProps. */\nconst EMPTY_PROPS = {};\nfunction h(type, props, ...children) {\n\treturn {\n\t\ttype,\n\t\tprops: props ?? EMPTY_PROPS,\n\t\tchildren: normalizeChildren(children),\n\t\tkey: props?.key ?? null\n\t};\n}\nfunction normalizeChildren(children) {\n\tfor (let i = 0; i < children.length; i++) if (Array.isArray(children[i])) return flattenChildren(children);\n\treturn children;\n}\nfunction flattenChildren(children) {\n\tconst result = [];\n\tfor (const child of children) if (Array.isArray(child)) result.push(...flattenChildren(child));\n\telse result.push(child);\n\treturn result;\n}\n\n//#endregion\n//#region src/jsx-runtime.ts\n/**\n* JSX automatic runtime.\n*\n* When tsconfig has `\"jsxImportSource\": \"@pyreon/core\"`, the TS/bundler compiler\n* rewrites JSX to imports from this file automatically:\n* <div class=\"x\" /> → jsx(\"div\", { class: \"x\" })\n*/\nfunction jsx(type, props, key) {\n\tconst { children, ...rest } = props;\n\tconst propsWithKey = key != null ? {\n\t\t...rest,\n\t\tkey\n\t} : rest;\n\tif (typeof type === \"function\") return h(type, children !== void 0 ? {\n\t\t...propsWithKey,\n\t\tchildren\n\t} : propsWithKey);\n\treturn h(type, propsWithKey, ...children === void 0 ? [] : Array.isArray(children) ? children : [children]);\n}\nconst jsxs = jsx;\n\n//#endregion\nexport { Fragment, jsx, jsxs };\n//# sourceMappingURL=jsx-runtime.js.map","import type { Props, VNode } from '@pyreon/core'\nimport type { InterpolationValues } from './types'\n\nconst TAG_RE = /<(\\w+)>([^<]*)<\\/\\1>/g\n\ninterface RichPart {\n tag: string\n children: string\n}\n\n/**\n * Parse a translated string into an array of plain text and rich tag segments.\n *\n * @example\n * parseRichText(\"Hello <bold>world</bold>, click <link>here</link>\")\n * // → [\"Hello \", { tag: \"bold\", children: \"world\" }, \", click \", { tag: \"link\", children: \"here\" }]\n */\nexport function parseRichText(text: string): (string | RichPart)[] {\n const parts: (string | RichPart)[] = []\n let lastIndex = 0\n\n for (const match of text.matchAll(TAG_RE)) {\n const before = text.slice(lastIndex, match.index)\n if (before) parts.push(before)\n parts.push({ tag: match[1]!, children: match[2]! })\n lastIndex = match.index! + match[0].length\n }\n\n const after = text.slice(lastIndex)\n if (after) parts.push(after)\n\n return parts\n}\n\nexport interface TransProps extends Props {\n /** Translation key (supports namespace:key syntax). */\n i18nKey: string\n /** Interpolation values for {{placeholder}} syntax. */\n values?: InterpolationValues\n /**\n * Component map for rich interpolation.\n * Keys match tag names in the translation string.\n * Values are component functions: `(children: any) => VNode`\n *\n * @example\n * // Translation: \"Read the <terms>terms</terms> and <privacy>policy</privacy>\"\n * components={{\n * terms: (children) => <a href=\"/terms\">{children}</a>,\n * privacy: (children) => <a href=\"/privacy\">{children}</a>,\n * }}\n */\n components?: Record<string, (children: any) => any>\n /**\n * The i18n instance's `t` function.\n * Can be obtained from `useI18n()` or passed directly.\n */\n t: (key: string, values?: InterpolationValues) => string\n}\n\n/**\n * Rich JSX interpolation component for translations.\n *\n * Allows embedding JSX components within translated strings using XML-like tags.\n * The `t` function resolves the translation and interpolates `{{values}}` first,\n * then `<tag>content</tag>` patterns are mapped to the provided components.\n *\n * @example\n * // Translation: \"You have <bold>{{count}}</bold> unread messages\"\n * const { t } = useI18n()\n * <Trans\n * t={t}\n * i18nKey=\"messages.unread\"\n * values={{ count: 5 }}\n * components={{\n * bold: (children) => <strong>{children}</strong>,\n * }}\n * />\n * // Renders: You have <strong>5</strong> unread messages\n *\n * @example\n * // Translation: \"Read our <terms>terms of service</terms> and <privacy>privacy policy</privacy>\"\n * <Trans\n * t={t}\n * i18nKey=\"legal\"\n * components={{\n * terms: (children) => <a href=\"/terms\">{children}</a>,\n * privacy: (children) => <a href=\"/privacy\">{children}</a>,\n * }}\n * />\n */\nexport function Trans(props: TransProps): VNode | string {\n const translated = props.t(props.i18nKey, props.values)\n\n if (!props.components) return translated\n\n const parts = parseRichText(translated)\n\n // If the result is a single plain string, return it directly\n if (parts.length === 1 && typeof parts[0] === 'string') return parts[0]\n\n const children = parts.map((part) => {\n if (typeof part === 'string') return part\n const component = props.components![part.tag]\n // Unmatched tags: render children as plain text (no raw HTML markup)\n if (!component) return part.children\n return component(part.children)\n })\n\n return <>{children}</>\n}\n"],"x_google_ignoreList":[4],"mappings":";;;;AAIA,MAAa,cAAc,cAAmC,KAAK;;;;;;;;;;;;AAkBnE,SAAgB,aAAa,OAAiC;AAC5D,SAAQ,aAAa,MAAM,SAAS;CAEpC,MAAM,KAAK,MAAM;AACjB,QAAQ,OAAO,OAAO,aAAc,IAAyB,GAAG;;;;;;;;;;;;AAalE,SAAgB,UAAwB;CACtC,MAAM,WAAW,WAAW,YAAY;AACxC,KAAI,CAAC,SACH,OAAM,IAAI,MACR,kEACD;AAEH,QAAO;;;;;AC5CT,MAAM,mBAAmB;;;;;;AAOzB,SAAgB,YACd,UACA,QACQ;AACR,KAAI,CAAC,UAAU,CAAC,SAAS,SAAS,KAAK,CAAE,QAAO;AAChD,QAAO,SAAS,QAAQ,mBAAmB,GAAG,QAAgB;EAC5D,MAAM,UAAU,IAAI,MAAM;EAC1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../../node_modules/.bun/@pyreon+core@0.7.3/node_modules/@pyreon/core/lib/jsx-runtime.js","../src/trans.tsx"],"sourcesContent":["import type { Props, VNode, VNodeChild } from '@pyreon/core'\nimport { createContext, provide, useContext } from '@pyreon/core'\nimport type { I18nInstance } from './types'\n\nexport const I18nContext = createContext<I18nInstance | null>(null)\n\nexport interface I18nProviderProps extends Props {\n instance: I18nInstance\n children?: VNodeChild\n}\n\n/**\n * Provide an i18n instance to the component tree.\n *\n * @example\n * const i18n = createI18n({ locale: 'en', messages: { en: { hello: 'Hello' } } })\n *\n * // In JSX:\n * <I18nProvider instance={i18n}>\n * <App />\n * </I18nProvider>\n */\nexport function I18nProvider(props: I18nProviderProps): VNode {\n provide(I18nContext, props.instance)\n\n const ch = props.children\n return (typeof ch === 'function' ? (ch as () => VNodeChild)() : ch) as VNode\n}\n\n/**\n * Access the i18n instance from the nearest I18nProvider.\n * Must be called within a component tree wrapped by I18nProvider.\n *\n * @example\n * function Greeting() {\n * const { t, locale } = useI18n()\n * return <h1>{t('greeting', { name: 'World' })}</h1>\n * }\n */\nexport function useI18n(): I18nInstance {\n const instance = useContext(I18nContext)\n if (!instance) {\n throw new Error(\n '[@pyreon/i18n] useI18n() must be used within an <I18nProvider>.',\n )\n }\n return instance\n}\n","import type { InterpolationValues } from './types'\n\nconst INTERPOLATION_RE = /\\{\\{(\\s*\\w+\\s*)\\}\\}/g\n\n/**\n * Replace `{{key}}` placeholders in a string with values from the given record.\n * Supports optional whitespace inside braces: `{{ name }}` works too.\n * Unmatched placeholders are left as-is.\n */\nexport function interpolate(\n template: string,\n values?: InterpolationValues,\n): string {\n if (!values || !template.includes('{{')) return template\n return template.replace(INTERPOLATION_RE, (_, key: string) => {\n const trimmed = key.trim()\n const value = values[trimmed]\n if (value === undefined) return `{{${trimmed}}}`\n // Safely coerce — guard against malicious toString/valueOf\n try {\n return typeof value === 'object' && value !== null\n ? JSON.stringify(value)\n : `${value}`\n } catch {\n return `{{${trimmed}}}`\n }\n })\n}\n","import type { PluralRules } from './types'\n\n/**\n * Resolve the plural category for a given count and locale.\n *\n * Uses custom rules if provided, otherwise falls back to `Intl.PluralRules`.\n * Returns CLDR plural categories: \"zero\", \"one\", \"two\", \"few\", \"many\", \"other\".\n */\nexport function resolvePluralCategory(\n locale: string,\n count: number,\n customRules?: PluralRules,\n): string {\n // Custom rules take priority\n if (customRules?.[locale]) {\n return customRules[locale](count)\n }\n\n // Use Intl.PluralRules if available\n if (typeof Intl !== 'undefined' && Intl.PluralRules) {\n try {\n const pr = new Intl.PluralRules(locale)\n return pr.select(count)\n } catch {\n // Invalid locale — fall through\n }\n }\n\n // Basic fallback\n return count === 1 ? 'one' : 'other'\n}\n","import { computed, signal } from '@pyreon/reactivity'\nimport { interpolate } from './interpolation'\nimport { resolvePluralCategory } from './pluralization'\nimport type {\n I18nInstance,\n I18nOptions,\n InterpolationValues,\n TranslationDictionary,\n} from './types'\n\n/**\n * Resolve a dot-separated key path in a nested dictionary.\n * E.g. \"user.greeting\" → dictionary.user.greeting\n */\nfunction resolveKey(\n dict: TranslationDictionary,\n keyPath: string,\n): string | undefined {\n const parts = keyPath.split('.')\n let current: TranslationDictionary | string = dict\n\n for (const part of parts) {\n if (current == null || typeof current === 'string') return undefined\n current = current[part] as TranslationDictionary | string\n }\n\n return typeof current === 'string' ? current : undefined\n}\n\n/**\n * Deep-merge source into target (mutates target).\n */\nfunction deepMerge(\n target: TranslationDictionary,\n source: TranslationDictionary,\n): void {\n for (const key of Object.keys(source)) {\n if (key === '__proto__' || key === 'constructor' || key === 'prototype')\n continue\n const sourceVal = source[key]\n const targetVal = target[key]\n if (\n typeof sourceVal === 'object' &&\n sourceVal !== null &&\n typeof targetVal === 'object' &&\n targetVal !== null\n ) {\n deepMerge(\n targetVal as TranslationDictionary,\n sourceVal as TranslationDictionary,\n )\n } else {\n target[key] = sourceVal!\n }\n }\n}\n\n/**\n * Create a reactive i18n instance.\n *\n * @example\n * const i18n = createI18n({\n * locale: 'en',\n * fallbackLocale: 'en',\n * messages: {\n * en: { greeting: 'Hello {{name}}!' },\n * de: { greeting: 'Hallo {{name}}!' },\n * },\n * })\n *\n * // Reactive translation — re-evaluates on locale change\n * i18n.t('greeting', { name: 'Alice' }) // \"Hello Alice!\"\n * i18n.locale.set('de')\n * i18n.t('greeting', { name: 'Alice' }) // \"Hallo Alice!\"\n *\n * @example\n * // Async namespace loading\n * const i18n = createI18n({\n * locale: 'en',\n * loader: async (locale, namespace) => {\n * const mod = await import(`./locales/${locale}/${namespace}.json`)\n * return mod.default\n * },\n * })\n * await i18n.loadNamespace('auth')\n * i18n.t('auth:errors.invalid') // looks up \"errors.invalid\" in \"auth\" namespace\n */\nexport function createI18n(options: I18nOptions): I18nInstance {\n const {\n fallbackLocale,\n loader,\n defaultNamespace = 'common',\n pluralRules,\n onMissingKey,\n } = options\n\n // ── Reactive state ──────────────────────────────────────────────────\n\n const locale = signal(options.locale)\n\n // Internal store: locale → namespace → dictionary\n // We use a version counter to trigger reactive updates when messages change,\n // since the store is mutated in place (Object.is would skip same-reference sets).\n const store = new Map<string, Map<string, TranslationDictionary>>()\n const storeVersion = signal(0)\n\n // Loading state\n const pendingLoads = signal(0)\n const loadedNsVersion = signal(0)\n\n // In-flight load promises — deduplicates concurrent loads for the same locale:namespace\n const pendingPromises = new Map<string, Promise<void>>()\n\n const isLoading = computed(() => pendingLoads() > 0)\n const loadedNamespaces = computed(() => {\n loadedNsVersion()\n const currentLocale = locale()\n const nsMap = store.get(currentLocale)\n return new Set(nsMap ? nsMap.keys() : [])\n })\n const availableLocales = computed(() => {\n storeVersion() // subscribe to store changes\n return [...store.keys()]\n })\n\n // ── Initialize static messages ──────────────────────────────────────\n\n if (options.messages) {\n for (const [loc, dict] of Object.entries(options.messages)) {\n const nsMap = new Map<string, TranslationDictionary>()\n nsMap.set(defaultNamespace, dict)\n store.set(loc, nsMap)\n }\n }\n\n // ── Internal helpers ────────────────────────────────────────────────\n\n function getNamespaceMap(loc: string): Map<string, TranslationDictionary> {\n let nsMap = store.get(loc)\n if (!nsMap) {\n nsMap = new Map()\n store.set(loc, nsMap)\n }\n return nsMap\n }\n\n function lookupKey(\n loc: string,\n namespace: string,\n keyPath: string,\n ): string | undefined {\n const nsMap = store.get(loc)\n if (!nsMap) return undefined\n const dict = nsMap.get(namespace)\n if (!dict) return undefined\n return resolveKey(dict, keyPath)\n }\n\n function resolveTranslation(\n key: string,\n values?: InterpolationValues,\n ): string {\n // Subscribe to reactive dependencies\n const currentLocale = locale()\n storeVersion()\n\n // Parse key: \"namespace:key.path\" or just \"key.path\"\n let namespace = defaultNamespace\n let keyPath = key\n\n const colonIndex = key.indexOf(':')\n if (colonIndex > 0) {\n namespace = key.slice(0, colonIndex)\n keyPath = key.slice(colonIndex + 1)\n }\n\n // Handle pluralization: if values contain `count`, try plural suffixes\n if (values && 'count' in values) {\n const count = Number(values.count)\n const category = resolvePluralCategory(currentLocale, count, pluralRules)\n\n // Try exact form first (e.g. \"items_one\"), then fall back to base key\n const pluralKey = `${keyPath}_${category}`\n const pluralResult =\n lookupKey(currentLocale, namespace, pluralKey) ??\n (fallbackLocale\n ? lookupKey(fallbackLocale, namespace, pluralKey)\n : undefined)\n\n if (pluralResult) {\n return interpolate(pluralResult, values)\n }\n }\n\n // Standard lookup: current locale → fallback locale\n const result =\n lookupKey(currentLocale, namespace, keyPath) ??\n (fallbackLocale\n ? lookupKey(fallbackLocale, namespace, keyPath)\n : undefined)\n\n if (result !== undefined) {\n return interpolate(result, values)\n }\n\n // Missing key handler\n if (onMissingKey) {\n const custom = onMissingKey(currentLocale, key, namespace)\n if (custom !== undefined) return custom!\n }\n\n // Return the key itself as a visual fallback\n return key\n }\n\n // ── Public API ──────────────────────────────────────────────────────\n\n const t = (key: string, values?: InterpolationValues): string => {\n return resolveTranslation(key, values)\n }\n\n const loadNamespace = async (\n namespace: string,\n loc?: string,\n ): Promise<void> => {\n if (!loader) return\n\n const targetLocale = loc ?? locale.peek()\n const cacheKey = `${targetLocale}:${namespace}`\n const nsMap = getNamespaceMap(targetLocale)\n\n // Skip if already loaded\n if (nsMap.has(namespace)) return\n\n // Deduplicate concurrent loads for the same locale:namespace\n const existing = pendingPromises.get(cacheKey)\n if (existing) return existing\n\n pendingLoads.update((n) => n + 1)\n\n const promise = loader(targetLocale, namespace)\n .then((dict) => {\n if (dict) {\n nsMap.set(namespace, dict)\n storeVersion.update((n) => n + 1)\n loadedNsVersion.update((n) => n + 1)\n }\n })\n .finally(() => {\n pendingPromises.delete(cacheKey)\n pendingLoads.update((n) => n - 1)\n })\n\n pendingPromises.set(cacheKey, promise)\n return promise\n }\n\n const exists = (key: string): boolean => {\n const currentLocale = locale.peek()\n\n let namespace = defaultNamespace\n let keyPath = key\n const colonIndex = key.indexOf(':')\n if (colonIndex > 0) {\n namespace = key.slice(0, colonIndex)\n keyPath = key.slice(colonIndex + 1)\n }\n\n return (\n lookupKey(currentLocale, namespace, keyPath) !== undefined ||\n (fallbackLocale\n ? lookupKey(fallbackLocale, namespace, keyPath) !== undefined\n : false)\n )\n }\n\n const addMessages = (\n loc: string,\n messages: TranslationDictionary,\n namespace?: string,\n ): void => {\n const ns = namespace ?? defaultNamespace\n const nsMap = getNamespaceMap(loc)\n const existing = nsMap.get(ns)\n\n if (existing) {\n deepMerge(existing, messages)\n } else {\n // Deep-clone to prevent external mutation from corrupting the store\n const cloned: TranslationDictionary = {}\n deepMerge(cloned, messages)\n nsMap.set(ns, cloned)\n }\n\n storeVersion.update((n) => n + 1)\n loadedNsVersion.update((n) => n + 1)\n }\n\n return {\n t,\n locale,\n loadNamespace,\n isLoading,\n loadedNamespaces,\n exists,\n addMessages,\n availableLocales,\n }\n}\n","//#region src/h.ts\n/** Marker for fragment nodes — renders children without a wrapper element */\nconst Fragment = Symbol(\"Pyreon.Fragment\");\n/**\n* Hyperscript function — the compiled output of JSX.\n* `<div class=\"x\">hello</div>` → `h(\"div\", { class: \"x\" }, \"hello\")`\n*\n* Generic on P so TypeScript validates props match the component's signature\n* at the call site, then stores the result in the loosely-typed VNode.\n*/\n/** Shared empty props sentinel — identity-checked in mountElement to skip applyProps. */\nconst EMPTY_PROPS = {};\nfunction h(type, props, ...children) {\n\treturn {\n\t\ttype,\n\t\tprops: props ?? EMPTY_PROPS,\n\t\tchildren: normalizeChildren(children),\n\t\tkey: props?.key ?? null\n\t};\n}\nfunction normalizeChildren(children) {\n\tfor (let i = 0; i < children.length; i++) if (Array.isArray(children[i])) return flattenChildren(children);\n\treturn children;\n}\nfunction flattenChildren(children) {\n\tconst result = [];\n\tfor (const child of children) if (Array.isArray(child)) result.push(...flattenChildren(child));\n\telse result.push(child);\n\treturn result;\n}\n\n//#endregion\n//#region src/jsx-runtime.ts\n/**\n* JSX automatic runtime.\n*\n* When tsconfig has `\"jsxImportSource\": \"@pyreon/core\"`, the TS/bundler compiler\n* rewrites JSX to imports from this file automatically:\n* <div class=\"x\" /> → jsx(\"div\", { class: \"x\" })\n*/\nfunction jsx(type, props, key) {\n\tconst { children, ...rest } = props;\n\tconst propsWithKey = key != null ? {\n\t\t...rest,\n\t\tkey\n\t} : rest;\n\tif (typeof type === \"function\") return h(type, children !== void 0 ? {\n\t\t...propsWithKey,\n\t\tchildren\n\t} : propsWithKey);\n\treturn h(type, propsWithKey, ...children === void 0 ? [] : Array.isArray(children) ? children : [children]);\n}\nconst jsxs = jsx;\n\n//#endregion\nexport { Fragment, jsx, jsxs };\n//# sourceMappingURL=jsx-runtime.js.map","import type { Props, VNode } from '@pyreon/core'\nimport type { InterpolationValues } from './types'\n\nconst TAG_RE = /<(\\w+)>([^<]*)<\\/\\1>/g\n\ninterface RichPart {\n tag: string\n children: string\n}\n\n/**\n * Parse a translated string into an array of plain text and rich tag segments.\n *\n * @example\n * parseRichText(\"Hello <bold>world</bold>, click <link>here</link>\")\n * // → [\"Hello \", { tag: \"bold\", children: \"world\" }, \", click \", { tag: \"link\", children: \"here\" }]\n */\nexport function parseRichText(text: string): (string | RichPart)[] {\n const parts: (string | RichPart)[] = []\n let lastIndex = 0\n\n for (const match of text.matchAll(TAG_RE)) {\n const before = text.slice(lastIndex, match.index)\n if (before) parts.push(before)\n parts.push({ tag: match[1]!, children: match[2]! })\n lastIndex = match.index! + match[0].length\n }\n\n const after = text.slice(lastIndex)\n if (after) parts.push(after)\n\n return parts\n}\n\nexport interface TransProps extends Props {\n /** Translation key (supports namespace:key syntax). */\n i18nKey: string\n /** Interpolation values for {{placeholder}} syntax. */\n values?: InterpolationValues\n /**\n * Component map for rich interpolation.\n * Keys match tag names in the translation string.\n * Values are component functions: `(children: any) => VNode`\n *\n * @example\n * // Translation: \"Read the <terms>terms</terms> and <privacy>policy</privacy>\"\n * components={{\n * terms: (children) => <a href=\"/terms\">{children}</a>,\n * privacy: (children) => <a href=\"/privacy\">{children}</a>,\n * }}\n */\n components?: Record<string, (children: any) => any>\n /**\n * The i18n instance's `t` function.\n * Can be obtained from `useI18n()` or passed directly.\n */\n t: (key: string, values?: InterpolationValues) => string\n}\n\n/**\n * Rich JSX interpolation component for translations.\n *\n * Allows embedding JSX components within translated strings using XML-like tags.\n * The `t` function resolves the translation and interpolates `{{values}}` first,\n * then `<tag>content</tag>` patterns are mapped to the provided components.\n *\n * @example\n * // Translation: \"You have <bold>{{count}}</bold> unread messages\"\n * const { t } = useI18n()\n * <Trans\n * t={t}\n * i18nKey=\"messages.unread\"\n * values={{ count: 5 }}\n * components={{\n * bold: (children) => <strong>{children}</strong>,\n * }}\n * />\n * // Renders: You have <strong>5</strong> unread messages\n *\n * @example\n * // Translation: \"Read our <terms>terms of service</terms> and <privacy>privacy policy</privacy>\"\n * <Trans\n * t={t}\n * i18nKey=\"legal\"\n * components={{\n * terms: (children) => <a href=\"/terms\">{children}</a>,\n * privacy: (children) => <a href=\"/privacy\">{children}</a>,\n * }}\n * />\n */\nexport function Trans(props: TransProps): VNode | string {\n const translated = props.t(props.i18nKey, props.values)\n\n if (!props.components) return translated\n\n const parts = parseRichText(translated)\n\n // If the result is a single plain string, return it directly\n if (parts.length === 1 && typeof parts[0] === 'string') return parts[0]\n\n const children = parts.map((part) => {\n if (typeof part === 'string') return part\n const component = props.components![part.tag]\n // Unmatched tags: render children as plain text (no raw HTML markup)\n if (!component) return part.children\n return component(part.children)\n })\n\n return <>{children}</>\n}\n"],"x_google_ignoreList":[4],"mappings":";;;;AAIA,MAAa,cAAc,cAAmC,KAAK;;;;;;;;;;;;AAkBnE,SAAgB,aAAa,OAAiC;AAC5D,SAAQ,aAAa,MAAM,SAAS;CAEpC,MAAM,KAAK,MAAM;AACjB,QAAQ,OAAO,OAAO,aAAc,IAAyB,GAAG;;;;;;;;;;;;AAalE,SAAgB,UAAwB;CACtC,MAAM,WAAW,WAAW,YAAY;AACxC,KAAI,CAAC,SACH,OAAM,IAAI,MACR,kEACD;AAEH,QAAO;;;;;AC5CT,MAAM,mBAAmB;;;;;;AAOzB,SAAgB,YACd,UACA,QACQ;AACR,KAAI,CAAC,UAAU,CAAC,SAAS,SAAS,KAAK,CAAE,QAAO;AAChD,QAAO,SAAS,QAAQ,mBAAmB,GAAG,QAAgB;EAC5D,MAAM,UAAU,IAAI,MAAM;EAC1B,MAAM,QAAQ,OAAO;AACrB,MAAI,UAAU,OAAW,QAAO,KAAK,QAAQ;AAE7C,MAAI;AACF,UAAO,OAAO,UAAU,YAAY,UAAU,OAC1C,KAAK,UAAU,MAAM,GACrB,GAAG;UACD;AACN,UAAO,KAAK,QAAQ;;GAEtB;;;;;;;;;;;AClBJ,SAAgB,sBACd,QACA,OACA,aACQ;AAER,KAAI,cAAc,QAChB,QAAO,YAAY,QAAQ,MAAM;AAInC,KAAI,OAAO,SAAS,eAAe,KAAK,YACtC,KAAI;AAEF,SADW,IAAI,KAAK,YAAY,OAAO,CAC7B,OAAO,MAAM;SACjB;AAMV,QAAO,UAAU,IAAI,QAAQ;;;;;;;;;ACf/B,SAAS,WACP,MACA,SACoB;CACpB,MAAM,QAAQ,QAAQ,MAAM,IAAI;CAChC,IAAI,UAA0C;AAE9C,MAAK,MAAM,Q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AAU,MAAM;GAAK,CAAC;AACnD,cAAY,MAAM,QAAS,MAAM,GAAG;;CAGtC,MAAM,QAAQ,KAAK,MAAM,UAAU;AACnC,KAAI,MAAO,OAAM,KAAK,MAAM;AAE5B,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2DT,SAAgB,MAAM,OAAmC;CACvD,MAAM,aAAa,MAAM,EAAE,MAAM,SAAS,MAAM,OAAO;AAEvD,KAAI,CAAC,MAAM,WAAY,QAAO;CAE9B,MAAM,QAAQ,cAAc,WAAW;AAGvC,KAAI,MAAM,WAAW,KAAK,OAAO,MAAM,OAAO,SAAU,QAAO,MAAM;AAUrE,QAAO,0CARU,MAAM,KAAK,SAAS;AACnC,MAAI,OAAO,SAAS,SAAU,QAAO;EACrC,MAAM,YAAY,MAAM,WAAY,KAAK;AAEzC,MAAI,CAAC,UAAW,QAAO,KAAK;AAC5B,SAAO,UAAU,KAAK,SAAS;GAC/B,EAEoB"}
|
package/lib/types/devtools.d.ts
CHANGED
|
@@ -1,74 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
for (const listener of _listeners) listener();
|
|
3
|
-
}
|
|
1
|
+
//#region src/devtools.d.ts
|
|
4
2
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
*
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
3
|
+
* @pyreon/i18n devtools introspection API.
|
|
4
|
+
* Import: `import { ... } from "@pyreon/i18n/devtools"`
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Register an i18n instance for devtools inspection.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const i18n = createI18n({ ... })
|
|
11
|
+
* registerI18n("app", i18n)
|
|
12
|
+
*/
|
|
13
|
+
declare function registerI18n(name: string, instance: object): void;
|
|
15
14
|
/** Unregister an i18n instance. */
|
|
16
|
-
function unregisterI18n(name)
|
|
17
|
-
_activeInstances.delete(name);
|
|
18
|
-
_notify();
|
|
19
|
-
}
|
|
15
|
+
declare function unregisterI18n(name: string): void;
|
|
20
16
|
/** Get all registered i18n instance names. Cleans up garbage-collected instances. */
|
|
21
|
-
function getActiveI18nInstances()
|
|
22
|
-
for (const [name, ref] of _activeInstances) if (ref.deref() === void 0) _activeInstances.delete(name);
|
|
23
|
-
return [..._activeInstances.keys()];
|
|
24
|
-
}
|
|
17
|
+
declare function getActiveI18nInstances(): string[];
|
|
25
18
|
/** Get an i18n instance by name (or undefined if GC'd or not registered). */
|
|
26
|
-
function getI18nInstance(name)
|
|
27
|
-
const ref = _activeInstances.get(name);
|
|
28
|
-
if (!ref) return void 0;
|
|
29
|
-
const instance = ref.deref();
|
|
30
|
-
if (!instance) {
|
|
31
|
-
_activeInstances.delete(name);
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
return instance;
|
|
35
|
-
}
|
|
36
|
-
/** Safely read a property that may be a signal (callable). */
|
|
37
|
-
function safeRead(obj, key, fallback = void 0) {
|
|
38
|
-
try {
|
|
39
|
-
const val = obj[key];
|
|
40
|
-
return typeof val === "function" ? val() : fallback;
|
|
41
|
-
} catch {
|
|
42
|
-
return fallback;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
19
|
+
declare function getI18nInstance(name: string): object | undefined;
|
|
45
20
|
/**
|
|
46
|
-
* Get a snapshot of an i18n instance's state.
|
|
47
|
-
*/
|
|
48
|
-
function getI18nSnapshot(name)
|
|
49
|
-
const instance = getI18nInstance(name);
|
|
50
|
-
if (!instance) return void 0;
|
|
51
|
-
const ns = safeRead(instance, "loadedNamespaces", /* @__PURE__ */new Set());
|
|
52
|
-
return {
|
|
53
|
-
locale: safeRead(instance, "locale"),
|
|
54
|
-
availableLocales: safeRead(instance, "availableLocales", []),
|
|
55
|
-
loadedNamespaces: ns instanceof Set ? [...ns] : [],
|
|
56
|
-
isLoading: safeRead(instance, "isLoading", false)
|
|
57
|
-
};
|
|
58
|
-
}
|
|
21
|
+
* Get a snapshot of an i18n instance's state.
|
|
22
|
+
*/
|
|
23
|
+
declare function getI18nSnapshot(name: string): Record<string, unknown> | undefined;
|
|
59
24
|
/** Subscribe to i18n registry changes. Returns unsubscribe function. */
|
|
60
|
-
function onI18nChange(listener)
|
|
61
|
-
_listeners.add(listener);
|
|
62
|
-
return () => {
|
|
63
|
-
_listeners.delete(listener);
|
|
64
|
-
};
|
|
65
|
-
}
|
|
25
|
+
declare function onI18nChange(listener: () => void): () => void;
|
|
66
26
|
/** @internal — reset devtools registry (for tests). */
|
|
67
|
-
function _resetDevtools()
|
|
68
|
-
_activeInstances.clear();
|
|
69
|
-
_listeners.clear();
|
|
70
|
-
}
|
|
71
|
-
|
|
27
|
+
declare function _resetDevtools(): void;
|
|
72
28
|
//#endregion
|
|
73
29
|
export { _resetDevtools, getActiveI18nInstances, getI18nInstance, getI18nSnapshot, onI18nChange, registerI18n, unregisterI18n };
|
|
74
|
-
//# sourceMappingURL=
|
|
30
|
+
//# sourceMappingURL=devtools2.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"
|
|
1
|
+
{"version":3,"file":"devtools2.d.ts","names":[],"sources":["../../../src/devtools.ts"],"mappings":";;AAmBA;;;;;AAMA;;;;;iBANgB,YAAA,CAAa,IAAA,UAAc,QAAA;;iBAM3B,cAAA,CAAe,IAAA;;iBAMf,sBAAA,CAAA;AAQhB;AAAA,iBAAgB,eAAA,CAAgB,IAAA;;;;iBA4BhB,eAAA,CACd,IAAA,WACC,MAAA;;iBAaa,YAAA,CAAa,QAAA;;iBAQb,cAAA,CAAA"}
|
package/lib/types/index.d.ts
CHANGED
|
@@ -1,379 +1,244 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
//#region src/context.ts
|
|
1
|
+
import * as _pyreon_core0 from "@pyreon/core";
|
|
2
|
+
import { Props, VNode, VNodeChild } from "@pyreon/core";
|
|
3
|
+
import { Computed, Signal } from "@pyreon/reactivity";
|
|
5
4
|
|
|
5
|
+
//#region src/types.d.ts
|
|
6
|
+
/** A nested dictionary of translation strings. */
|
|
7
|
+
type TranslationDictionary = {
|
|
8
|
+
[key: string]: string | TranslationDictionary;
|
|
9
|
+
};
|
|
10
|
+
/** Map of locale → dictionary (or namespace → dictionary). */
|
|
11
|
+
type TranslationMessages = Record<string, TranslationDictionary>;
|
|
6
12
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
* Async function that loads translations for a locale and namespace.
|
|
14
|
+
* Return the dictionary for that namespace, or undefined if not found.
|
|
15
|
+
*/
|
|
16
|
+
type NamespaceLoader = (locale: string, namespace: string) => Promise<TranslationDictionary | undefined>;
|
|
17
|
+
/** Interpolation values for translation strings. */
|
|
18
|
+
type InterpolationValues = Record<string, string | number>;
|
|
19
|
+
/** Pluralization rules map — locale → function that picks the plural form. */
|
|
20
|
+
type PluralRules = Record<string, (count: number) => string>;
|
|
21
|
+
/** Options for creating an i18n instance. */
|
|
22
|
+
interface I18nOptions {
|
|
23
|
+
/** The initial locale (e.g. "en"). */
|
|
24
|
+
locale: string;
|
|
25
|
+
/** Fallback locale used when a key is missing in the active locale. */
|
|
26
|
+
fallbackLocale?: string;
|
|
27
|
+
/** Static messages keyed by locale. */
|
|
28
|
+
messages?: Record<string, TranslationDictionary>;
|
|
29
|
+
/**
|
|
30
|
+
* Async loader for namespace-based translation loading.
|
|
31
|
+
* Called with (locale, namespace) when `loadNamespace()` is invoked.
|
|
32
|
+
*/
|
|
33
|
+
loader?: NamespaceLoader;
|
|
34
|
+
/**
|
|
35
|
+
* Default namespace used when `t()` is called without a namespace prefix.
|
|
36
|
+
* Defaults to "common".
|
|
37
|
+
*/
|
|
38
|
+
defaultNamespace?: string;
|
|
39
|
+
/**
|
|
40
|
+
* Custom plural rules per locale.
|
|
41
|
+
* If not provided, uses `Intl.PluralRules` where available.
|
|
42
|
+
*/
|
|
43
|
+
pluralRules?: PluralRules;
|
|
44
|
+
/**
|
|
45
|
+
* Missing key handler — called when a translation key is not found.
|
|
46
|
+
* Useful for logging, reporting, or returning a custom fallback.
|
|
47
|
+
*/
|
|
48
|
+
onMissingKey?: (locale: string, key: string, namespace?: string) => string | undefined;
|
|
21
49
|
}
|
|
22
|
-
/**
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
50
|
+
/** The public i18n instance returned by `createI18n()`. */
|
|
51
|
+
interface I18nInstance {
|
|
52
|
+
/**
|
|
53
|
+
* Translate a key with optional interpolation.
|
|
54
|
+
* Reads the current locale reactively — re-evaluates in effects/computeds.
|
|
55
|
+
*
|
|
56
|
+
* Key format: "key" (uses default namespace) or "namespace:key".
|
|
57
|
+
* Nested keys use dots: "user.greeting" or "auth:errors.invalid".
|
|
58
|
+
*
|
|
59
|
+
* Interpolation: "Hello {{name}}" + { name: "Alice" } → "Hello Alice"
|
|
60
|
+
* Pluralization: key with "_one", "_other" etc. suffixes + { count: N }
|
|
61
|
+
*/
|
|
62
|
+
t: (key: string, values?: InterpolationValues) => string;
|
|
63
|
+
/** Current locale (reactive signal). */
|
|
64
|
+
locale: Signal<string>;
|
|
65
|
+
/**
|
|
66
|
+
* Load a namespace's translations for the given locale (or current locale).
|
|
67
|
+
* Returns a promise that resolves when loading is complete.
|
|
68
|
+
*/
|
|
69
|
+
loadNamespace: (namespace: string, locale?: string) => Promise<void>;
|
|
70
|
+
/**
|
|
71
|
+
* Whether any namespace is currently being loaded.
|
|
72
|
+
*/
|
|
73
|
+
isLoading: Computed<boolean>;
|
|
74
|
+
/**
|
|
75
|
+
* Set of namespaces that have been loaded for the current locale.
|
|
76
|
+
*/
|
|
77
|
+
loadedNamespaces: Computed<Set<string>>;
|
|
78
|
+
/**
|
|
79
|
+
* Check if a translation key exists in the current locale.
|
|
80
|
+
*/
|
|
81
|
+
exists: (key: string) => boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Add translations for a locale (merged with existing).
|
|
84
|
+
* Useful for adding translations at runtime without async loading.
|
|
85
|
+
*/
|
|
86
|
+
addMessages: (locale: string, messages: TranslationDictionary, namespace?: string) => void;
|
|
87
|
+
/**
|
|
88
|
+
* Get all available locales (those with any registered messages).
|
|
89
|
+
*/
|
|
90
|
+
availableLocales: Computed<string[]>;
|
|
36
91
|
}
|
|
37
|
-
|
|
38
92
|
//#endregion
|
|
39
|
-
//#region src/
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
* Unmatched placeholders are left as-is.
|
|
45
|
-
*/
|
|
46
|
-
function interpolate(template, values) {
|
|
47
|
-
if (!values || !template.includes("{{")) return template;
|
|
48
|
-
return template.replace(INTERPOLATION_RE, (_, key) => {
|
|
49
|
-
const trimmed = key.trim();
|
|
50
|
-
const value = values[trimmed];
|
|
51
|
-
if (value === void 0) return `{{${trimmed}}}`;
|
|
52
|
-
try {
|
|
53
|
-
return typeof value === "object" && value !== null ? JSON.stringify(value) : `${value}`;
|
|
54
|
-
} catch {
|
|
55
|
-
return `{{${trimmed}}}`;
|
|
56
|
-
}
|
|
57
|
-
});
|
|
93
|
+
//#region src/context.d.ts
|
|
94
|
+
declare const I18nContext: _pyreon_core0.Context<I18nInstance | null>;
|
|
95
|
+
interface I18nProviderProps extends Props {
|
|
96
|
+
instance: I18nInstance;
|
|
97
|
+
children?: VNodeChild;
|
|
58
98
|
}
|
|
59
|
-
|
|
60
|
-
//#endregion
|
|
61
|
-
//#region src/pluralization.ts
|
|
62
99
|
/**
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
//#endregion
|
|
77
|
-
//#region src/create-i18n.ts
|
|
100
|
+
* Provide an i18n instance to the component tree.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* const i18n = createI18n({ locale: 'en', messages: { en: { hello: 'Hello' } } })
|
|
104
|
+
*
|
|
105
|
+
* // In JSX:
|
|
106
|
+
* <I18nProvider instance={i18n}>
|
|
107
|
+
* <App />
|
|
108
|
+
* </I18nProvider>
|
|
109
|
+
*/
|
|
110
|
+
declare function I18nProvider(props: I18nProviderProps): VNode;
|
|
78
111
|
/**
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Deep-merge source into target (mutates target).
|
|
93
|
-
*/
|
|
94
|
-
function deepMerge(target, source) {
|
|
95
|
-
for (const key of Object.keys(source)) {
|
|
96
|
-
if (key === "__proto__" || key === "constructor" || key === "prototype") continue;
|
|
97
|
-
const sourceVal = source[key];
|
|
98
|
-
const targetVal = target[key];
|
|
99
|
-
if (typeof sourceVal === "object" && sourceVal !== null && typeof targetVal === "object" && targetVal !== null) deepMerge(targetVal, sourceVal);else target[key] = sourceVal;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
112
|
+
* Access the i18n instance from the nearest I18nProvider.
|
|
113
|
+
* Must be called within a component tree wrapped by I18nProvider.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* function Greeting() {
|
|
117
|
+
* const { t, locale } = useI18n()
|
|
118
|
+
* return <h1>{t('greeting', { name: 'World' })}</h1>
|
|
119
|
+
* }
|
|
120
|
+
*/
|
|
121
|
+
declare function useI18n(): I18nInstance;
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region src/create-i18n.d.ts
|
|
102
124
|
/**
|
|
103
|
-
* Create a reactive i18n instance.
|
|
104
|
-
*
|
|
105
|
-
* @example
|
|
106
|
-
* const i18n = createI18n({
|
|
107
|
-
* locale: 'en',
|
|
108
|
-
* fallbackLocale: 'en',
|
|
109
|
-
* messages: {
|
|
110
|
-
* en: { greeting: 'Hello {{name}}!' },
|
|
111
|
-
* de: { greeting: 'Hallo {{name}}!' },
|
|
112
|
-
* },
|
|
113
|
-
* })
|
|
114
|
-
*
|
|
115
|
-
* // Reactive translation — re-evaluates on locale change
|
|
116
|
-
* i18n.t('greeting', { name: 'Alice' }) // "Hello Alice!"
|
|
117
|
-
* i18n.locale.set('de')
|
|
118
|
-
* i18n.t('greeting', { name: 'Alice' }) // "Hallo Alice!"
|
|
119
|
-
*
|
|
120
|
-
* @example
|
|
121
|
-
* // Async namespace loading
|
|
122
|
-
* const i18n = createI18n({
|
|
123
|
-
* locale: 'en',
|
|
124
|
-
* loader: async (locale, namespace) => {
|
|
125
|
-
* const mod = await import(`./locales/${locale}/${namespace}.json`)
|
|
126
|
-
* return mod.default
|
|
127
|
-
* },
|
|
128
|
-
* })
|
|
129
|
-
* await i18n.loadNamespace('auth')
|
|
130
|
-
* i18n.t('auth:errors.invalid') // looks up "errors.invalid" in "auth" namespace
|
|
131
|
-
*/
|
|
132
|
-
function createI18n(options)
|
|
133
|
-
const {
|
|
134
|
-
fallbackLocale,
|
|
135
|
-
loader,
|
|
136
|
-
defaultNamespace = "common",
|
|
137
|
-
pluralRules,
|
|
138
|
-
onMissingKey
|
|
139
|
-
} = options;
|
|
140
|
-
const locale = signal(options.locale);
|
|
141
|
-
const store = /* @__PURE__ */new Map();
|
|
142
|
-
const storeVersion = signal(0);
|
|
143
|
-
const pendingLoads = signal(0);
|
|
144
|
-
const loadedNsVersion = signal(0);
|
|
145
|
-
const pendingPromises = /* @__PURE__ */new Map();
|
|
146
|
-
const isLoading = computed(() => pendingLoads() > 0);
|
|
147
|
-
const loadedNamespaces = computed(() => {
|
|
148
|
-
loadedNsVersion();
|
|
149
|
-
const currentLocale = locale();
|
|
150
|
-
const nsMap = store.get(currentLocale);
|
|
151
|
-
return new Set(nsMap ? nsMap.keys() : []);
|
|
152
|
-
});
|
|
153
|
-
const availableLocales = computed(() => {
|
|
154
|
-
storeVersion();
|
|
155
|
-
return [...store.keys()];
|
|
156
|
-
});
|
|
157
|
-
if (options.messages) for (const [loc, dict] of Object.entries(options.messages)) {
|
|
158
|
-
const nsMap = /* @__PURE__ */new Map();
|
|
159
|
-
nsMap.set(defaultNamespace, dict);
|
|
160
|
-
store.set(loc, nsMap);
|
|
161
|
-
}
|
|
162
|
-
function getNamespaceMap(loc) {
|
|
163
|
-
let nsMap = store.get(loc);
|
|
164
|
-
if (!nsMap) {
|
|
165
|
-
nsMap = /* @__PURE__ */new Map();
|
|
166
|
-
store.set(loc, nsMap);
|
|
167
|
-
}
|
|
168
|
-
return nsMap;
|
|
169
|
-
}
|
|
170
|
-
function lookupKey(loc, namespace, keyPath) {
|
|
171
|
-
const nsMap = store.get(loc);
|
|
172
|
-
if (!nsMap) return void 0;
|
|
173
|
-
const dict = nsMap.get(namespace);
|
|
174
|
-
if (!dict) return void 0;
|
|
175
|
-
return resolveKey(dict, keyPath);
|
|
176
|
-
}
|
|
177
|
-
function resolveTranslation(key, values) {
|
|
178
|
-
const currentLocale = locale();
|
|
179
|
-
storeVersion();
|
|
180
|
-
let namespace = defaultNamespace;
|
|
181
|
-
let keyPath = key;
|
|
182
|
-
const colonIndex = key.indexOf(":");
|
|
183
|
-
if (colonIndex > 0) {
|
|
184
|
-
namespace = key.slice(0, colonIndex);
|
|
185
|
-
keyPath = key.slice(colonIndex + 1);
|
|
186
|
-
}
|
|
187
|
-
if (values && "count" in values) {
|
|
188
|
-
const category = resolvePluralCategory(currentLocale, Number(values.count), pluralRules);
|
|
189
|
-
const pluralKey = `${keyPath}_${category}`;
|
|
190
|
-
const pluralResult = lookupKey(currentLocale, namespace, pluralKey) ?? (fallbackLocale ? lookupKey(fallbackLocale, namespace, pluralKey) : void 0);
|
|
191
|
-
if (pluralResult) return interpolate(pluralResult, values);
|
|
192
|
-
}
|
|
193
|
-
const result = lookupKey(currentLocale, namespace, keyPath) ?? (fallbackLocale ? lookupKey(fallbackLocale, namespace, keyPath) : void 0);
|
|
194
|
-
if (result !== void 0) return interpolate(result, values);
|
|
195
|
-
if (onMissingKey) {
|
|
196
|
-
const custom = onMissingKey(currentLocale, key, namespace);
|
|
197
|
-
if (custom !== void 0) return custom;
|
|
198
|
-
}
|
|
199
|
-
return key;
|
|
200
|
-
}
|
|
201
|
-
const t = (key, values) => {
|
|
202
|
-
return resolveTranslation(key, values);
|
|
203
|
-
};
|
|
204
|
-
const loadNamespace = async (namespace, loc) => {
|
|
205
|
-
if (!loader) return;
|
|
206
|
-
const targetLocale = loc ?? locale.peek();
|
|
207
|
-
const cacheKey = `${targetLocale}:${namespace}`;
|
|
208
|
-
const nsMap = getNamespaceMap(targetLocale);
|
|
209
|
-
if (nsMap.has(namespace)) return;
|
|
210
|
-
const existing = pendingPromises.get(cacheKey);
|
|
211
|
-
if (existing) return existing;
|
|
212
|
-
pendingLoads.update(n => n + 1);
|
|
213
|
-
const promise = loader(targetLocale, namespace).then(dict => {
|
|
214
|
-
if (dict) {
|
|
215
|
-
nsMap.set(namespace, dict);
|
|
216
|
-
storeVersion.update(n => n + 1);
|
|
217
|
-
loadedNsVersion.update(n => n + 1);
|
|
218
|
-
}
|
|
219
|
-
}).finally(() => {
|
|
220
|
-
pendingPromises.delete(cacheKey);
|
|
221
|
-
pendingLoads.update(n => n - 1);
|
|
222
|
-
});
|
|
223
|
-
pendingPromises.set(cacheKey, promise);
|
|
224
|
-
return promise;
|
|
225
|
-
};
|
|
226
|
-
const exists = key => {
|
|
227
|
-
const currentLocale = locale.peek();
|
|
228
|
-
let namespace = defaultNamespace;
|
|
229
|
-
let keyPath = key;
|
|
230
|
-
const colonIndex = key.indexOf(":");
|
|
231
|
-
if (colonIndex > 0) {
|
|
232
|
-
namespace = key.slice(0, colonIndex);
|
|
233
|
-
keyPath = key.slice(colonIndex + 1);
|
|
234
|
-
}
|
|
235
|
-
return lookupKey(currentLocale, namespace, keyPath) !== void 0 || (fallbackLocale ? lookupKey(fallbackLocale, namespace, keyPath) !== void 0 : false);
|
|
236
|
-
};
|
|
237
|
-
const addMessages = (loc, messages, namespace) => {
|
|
238
|
-
const ns = namespace ?? defaultNamespace;
|
|
239
|
-
const nsMap = getNamespaceMap(loc);
|
|
240
|
-
const existing = nsMap.get(ns);
|
|
241
|
-
if (existing) deepMerge(existing, messages);else {
|
|
242
|
-
const cloned = {};
|
|
243
|
-
deepMerge(cloned, messages);
|
|
244
|
-
nsMap.set(ns, cloned);
|
|
245
|
-
}
|
|
246
|
-
storeVersion.update(n => n + 1);
|
|
247
|
-
loadedNsVersion.update(n => n + 1);
|
|
248
|
-
};
|
|
249
|
-
return {
|
|
250
|
-
t,
|
|
251
|
-
locale,
|
|
252
|
-
loadNamespace,
|
|
253
|
-
isLoading,
|
|
254
|
-
loadedNamespaces,
|
|
255
|
-
exists,
|
|
256
|
-
addMessages,
|
|
257
|
-
availableLocales
|
|
258
|
-
};
|
|
259
|
-
}
|
|
260
|
-
|
|
125
|
+
* Create a reactive i18n instance.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* const i18n = createI18n({
|
|
129
|
+
* locale: 'en',
|
|
130
|
+
* fallbackLocale: 'en',
|
|
131
|
+
* messages: {
|
|
132
|
+
* en: { greeting: 'Hello {{name}}!' },
|
|
133
|
+
* de: { greeting: 'Hallo {{name}}!' },
|
|
134
|
+
* },
|
|
135
|
+
* })
|
|
136
|
+
*
|
|
137
|
+
* // Reactive translation — re-evaluates on locale change
|
|
138
|
+
* i18n.t('greeting', { name: 'Alice' }) // "Hello Alice!"
|
|
139
|
+
* i18n.locale.set('de')
|
|
140
|
+
* i18n.t('greeting', { name: 'Alice' }) // "Hallo Alice!"
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* // Async namespace loading
|
|
144
|
+
* const i18n = createI18n({
|
|
145
|
+
* locale: 'en',
|
|
146
|
+
* loader: async (locale, namespace) => {
|
|
147
|
+
* const mod = await import(`./locales/${locale}/${namespace}.json`)
|
|
148
|
+
* return mod.default
|
|
149
|
+
* },
|
|
150
|
+
* })
|
|
151
|
+
* await i18n.loadNamespace('auth')
|
|
152
|
+
* i18n.t('auth:errors.invalid') // looks up "errors.invalid" in "auth" namespace
|
|
153
|
+
*/
|
|
154
|
+
declare function createI18n(options: I18nOptions): I18nInstance;
|
|
261
155
|
//#endregion
|
|
262
|
-
//#region
|
|
263
|
-
/** Marker for fragment nodes — renders children without a wrapper element */
|
|
264
|
-
|
|
265
|
-
function h(type, props, ...children) {
|
|
266
|
-
return {
|
|
267
|
-
type,
|
|
268
|
-
props: props ?? EMPTY_PROPS,
|
|
269
|
-
children: normalizeChildren(children),
|
|
270
|
-
key: props?.key ?? null
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
function normalizeChildren(children) {
|
|
274
|
-
for (let i = 0; i < children.length; i++) if (Array.isArray(children[i])) return flattenChildren(children);
|
|
275
|
-
return children;
|
|
276
|
-
}
|
|
277
|
-
function flattenChildren(children) {
|
|
278
|
-
const result = [];
|
|
279
|
-
for (const child of children) if (Array.isArray(child)) result.push(...flattenChildren(child));else result.push(child);
|
|
280
|
-
return result;
|
|
281
|
-
}
|
|
156
|
+
//#region src/interpolation.d.ts
|
|
282
157
|
/**
|
|
283
|
-
*
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
*/
|
|
289
|
-
function jsx(type, props, key) {
|
|
290
|
-
const {
|
|
291
|
-
children,
|
|
292
|
-
...rest
|
|
293
|
-
} = props;
|
|
294
|
-
const propsWithKey = key != null ? {
|
|
295
|
-
...rest,
|
|
296
|
-
key
|
|
297
|
-
} : rest;
|
|
298
|
-
if (typeof type === "function") return h(type, children !== void 0 ? {
|
|
299
|
-
...propsWithKey,
|
|
300
|
-
children
|
|
301
|
-
} : propsWithKey);
|
|
302
|
-
return h(type, propsWithKey, ...(children === void 0 ? [] : Array.isArray(children) ? children : [children]));
|
|
303
|
-
}
|
|
304
|
-
|
|
158
|
+
* Replace `{{key}}` placeholders in a string with values from the given record.
|
|
159
|
+
* Supports optional whitespace inside braces: `{{ name }}` works too.
|
|
160
|
+
* Unmatched placeholders are left as-is.
|
|
161
|
+
*/
|
|
162
|
+
declare function interpolate(template: string, values?: InterpolationValues): string;
|
|
305
163
|
//#endregion
|
|
306
|
-
//#region src/
|
|
307
|
-
|
|
164
|
+
//#region src/pluralization.d.ts
|
|
308
165
|
/**
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
*
|
|
312
|
-
*
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
if (before) parts.push(before);
|
|
321
|
-
parts.push({
|
|
322
|
-
tag: match[1],
|
|
323
|
-
children: match[2]
|
|
324
|
-
});
|
|
325
|
-
lastIndex = match.index + match[0].length;
|
|
326
|
-
}
|
|
327
|
-
const after = text.slice(lastIndex);
|
|
328
|
-
if (after) parts.push(after);
|
|
329
|
-
return parts;
|
|
166
|
+
* Resolve the plural category for a given count and locale.
|
|
167
|
+
*
|
|
168
|
+
* Uses custom rules if provided, otherwise falls back to `Intl.PluralRules`.
|
|
169
|
+
* Returns CLDR plural categories: "zero", "one", "two", "few", "many", "other".
|
|
170
|
+
*/
|
|
171
|
+
declare function resolvePluralCategory(locale: string, count: number, customRules?: PluralRules): string;
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region src/trans.d.ts
|
|
174
|
+
interface RichPart {
|
|
175
|
+
tag: string;
|
|
176
|
+
children: string;
|
|
330
177
|
}
|
|
331
178
|
/**
|
|
332
|
-
*
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
*
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
352
|
-
*
|
|
353
|
-
* <
|
|
354
|
-
*
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
*
|
|
359
|
-
*
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
function Trans(props) {
|
|
363
|
-
const translated = props.t(props.i18nKey, props.values);
|
|
364
|
-
if (!props.components) return translated;
|
|
365
|
-
const parts = parseRichText(translated);
|
|
366
|
-
if (parts.length === 1 && typeof parts[0] === "string") return parts[0];
|
|
367
|
-
return /* @__PURE__ */jsx(Fragment, {
|
|
368
|
-
children: parts.map(part => {
|
|
369
|
-
if (typeof part === "string") return part;
|
|
370
|
-
const component = props.components[part.tag];
|
|
371
|
-
if (!component) return part.children;
|
|
372
|
-
return component(part.children);
|
|
373
|
-
})
|
|
374
|
-
});
|
|
179
|
+
* Parse a translated string into an array of plain text and rich tag segments.
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* parseRichText("Hello <bold>world</bold>, click <link>here</link>")
|
|
183
|
+
* // → ["Hello ", { tag: "bold", children: "world" }, ", click ", { tag: "link", children: "here" }]
|
|
184
|
+
*/
|
|
185
|
+
declare function parseRichText(text: string): (string | RichPart)[];
|
|
186
|
+
interface TransProps extends Props {
|
|
187
|
+
/** Translation key (supports namespace:key syntax). */
|
|
188
|
+
i18nKey: string;
|
|
189
|
+
/** Interpolation values for {{placeholder}} syntax. */
|
|
190
|
+
values?: InterpolationValues;
|
|
191
|
+
/**
|
|
192
|
+
* Component map for rich interpolation.
|
|
193
|
+
* Keys match tag names in the translation string.
|
|
194
|
+
* Values are component functions: `(children: any) => VNode`
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* // Translation: "Read the <terms>terms</terms> and <privacy>policy</privacy>"
|
|
198
|
+
* components={{
|
|
199
|
+
* terms: (children) => <a href="/terms">{children}</a>,
|
|
200
|
+
* privacy: (children) => <a href="/privacy">{children}</a>,
|
|
201
|
+
* }}
|
|
202
|
+
*/
|
|
203
|
+
components?: Record<string, (children: any) => any>;
|
|
204
|
+
/**
|
|
205
|
+
* The i18n instance's `t` function.
|
|
206
|
+
* Can be obtained from `useI18n()` or passed directly.
|
|
207
|
+
*/
|
|
208
|
+
t: (key: string, values?: InterpolationValues) => string;
|
|
375
209
|
}
|
|
376
|
-
|
|
210
|
+
/**
|
|
211
|
+
* Rich JSX interpolation component for translations.
|
|
212
|
+
*
|
|
213
|
+
* Allows embedding JSX components within translated strings using XML-like tags.
|
|
214
|
+
* The `t` function resolves the translation and interpolates `{{values}}` first,
|
|
215
|
+
* then `<tag>content</tag>` patterns are mapped to the provided components.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* // Translation: "You have <bold>{{count}}</bold> unread messages"
|
|
219
|
+
* const { t } = useI18n()
|
|
220
|
+
* <Trans
|
|
221
|
+
* t={t}
|
|
222
|
+
* i18nKey="messages.unread"
|
|
223
|
+
* values={{ count: 5 }}
|
|
224
|
+
* components={{
|
|
225
|
+
* bold: (children) => <strong>{children}</strong>,
|
|
226
|
+
* }}
|
|
227
|
+
* />
|
|
228
|
+
* // Renders: You have <strong>5</strong> unread messages
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* // Translation: "Read our <terms>terms of service</terms> and <privacy>privacy policy</privacy>"
|
|
232
|
+
* <Trans
|
|
233
|
+
* t={t}
|
|
234
|
+
* i18nKey="legal"
|
|
235
|
+
* components={{
|
|
236
|
+
* terms: (children) => <a href="/terms">{children}</a>,
|
|
237
|
+
* privacy: (children) => <a href="/privacy">{children}</a>,
|
|
238
|
+
* }}
|
|
239
|
+
* />
|
|
240
|
+
*/
|
|
241
|
+
declare function Trans(props: TransProps): VNode | string;
|
|
377
242
|
//#endregion
|
|
378
|
-
export { I18nContext, I18nProvider, Trans, createI18n, interpolate, parseRichText, resolvePluralCategory, useI18n };
|
|
379
|
-
//# sourceMappingURL=
|
|
243
|
+
export { I18nContext, type I18nInstance, type I18nOptions, I18nProvider, type I18nProviderProps, type InterpolationValues, type NamespaceLoader, type PluralRules, Trans, type TransProps, type TranslationDictionary, type TranslationMessages, createI18n, interpolate, parseRichText, resolvePluralCategory, useI18n };
|
|
244
|
+
//# sourceMappingURL=index2.d.ts.map
|
package/lib/types/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"
|
|
1
|
+
{"version":3,"file":"index2.d.ts","names":[],"sources":["../../../src/types.ts","../../../src/context.ts","../../../src/create-i18n.ts","../../../src/interpolation.ts","../../../src/pluralization.ts","../../../src/trans.tsx"],"mappings":";;;;;;KAGY,qBAAA;EAAA,CACT,GAAA,oBAAuB,qBAAA;AAAA;AAD1B;AAAA,KAKY,mBAAA,GAAsB,MAAA,SAAe,qBAAA;;;;AAAjD;KAMY,eAAA,IACV,MAAA,UACA,SAAA,aACG,OAAA,CAAQ,qBAAA;;KAGD,mBAAA,GAAsB,MAAA;;KAGtB,WAAA,GAAc,MAAA,UAAgB,KAAA;;UAGzB,WAAA;EATL;EAWV,MAAA;EAZA;EAcA,cAAA;EAbW;EAeX,QAAA,GAAW,MAAA,SAAe,qBAAA;EAfM;AAGlC;;;EAiBE,MAAA,GAAS,eAAA;EAjB6B;AAGxC;;;EAmBE,gBAAA;EAnBqD;AAGvD;;;EAqBE,WAAA,GAAc,WAAA;EAfH;;;;EAoBX,YAAA,IACE,MAAA,UACA,GAAA,UACA,SAAA;AAAA;;UAKa,YAAA;EA5BJ;;;;;;;;;;EAuCX,CAAA,GAAI,GAAA,UAAa,MAAA,GAAS,mBAAA;EAhBN;EAmBpB,MAAA,E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,KAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pyreon/i18n",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Reactive internationalization for Pyreon with async namespace loading",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"typecheck": "tsc --noEmit"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
|
-
"@pyreon/core": ">=0.
|
|
49
|
-
"@pyreon/reactivity": ">=0.
|
|
48
|
+
"@pyreon/core": ">=0.7.0 <0.8.0",
|
|
49
|
+
"@pyreon/reactivity": ">=0.7.0 <0.8.0"
|
|
50
50
|
}
|
|
51
51
|
}
|
package/lib/types/devtools2.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
//#region src/devtools.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* @pyreon/i18n devtools introspection API.
|
|
4
|
-
* Import: `import { ... } from "@pyreon/i18n/devtools"`
|
|
5
|
-
*/
|
|
6
|
-
/**
|
|
7
|
-
* Register an i18n instance for devtools inspection.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* const i18n = createI18n({ ... })
|
|
11
|
-
* registerI18n("app", i18n)
|
|
12
|
-
*/
|
|
13
|
-
declare function registerI18n(name: string, instance: object): void;
|
|
14
|
-
/** Unregister an i18n instance. */
|
|
15
|
-
declare function unregisterI18n(name: string): void;
|
|
16
|
-
/** Get all registered i18n instance names. Cleans up garbage-collected instances. */
|
|
17
|
-
declare function getActiveI18nInstances(): string[];
|
|
18
|
-
/** Get an i18n instance by name (or undefined if GC'd or not registered). */
|
|
19
|
-
declare function getI18nInstance(name: string): object | undefined;
|
|
20
|
-
/**
|
|
21
|
-
* Get a snapshot of an i18n instance's state.
|
|
22
|
-
*/
|
|
23
|
-
declare function getI18nSnapshot(name: string): Record<string, unknown> | undefined;
|
|
24
|
-
/** Subscribe to i18n registry changes. Returns unsubscribe function. */
|
|
25
|
-
declare function onI18nChange(listener: () => void): () => void;
|
|
26
|
-
/** @internal — reset devtools registry (for tests). */
|
|
27
|
-
declare function _resetDevtools(): void;
|
|
28
|
-
//#endregion
|
|
29
|
-
export { _resetDevtools, getActiveI18nInstances, getI18nInstance, getI18nSnapshot, onI18nChange, registerI18n, unregisterI18n };
|
|
30
|
-
//# sourceMappingURL=devtools2.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"devtools2.d.ts","names":[],"sources":["../../src/devtools.ts"],"mappings":";;AAmBA;;;;;AAMA;;;;;iBANgB,YAAA,CAAa,IAAA,UAAc,QAAA;;iBAM3B,cAAA,CAAe,IAAA;;iBAMf,sBAAA,CAAA;AAQhB;AAAA,iBAAgB,eAAA,CAAgB,IAAA;;;;iBA4BhB,eAAA,CACd,IAAA,WACC,MAAA;;iBAaa,YAAA,CAAa,QAAA;;iBAQb,cAAA,CAAA"}
|
package/lib/types/index2.d.ts
DELETED
|
@@ -1,244 +0,0 @@
|
|
|
1
|
-
import * as _pyreon_core0 from "@pyreon/core";
|
|
2
|
-
import { Props, VNode, VNodeChild } from "@pyreon/core";
|
|
3
|
-
import { Computed, Signal } from "@pyreon/reactivity";
|
|
4
|
-
|
|
5
|
-
//#region src/types.d.ts
|
|
6
|
-
/** A nested dictionary of translation strings. */
|
|
7
|
-
type TranslationDictionary = {
|
|
8
|
-
[key: string]: string | TranslationDictionary;
|
|
9
|
-
};
|
|
10
|
-
/** Map of locale → dictionary (or namespace → dictionary). */
|
|
11
|
-
type TranslationMessages = Record<string, TranslationDictionary>;
|
|
12
|
-
/**
|
|
13
|
-
* Async function that loads translations for a locale and namespace.
|
|
14
|
-
* Return the dictionary for that namespace, or undefined if not found.
|
|
15
|
-
*/
|
|
16
|
-
type NamespaceLoader = (locale: string, namespace: string) => Promise<TranslationDictionary | undefined>;
|
|
17
|
-
/** Interpolation values for translation strings. */
|
|
18
|
-
type InterpolationValues = Record<string, string | number>;
|
|
19
|
-
/** Pluralization rules map — locale → function that picks the plural form. */
|
|
20
|
-
type PluralRules = Record<string, (count: number) => string>;
|
|
21
|
-
/** Options for creating an i18n instance. */
|
|
22
|
-
interface I18nOptions {
|
|
23
|
-
/** The initial locale (e.g. "en"). */
|
|
24
|
-
locale: string;
|
|
25
|
-
/** Fallback locale used when a key is missing in the active locale. */
|
|
26
|
-
fallbackLocale?: string;
|
|
27
|
-
/** Static messages keyed by locale. */
|
|
28
|
-
messages?: Record<string, TranslationDictionary>;
|
|
29
|
-
/**
|
|
30
|
-
* Async loader for namespace-based translation loading.
|
|
31
|
-
* Called with (locale, namespace) when `loadNamespace()` is invoked.
|
|
32
|
-
*/
|
|
33
|
-
loader?: NamespaceLoader;
|
|
34
|
-
/**
|
|
35
|
-
* Default namespace used when `t()` is called without a namespace prefix.
|
|
36
|
-
* Defaults to "common".
|
|
37
|
-
*/
|
|
38
|
-
defaultNamespace?: string;
|
|
39
|
-
/**
|
|
40
|
-
* Custom plural rules per locale.
|
|
41
|
-
* If not provided, uses `Intl.PluralRules` where available.
|
|
42
|
-
*/
|
|
43
|
-
pluralRules?: PluralRules;
|
|
44
|
-
/**
|
|
45
|
-
* Missing key handler — called when a translation key is not found.
|
|
46
|
-
* Useful for logging, reporting, or returning a custom fallback.
|
|
47
|
-
*/
|
|
48
|
-
onMissingKey?: (locale: string, key: string, namespace?: string) => string | undefined;
|
|
49
|
-
}
|
|
50
|
-
/** The public i18n instance returned by `createI18n()`. */
|
|
51
|
-
interface I18nInstance {
|
|
52
|
-
/**
|
|
53
|
-
* Translate a key with optional interpolation.
|
|
54
|
-
* Reads the current locale reactively — re-evaluates in effects/computeds.
|
|
55
|
-
*
|
|
56
|
-
* Key format: "key" (uses default namespace) or "namespace:key".
|
|
57
|
-
* Nested keys use dots: "user.greeting" or "auth:errors.invalid".
|
|
58
|
-
*
|
|
59
|
-
* Interpolation: "Hello {{name}}" + { name: "Alice" } → "Hello Alice"
|
|
60
|
-
* Pluralization: key with "_one", "_other" etc. suffixes + { count: N }
|
|
61
|
-
*/
|
|
62
|
-
t: (key: string, values?: InterpolationValues) => string;
|
|
63
|
-
/** Current locale (reactive signal). */
|
|
64
|
-
locale: Signal<string>;
|
|
65
|
-
/**
|
|
66
|
-
* Load a namespace's translations for the given locale (or current locale).
|
|
67
|
-
* Returns a promise that resolves when loading is complete.
|
|
68
|
-
*/
|
|
69
|
-
loadNamespace: (namespace: string, locale?: string) => Promise<void>;
|
|
70
|
-
/**
|
|
71
|
-
* Whether any namespace is currently being loaded.
|
|
72
|
-
*/
|
|
73
|
-
isLoading: Computed<boolean>;
|
|
74
|
-
/**
|
|
75
|
-
* Set of namespaces that have been loaded for the current locale.
|
|
76
|
-
*/
|
|
77
|
-
loadedNamespaces: Computed<Set<string>>;
|
|
78
|
-
/**
|
|
79
|
-
* Check if a translation key exists in the current locale.
|
|
80
|
-
*/
|
|
81
|
-
exists: (key: string) => boolean;
|
|
82
|
-
/**
|
|
83
|
-
* Add translations for a locale (merged with existing).
|
|
84
|
-
* Useful for adding translations at runtime without async loading.
|
|
85
|
-
*/
|
|
86
|
-
addMessages: (locale: string, messages: TranslationDictionary, namespace?: string) => void;
|
|
87
|
-
/**
|
|
88
|
-
* Get all available locales (those with any registered messages).
|
|
89
|
-
*/
|
|
90
|
-
availableLocales: Computed<string[]>;
|
|
91
|
-
}
|
|
92
|
-
//#endregion
|
|
93
|
-
//#region src/context.d.ts
|
|
94
|
-
declare const I18nContext: _pyreon_core0.Context<I18nInstance | null>;
|
|
95
|
-
interface I18nProviderProps extends Props {
|
|
96
|
-
instance: I18nInstance;
|
|
97
|
-
children?: VNodeChild;
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Provide an i18n instance to the component tree.
|
|
101
|
-
*
|
|
102
|
-
* @example
|
|
103
|
-
* const i18n = createI18n({ locale: 'en', messages: { en: { hello: 'Hello' } } })
|
|
104
|
-
*
|
|
105
|
-
* // In JSX:
|
|
106
|
-
* <I18nProvider instance={i18n}>
|
|
107
|
-
* <App />
|
|
108
|
-
* </I18nProvider>
|
|
109
|
-
*/
|
|
110
|
-
declare function I18nProvider(props: I18nProviderProps): VNode;
|
|
111
|
-
/**
|
|
112
|
-
* Access the i18n instance from the nearest I18nProvider.
|
|
113
|
-
* Must be called within a component tree wrapped by I18nProvider.
|
|
114
|
-
*
|
|
115
|
-
* @example
|
|
116
|
-
* function Greeting() {
|
|
117
|
-
* const { t, locale } = useI18n()
|
|
118
|
-
* return <h1>{t('greeting', { name: 'World' })}</h1>
|
|
119
|
-
* }
|
|
120
|
-
*/
|
|
121
|
-
declare function useI18n(): I18nInstance;
|
|
122
|
-
//#endregion
|
|
123
|
-
//#region src/create-i18n.d.ts
|
|
124
|
-
/**
|
|
125
|
-
* Create a reactive i18n instance.
|
|
126
|
-
*
|
|
127
|
-
* @example
|
|
128
|
-
* const i18n = createI18n({
|
|
129
|
-
* locale: 'en',
|
|
130
|
-
* fallbackLocale: 'en',
|
|
131
|
-
* messages: {
|
|
132
|
-
* en: { greeting: 'Hello {{name}}!' },
|
|
133
|
-
* de: { greeting: 'Hallo {{name}}!' },
|
|
134
|
-
* },
|
|
135
|
-
* })
|
|
136
|
-
*
|
|
137
|
-
* // Reactive translation — re-evaluates on locale change
|
|
138
|
-
* i18n.t('greeting', { name: 'Alice' }) // "Hello Alice!"
|
|
139
|
-
* i18n.locale.set('de')
|
|
140
|
-
* i18n.t('greeting', { name: 'Alice' }) // "Hallo Alice!"
|
|
141
|
-
*
|
|
142
|
-
* @example
|
|
143
|
-
* // Async namespace loading
|
|
144
|
-
* const i18n = createI18n({
|
|
145
|
-
* locale: 'en',
|
|
146
|
-
* loader: async (locale, namespace) => {
|
|
147
|
-
* const mod = await import(`./locales/${locale}/${namespace}.json`)
|
|
148
|
-
* return mod.default
|
|
149
|
-
* },
|
|
150
|
-
* })
|
|
151
|
-
* await i18n.loadNamespace('auth')
|
|
152
|
-
* i18n.t('auth:errors.invalid') // looks up "errors.invalid" in "auth" namespace
|
|
153
|
-
*/
|
|
154
|
-
declare function createI18n(options: I18nOptions): I18nInstance;
|
|
155
|
-
//#endregion
|
|
156
|
-
//#region src/interpolation.d.ts
|
|
157
|
-
/**
|
|
158
|
-
* Replace `{{key}}` placeholders in a string with values from the given record.
|
|
159
|
-
* Supports optional whitespace inside braces: `{{ name }}` works too.
|
|
160
|
-
* Unmatched placeholders are left as-is.
|
|
161
|
-
*/
|
|
162
|
-
declare function interpolate(template: string, values?: InterpolationValues): string;
|
|
163
|
-
//#endregion
|
|
164
|
-
//#region src/pluralization.d.ts
|
|
165
|
-
/**
|
|
166
|
-
* Resolve the plural category for a given count and locale.
|
|
167
|
-
*
|
|
168
|
-
* Uses custom rules if provided, otherwise falls back to `Intl.PluralRules`.
|
|
169
|
-
* Returns CLDR plural categories: "zero", "one", "two", "few", "many", "other".
|
|
170
|
-
*/
|
|
171
|
-
declare function resolvePluralCategory(locale: string, count: number, customRules?: PluralRules): string;
|
|
172
|
-
//#endregion
|
|
173
|
-
//#region src/trans.d.ts
|
|
174
|
-
interface RichPart {
|
|
175
|
-
tag: string;
|
|
176
|
-
children: string;
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Parse a translated string into an array of plain text and rich tag segments.
|
|
180
|
-
*
|
|
181
|
-
* @example
|
|
182
|
-
* parseRichText("Hello <bold>world</bold>, click <link>here</link>")
|
|
183
|
-
* // → ["Hello ", { tag: "bold", children: "world" }, ", click ", { tag: "link", children: "here" }]
|
|
184
|
-
*/
|
|
185
|
-
declare function parseRichText(text: string): (string | RichPart)[];
|
|
186
|
-
interface TransProps extends Props {
|
|
187
|
-
/** Translation key (supports namespace:key syntax). */
|
|
188
|
-
i18nKey: string;
|
|
189
|
-
/** Interpolation values for {{placeholder}} syntax. */
|
|
190
|
-
values?: InterpolationValues;
|
|
191
|
-
/**
|
|
192
|
-
* Component map for rich interpolation.
|
|
193
|
-
* Keys match tag names in the translation string.
|
|
194
|
-
* Values are component functions: `(children: any) => VNode`
|
|
195
|
-
*
|
|
196
|
-
* @example
|
|
197
|
-
* // Translation: "Read the <terms>terms</terms> and <privacy>policy</privacy>"
|
|
198
|
-
* components={{
|
|
199
|
-
* terms: (children) => <a href="/terms">{children}</a>,
|
|
200
|
-
* privacy: (children) => <a href="/privacy">{children}</a>,
|
|
201
|
-
* }}
|
|
202
|
-
*/
|
|
203
|
-
components?: Record<string, (children: any) => any>;
|
|
204
|
-
/**
|
|
205
|
-
* The i18n instance's `t` function.
|
|
206
|
-
* Can be obtained from `useI18n()` or passed directly.
|
|
207
|
-
*/
|
|
208
|
-
t: (key: string, values?: InterpolationValues) => string;
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Rich JSX interpolation component for translations.
|
|
212
|
-
*
|
|
213
|
-
* Allows embedding JSX components within translated strings using XML-like tags.
|
|
214
|
-
* The `t` function resolves the translation and interpolates `{{values}}` first,
|
|
215
|
-
* then `<tag>content</tag>` patterns are mapped to the provided components.
|
|
216
|
-
*
|
|
217
|
-
* @example
|
|
218
|
-
* // Translation: "You have <bold>{{count}}</bold> unread messages"
|
|
219
|
-
* const { t } = useI18n()
|
|
220
|
-
* <Trans
|
|
221
|
-
* t={t}
|
|
222
|
-
* i18nKey="messages.unread"
|
|
223
|
-
* values={{ count: 5 }}
|
|
224
|
-
* components={{
|
|
225
|
-
* bold: (children) => <strong>{children}</strong>,
|
|
226
|
-
* }}
|
|
227
|
-
* />
|
|
228
|
-
* // Renders: You have <strong>5</strong> unread messages
|
|
229
|
-
*
|
|
230
|
-
* @example
|
|
231
|
-
* // Translation: "Read our <terms>terms of service</terms> and <privacy>privacy policy</privacy>"
|
|
232
|
-
* <Trans
|
|
233
|
-
* t={t}
|
|
234
|
-
* i18nKey="legal"
|
|
235
|
-
* components={{
|
|
236
|
-
* terms: (children) => <a href="/terms">{children}</a>,
|
|
237
|
-
* privacy: (children) => <a href="/privacy">{children}</a>,
|
|
238
|
-
* }}
|
|
239
|
-
* />
|
|
240
|
-
*/
|
|
241
|
-
declare function Trans(props: TransProps): VNode | string;
|
|
242
|
-
//#endregion
|
|
243
|
-
export { I18nContext, type I18nInstance, type I18nOptions, I18nProvider, type I18nProviderProps, type InterpolationValues, type NamespaceLoader, type PluralRules, Trans, type TransProps, type TranslationDictionary, type TranslationMessages, createI18n, interpolate, parseRichText, resolvePluralCategory, useI18n };
|
|
244
|
-
//# sourceMappingURL=index2.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index2.d.ts","names":[],"sources":["../../src/types.ts","../../src/context.ts","../../src/create-i18n.ts","../../src/interpolation.ts","../../src/pluralization.ts","../../src/trans.tsx"],"mappings":";;;;;;KAGY,qBAAA;EAAA,CACT,GAAA,oBAAuB,qBAAA;AAAA;AAD1B;AAAA,KAKY,mBAAA,GAAsB,MAAA,SAAe,qBAAA;;;;AAAjD;KAMY,eAAA,IACV,MAAA,UACA,SAAA,aACG,OAAA,CAAQ,qBAAA;;KAGD,mBAAA,GAAsB,MAAA;;KAGtB,WAAA,GAAc,MAAA,UAAgB,KAAA;;UAGzB,WAAA;EATL;EAWV,MAAA;EAZA;EAcA,cAAA;EAbW;EAeX,QAAA,GAAW,MAAA,SAAe,qBAAA;EAfM;AAGlC;;;EAiBE,MAAA,GAAS,eAAA;EAjB6B;AAGxC;;;EAmBE,gBAAA;EAnBqD;AAGvD;;;EAqBE,WAAA,GAAc,WAAA;EAfH;;;;EAoBX,YAAA,IACE,MAAA,UACA,GAAA,UACA,SAAA;AAAA;;UAKa,YAAA;EA5BJ;;;;;;;;;;EAuCX,CAAA,GAAI,GAAA,UAAa,MAAA,GAAS,mBAAA;EAhBN;EAmBpB,MAAA,EAAQ,MAAA;EAdO;;;;EAoBf,aAAA,GAAgB,SAAA,UAAmB,MAAA,cAAoB,OAAA;EAAA;;;EAKvD,SAAA,EAAW,QAAA;EAkBC;;;EAbZ,gBAAA,EAAkB,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