@fluentic/i18n 0.0.0-stage → 0.1.0-beta.2
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 +23 -0
- package/LICENSE +21 -0
- package/README.md +29 -2
- package/dist/adapter.d.ts +6 -0
- package/dist/adapter.js +5 -0
- package/dist/compact-E3zj3Z8d.js +34 -0
- package/dist/compact-E3zj3Z8d.js.map +1 -0
- package/dist/compiled.d.ts +29 -0
- package/dist/compiled.js +53 -0
- package/dist/compiled.js.map +1 -0
- package/dist/compiler-support.d.ts +4 -0
- package/dist/compiler-support.js +4 -0
- package/dist/core/format.d.ts +11 -0
- package/dist/core/i18n.d.ts +26 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +5 -0
- package/dist/core/types.d.ts +79 -0
- package/dist/hash-VJlyT31Z.js +90 -0
- package/dist/hash-VJlyT31Z.js.map +1 -0
- package/dist/hydration-store-BK6UxrtJ.js +13 -0
- package/dist/hydration-store-BK6UxrtJ.js.map +1 -0
- package/dist/hydration-store.d.ts +29 -0
- package/dist/hydration.d.ts +7 -0
- package/dist/hydration.js +35 -0
- package/dist/hydration.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5 -0
- package/dist/interpreter-BfzvLLrD.js +187 -0
- package/dist/interpreter-BfzvLLrD.js.map +1 -0
- package/dist/manager-BgMJWXWY.js +183 -0
- package/dist/manager-BgMJWXWY.js.map +1 -0
- package/dist/manager.d.ts +62 -0
- package/dist/message-D4jK2Lzc.js +9 -0
- package/dist/message-D4jK2Lzc.js.map +1 -0
- package/dist/react.d.ts +48 -0
- package/dist/react.js +64 -0
- package/dist/react.js.map +1 -0
- package/dist/registry-manager.d.ts +7 -0
- package/dist/runtime-DXnGeCBw.js +110 -0
- package/dist/runtime-DXnGeCBw.js.map +1 -0
- package/dist/shared/compact.d.ts +5 -0
- package/dist/shared/formatting.d.ts +11 -0
- package/dist/shared/hash.d.ts +7 -0
- package/dist/shared/interpreter.d.ts +2 -0
- package/dist/shared/message.d.ts +2 -0
- package/dist/shared/runtime.d.ts +49 -0
- package/dist/src-ON68XIog.js +144 -0
- package/dist/src-ON68XIog.js.map +1 -0
- package/dist/state-manager.d.ts +6 -0
- package/dist/state.d.ts +8 -0
- package/dist/storage-manager.d.ts +14 -0
- package/dist/translation-manager-BkmFnlb_.js +114 -0
- package/dist/translation-manager-BkmFnlb_.js.map +1 -0
- package/dist/translation-manager.d.ts +34 -0
- package/dist/translation.d.ts +20 -0
- package/dist/types-BkDsJfVD.js +13 -0
- package/dist/types-BkDsJfVD.js.map +1 -0
- package/dist/types.d.ts +3 -0
- package/dist/types.js +1 -0
- package/package.json +96 -3
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// src/shared/runtime.ts
|
|
2
|
+
function createRuntime() {
|
|
3
|
+
return {
|
|
4
|
+
languageId: "en",
|
|
5
|
+
ready: false,
|
|
6
|
+
translationsEnabled: true,
|
|
7
|
+
translationGetter: null,
|
|
8
|
+
formats: {},
|
|
9
|
+
registry: {
|
|
10
|
+
number: {},
|
|
11
|
+
date: {},
|
|
12
|
+
time: {}
|
|
13
|
+
},
|
|
14
|
+
jsxLineBreak: "\n",
|
|
15
|
+
jsxHandler: null,
|
|
16
|
+
jsxCallbackHandler: null,
|
|
17
|
+
tagElement: null,
|
|
18
|
+
subscribe: () => () => {},
|
|
19
|
+
getSnapshot: () => null
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
const defaultRuntime = createRuntime();
|
|
23
|
+
let resolver = () => defaultRuntime;
|
|
24
|
+
function setRuntimeResolver(next) {
|
|
25
|
+
resolver = next;
|
|
26
|
+
}
|
|
27
|
+
function getRuntime() {
|
|
28
|
+
return resolver();
|
|
29
|
+
}
|
|
30
|
+
function setTranslationGetter(getter) {
|
|
31
|
+
getRuntime().translationGetter = getter;
|
|
32
|
+
}
|
|
33
|
+
function setLanguageId(languageId) {
|
|
34
|
+
getRuntime().languageId = languageId;
|
|
35
|
+
}
|
|
36
|
+
function setFormats(formats) {
|
|
37
|
+
getRuntime().formats = formats;
|
|
38
|
+
}
|
|
39
|
+
function setGetJsxHandler(handler) {
|
|
40
|
+
getRuntime().jsxHandler = handler;
|
|
41
|
+
}
|
|
42
|
+
function renderJsxCallback(render) {
|
|
43
|
+
const handler = getRuntime().jsxCallbackHandler;
|
|
44
|
+
if (!handler) throw new Error("[i18n] t.jsx callbacks require a platform adapter");
|
|
45
|
+
return handler(render);
|
|
46
|
+
}
|
|
47
|
+
const I18nRegistry = {
|
|
48
|
+
addNumberFormat(name, format) {
|
|
49
|
+
getRuntime().registry.number[name] = format;
|
|
50
|
+
},
|
|
51
|
+
addDateFormat(name, format) {
|
|
52
|
+
getRuntime().registry.date[name] = format;
|
|
53
|
+
},
|
|
54
|
+
addTimeFormat(name, format) {
|
|
55
|
+
getRuntime().registry.time[name] = format;
|
|
56
|
+
},
|
|
57
|
+
setLanguageId,
|
|
58
|
+
getConfig() {
|
|
59
|
+
const r = getRuntime().registry;
|
|
60
|
+
return {
|
|
61
|
+
number: { ...r.number },
|
|
62
|
+
date: { ...r.date },
|
|
63
|
+
time: { ...r.time }
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
function getFormats(runtime, languageId) {
|
|
68
|
+
const resolve = (entries) => Object.fromEntries(Object.entries(entries).map(([key, value]) => [key, typeof value === "function" ? value({ languageId }) : value]));
|
|
69
|
+
return {
|
|
70
|
+
number: {
|
|
71
|
+
...runtime.formats.number,
|
|
72
|
+
...resolve(runtime.registry.number)
|
|
73
|
+
},
|
|
74
|
+
date: {
|
|
75
|
+
...runtime.formats.date,
|
|
76
|
+
...resolve(runtime.registry.date)
|
|
77
|
+
},
|
|
78
|
+
time: {
|
|
79
|
+
...runtime.formats.time,
|
|
80
|
+
...resolve(runtime.registry.time)
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function noTranslate(fn) {
|
|
85
|
+
const runtime = getRuntime();
|
|
86
|
+
const previous = runtime.translationsEnabled;
|
|
87
|
+
runtime.translationsEnabled = false;
|
|
88
|
+
try {
|
|
89
|
+
return fn();
|
|
90
|
+
} finally {
|
|
91
|
+
runtime.translationsEnabled = previous;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function createTagHandler(handler) {
|
|
95
|
+
Object.defineProperty(handler, "__fluenticTagHandler", { value: true });
|
|
96
|
+
return handler;
|
|
97
|
+
}
|
|
98
|
+
/** Synchronous binding only: async callers must keep and use their own instance. */
|
|
99
|
+
function withRuntime(runtime, fn) {
|
|
100
|
+
const previous = resolver;
|
|
101
|
+
resolver = () => runtime;
|
|
102
|
+
try {
|
|
103
|
+
return fn();
|
|
104
|
+
} finally {
|
|
105
|
+
resolver = previous;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
export { getFormats as a, renderJsxCallback as c, setLanguageId as d, setRuntimeResolver as f, defaultRuntime as i, setFormats as l, withRuntime as m, createRuntime as n, getRuntime as o, setTranslationGetter as p, createTagHandler as r, noTranslate as s, I18nRegistry as t, setGetJsxHandler as u };
|
|
109
|
+
|
|
110
|
+
//# sourceMappingURL=runtime-DXnGeCBw.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime-DXnGeCBw.js","names":[],"sources":["../src/shared/runtime.ts"],"sourcesContent":["import type { MessageFormatOptions, MessageTagHandler, TranslationGetter } from '../core/types';\n\nexport type Formats = {\n number: Record<string, Intl.NumberFormatOptions>;\n date: Record<string, Intl.DateTimeFormatOptions>;\n time: Record<string, Intl.DateTimeFormatOptions>;\n};\n\nexport type FormatContext = { languageId: string; };\nexport type FormatFactory<T> = T | ((context: FormatContext) => T);\nexport type RegistryConfig = { [K in keyof Formats]: Record<string, FormatFactory<Formats[K][string]>>; };\nexport type PartsRenderer = (runtime?: Runtime) => unknown[];\n\nexport type Runtime = {\n languageId: string;\n ready: boolean;\n translationsEnabled: boolean;\n translationGetter: TranslationGetter | null;\n formats: Partial<Formats>;\n registry: RegistryConfig;\n jsxLineBreak: unknown;\n jsxHandler: ((options: MessageFormatOptions, render: PartsRenderer) => unknown) | null;\n jsxCallbackHandler: ((render: () => unknown) => unknown) | null;\n tagElement: ((element: unknown) => (chunks: unknown[]) => unknown) | null;\n subscribe: (listener: () => void) => () => void;\n getSnapshot: () => unknown;\n};\n\nexport function createRuntime(): Runtime {\n return {\n languageId: 'en',\n ready: false,\n translationsEnabled: true,\n translationGetter: null,\n formats: {},\n registry: { number: {}, date: {}, time: {} },\n jsxLineBreak: '\\n',\n jsxHandler: null,\n jsxCallbackHandler: null,\n tagElement: null,\n subscribe: () => () => {},\n getSnapshot: () => null,\n };\n}\n\nexport const defaultRuntime = createRuntime();\nlet resolver = () => defaultRuntime;\n\nexport function setRuntimeResolver(next: () => Runtime): void {\n resolver = next;\n}\n\nexport function getRuntime(): Runtime {\n return resolver();\n}\n\nexport function setTranslationGetter(getter: TranslationGetter | null): void {\n getRuntime().translationGetter = getter;\n}\n\nexport function setLanguageId(languageId: string): void {\n getRuntime().languageId = languageId;\n}\n\nexport function setFormats(formats: Partial<Formats>): void {\n getRuntime().formats = formats;\n}\n\nexport function setGetJsxHandler(handler: Runtime['jsxHandler']): void {\n getRuntime().jsxHandler = handler;\n}\n\nexport function renderJsxCallback(render: () => unknown): unknown {\n const handler = getRuntime().jsxCallbackHandler;\n\n if (!handler) throw new Error('[i18n] t.jsx callbacks require a platform adapter');\n\n return handler(render);\n}\n\nexport const I18nRegistry = {\n addNumberFormat(name: string, format: FormatFactory<Intl.NumberFormatOptions>) {\n getRuntime().registry.number[name] = format;\n },\n addDateFormat(name: string, format: FormatFactory<Intl.DateTimeFormatOptions>) {\n getRuntime().registry.date[name] = format;\n },\n addTimeFormat(name: string, format: FormatFactory<Intl.DateTimeFormatOptions>) {\n getRuntime().registry.time[name] = format;\n },\n setLanguageId,\n getConfig(): RegistryConfig {\n const r = getRuntime().registry;\n\n return { number: { ...r.number }, date: { ...r.date }, time: { ...r.time } };\n },\n};\n\nexport function getFormats(runtime: Runtime, languageId: string): Partial<Formats> {\n const resolve = <T>(entries: Record<string, FormatFactory<T>>) =>\n Object.fromEntries(\n Object.entries(entries).map((\n [key, value],\n ) => [key, typeof value === 'function' ? (value as (context: FormatContext) => T)({ languageId }) : value]),\n );\n\n return {\n number: { ...runtime.formats.number, ...resolve(runtime.registry.number) },\n date: { ...runtime.formats.date, ...resolve(runtime.registry.date) },\n time: { ...runtime.formats.time, ...resolve(runtime.registry.time) },\n };\n}\n\nexport function noTranslate<T>(fn: () => T): T {\n const runtime = getRuntime();\n const previous = runtime.translationsEnabled;\n runtime.translationsEnabled = false;\n\n try {\n return fn();\n } finally {\n runtime.translationsEnabled = previous;\n }\n}\n\nexport function createTagHandler<T, R>(handler: (chunks: Array<string | T>) => R): MessageTagHandler<T, R> {\n Object.defineProperty(handler, '__fluenticTagHandler', { value: true });\n\n return handler as MessageTagHandler<T, R>;\n}\n/** Synchronous binding only: async callers must keep and use their own instance. */\nexport function withRuntime<T>(runtime: Runtime, fn: () => T): T {\n const previous = resolver;\n resolver = () => runtime;\n\n try {\n return fn();\n } finally {\n resolver = previous;\n }\n}\n"],"mappings":";AA4BA,SAAgB,gBAAyB;CACvC,OAAO;EACL,YAAY;EACZ,OAAO;EACP,qBAAqB;EACrB,mBAAmB;EACnB,SAAS,CAAC;EACV,UAAU;GAAE,QAAQ,CAAC;GAAG,MAAM,CAAC;GAAG,MAAM,CAAC;EAAE;EAC3C,cAAc;EACd,YAAY;EACZ,oBAAoB;EACpB,YAAY;EACZ,uBAAuB,CAAC;EACxB,mBAAmB;CACrB;AACF;AAEA,MAAa,iBAAiB,cAAc;AAC5C,IAAI,iBAAiB;AAErB,SAAgB,mBAAmB,MAA2B;CAC5D,WAAW;AACb;AAEA,SAAgB,aAAsB;CACpC,OAAO,SAAS;AAClB;AAEA,SAAgB,qBAAqB,QAAwC;CAC3E,WAAW,CAAC,CAAC,oBAAoB;AACnC;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SAAS,IAAI;EAAE;CACrE;AACF;AAEA,SAAgB,YAAe,IAAgB;CAC7C,MAAM,UAAU,WAAW;CAC3B,MAAM,WAAW,QAAQ;CACzB,QAAQ,sBAAsB;CAE9B,IAAI;EACF,OAAO,GAAG;CACZ,UAAU;EACR,QAAQ,sBAAsB;CAChC;AACF;AAEA,SAAgB,iBAAuB,SAAoE;CACzG,OAAO,eAAe,SAAS,wBAAwB,EAAE,OAAO,KAAK,CAAC;CAEtE,OAAO;AACT;;AAEA,SAAgB,YAAe,SAAkB,IAAgB;CAC/D,MAAM,WAAW;CACjB,iBAAiB;CAEjB,IAAI;EACF,OAAO,GAAG;CACZ,UAAU;EACR,WAAW;CACb;AACF"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** Compact message format v1. Strings are literal messages or literal parts. */
|
|
2
|
+
export type CompactMessage = string | CompactPart[];
|
|
3
|
+
export type CompactPart = string | [0, string] | [1, string, string | Record<string, unknown> | null] | [2 | 3, string, string | Record<string, unknown> | null] | [4, string, Record<string, CompactMessage>] | [5, string, number, 0 | 1, Record<string, CompactMessage>] | [6] | [7, string, CompactMessage];
|
|
4
|
+
export declare const COMPACT_VERSION = 1;
|
|
5
|
+
export declare function validateCompact(value: unknown, depth?: number): asserts value is CompactMessage;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { MessageFormatOptions } from '../core/types';
|
|
2
|
+
import type { CompactMessage } from './compact';
|
|
3
|
+
import { type Formats, type Runtime } from './runtime';
|
|
4
|
+
export type Engine = (message: CompactMessage, languageId: string, formats: Partial<Formats>, values: Record<string, unknown>, compiled: boolean) => unknown[];
|
|
5
|
+
export type Formatting = ReturnType<typeof createFormatting>;
|
|
6
|
+
export declare function createFormatting(engine: Engine, compiled?: boolean, getCompiledId?: (context: string | null, message: string) => string): {
|
|
7
|
+
getText: (options: MessageFormatOptions, runtime?: Runtime) => string;
|
|
8
|
+
getJsx: (options: MessageFormatOptions) => unknown;
|
|
9
|
+
getMessageParts: (options: MessageFormatOptions, runtime?: Runtime, jsx?: boolean) => unknown[];
|
|
10
|
+
formatMessage: (options: MessageFormatOptions, runtime?: Runtime) => string;
|
|
11
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const DEFAULT_HASH_LENGTH = 8;
|
|
2
|
+
export declare const MIN_HASH_LENGTH = 6;
|
|
3
|
+
export declare const MAX_HASH_LENGTH = 22;
|
|
4
|
+
export declare function normalizeHashLength(length?: number): number;
|
|
5
|
+
/** MurmurHash3 x64 128-bit, encoded as unpadded base64url. */
|
|
6
|
+
export declare function murmurHash128(value: string): string;
|
|
7
|
+
export declare function messageHash(identity: string, length?: number): string;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { MessageFormatOptions, MessageTagHandler, TranslationGetter } from '../core/types';
|
|
2
|
+
export type Formats = {
|
|
3
|
+
number: Record<string, Intl.NumberFormatOptions>;
|
|
4
|
+
date: Record<string, Intl.DateTimeFormatOptions>;
|
|
5
|
+
time: Record<string, Intl.DateTimeFormatOptions>;
|
|
6
|
+
};
|
|
7
|
+
export type FormatContext = {
|
|
8
|
+
languageId: string;
|
|
9
|
+
};
|
|
10
|
+
export type FormatFactory<T> = T | ((context: FormatContext) => T);
|
|
11
|
+
export type RegistryConfig = {
|
|
12
|
+
[K in keyof Formats]: Record<string, FormatFactory<Formats[K][string]>>;
|
|
13
|
+
};
|
|
14
|
+
export type PartsRenderer = (runtime?: Runtime) => unknown[];
|
|
15
|
+
export type Runtime = {
|
|
16
|
+
languageId: string;
|
|
17
|
+
ready: boolean;
|
|
18
|
+
translationsEnabled: boolean;
|
|
19
|
+
translationGetter: TranslationGetter | null;
|
|
20
|
+
formats: Partial<Formats>;
|
|
21
|
+
registry: RegistryConfig;
|
|
22
|
+
jsxLineBreak: unknown;
|
|
23
|
+
jsxHandler: ((options: MessageFormatOptions, render: PartsRenderer) => unknown) | null;
|
|
24
|
+
jsxCallbackHandler: ((render: () => unknown) => unknown) | null;
|
|
25
|
+
tagElement: ((element: unknown) => (chunks: unknown[]) => unknown) | null;
|
|
26
|
+
subscribe: (listener: () => void) => () => void;
|
|
27
|
+
getSnapshot: () => unknown;
|
|
28
|
+
};
|
|
29
|
+
export declare function createRuntime(): Runtime;
|
|
30
|
+
export declare const defaultRuntime: Runtime;
|
|
31
|
+
export declare function setRuntimeResolver(next: () => Runtime): void;
|
|
32
|
+
export declare function getRuntime(): Runtime;
|
|
33
|
+
export declare function setTranslationGetter(getter: TranslationGetter | null): void;
|
|
34
|
+
export declare function setLanguageId(languageId: string): void;
|
|
35
|
+
export declare function setFormats(formats: Partial<Formats>): void;
|
|
36
|
+
export declare function setGetJsxHandler(handler: Runtime['jsxHandler']): void;
|
|
37
|
+
export declare function renderJsxCallback(render: () => unknown): unknown;
|
|
38
|
+
export declare const I18nRegistry: {
|
|
39
|
+
addNumberFormat(name: string, format: FormatFactory<Intl.NumberFormatOptions>): void;
|
|
40
|
+
addDateFormat(name: string, format: FormatFactory<Intl.DateTimeFormatOptions>): void;
|
|
41
|
+
addTimeFormat(name: string, format: FormatFactory<Intl.DateTimeFormatOptions>): void;
|
|
42
|
+
setLanguageId: typeof setLanguageId;
|
|
43
|
+
getConfig(): RegistryConfig;
|
|
44
|
+
};
|
|
45
|
+
export declare function getFormats(runtime: Runtime, languageId: string): Partial<Formats>;
|
|
46
|
+
export declare function noTranslate<T>(fn: () => T): T;
|
|
47
|
+
export declare function createTagHandler<T, R>(handler: (chunks: Array<string | T>) => R): MessageTagHandler<T, R>;
|
|
48
|
+
/** Synchronous binding only: async callers must keep and use their own instance. */
|
|
49
|
+
export declare function withRuntime<T>(runtime: Runtime, fn: () => T): T;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { t as normalizeMessage } from "./message-D4jK2Lzc.js";
|
|
2
|
+
import { a as getFormats, c as renderJsxCallback, d as setLanguageId, f as setRuntimeResolver, i as defaultRuntime, l as setFormats, m as withRuntime, n as createRuntime, o as getRuntime, p as setTranslationGetter, r as createTagHandler, s as noTranslate, t as I18nRegistry, u as setGetJsxHandler } from "./runtime-DXnGeCBw.js";
|
|
3
|
+
import { t as isMessageDescriptor } from "./types-BkDsJfVD.js";
|
|
4
|
+
import { n as createFormatting, t as interpret } from "./interpreter-BfzvLLrD.js";
|
|
5
|
+
import { i as messageHash } from "./hash-VJlyT31Z.js";
|
|
6
|
+
import { IntlMessageFormat } from "intl-messageformat";
|
|
7
|
+
// \0rolldown/runtime.js
|
|
8
|
+
var __defProp = Object.defineProperty;
|
|
9
|
+
var __exportAll = (all, no_symbols) => {
|
|
10
|
+
let target = {};
|
|
11
|
+
for (var name in all) __defProp(target, name, {
|
|
12
|
+
get: all[name],
|
|
13
|
+
enumerable: true
|
|
14
|
+
});
|
|
15
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
16
|
+
return target;
|
|
17
|
+
};
|
|
18
|
+
// src/core/i18n.ts
|
|
19
|
+
function createT({ getText, getJsx }) {
|
|
20
|
+
const createScope = (scope) => {
|
|
21
|
+
return {
|
|
22
|
+
context(name) {
|
|
23
|
+
return createScope({
|
|
24
|
+
...scope,
|
|
25
|
+
context: scope.context ? `${scope.context}.${name}` : name
|
|
26
|
+
});
|
|
27
|
+
},
|
|
28
|
+
note(note) {
|
|
29
|
+
return createScope({
|
|
30
|
+
...scope,
|
|
31
|
+
notes: [...scope.notes, note]
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
meta(meta) {
|
|
35
|
+
return createScope({
|
|
36
|
+
...scope,
|
|
37
|
+
meta
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
msg(message, schema) {
|
|
41
|
+
return createDescriptor(message, schema, scope);
|
|
42
|
+
},
|
|
43
|
+
text(message, params) {
|
|
44
|
+
return getText(toFormatOptions(message, params, scope));
|
|
45
|
+
},
|
|
46
|
+
jsx(message, params) {
|
|
47
|
+
if (typeof message === "function") return renderJsxCallback(message);
|
|
48
|
+
return getJsx(toFormatOptions(message, params, scope));
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
const createDescriptor = (message, schema, scope) => {
|
|
53
|
+
return Object.freeze({
|
|
54
|
+
__fluenticMessage: true,
|
|
55
|
+
text: Array.isArray(message) ? message[0] : normalizeMessage(message),
|
|
56
|
+
...Array.isArray(message) ? { compiled: message } : {},
|
|
57
|
+
context: scope.context,
|
|
58
|
+
schema: schema ?? {},
|
|
59
|
+
notes: scope.notes,
|
|
60
|
+
meta: scope.meta
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
const toFormatOptions = (message, params, scope) => {
|
|
64
|
+
if (isMessageDescriptor(message)) return {
|
|
65
|
+
msg: message,
|
|
66
|
+
params
|
|
67
|
+
};
|
|
68
|
+
return {
|
|
69
|
+
msg: createDescriptor(message, void 0, scope),
|
|
70
|
+
params
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
const rootScope = createScope({
|
|
74
|
+
context: null,
|
|
75
|
+
notes: []
|
|
76
|
+
});
|
|
77
|
+
return Object.assign(rootScope, {
|
|
78
|
+
clone(message) {
|
|
79
|
+
return Object.freeze({ ...message });
|
|
80
|
+
},
|
|
81
|
+
params(params) {
|
|
82
|
+
return params ?? {};
|
|
83
|
+
},
|
|
84
|
+
tag(handler) {
|
|
85
|
+
if (typeof handler !== "function") {
|
|
86
|
+
const adapt = getRuntime().tagElement;
|
|
87
|
+
if (!adapt) throw new Error("[i18n] element tags require a platform adapter");
|
|
88
|
+
return createTagHandler(adapt(handler));
|
|
89
|
+
}
|
|
90
|
+
return createTagHandler(handler);
|
|
91
|
+
},
|
|
92
|
+
try(renderer, value, params) {
|
|
93
|
+
return isMessageDescriptor(value) ? renderer(value, params) : value;
|
|
94
|
+
},
|
|
95
|
+
notranslate: noTranslate
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
// src/core/format.ts
|
|
99
|
+
var format_exports = /* @__PURE__ */ __exportAll({
|
|
100
|
+
I18nRegistry: () => I18nRegistry,
|
|
101
|
+
createRuntime: () => createRuntime,
|
|
102
|
+
createTagHandler: () => createTagHandler,
|
|
103
|
+
defaultRuntime: () => defaultRuntime,
|
|
104
|
+
formatMessage: () => formatMessage,
|
|
105
|
+
getFormats: () => getFormats,
|
|
106
|
+
getJsx: () => getJsx,
|
|
107
|
+
getMessageParts: () => getMessageParts,
|
|
108
|
+
getRuntime: () => getRuntime,
|
|
109
|
+
getText: () => getText,
|
|
110
|
+
noTranslate: () => noTranslate,
|
|
111
|
+
renderJsxCallback: () => renderJsxCallback,
|
|
112
|
+
setFormats: () => setFormats,
|
|
113
|
+
setGetJsxHandler: () => setGetJsxHandler,
|
|
114
|
+
setLanguageId: () => setLanguageId,
|
|
115
|
+
setRuntimeResolver: () => setRuntimeResolver,
|
|
116
|
+
setTranslationGetter: () => setTranslationGetter,
|
|
117
|
+
withRuntime: () => withRuntime
|
|
118
|
+
});
|
|
119
|
+
const cache = /* @__PURE__ */ new Map();
|
|
120
|
+
const formatting = /* @__PURE__ */ createFormatting((message, languageId, formats, values, compiled) => {
|
|
121
|
+
if (compiled) return interpret(message, languageId, formats, values, true);
|
|
122
|
+
if (typeof message !== "string") throw new TypeError("[i18n] invalid translation");
|
|
123
|
+
const key = JSON.stringify([
|
|
124
|
+
message,
|
|
125
|
+
languageId,
|
|
126
|
+
formats
|
|
127
|
+
]);
|
|
128
|
+
let formatter = cache.get(key);
|
|
129
|
+
if (!formatter) {
|
|
130
|
+
formatter = new IntlMessageFormat(message, languageId, formats);
|
|
131
|
+
if (cache.size >= 500) cache.delete(cache.keys().next().value);
|
|
132
|
+
cache.set(key, formatter);
|
|
133
|
+
}
|
|
134
|
+
return formatter.formatToParts(values).map((part) => part.value);
|
|
135
|
+
}, false, (context, message) => messageHash(context ? `${context}\0${message}` : message));
|
|
136
|
+
const getText = (...args) => formatting.getText(...args);
|
|
137
|
+
const getJsx = (...args) => formatting.getJsx(...args);
|
|
138
|
+
const getMessageParts = (...args) => formatting.getMessageParts(...args);
|
|
139
|
+
const formatMessage = getText;
|
|
140
|
+
// src/index.ts
|
|
141
|
+
const t = /* @__PURE__ */ createT(format_exports);
|
|
142
|
+
export { t };
|
|
143
|
+
|
|
144
|
+
//# sourceMappingURL=src-ON68XIog.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"src-ON68XIog.js","names":["formatting"],"sources":["../src/core/i18n.ts","../src/core/format.ts","../src/index.ts"],"sourcesContent":["import type { Formatting } from '../shared/formatting';\nimport { normalizeMessage } from '../shared/message';\nimport { createTagHandler, getRuntime, noTranslate, renderJsxCallback } from '../shared/runtime';\nimport type { I18nJsxOutput, I18nTagElement, I18nTagValue } from './types';\nimport { isMessageDescriptor } from './types';\nimport type {\n JsxRenderer,\n MessageDescriptor,\n MessageInput,\n MessageMeta,\n MessageParams,\n MessageSchema,\n MessageTagHandler,\n TextRenderer,\n TryRenderArgs,\n} from './types';\n\ntype MessageScope = { context: string | null; notes: string[]; meta?: MessageMeta; };\n\nexport type MessageApi<JsxOutput = unknown> = {\n context(name: string): MessageApi<JsxOutput>;\n note(note: string): MessageApi<JsxOutput>;\n meta(meta: MessageMeta): MessageApi<JsxOutput>;\n msg<const Text extends string, const Schema extends MessageSchema | undefined = undefined>(\n message: Text,\n schema?: Schema,\n ): MessageDescriptor<Text, Schema>;\n text: TextRenderer;\n jsx: JsxRenderer<JsxOutput>;\n};\n\nexport type TApi<JsxOutput = unknown, TagValue = unknown> = MessageApi<JsxOutput> & {\n clone<const Text extends string, const Schema extends MessageSchema | undefined>(\n message: MessageDescriptor<Text, Schema>,\n ): MessageDescriptor<Text, Schema>;\n params<Params extends MessageParams>(params?: Params | null): Params;\n tag(element: I18nTagElement): MessageTagHandler<I18nTagValue, I18nTagElement>;\n tag<T = TagValue, R = TagValue>(handler: (chunks: Array<string | T>) => R): MessageTagHandler<T, R>;\n try<T>(\n renderer: TextRenderer,\n value: T,\n ...args: TryRenderArgs<T>\n ): T extends { readonly __fluenticMessage: true; } ? string : T;\n try<T>(\n renderer: JsxRenderer<JsxOutput>,\n value: T,\n ...args: TryRenderArgs<T>\n ): T extends { readonly __fluenticMessage: true; } ? JsxOutput : T;\n notranslate: typeof noTranslate;\n};\n\nexport function createT({ getText, getJsx }: Formatting): TApi<I18nJsxOutput, I18nTagValue> {\n const createScope = (scope: MessageScope): MessageApi => {\n return {\n context(name) {\n return createScope({ ...scope, context: scope.context ? `${scope.context}.${name}` : name });\n },\n note(note) {\n return createScope({ ...scope, notes: [...scope.notes, note] });\n },\n meta(meta) {\n return createScope({ ...scope, meta });\n },\n msg(message, schema) {\n return createDescriptor(message, schema, scope);\n },\n text(message: MessageInput, params?: MessageParams) {\n return getText(toFormatOptions(message, params, scope));\n },\n jsx(message: MessageInput | (() => I18nTagElement), params?: MessageParams) {\n if (typeof message === 'function') return renderJsxCallback(message);\n\n return getJsx(toFormatOptions(message, params, scope));\n },\n };\n };\n\n const createDescriptor = <const Text extends string, const Schema extends MessageSchema | undefined>(\n message: Text,\n schema: Schema | undefined,\n scope: MessageScope,\n ): MessageDescriptor<Text, Schema> => {\n return Object.freeze({\n __fluenticMessage: true,\n text: Array.isArray(message) ? message[0] : normalizeMessage(message),\n ...(Array.isArray(message) ? { compiled: message } : {}),\n context: scope.context,\n schema: schema ?? {},\n notes: scope.notes,\n meta: scope.meta,\n }) as unknown as MessageDescriptor<Text, Schema>;\n };\n\n const toFormatOptions = (message: MessageInput, params: MessageParams | undefined, scope: MessageScope) => {\n if (isMessageDescriptor(message)) return { msg: message, params };\n\n return { msg: createDescriptor(message, undefined, scope), params };\n };\n\n const rootScope = createScope({ context: null, notes: [] });\n\n return Object.assign(rootScope, {\n clone(message: MessageDescriptor) {\n return Object.freeze({ ...message });\n },\n params<Params extends MessageParams>(params?: Params | null): Params {\n return (params ?? {}) as Params;\n },\n tag<T = unknown, R = unknown>(handler: (chunks: Array<string | T>) => R) {\n if (typeof handler !== 'function') {\n const adapt = getRuntime().tagElement;\n\n if (!adapt) throw new Error('[i18n] element tags require a platform adapter');\n\n return createTagHandler(adapt(handler));\n }\n\n return createTagHandler(handler);\n },\n try<T>(renderer: TextRenderer | JsxRenderer, value: T, params?: MessageParams) {\n return isMessageDescriptor(value)\n ? (renderer as (message: MessageInput, params?: MessageParams) => unknown)(value, params)\n : value;\n },\n notranslate: noTranslate,\n }) as TApi<I18nJsxOutput, I18nTagValue>;\n}\n","import { IntlMessageFormat } from 'intl-messageformat';\nimport { createFormatting } from '../shared/formatting';\nimport { messageHash } from '../shared/hash';\nimport { interpret } from '../shared/interpreter';\n\nconst cache = new Map<string, IntlMessageFormat>();\nconst formatting = /* @__PURE__ */ createFormatting(\n (message, languageId, formats, values, compiled) => {\n if (compiled) return interpret(message, languageId, formats, values, true);\n if (typeof message !== 'string') throw new TypeError('[i18n] invalid translation');\n\n const key = JSON.stringify([message, languageId, formats]);\n\n let formatter = cache.get(key);\n\n if (!formatter) {\n formatter = new IntlMessageFormat(\n message,\n languageId,\n formats as ConstructorParameters<typeof IntlMessageFormat>[2],\n );\n if (cache.size >= 500) cache.delete(cache.keys().next().value!);\n cache.set(key, formatter);\n }\n\n return formatter.formatToParts(values as Parameters<IntlMessageFormat['formatToParts']>[0]).map((part) =>\n part.value\n );\n },\n false,\n (context, message) => messageHash(context ? `${context}\\0${message}` : message),\n);\nexport * from '../shared/runtime';\n\nexport const getText = (...args: Parameters<typeof formatting.getText>) => formatting.getText(...args);\nexport const getJsx = (...args: Parameters<typeof formatting.getJsx>) => formatting.getJsx(...args);\nexport const getMessageParts = (...args: Parameters<typeof formatting.getMessageParts>) =>\n formatting.getMessageParts(...args);\nexport const formatMessage = getText;\n","import * as formatting from './core/format';\nimport { createT } from './core/i18n';\n\nexport const t = /* @__PURE__ */ createT(formatting);\nexport type { MessageApi, TApi } from './core/i18n';\nexport * from './core/types';\nexport { I18nRegistry, noTranslate, setFormats } from './shared/runtime';\nexport { defineTranslationLoader } from './translation-manager';\nexport type { TranslationData, TranslationItem, TranslationLoader, TranslationLoaderMap }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AAW,QAAQ,GAAG,IAAI;AACrG,MAAa,UAAU,GAAG,SAA+C,WAAW,OAAO,GAAG,IAAI;AAClG,MAAa,mBAAmB,GAAG,SACjC,WAAW,gBAAgB,GAAG,IAAI;AACpC,MAAa,gBAAgB;;ACnC7B,MAAa,IAAoB,wBAAQA,cAAU"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type StateManager<StateData> = {
|
|
2
|
+
get(): StateData;
|
|
3
|
+
subscribe(listener: () => void): () => void;
|
|
4
|
+
set(partial: Partial<StateData>): void;
|
|
5
|
+
};
|
|
6
|
+
export declare function createStateManager<StateData extends object>(initial: StateData): StateManager<StateData>;
|
package/dist/state.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type StateData } from './manager';
|
|
2
|
+
export type { StateData } from './manager';
|
|
3
|
+
export declare const I18nState: {
|
|
4
|
+
get(): StateData;
|
|
5
|
+
subscribe(listener: () => void): () => void;
|
|
6
|
+
use(): StateData;
|
|
7
|
+
};
|
|
8
|
+
export declare function updateState(next: Partial<StateData>): void;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { LanguageId } from './core/types';
|
|
2
|
+
export type StoredState = {
|
|
3
|
+
languageId: LanguageId;
|
|
4
|
+
};
|
|
5
|
+
export type I18nStorage = {
|
|
6
|
+
read(): StoredState | null | Promise<StoredState | null>;
|
|
7
|
+
write(state: StoredState): void | Promise<void>;
|
|
8
|
+
};
|
|
9
|
+
export declare function createStorageManager(initial?: I18nStorage | null): {
|
|
10
|
+
setStorage(next: I18nStorage | null): void;
|
|
11
|
+
getStorage(): I18nStorage;
|
|
12
|
+
hasStorage(): boolean;
|
|
13
|
+
};
|
|
14
|
+
export declare function createLocalStorage(): I18nStorage | null;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { n as validateCompact } from "./compact-E3zj3Z8d.js";
|
|
2
|
+
// src/translation.ts
|
|
3
|
+
function parseCatalog(data) {
|
|
4
|
+
const catalog = Object.create(null);
|
|
5
|
+
const rows = parseRows(data);
|
|
6
|
+
const header = rows[0];
|
|
7
|
+
const hasHeader = isCompiledHeader(header);
|
|
8
|
+
const compiled = hasHeader || Array.isArray(data) && rows.some((row) => Array.isArray(row) && row.length === 2 && Array.isArray(row[1]));
|
|
9
|
+
if (hasHeader) rows.shift();
|
|
10
|
+
if (Array.isArray(data) && rows.at(-1) === null) rows.pop();
|
|
11
|
+
for (const row of rows) if (Array.isArray(row)) {
|
|
12
|
+
const [id, message] = compiled ? parseCompiledRow(row) : parseTranslationRow(row, Array.isArray(data));
|
|
13
|
+
if (compiled || message !== "") catalog[id] = message;
|
|
14
|
+
} else {
|
|
15
|
+
if (compiled) throw new TypeError("[i18n] invalid compiled catalog row");
|
|
16
|
+
const item = parseTranslationItem(row);
|
|
17
|
+
if (item.translation !== "") catalog[item.id] = item.translation;
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
catalog,
|
|
21
|
+
compiled
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function parseRows(data) {
|
|
25
|
+
if (Array.isArray(data)) return [...data];
|
|
26
|
+
return (typeof data === "string" ? data : data.jsonl).split(/\r?\n/).filter((line) => line.trim()).map((line) => JSON.parse(line));
|
|
27
|
+
}
|
|
28
|
+
function isCompiledHeader(row) {
|
|
29
|
+
return typeof row === "object" && row !== null && !Array.isArray(row) && "compiled" in row && row.compiled === true;
|
|
30
|
+
}
|
|
31
|
+
function parseCompiledRow(row) {
|
|
32
|
+
if (row.length !== 2 || typeof row[0] !== "string") throw new TypeError("[i18n] invalid compiled catalog row");
|
|
33
|
+
validateCompact(row[1]);
|
|
34
|
+
return [row[0], row[1]];
|
|
35
|
+
}
|
|
36
|
+
function parseTranslationRow(row, allowCompact) {
|
|
37
|
+
if (row.length === 2 && typeof row[0] === "string") {
|
|
38
|
+
if (allowCompact) validateCompact(row[1]);
|
|
39
|
+
else if (typeof row[1] !== "string") throw new TypeError("[i18n] invalid translation row");
|
|
40
|
+
return [row[0], row[1]];
|
|
41
|
+
}
|
|
42
|
+
if (row.length === 3 && row.every((value) => typeof value === "string")) return [`${row[0]}\0${row[1]}`, row[2]];
|
|
43
|
+
throw new TypeError("[i18n] invalid translation row");
|
|
44
|
+
}
|
|
45
|
+
function parseTranslationItem(row) {
|
|
46
|
+
const item = row;
|
|
47
|
+
if (!item || typeof item.translation !== "string") throw new TypeError("[i18n] invalid translation item");
|
|
48
|
+
const source = item.source ?? (Array.isArray(item.message) ? item.message[1] : item.message);
|
|
49
|
+
const context = Array.isArray(item.message) ? item.message[0] : item.context;
|
|
50
|
+
const id = item.id ?? (source === void 0 ? void 0 : context ? `${context}\0${source}` : source);
|
|
51
|
+
if (typeof id !== "string") throw new TypeError("[i18n] translation id is missing");
|
|
52
|
+
return {
|
|
53
|
+
id,
|
|
54
|
+
translation: item.translation
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
// src/translation-manager.ts
|
|
58
|
+
function defineTranslationLoader(loader) {
|
|
59
|
+
if (typeof loader === "function") return loader;
|
|
60
|
+
return async ({ languageId }) => {
|
|
61
|
+
if (!Object.prototype.hasOwnProperty.call(loader, languageId)) return [];
|
|
62
|
+
const entry = loader[languageId];
|
|
63
|
+
const source = await (typeof entry === "function" ? entry() : entry);
|
|
64
|
+
return typeof source === "object" && source !== null && "default" in source ? source.default : source;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function createTranslationManager() {
|
|
68
|
+
const catalogs = Object.create(null);
|
|
69
|
+
const pending = /* @__PURE__ */ new Map();
|
|
70
|
+
const compiledCatalogs = Object.create(null);
|
|
71
|
+
let loader = null;
|
|
72
|
+
let generation = 0;
|
|
73
|
+
const setTranslationLoader = (next) => {
|
|
74
|
+
loader = next;
|
|
75
|
+
generation++;
|
|
76
|
+
pending.clear();
|
|
77
|
+
for (const key of Object.keys(catalogs)) delete catalogs[key];
|
|
78
|
+
for (const key of Object.keys(compiledCatalogs)) delete compiledCatalogs[key];
|
|
79
|
+
};
|
|
80
|
+
const loadTranslations = async (languageId) => {
|
|
81
|
+
if (!loader || Object.prototype.hasOwnProperty.call(catalogs, languageId)) return;
|
|
82
|
+
if (pending.has(languageId)) return pending.get(languageId);
|
|
83
|
+
const version = generation;
|
|
84
|
+
const currentLoader = loader;
|
|
85
|
+
const task = Promise.resolve().then(() => currentLoader({ languageId })).then((data) => {
|
|
86
|
+
if (version === generation) {
|
|
87
|
+
const result = parseCatalog(data);
|
|
88
|
+
catalogs[languageId] = result.catalog;
|
|
89
|
+
compiledCatalogs[languageId] = result.compiled;
|
|
90
|
+
}
|
|
91
|
+
}).finally(() => {
|
|
92
|
+
if (version === generation) pending.delete(languageId);
|
|
93
|
+
});
|
|
94
|
+
pending.set(languageId, task);
|
|
95
|
+
return task;
|
|
96
|
+
};
|
|
97
|
+
const getTranslation = (args) => {
|
|
98
|
+
const catalog = catalogs[args.languageId];
|
|
99
|
+
const id = args.context ? `${args.context}\0${args.message}` : args.message;
|
|
100
|
+
return catalog?.[id] ?? catalog?.[args.message] ?? null;
|
|
101
|
+
};
|
|
102
|
+
return {
|
|
103
|
+
catalogs,
|
|
104
|
+
compiledCatalogs,
|
|
105
|
+
setTranslationLoader,
|
|
106
|
+
loadTranslations,
|
|
107
|
+
getTranslation,
|
|
108
|
+
languageLoading: () => loader?.onLanguageLoading?.(),
|
|
109
|
+
languageSelected: (languageId) => loader?.onLanguageSelected?.(languageId)
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
export { defineTranslationLoader as n, createTranslationManager as t };
|
|
113
|
+
|
|
114
|
+
//# sourceMappingURL=translation-manager-BkmFnlb_.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"translation-manager-BkmFnlb_.js","names":[],"sources":["../src/translation.ts","../src/translation-manager.ts"],"sourcesContent":["import { type CompactMessage, validateCompact } from './shared/compact';\n\nexport type TranslationItem = { id: string; translation: string; };\nexport type TranslationRow = [id: string, translation: string] | [context: string, id: string, translation: string];\nexport type CompiledTranslationItem = [id: string, message: CompactMessage];\nexport type CompiledCatalogHeader = { compiled: true; };\nexport type TranslationJsonl = string;\n\nexport type TranslationData =\n | unknown[]\n | TranslationJsonl\n | { jsonl: TranslationJsonl; };\n\nexport type Catalog = Record<string, CompactMessage>;\n\ntype TranslationItemRaw = {\n id?: string;\n source?: string;\n context?: string;\n message?: string | [string, string];\n translation?: string;\n};\n\nexport function normalizeCatalog(data: TranslationData): Catalog {\n return parseCatalog(data).catalog;\n}\n\nexport function parseCatalog(data: TranslationData): { catalog: Catalog; compiled: boolean; } {\n const catalog: Catalog = Object.create(null);\n const rows = parseRows(data);\n const header = rows[0];\n const hasHeader = isCompiledHeader(header);\n const compiled = hasHeader ||\n (Array.isArray(data) && rows.some((row) => Array.isArray(row) && row.length === 2 && Array.isArray(row[1])));\n\n if (hasHeader) rows.shift();\n if (Array.isArray(data) && rows.at(-1) === null) rows.pop();\n\n for (const row of rows) {\n if (Array.isArray(row)) {\n const [id, message] = compiled ? parseCompiledRow(row) : parseTranslationRow(row, Array.isArray(data));\n\n if (compiled || message !== '') catalog[id] = message;\n } else {\n if (compiled) throw new TypeError('[i18n] invalid compiled catalog row');\n\n const item = parseTranslationItem(row);\n\n if (item.translation !== '') catalog[item.id] = item.translation;\n }\n }\n\n return { catalog, compiled };\n}\n\nfunction parseRows(data: TranslationData): unknown[] {\n if (Array.isArray(data)) return [...data];\n\n const jsonl = typeof data === 'string' ? data : data.jsonl;\n\n return jsonl.split(/\\r?\\n/).filter((line) => line.trim()).map((line) => JSON.parse(line));\n}\n\nfunction isCompiledHeader(row: unknown): row is CompiledCatalogHeader {\n return typeof row === 'object' && row !== null && !Array.isArray(row) &&\n 'compiled' in row && row.compiled === true;\n}\n\nfunction parseCompiledRow(row: unknown[]): CompiledTranslationItem {\n if (row.length !== 2 || typeof row[0] !== 'string') {\n throw new TypeError('[i18n] invalid compiled catalog row');\n }\n\n validateCompact(row[1]);\n\n return [row[0], row[1]];\n}\n\nfunction parseTranslationRow(row: unknown[], allowCompact: boolean): [string, CompactMessage] {\n if (row.length === 2 && typeof row[0] === 'string') {\n // Programmatic compileCatalog() results can be passed directly as arrays.\n if (allowCompact) validateCompact(row[1]);\n else if (typeof row[1] !== 'string') throw new TypeError('[i18n] invalid translation row');\n\n return [row[0], row[1]];\n }\n if (row.length === 3 && row.every((value) => typeof value === 'string')) {\n return [`${row[0]}\\0${row[1]}`, row[2] as string];\n }\n\n throw new TypeError('[i18n] invalid translation row');\n}\n\nfunction parseTranslationItem(row: unknown): TranslationItem {\n const item = row as TranslationItemRaw;\n\n if (!item || typeof item.translation !== 'string') throw new TypeError('[i18n] invalid translation item');\n\n const source = item.source ?? (Array.isArray(item.message) ? item.message[1] : item.message);\n const context = Array.isArray(item.message) ? item.message[0] : item.context;\n const id = item.id ?? (source === undefined ? undefined : context ? `${context}\\0${source}` : source);\n\n if (typeof id !== 'string') throw new TypeError('[i18n] translation id is missing');\n\n return { id, translation: item.translation };\n}\n","import type { LanguageId, MessageContext } from './core/types';\nimport { type Catalog, parseCatalog, type TranslationData } from './translation';\n\nexport type * from './translation';\nexport { normalizeCatalog } from './translation';\n\nexport type TranslationLoaderArgs = { languageId: LanguageId; };\nexport type TranslationLookupArgs = { languageId: LanguageId; context: MessageContext | null; message: string; };\nexport type TranslationLoader = ((args: TranslationLoaderArgs) => TranslationData | Promise<TranslationData>) & {\n /** Called before a manager starts loading a selected locale and its fallbacks. */\n onLanguageLoading?: () => void;\n /** Called only after the manager successfully commits a language selection, including in-memory cache hits. */\n onLanguageSelected?: (languageId: LanguageId) => void;\n};\nexport type TranslationModule = { default: TranslationData; };\nexport type TranslationSource = TranslationData | TranslationModule;\n\nexport type TranslationLoaderEntry =\n | TranslationSource\n | Promise<TranslationSource>\n | (() => TranslationSource | Promise<TranslationSource>);\n\nexport type TranslationLoaderMap = Record<string, TranslationLoaderEntry>;\n\nexport function defineTranslationLoader(loader: TranslationLoader | TranslationLoaderMap): TranslationLoader {\n if (typeof loader === 'function') return loader;\n\n return async ({ languageId }) => {\n if (!Object.prototype.hasOwnProperty.call(loader, languageId)) return [];\n\n const entry = loader[languageId];\n const source = await (typeof entry === 'function' ? entry() : entry);\n\n return typeof source === 'object' && source !== null && 'default' in source\n ? source.default\n : source as TranslationData;\n };\n}\n\nexport function createTranslationManager() {\n const catalogs: Record<string, Catalog> = Object.create(null);\n const pending = new Map<string, Promise<void>>();\n const compiledCatalogs: Record<string, boolean> = Object.create(null);\n\n let loader: TranslationLoader | null = null;\n let generation = 0;\n\n const setTranslationLoader = (next: TranslationLoader | null) => {\n loader = next;\n generation++;\n pending.clear();\n\n for (const key of Object.keys(catalogs)) delete catalogs[key];\n for (const key of Object.keys(compiledCatalogs)) delete compiledCatalogs[key];\n };\n\n const loadTranslations = async (languageId: string) => {\n if (!loader || Object.prototype.hasOwnProperty.call(catalogs, languageId)) return;\n if (pending.has(languageId)) return pending.get(languageId);\n\n const version = generation;\n const currentLoader = loader;\n\n const task = Promise.resolve().then(() => currentLoader({ languageId })).then((data) => {\n if (version === generation) {\n const result = parseCatalog(data);\n catalogs[languageId] = result.catalog;\n compiledCatalogs[languageId] = result.compiled;\n }\n }).finally(() => {\n if (version === generation) pending.delete(languageId);\n });\n\n pending.set(languageId, task);\n\n return task;\n };\n\n const getTranslation = (args: TranslationLookupArgs) => {\n const catalog = catalogs[args.languageId];\n const id = args.context ? `${args.context}\\0${args.message}` : args.message;\n\n return catalog?.[id] ?? catalog?.[args.message] ?? null;\n };\n\n return {\n catalogs,\n compiledCatalogs,\n setTranslationLoader,\n loadTranslations,\n getTranslation,\n languageLoading: () => loader?.onLanguageLoading?.(),\n languageSelected: (languageId: string) => loader?.onLanguageSelected?.(languageId),\n };\n}\n"],"mappings":";;AA2BA,SAAgB,aAAa,MAAiE;CAC5F,MAAM,UAAmB,OAAO,OAAO,IAAI;CAC3C,MAAM,OAAO,UAAU,IAAI;CAC3B,MAAM,SAAS,KAAK;CACpB,MAAM,YAAY,iBAAiB,MAAM;CACzC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
|
|
|
1
|
+
import type { LanguageId, MessageContext } from './core/types';
|
|
2
|
+
import { type Catalog, type TranslationData } from './translation';
|
|
3
|
+
export type * from './translation';
|
|
4
|
+
export { normalizeCatalog } from './translation';
|
|
5
|
+
export type TranslationLoaderArgs = {
|
|
6
|
+
languageId: LanguageId;
|
|
7
|
+
};
|
|
8
|
+
export type TranslationLookupArgs = {
|
|
9
|
+
languageId: LanguageId;
|
|
10
|
+
context: MessageContext | null;
|
|
11
|
+
message: string;
|
|
12
|
+
};
|
|
13
|
+
export type TranslationLoader = ((args: TranslationLoaderArgs) => TranslationData | Promise<TranslationData>) & {
|
|
14
|
+
/** Called before a manager starts loading a selected locale and its fallbacks. */
|
|
15
|
+
onLanguageLoading?: () => void;
|
|
16
|
+
/** Called only after the manager successfully commits a language selection, including in-memory cache hits. */
|
|
17
|
+
onLanguageSelected?: (languageId: LanguageId) => void;
|
|
18
|
+
};
|
|
19
|
+
export type TranslationModule = {
|
|
20
|
+
default: TranslationData;
|
|
21
|
+
};
|
|
22
|
+
export type TranslationSource = TranslationData | TranslationModule;
|
|
23
|
+
export type TranslationLoaderEntry = TranslationSource | Promise<TranslationSource> | (() => TranslationSource | Promise<TranslationSource>);
|
|
24
|
+
export type TranslationLoaderMap = Record<string, TranslationLoaderEntry>;
|
|
25
|
+
export declare function defineTranslationLoader(loader: TranslationLoader | TranslationLoaderMap): TranslationLoader;
|
|
26
|
+
export declare function createTranslationManager(): {
|
|
27
|
+
catalogs: Record<string, Catalog>;
|
|
28
|
+
compiledCatalogs: Record<string, boolean>;
|
|
29
|
+
setTranslationLoader: (next: TranslationLoader | null) => void;
|
|
30
|
+
loadTranslations: (languageId: string) => Promise<void>;
|
|
31
|
+
getTranslation: (args: TranslationLookupArgs) => import("./adapter").CompactMessage;
|
|
32
|
+
languageLoading: () => void | undefined;
|
|
33
|
+
languageSelected: (languageId: string) => void | undefined;
|
|
34
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type CompactMessage } from './shared/compact';
|
|
2
|
+
export type TranslationItem = {
|
|
3
|
+
id: string;
|
|
4
|
+
translation: string;
|
|
5
|
+
};
|
|
6
|
+
export type TranslationRow = [id: string, translation: string] | [context: string, id: string, translation: string];
|
|
7
|
+
export type CompiledTranslationItem = [id: string, message: CompactMessage];
|
|
8
|
+
export type CompiledCatalogHeader = {
|
|
9
|
+
compiled: true;
|
|
10
|
+
};
|
|
11
|
+
export type TranslationJsonl = string;
|
|
12
|
+
export type TranslationData = unknown[] | TranslationJsonl | {
|
|
13
|
+
jsonl: TranslationJsonl;
|
|
14
|
+
};
|
|
15
|
+
export type Catalog = Record<string, CompactMessage>;
|
|
16
|
+
export declare function normalizeCatalog(data: TranslationData): Catalog;
|
|
17
|
+
export declare function parseCatalog(data: TranslationData): {
|
|
18
|
+
catalog: Catalog;
|
|
19
|
+
compiled: boolean;
|
|
20
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// src/core/types.ts
|
|
2
|
+
function isMessageDescriptor(value) {
|
|
3
|
+
return value !== null && typeof value === "object" && "__fluenticMessage" in value && value.__fluenticMessage === true;
|
|
4
|
+
}
|
|
5
|
+
function isMessageTagHandler(value) {
|
|
6
|
+
return typeof value === "function" && "__fluenticTagHandler" in value && value.__fluenticTagHandler === true;
|
|
7
|
+
}
|
|
8
|
+
function isMessageSchemaValue(value) {
|
|
9
|
+
return value === String || value === Number || value === Boolean || value === Date;
|
|
10
|
+
}
|
|
11
|
+
export { isMessageSchemaValue as n, isMessageTagHandler as r, isMessageDescriptor as t };
|
|
12
|
+
|
|
13
|
+
//# sourceMappingURL=types-BkDsJfVD.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types-BkDsJfVD.js","names":[],"sources":["../src/core/types.ts"],"sourcesContent":["import type { CompactMessage } from '../shared/compact';\nimport type { I18nJsxConfig } from '../types';\n\nexport type { I18nJsxConfig } from '../types';\n\nexport type I18nJsxOutput = I18nJsxConfig extends { JSX: infer Output; } ? NonNullable<Output> : unknown;\nexport type I18nTagElement = I18nJsxConfig extends { Element: infer Element; } ? Element : never;\nexport type I18nTagValue = I18nJsxConfig extends { Value: infer Value; } ? Value : unknown;\nexport type PrimitiveType = string | number | boolean | null | undefined;\n\nexport type LanguageId = string;\nexport type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue; };\nexport type MessageMeta = JsonValue;\nexport type MessageContext = string;\n\nexport type MessageSchemaValue = StringConstructor | NumberConstructor | BooleanConstructor | DateConstructor;\nexport type MessageSchema = Partial<Record<string, MessageSchemaValue>>;\n\nexport type InferMessageSchema<Schema extends MessageSchema> = {\n [Key in keyof Schema]: Schema[Key] extends StringConstructor ? string\n : Schema[Key] extends NumberConstructor ? number\n : Schema[Key] extends BooleanConstructor ? boolean\n : Schema[Key] extends DateConstructor ? Date\n : never;\n};\n\nexport type MessageDescriptor<Text extends string = string, Schema extends MessageSchema | undefined = MessageSchema> =\n {\n readonly __fluenticMessage: true;\n readonly text: Text;\n readonly compiled?: readonly [string, CompactMessage];\n readonly context: MessageContext | null;\n readonly schema: Schema extends MessageSchema ? Schema : MessageSchema;\n readonly notes: readonly string[];\n readonly meta: MessageMeta | undefined;\n };\n\nexport type AnyMessageDescriptor = MessageDescriptor<string, MessageSchema | undefined>;\n\nexport type MessageValueContext = { languageId: LanguageId; };\nexport type MessageValueFactory<T = unknown> = (context: MessageValueContext) => T;\n\nexport type MessageTagHandler<T = unknown, R = unknown> = ((chunks: Array<string | T>) => R) & {\n readonly __fluenticTagHandler: true;\n};\n\nexport type MessageRuntimeValue =\n | PrimitiveType\n | Date\n | AnyMessageDescriptor\n | MessageValueFactory\n | Pick<MessageTagHandler, '__fluenticTagHandler'>;\n\nexport type MessageParam = MessageRuntimeValue;\nexport type MessageParams = Record<string, MessageParam>;\nexport type MessageInput = string | AnyMessageDescriptor;\n\nexport type InferRuntimeParams<Schema extends MessageSchema> = {\n [Key in keyof InferMessageSchema<Schema>]:\n | InferMessageSchema<Schema>[Key]\n | MessageValueFactory<InferMessageSchema<Schema>[Key]>;\n};\n\nexport type DescriptorRenderArgs<Schema extends MessageSchema | undefined> = Schema extends undefined\n ? [params?: MessageParams]\n : keyof Schema extends never ? []\n : Schema extends MessageSchema ? [params: InferRuntimeParams<Schema>]\n : never;\n\nexport type TryRenderArgs<Value> = Value extends { readonly __fluenticMessage: true; }\n ? Value extends MessageDescriptor<string, infer Schema>\n ? Schema extends MessageSchema | undefined ? DescriptorRenderArgs<Schema>\n : [params: MessageParams]\n : [params: MessageParams]\n : [params?: MessageParams];\n\nexport type TextRenderer = {\n <Text extends string>(message: Text, params?: MessageParams): string;\n <Text extends string, Schema extends MessageSchema | undefined>(\n message: MessageDescriptor<Text, Schema>,\n ...args: DescriptorRenderArgs<Schema>\n ): string;\n};\n\nexport type JsxRenderer<Output = unknown> = {\n (render: () => I18nTagElement): Output;\n <Text extends string>(message: Text, params?: MessageParams): Output;\n <Text extends string, Schema extends MessageSchema | undefined>(\n message: MessageDescriptor<Text, Schema>,\n ...args: DescriptorRenderArgs<Schema>\n ): Output;\n};\n\nexport type MessageFormatOptions = {\n msg: MessageInput;\n params?: MessageParams;\n languageId?: LanguageId;\n};\n\nexport type GetJsxHandler<Output = unknown> = (options: MessageFormatOptions) => Output;\n\nexport type CompiledTranslation = { compiled: CompactMessage; };\n\nexport type TranslationGetter = (args: {\n languageId: LanguageId;\n context: MessageContext | null;\n message: string;\n getCompiledId?: () => string;\n}) => CompactMessage | CompiledTranslation | null;\n\nexport function isMessageDescriptor(value: unknown): value is AnyMessageDescriptor {\n return value !== null && typeof value === 'object' && '__fluenticMessage' in value &&\n value.__fluenticMessage === true;\n}\n\nexport function isMessageTagHandler(value: unknown): value is MessageTagHandler {\n return typeof value === 'function' && '__fluenticTagHandler' in value && value.__fluenticTagHandler === true;\n}\n\nexport function isMessageSchemaValue(value: unknown): value is MessageSchemaValue {\n return value === String || value === Number || value === Boolean || value === Date;\n}\n"],"mappings":";AA8GA,SAAgB,oBAAoB,OAA+C;CACjF,OAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,uBAAuB,SAC3E,MAAM,sBAAsB;AAChC;AAEA,SAAgB,oBAAoB,OAA4C;CAC9E,OAAO,OAAO,UAAU,cAAc,0BAA0B,SAAS,MAAM,yBAAyB;AAC1G;AAEA,SAAgB,qBAAqB,OAA6C;CAChF,OAAO,UAAU,UAAU,UAAU,UAAU,UAAU,WAAW,UAAU;AAChF"}
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as isMessageDescriptor, n as isMessageSchemaValue, r as isMessageTagHandler } from "./types-BkDsJfVD.js";
|