@fluentic/i18n 0.0.0-stage → 0.1.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/LICENSE +21 -0
- package/README.md +21 -2
- package/dist/adapter-CWC8rng-.d.ts +178 -0
- package/dist/adapter.d.ts +4 -0
- package/dist/adapter.js +4 -0
- package/dist/analyze-CdXCxQDg.d.ts +14 -0
- package/dist/compact-CJ0OWoIv.d.ts +7 -0
- package/dist/compact-E3zj3Z8d.js +34 -0
- package/dist/compact-E3zj3Z8d.js.map +1 -0
- package/dist/compiled.d.ts +27 -0
- package/dist/compiled.js +51 -0
- package/dist/compiled.js.map +1 -0
- package/dist/compiler-BZtB9WLi.js +365 -0
- package/dist/compiler-BZtB9WLi.js.map +1 -0
- package/dist/compiler.d.ts +35 -0
- package/dist/compiler.js +3 -0
- package/dist/core/index.d.ts +5 -0
- package/dist/core/index.js +4 -0
- package/dist/i18n-CfYNwubL.d.ts +25 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +4 -0
- package/dist/interpreter-Dld8QyOI.js +186 -0
- package/dist/interpreter-Dld8QyOI.js.map +1 -0
- package/dist/manager-BW778X1Y.js +193 -0
- package/dist/manager-BW778X1Y.js.map +1 -0
- package/dist/options-DE7vYj0e.d.ts +7 -0
- package/dist/options-Dxxx0aOz.js +23 -0
- package/dist/options-Dxxx0aOz.js.map +1 -0
- package/dist/plugin/babel.d.ts +11 -0
- package/dist/plugin/babel.js +31 -0
- package/dist/plugin/babel.js.map +1 -0
- package/dist/plugin/esbuild.d.ts +7 -0
- package/dist/plugin/esbuild.js +37 -0
- package/dist/plugin/esbuild.js.map +1 -0
- package/dist/plugin/rollup.d.ts +30 -0
- package/dist/plugin/rollup.js +34 -0
- package/dist/plugin/rollup.js.map +1 -0
- package/dist/plugin/vite.d.ts +31 -0
- package/dist/plugin/vite.js +11 -0
- package/dist/plugin/vite.js.map +1 -0
- package/dist/plugin/webpack-loader.d.ts +3 -0
- package/dist/plugin/webpack-loader.js +21 -0
- package/dist/plugin/webpack-loader.js.map +1 -0
- package/dist/plugin/webpack.d.ts +8 -0
- package/dist/plugin/webpack.js +22 -0
- package/dist/plugin/webpack.js.map +1 -0
- package/dist/react.d.ts +53 -0
- package/dist/react.js +60 -0
- package/dist/react.js.map +1 -0
- package/dist/src-BovRx-mX.js +142 -0
- package/dist/src-BovRx-mX.js.map +1 -0
- package/dist/translation-manager-C79eKu2K.js +213 -0
- package/dist/translation-manager-C79eKu2K.js.map +1 -0
- package/dist/types-BkDsJfVD.js +13 -0
- package/dist/types-BkDsJfVD.js.map +1 -0
- package/dist/types-BuGoCU3P.d.ts +75 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.js +1 -0
- package/dist-cjs/babel.cjs +379 -0
- package/dist-cjs/babel.cjs.map +1 -0
- package/package.json +130 -3
- package/src/adapter.ts +6 -0
- package/src/compiled.ts +116 -0
- package/src/compiler/analyze.ts +182 -0
- package/src/compiler/index.ts +208 -0
- package/src/compiler/message.ts +74 -0
- package/src/core/format.ts +40 -0
- package/src/core/i18n.ts +124 -0
- package/src/core/index.ts +1 -0
- package/src/core/types.ts +121 -0
- package/src/index.ts +9 -0
- package/src/manager.ts +175 -0
- package/src/plugin/babel.ts +34 -0
- package/src/plugin/esbuild.ts +45 -0
- package/src/plugin/options.ts +24 -0
- package/src/plugin/rollup.ts +29 -0
- package/src/plugin/vite.ts +6 -0
- package/src/plugin/webpack-loader.ts +22 -0
- package/src/plugin/webpack.ts +15 -0
- package/src/react.ts +97 -0
- package/src/registry-manager.ts +3 -0
- package/src/shared/compact.ts +51 -0
- package/src/shared/formatting.ts +121 -0
- package/src/shared/interpreter.ts +128 -0
- package/src/shared/runtime.ts +131 -0
- package/src/state-manager.ts +29 -0
- package/src/state.ts +20 -0
- package/src/storage-manager.ts +47 -0
- package/src/translation-manager.ts +82 -0
- package/src/translation.ts +106 -0
- package/src/types.ts +2 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { t as ExtractedMessage } from "../analyze-CdXCxQDg.js";
|
|
2
|
+
import { t as PluginOptions } from "../options-DE7vYj0e.js";
|
|
3
|
+
declare function fluentic(options?: PluginOptions): {
|
|
4
|
+
enforce: "pre";
|
|
5
|
+
name: string;
|
|
6
|
+
buildStart(this: {
|
|
7
|
+
addWatchFile(file: string): void;
|
|
8
|
+
}): void;
|
|
9
|
+
watchChange(id: string): void;
|
|
10
|
+
load(id: string): Promise<{
|
|
11
|
+
code: string;
|
|
12
|
+
map: {
|
|
13
|
+
mappings: string;
|
|
14
|
+
};
|
|
15
|
+
} | null>;
|
|
16
|
+
transform(code: string, id: string): {
|
|
17
|
+
code: string;
|
|
18
|
+
map: {
|
|
19
|
+
version: number;
|
|
20
|
+
sources: string[];
|
|
21
|
+
names: string[];
|
|
22
|
+
sourceRoot?: string | undefined;
|
|
23
|
+
sourcesContent?: string[] | undefined;
|
|
24
|
+
mappings: string;
|
|
25
|
+
file: string;
|
|
26
|
+
} | null;
|
|
27
|
+
messages: ExtractedMessage[];
|
|
28
|
+
} | null;
|
|
29
|
+
};
|
|
30
|
+
export { fluentic as default };
|
|
31
|
+
//# sourceMappingURL=vite.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vite.js","names":["rollup"],"sources":["../../src/plugin/vite.ts"],"sourcesContent":["import type { PluginOptions } from './options';\nimport rollup from './rollup';\n\nexport default function fluentic(options: PluginOptions = {}) {\n return { ...rollup(options), enforce: 'pre' as const };\n}\n"],"mappings":";;AAGA,SAAwB,SAAS,UAAyB,CAAC,GAAG;CAC5D,OAAO;EAAE,GAAGA,WAAO,OAAO;EAAG,SAAS;CAAe;AACvD"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { n as transform } from "../compiler-BZtB9WLi.js";
|
|
2
|
+
import { n as loadOptions } from "../options-Dxxx0aOz.js";
|
|
3
|
+
import remapping from "@jridgewell/remapping";
|
|
4
|
+
// src/plugin/webpack-loader.ts
|
|
5
|
+
function loader(code, inputMap) {
|
|
6
|
+
this.cacheable?.();
|
|
7
|
+
const options = loadOptions(this.getOptions?.() ?? {}, this.rootContext);
|
|
8
|
+
if (options.configFile) this.addDependency(options.configFile);
|
|
9
|
+
if (this.resourcePath.endsWith(".jsonl")) {
|
|
10
|
+
this.callback(null, "export default " + JSON.stringify(code));
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const result = options.compiled ? transform(code, {
|
|
14
|
+
filename: this.resourcePath,
|
|
15
|
+
...options
|
|
16
|
+
}) : null;
|
|
17
|
+
this.callback(null, result?.code ?? code, result?.map && inputMap ? remapping([result.map, inputMap], () => null) : result?.map ?? inputMap);
|
|
18
|
+
}
|
|
19
|
+
export { loader as default };
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=webpack-loader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webpack-loader.js","names":[],"sources":["../../src/plugin/webpack-loader.ts"],"sourcesContent":["import remapping from '@jridgewell/remapping';\nimport { transform } from '../compiler';\nimport { loadOptions } from './options';\n\nexport default function loader(this: any, code: string, inputMap: unknown) {\n this.cacheable?.();\n const options = loadOptions(this.getOptions?.() ?? {}, this.rootContext);\n\n if (options.configFile) this.addDependency(options.configFile);\n if (this.resourcePath.endsWith('.jsonl')) {\n this.callback(null, 'export default ' + JSON.stringify(code));\n\n return;\n }\n\n const result = options.compiled ? transform(code, { filename: this.resourcePath, ...options }) : null;\n this.callback(\n null,\n result?.code ?? code,\n result?.map && inputMap ? remapping([result.map as any, inputMap as any], () => null) : result?.map ?? inputMap,\n );\n}\n"],"mappings":";;;;AAIA,SAAwB,OAAkB,MAAc,UAAmB;CACzE,KAAK,YAAY;CACjB,MAAM,UAAU,YAAY,KAAK,aAAa,KAAK,CAAC,GAAG,KAAK,WAAW;CAEvE,IAAI,QAAQ,YAAY,KAAK,cAAc,QAAQ,UAAU;CAC7D,IAAI,KAAK,aAAa,SAAS,QAAQ,GAAG;EACxC,KAAK,SAAS,MAAM,oBAAoB,KAAK,UAAU,IAAI,CAAC;EAE5D;CACF;CAEA,MAAM,SAAS,QAAQ,WAAW,UAAU,MAAM;EAAE,UAAU,KAAK;EAAc,GAAG;CAAQ,CAAC,IAAI;CACjG,KAAK,SACH,MACA,QAAQ,QAAQ,MAChB,QAAQ,OAAO,WAAW,UAAU,CAAC,OAAO,KAAY,QAAe,SAAS,IAAI,IAAI,QAAQ,OAAO,QACzG;AACF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { t as PluginOptions } from "../options-DE7vYj0e.js";
|
|
2
|
+
declare class FluenticI18nPlugin {
|
|
3
|
+
private options;
|
|
4
|
+
constructor(options?: PluginOptions);
|
|
5
|
+
apply(compiler: any): void;
|
|
6
|
+
}
|
|
7
|
+
export { FluenticI18nPlugin as default };
|
|
8
|
+
//# sourceMappingURL=webpack.d.ts.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
// src/plugin/webpack.ts
|
|
3
|
+
const require = createRequire(import.meta.url);
|
|
4
|
+
var FluenticI18nPlugin = class {
|
|
5
|
+
constructor(options = {}) {
|
|
6
|
+
this.options = options;
|
|
7
|
+
}
|
|
8
|
+
apply(compiler) {
|
|
9
|
+
compiler.options.module.rules.push({
|
|
10
|
+
test: /(?:\.[cm]?[jt]sx?|\.jsonl)$/,
|
|
11
|
+
enforce: "pre",
|
|
12
|
+
...this.options.includeDependencies ? {} : { exclude: /node_modules/ },
|
|
13
|
+
use: [{
|
|
14
|
+
loader: require.resolve("@fluentic/i18n/plugin/webpack-loader"),
|
|
15
|
+
options: this.options
|
|
16
|
+
}]
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
export { FluenticI18nPlugin as default };
|
|
21
|
+
|
|
22
|
+
//# sourceMappingURL=webpack.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webpack.js","names":[],"sources":["../../src/plugin/webpack.ts"],"sourcesContent":["import { createRequire } from 'node:module';\nimport type { PluginOptions } from './options';\n\nconst require = createRequire(import.meta.url);\nexport default class FluenticI18nPlugin {\n constructor(private options: PluginOptions = {}) {}\n apply(compiler: any) {\n compiler.options.module.rules.push({\n test: /(?:\\.[cm]?[jt]sx?|\\.jsonl)$/,\n enforce: 'pre',\n ...(this.options.includeDependencies ? {} : { exclude: /node_modules/ }),\n use: [{ loader: require.resolve('@fluentic/i18n/plugin/webpack-loader'), options: this.options }],\n });\n }\n}\n"],"mappings":";;AAGA,MAAM,UAAU,cAAc,YAAY,GAAG;AAC7C,IAAqB,qBAArB,MAAwC;CACtC,YAAY,UAAiC,CAAC,GAAG;EAA7B,KAAA,UAAA;CAA8B;CAClD,MAAM,UAAe;EACnB,SAAS,QAAQ,OAAO,MAAM,KAAK;GACjC,MAAM;GACN,SAAS;GACT,GAAI,KAAK,QAAQ,sBAAsB,CAAC,IAAI,EAAE,SAAS,eAAe;GACtE,KAAK,CAAC;IAAE,QAAQ,QAAQ,QAAQ,sCAAsC;IAAG,SAAS,KAAK;GAAQ,CAAC;EAClG,CAAC;CACH;AACF"}
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { n as CompactMessage } from "./compact-CJ0OWoIv.js";
|
|
2
|
+
import { Q as StateManager, a as I18nSnapshot, c as createI18nManager, d as TranslationLoader, i as I18nManagerOptions, k as I18nStorage, n as GetTranslationArgs, o as ManagedI18n, s as StateData, y as defineTranslationLoader, z as Runtime } from "./adapter-CWC8rng-.js";
|
|
3
|
+
import React from "react";
|
|
4
|
+
declare module '@fluentic/i18n/types' {
|
|
5
|
+
interface I18nJsxConfig {
|
|
6
|
+
JSX: React.ReactElement;
|
|
7
|
+
Element: React.ReactElement;
|
|
8
|
+
Value: React.ReactNode;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
declare function installReactAdapter(runtime: Runtime, lineBreak?: React.ReactNode): void;
|
|
12
|
+
declare const I18nManager: {
|
|
13
|
+
configure: (config: I18nManagerOptions) => void;
|
|
14
|
+
setStorage: (next: I18nStorage | null) => void;
|
|
15
|
+
setTranslationLoader: (loader: TranslationLoader | null) => void;
|
|
16
|
+
init: () => Promise<void>;
|
|
17
|
+
setLanguageId: (languageId: string) => Promise<void>;
|
|
18
|
+
getLanguageId: () => string;
|
|
19
|
+
getTranslation: (args: GetTranslationArgs) => CompactMessage | null;
|
|
20
|
+
getSnapshot: () => I18nSnapshot;
|
|
21
|
+
hydrate: (snapshot: I18nSnapshot) => void;
|
|
22
|
+
};
|
|
23
|
+
declare const I18nState: {
|
|
24
|
+
get: () => StateData;
|
|
25
|
+
subscribe: (listener: () => void) => () => void;
|
|
26
|
+
use(): StateData;
|
|
27
|
+
};
|
|
28
|
+
declare function I18nProvider({ children, snapshot, instance, lineBreak }: {
|
|
29
|
+
children?: React.ReactNode;
|
|
30
|
+
snapshot?: I18nSnapshot;
|
|
31
|
+
instance?: ManagedI18n;
|
|
32
|
+
lineBreak?: React.ReactNode;
|
|
33
|
+
}): React.FunctionComponentElement<React.ProviderProps<{
|
|
34
|
+
manager: {
|
|
35
|
+
configure: (config: I18nManagerOptions) => void;
|
|
36
|
+
setStorage: (next: I18nStorage | null) => void;
|
|
37
|
+
setTranslationLoader: (loader: TranslationLoader | null) => void;
|
|
38
|
+
init: () => Promise<void>;
|
|
39
|
+
setLanguageId: (languageId: string) => Promise<void>;
|
|
40
|
+
getLanguageId: () => string;
|
|
41
|
+
getTranslation: (args: GetTranslationArgs) => CompactMessage | null;
|
|
42
|
+
getSnapshot: () => I18nSnapshot;
|
|
43
|
+
hydrate: (snapshot: I18nSnapshot) => void;
|
|
44
|
+
};
|
|
45
|
+
state: StateManager<StateData>;
|
|
46
|
+
runtime: Runtime;
|
|
47
|
+
} | null>>;
|
|
48
|
+
/** Explicit binding for text attributes in isolated providers and SSR. */
|
|
49
|
+
declare function useI18nRuntime(): Runtime;
|
|
50
|
+
/** Bind an existing universal renderer to this provider (including client SSR). */
|
|
51
|
+
declare function useI18nRenderer<F extends (...args: any[]) => any>(renderer: F): F;
|
|
52
|
+
export { I18nManager, I18nProvider, type I18nSnapshot, I18nState, type I18nStorage, type ManagedI18n, type StateData, createI18nManager, defineTranslationLoader, installReactAdapter, useI18nRenderer, useI18nRuntime };
|
|
53
|
+
//# sourceMappingURL=react.d.ts.map
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { c as getRuntime, h as withRuntime, n as defineTranslationLoader, o as defaultRuntime } from "./translation-manager-C79eKu2K.js";
|
|
2
|
+
import { i as defaultManaged, n as createI18nManager } from "./manager-BW778X1Y.js";
|
|
3
|
+
import React, { createContext, useContext, useRef, useSyncExternalStore } from "react";
|
|
4
|
+
// src/react.ts
|
|
5
|
+
const Context = createContext(null);
|
|
6
|
+
function TranslatedMessage({ render }) {
|
|
7
|
+
const runtime = useContext(Context)?.runtime ?? getRuntime();
|
|
8
|
+
useSyncExternalStore(runtime.subscribe, runtime.getSnapshot, runtime.getSnapshot);
|
|
9
|
+
return React.createElement(React.Fragment, null, ...render(runtime).map((part, index) => React.createElement(React.Fragment, { key: index }, part)));
|
|
10
|
+
}
|
|
11
|
+
function installReactAdapter(runtime, lineBreak = React.createElement("br")) {
|
|
12
|
+
runtime.jsxLineBreak = lineBreak;
|
|
13
|
+
runtime.jsxHandler = (_options, render) => React.createElement(TranslatedMessage, { render });
|
|
14
|
+
runtime.tagElement = (element) => {
|
|
15
|
+
if (!React.isValidElement(element)) throw new TypeError("[i18n] t.tag expects a handler or React element");
|
|
16
|
+
return (chunks) => React.cloneElement(element, void 0, ...chunks);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
installReactAdapter(defaultRuntime);
|
|
20
|
+
const I18nManager = defaultManaged.manager;
|
|
21
|
+
const I18nState = {
|
|
22
|
+
get: defaultManaged.state.get,
|
|
23
|
+
subscribe: defaultManaged.state.subscribe,
|
|
24
|
+
use() {
|
|
25
|
+
const managed = useContext(Context) ?? defaultManaged;
|
|
26
|
+
return useSyncExternalStore(managed.state.subscribe, managed.state.get, managed.state.get);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
function I18nProvider({ children, snapshot, instance, lineBreak }) {
|
|
30
|
+
const ref = useRef(null);
|
|
31
|
+
if (!ref.current || ref.current.snapshot !== snapshot || ref.current.instance !== instance || ref.current.lineBreak !== lineBreak) {
|
|
32
|
+
if (snapshot && instance) throw new Error("[i18n] pass either snapshot or instance, not both");
|
|
33
|
+
const managed = instance ?? createI18nManager();
|
|
34
|
+
installReactAdapter(managed.runtime, lineBreak);
|
|
35
|
+
if (snapshot) managed.manager.hydrate(snapshot);
|
|
36
|
+
ref.current = {
|
|
37
|
+
managed,
|
|
38
|
+
snapshot,
|
|
39
|
+
instance,
|
|
40
|
+
lineBreak
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
const managed = ref.current.managed;
|
|
44
|
+
if (!useSyncExternalStore(managed.state.subscribe, managed.state.get, managed.state.get).ready) throw new Error("[i18n] initialize the provider instance or pass a ready snapshot");
|
|
45
|
+
return React.createElement(Context.Provider, { value: managed }, children);
|
|
46
|
+
}
|
|
47
|
+
/** Explicit binding for text attributes in isolated providers and SSR. */
|
|
48
|
+
function useI18nRuntime() {
|
|
49
|
+
const managed = useContext(Context) ?? defaultManaged;
|
|
50
|
+
useSyncExternalStore(managed.state.subscribe, managed.state.get, managed.state.get);
|
|
51
|
+
return managed.runtime;
|
|
52
|
+
}
|
|
53
|
+
/** Bind an existing universal renderer to this provider (including client SSR). */
|
|
54
|
+
function useI18nRenderer(renderer) {
|
|
55
|
+
const runtime = useI18nRuntime();
|
|
56
|
+
return ((...args) => withRuntime(runtime, () => renderer(...args)));
|
|
57
|
+
}
|
|
58
|
+
export { I18nManager, I18nProvider, I18nState, createI18nManager, defineTranslationLoader, installReactAdapter, useI18nRenderer, useI18nRuntime };
|
|
59
|
+
|
|
60
|
+
//# sourceMappingURL=react.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.js","names":[],"sources":["../src/react.ts"],"sourcesContent":["import React, { createContext, useContext, useRef, useSyncExternalStore } from 'react';\nimport type { MessageFormatOptions } from './core/types';\nimport { createI18nManager, defaultManaged, type I18nSnapshot, type ManagedI18n } from './manager';\nimport { defaultRuntime, getRuntime, type PartsRenderer, type Runtime, withRuntime } from './shared/runtime';\n\ndeclare module '@fluentic/i18n/types' {\n interface I18nJsxConfig {\n JSX: React.ReactElement;\n Element: React.ReactElement;\n Value: React.ReactNode;\n }\n}\nconst Context = createContext<ManagedI18n | null>(null);\n\nfunction TranslatedMessage({ render }: { render: PartsRenderer; }) {\n const managed = useContext(Context);\n const runtime = managed?.runtime ?? getRuntime();\n useSyncExternalStore(runtime.subscribe, runtime.getSnapshot, runtime.getSnapshot);\n\n return React.createElement(\n React.Fragment,\n null,\n ...render(runtime).map((part, index) =>\n React.createElement(React.Fragment, { key: index }, part as React.ReactNode)\n ),\n );\n}\n\nexport function installReactAdapter(runtime: Runtime, lineBreak: React.ReactNode = React.createElement('br')): void {\n runtime.jsxLineBreak = lineBreak;\n runtime.jsxHandler = (_options: MessageFormatOptions, render: PartsRenderer) =>\n React.createElement(TranslatedMessage, { render });\n runtime.tagElement = (element) => {\n if (!React.isValidElement(element)) throw new TypeError('[i18n] t.tag expects a handler or React element');\n\n return (chunks) => React.cloneElement(element, undefined, ...chunks as React.ReactNode[]);\n };\n}\ninstallReactAdapter(defaultRuntime);\nexport const I18nManager = defaultManaged.manager;\nexport const I18nState = {\n get: defaultManaged.state.get,\n subscribe: defaultManaged.state.subscribe,\n use() {\n const managed = useContext(Context) ?? defaultManaged;\n\n return useSyncExternalStore(managed.state.subscribe, managed.state.get, managed.state.get);\n },\n};\n\nexport function I18nProvider(\n { children, snapshot, instance, lineBreak }: {\n children?: React.ReactNode;\n snapshot?: I18nSnapshot;\n instance?: ManagedI18n;\n lineBreak?: React.ReactNode;\n },\n) {\n const ref = useRef<\n { managed: ManagedI18n; snapshot?: I18nSnapshot; instance?: ManagedI18n; lineBreak?: React.ReactNode; } | null\n >(null);\n\n if (\n !ref.current || ref.current.snapshot !== snapshot || ref.current.instance !== instance ||\n ref.current.lineBreak !== lineBreak\n ) {\n if (snapshot && instance) throw new Error('[i18n] pass either snapshot or instance, not both');\n\n const managed = instance ?? createI18nManager();\n installReactAdapter(managed.runtime, lineBreak);\n if (snapshot) managed.manager.hydrate(snapshot);\n ref.current = { managed, snapshot, instance, lineBreak };\n }\n\n const managed = ref.current.managed;\n const state = useSyncExternalStore(managed.state.subscribe, managed.state.get, managed.state.get);\n\n if (!state.ready) throw new Error('[i18n] initialize the provider instance or pass a ready snapshot');\n\n return React.createElement(Context.Provider, { value: managed }, children);\n}\n/** Explicit binding for text attributes in isolated providers and SSR. */\nexport function useI18nRuntime(): Runtime {\n const managed = useContext(Context) ?? defaultManaged;\n useSyncExternalStore(managed.state.subscribe, managed.state.get, managed.state.get);\n\n return managed.runtime;\n}\n\nexport { createI18nManager, defineTranslationLoader } from './manager';\nexport type { I18nSnapshot, I18nStorage, ManagedI18n, StateData } from './manager';\n/** Bind an existing universal renderer to this provider (including client SSR). */\nexport function useI18nRenderer<F extends (...args: any[]) => any>(renderer: F): F {\n const runtime = useI18nRuntime();\n\n return ((...args: Parameters<F>) => withRuntime(runtime, () => renderer(...args))) as F;\n}\n"],"mappings":";;;;AAYA,MAAM,UAAU,cAAkC,IAAI;AAEtD,SAAS,kBAAkB,EAAE,UAAsC;CAEjE,MAAM,UADU,WAAW,OACL,CAAC,EAAE,WAAW,WAAW;CAC/C,qBAAqB,QAAQ,WAAW,QAAQ,aAAa,QAAQ,WAAW;CAEhF,OAAO,MAAM,cACX,MAAM,UACN,MACA,GAAG,OAAO,OAAO,CAAC,CAAC,KAAK,MAAM,UAC5B,MAAM,cAAc,MAAM,UAAU,EAAE,KAAK,MAAM,GAAG,IAAuB,CAC7E,CACF;AACF;AAEA,SAAgB,oBAAoB,SAAkB,YAA6B,MAAM,cAAc,IAAI,GAAS;CAClH,QAAQ,eAAe;CACvB,QAAQ,cAAc,UAAgC,WACpD,MAAM,cAAc,mBAAmB,EAAE,OAAO,CAAC;CACnD,QAAQ,cAAc,YAAY;EAChC,IAAI,CAAC,MAAM,eAAe,OAAO,GAAG,MAAM,IAAI,UAAU,iDAAiD;EAEzG,QAAQ,WAAW,MAAM,aAAa,SAAS,KAAA,GAAW,GAAG,MAA2B;CAC1F;AACF;AACA,oBAAoB,cAAc;AAClC,MAAa,cAAc,eAAe;AAC1C,MAAa,YAAY;CACvB,KAAK,eAAe,MAAM;CAC1B,WAAW,eAAe,MAAM;CAChC,MAAM;EACJ,MAAM,UAAU,WAAW,OAAO,KAAK;EAEvC,OAAO,qBAAqB,QAAQ,MAAM,WAAW,QAAQ,MAAM,KAAK,QAAQ,MAAM,GAAG;CAC3F;AACF;AAEA,SAAgB,aACd,EAAE,UAAU,UAAU,UAAU,aAMhC;CACA,MAAM,MAAM,OAEV,IAAI;CAEN,IACE,CAAC,IAAI,WAAW,IAAI,QAAQ,aAAa,YAAY,IAAI,QAAQ,aAAa,YAC9E,IAAI,QAAQ,cAAc,WAC1B;EACA,IAAI,YAAY,UAAU,MAAM,IAAI,MAAM,mDAAmD;EAE7F,MAAM,UAAU,YAAY,kBAAkB;EAC9C,oBAAoB,QAAQ,SAAS,SAAS;EAC9C,IAAI,UAAU,QAAQ,QAAQ,QAAQ,QAAQ;EAC9C,IAAI,UAAU;GAAE;GAAS;GAAU;GAAU;EAAU;CACzD;CAEA,MAAM,UAAU,IAAI,QAAQ;CAG5B,IAAI,CAFU,qBAAqB,QAAQ,MAAM,WAAW,QAAQ,MAAM,KAAK,QAAQ,MAAM,GAEpF,CAAC,CAAC,OAAO,MAAM,IAAI,MAAM,kEAAkE;CAEpG,OAAO,MAAM,cAAc,QAAQ,UAAU,EAAE,OAAO,QAAQ,GAAG,QAAQ;AAC3E;;AAEA,SAAgB,iBAA0B;CACxC,MAAM,UAAU,WAAW,OAAO,KAAK;CACvC,qBAAqB,QAAQ,MAAM,WAAW,QAAQ,MAAM,KAAK,QAAQ,MAAM,GAAG;CAElF,OAAO,QAAQ;AACjB;;AAKA,SAAgB,gBAAmD,UAAgB;CACjF,MAAM,UAAU,eAAe;CAE/B,SAAS,GAAG,SAAwB,YAAY,eAAe,SAAS,GAAG,IAAI,CAAC;AAClF"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { a as createTagHandler, c as getRuntime, d as setGetJsxHandler, f as setLanguageId, h as withRuntime, i as createRuntime, l as noTranslate, m as setTranslationGetter, o as defaultRuntime, p as setRuntimeResolver, r as I18nRegistry, s as getFormats, u as setFormats } from "./translation-manager-C79eKu2K.js";
|
|
2
|
+
import { t as isMessageDescriptor } from "./types-BkDsJfVD.js";
|
|
3
|
+
import { n as createFormatting, t as interpret } from "./interpreter-Dld8QyOI.js";
|
|
4
|
+
import { sha256 } from "@noble/hashes/sha256";
|
|
5
|
+
import { bytesToHex } from "@noble/hashes/utils";
|
|
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
|
+
return getJsx(toFormatOptions(message, params, scope));
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
const createDescriptor = (message, schema, scope) => {
|
|
52
|
+
return Object.freeze({
|
|
53
|
+
__fluenticMessage: true,
|
|
54
|
+
text: Array.isArray(message) ? message[0] : message,
|
|
55
|
+
...Array.isArray(message) ? { compiled: message } : {},
|
|
56
|
+
context: scope.context,
|
|
57
|
+
schema: schema ?? {},
|
|
58
|
+
notes: scope.notes,
|
|
59
|
+
meta: scope.meta
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
const toFormatOptions = (message, params, scope) => {
|
|
63
|
+
if (isMessageDescriptor(message)) return {
|
|
64
|
+
msg: message,
|
|
65
|
+
params
|
|
66
|
+
};
|
|
67
|
+
return {
|
|
68
|
+
msg: createDescriptor(message, void 0, scope),
|
|
69
|
+
params
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
const rootScope = createScope({
|
|
73
|
+
context: null,
|
|
74
|
+
notes: []
|
|
75
|
+
});
|
|
76
|
+
return Object.assign(rootScope, {
|
|
77
|
+
clone(message) {
|
|
78
|
+
return Object.freeze({ ...message });
|
|
79
|
+
},
|
|
80
|
+
params(params) {
|
|
81
|
+
return params ?? {};
|
|
82
|
+
},
|
|
83
|
+
tag(handler) {
|
|
84
|
+
if (typeof handler !== "function") {
|
|
85
|
+
const adapt = getRuntime().tagElement;
|
|
86
|
+
if (!adapt) throw new Error("[i18n] element tags require a platform adapter");
|
|
87
|
+
return createTagHandler(adapt(handler));
|
|
88
|
+
}
|
|
89
|
+
return createTagHandler(handler);
|
|
90
|
+
},
|
|
91
|
+
try(renderer, value, params) {
|
|
92
|
+
return isMessageDescriptor(value) ? renderer(value, params) : value;
|
|
93
|
+
},
|
|
94
|
+
notranslate: noTranslate
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
// src/core/format.ts
|
|
98
|
+
var format_exports = /* @__PURE__ */ __exportAll({
|
|
99
|
+
I18nRegistry: () => I18nRegistry,
|
|
100
|
+
createRuntime: () => createRuntime,
|
|
101
|
+
createTagHandler: () => createTagHandler,
|
|
102
|
+
defaultRuntime: () => defaultRuntime,
|
|
103
|
+
formatMessage: () => formatMessage,
|
|
104
|
+
getFormats: () => getFormats,
|
|
105
|
+
getJsx: () => getJsx,
|
|
106
|
+
getMessageParts: () => getMessageParts,
|
|
107
|
+
getRuntime: () => getRuntime,
|
|
108
|
+
getText: () => getText,
|
|
109
|
+
noTranslate: () => noTranslate,
|
|
110
|
+
setFormats: () => setFormats,
|
|
111
|
+
setGetJsxHandler: () => setGetJsxHandler,
|
|
112
|
+
setLanguageId: () => setLanguageId,
|
|
113
|
+
setRuntimeResolver: () => setRuntimeResolver,
|
|
114
|
+
setTranslationGetter: () => setTranslationGetter,
|
|
115
|
+
withRuntime: () => withRuntime
|
|
116
|
+
});
|
|
117
|
+
const cache = /* @__PURE__ */ new Map();
|
|
118
|
+
const formatting = /* @__PURE__ */ createFormatting((message, languageId, formats, values, compiled) => {
|
|
119
|
+
if (compiled) return interpret(message, languageId, formats, values, true);
|
|
120
|
+
if (typeof message !== "string") throw new TypeError("[i18n] invalid translation");
|
|
121
|
+
const key = JSON.stringify([
|
|
122
|
+
message,
|
|
123
|
+
languageId,
|
|
124
|
+
formats
|
|
125
|
+
]);
|
|
126
|
+
let formatter = cache.get(key);
|
|
127
|
+
if (!formatter) {
|
|
128
|
+
formatter = new IntlMessageFormat(message, languageId, formats);
|
|
129
|
+
if (cache.size >= 500) cache.delete(cache.keys().next().value);
|
|
130
|
+
cache.set(key, formatter);
|
|
131
|
+
}
|
|
132
|
+
return formatter.formatToParts(values).map((part) => part.value);
|
|
133
|
+
}, false, (context, message) => bytesToHex(sha256(context ? `${context}\0${message}` : message)).slice(0, 24));
|
|
134
|
+
const getText = (...args) => formatting.getText(...args);
|
|
135
|
+
const getJsx = (...args) => formatting.getJsx(...args);
|
|
136
|
+
const getMessageParts = (...args) => formatting.getMessageParts(...args);
|
|
137
|
+
const formatMessage = getText;
|
|
138
|
+
// src/index.ts
|
|
139
|
+
const t = /* @__PURE__ */ createT(format_exports);
|
|
140
|
+
export { t };
|
|
141
|
+
|
|
142
|
+
//# sourceMappingURL=src-BovRx-mX.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"src-BovRx-mX.js","names":["formatting"],"sources":["../src/core/i18n.ts","../src/core/format.ts","../src/index.ts"],"sourcesContent":["import type { Formatting } from '../shared/formatting';\nimport { createTagHandler, getRuntime, noTranslate } 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, params?: MessageParams) {\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] : 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 { sha256 } from '@noble/hashes/sha256';\nimport { bytesToHex } from '@noble/hashes/utils';\nimport { IntlMessageFormat } from 'intl-messageformat';\nimport { createFormatting } from '../shared/formatting';\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) => bytesToHex(sha256(context ? `${context}\\0${message}` : message)).slice(0, 24),\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 } from './translation-manager';\n"],"mappings":";;;;;;;;;;;;;;;;;;AAkDA,SAAgB,QAAQ,EAAE,SAAS,UAAyD;CAC1F,MAAM,eAAe,UAAoC;EACvD,OAAO;GACL,QAAQ,MAAM;IACZ,OAAO,YAAY;KAAE,GAAG;KAAO,SAAS,MAAM,UAAU,GAAG,MAAM,QAAQ,GAAG,SAAS;IAAK,CAAC;GAC7F;GACA,KAAK,MAAM;IACT,OAAO,YAAY;KAAE,GAAG;KAAO,OAAO,CAAC,GAAG,MAAM,OAAO,IAAI;IAAE,CAAC;GAChE;GACA,KAAK,MAAM;IACT,OAAO,YAAY;KAAE,GAAG;KAAO;IAAK,CAAC;GACvC;GACA,IAAI,SAAS,QAAQ;IACnB,OAAO,iBAAiB,SAAS,QAAQ,KAAK;GAChD;GACA,KAAK,SAAuB,QAAwB;IAClD,OAAO,QAAQ,gBAAgB,SAAS,QAAQ,KAAK,CAAC;GACxD;GACA,IAAI,SAAuB,QAAwB;IACjD,OAAO,OAAO,gBAAgB,SAAS,QAAQ,KAAK,CAAC;GACvD;EACF;CACF;CAEA,MAAM,oBACJ,SACA,QACA,UACoC;EACpC,OAAO,OAAO,OAAO;GACnB,mBAAmB;GACnB,MAAM,MAAM,QAAQ,OAAO,IAAI,QAAQ,KAAK;GAC5C,GAAI,MAAM,QAAQ,OAAO,IAAI,EAAE,UAAU,QAAQ,IAAI,CAAC;GACtD,SAAS,MAAM;GACf,QAAQ,UAAU,CAAC;GACnB,OAAO,MAAM;GACb,MAAM,MAAM;EACd,CAAC;CACH;CAEA,MAAM,mBAAmB,SAAuB,QAAmC,UAAwB;EACzG,IAAI,oBAAoB,OAAO,GAAG,OAAO;GAAE,KAAK;GAAS;EAAO;EAEhE,OAAO;GAAE,KAAK,iBAAiB,SAAS,KAAA,GAAW,KAAK;GAAG;EAAO;CACpE;CAEA,MAAM,YAAY,YAAY;EAAE,SAAS;EAAM,OAAO,CAAC;CAAE,CAAC;CAE1D,OAAO,OAAO,OAAO,WAAW;EAC9B,MAAM,SAA4B;GAChC,OAAO,OAAO,OAAO,EAAE,GAAG,QAAQ,CAAC;EACrC;EACA,OAAqC,QAAgC;GACnE,OAAQ,UAAU,CAAC;EACrB;EACA,IAA8B,SAA2C;GACvE,IAAI,OAAO,YAAY,YAAY;IACjC,MAAM,QAAQ,WAAW,CAAC,CAAC;IAE3B,IAAI,CAAC,OAAO,MAAM,IAAI,MAAM,gDAAgD;IAE5E,OAAO,iBAAiB,MAAM,OAAO,CAAC;GACxC;GAEA,OAAO,iBAAiB,OAAO;EACjC;EACA,IAAO,UAAsC,OAAU,QAAwB;GAC7E,OAAO,oBAAoB,KAAK,IAC3B,SAAwE,OAAO,MAAM,IACtF;EACN;EACA,aAAa;CACf,CAAC;AACH;;;;;;;;;;;;;;;;;;;;;ACrHA,MAAM,wBAAQ,IAAI,IAA+B;AACjD,MAAM,aAA6B,kCAChC,SAAS,YAAY,SAAS,QAAQ,aAAa;CAClD,IAAI,UAAU,OAAO,UAAU,SAAS,YAAY,SAAS,QAAQ,IAAI;CACzE,IAAI,OAAO,YAAY,UAAU,MAAM,IAAI,UAAU,4BAA4B;CAEjF,MAAM,MAAM,KAAK,UAAU;EAAC;EAAS;EAAY;CAAO,CAAC;CAEzD,IAAI,YAAY,MAAM,IAAI,GAAG;CAE7B,IAAI,CAAC,WAAW;EACd,YAAY,IAAI,kBACd,SACA,YACA,OACF;EACA,IAAI,MAAM,QAAQ,KAAK,MAAM,OAAO,MAAM,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,KAAM;EAC9D,MAAM,IAAI,KAAK,SAAS;CAC1B;CAEA,OAAO,UAAU,cAAc,MAA2D,CAAC,CAAC,KAAK,SAC/F,KAAK,KACP;AACF,GACA,QACC,SAAS,YAAY,WAAW,OAAO,UAAU,GAAG,QAAQ,IAAI,YAAY,OAAO,CAAC,CAAC,CAAC,MAAM,GAAG,EAAE,CACpG;AAGA,MAAa,WAAW,GAAG,SAAgD,WAAW,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;;ACpC7B,MAAa,IAAoB,wBAAQA,cAAU"}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { n as validateCompact } from "./compact-E3zj3Z8d.js";
|
|
2
|
+
// src/shared/runtime.ts
|
|
3
|
+
function createRuntime() {
|
|
4
|
+
return {
|
|
5
|
+
languageId: "en",
|
|
6
|
+
ready: false,
|
|
7
|
+
translationsEnabled: true,
|
|
8
|
+
translationGetter: null,
|
|
9
|
+
formats: {},
|
|
10
|
+
registry: {
|
|
11
|
+
number: {},
|
|
12
|
+
date: {},
|
|
13
|
+
time: {}
|
|
14
|
+
},
|
|
15
|
+
jsxLineBreak: "\n",
|
|
16
|
+
jsxHandler: 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
|
+
const I18nRegistry = {
|
|
43
|
+
addNumberFormat(name, format) {
|
|
44
|
+
getRuntime().registry.number[name] = format;
|
|
45
|
+
},
|
|
46
|
+
addDateFormat(name, format) {
|
|
47
|
+
getRuntime().registry.date[name] = format;
|
|
48
|
+
},
|
|
49
|
+
addTimeFormat(name, format) {
|
|
50
|
+
getRuntime().registry.time[name] = format;
|
|
51
|
+
},
|
|
52
|
+
setLanguageId,
|
|
53
|
+
getConfig() {
|
|
54
|
+
const r = getRuntime().registry;
|
|
55
|
+
return {
|
|
56
|
+
number: { ...r.number },
|
|
57
|
+
date: { ...r.date },
|
|
58
|
+
time: { ...r.time }
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
function getFormats(runtime, languageId) {
|
|
63
|
+
const resolve = (entries) => Object.fromEntries(Object.entries(entries).map(([key, value]) => [key, typeof value === "function" ? value({ languageId }) : value]));
|
|
64
|
+
return {
|
|
65
|
+
number: {
|
|
66
|
+
...runtime.formats.number,
|
|
67
|
+
...resolve(runtime.registry.number)
|
|
68
|
+
},
|
|
69
|
+
date: {
|
|
70
|
+
...runtime.formats.date,
|
|
71
|
+
...resolve(runtime.registry.date)
|
|
72
|
+
},
|
|
73
|
+
time: {
|
|
74
|
+
...runtime.formats.time,
|
|
75
|
+
...resolve(runtime.registry.time)
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function noTranslate(fn) {
|
|
80
|
+
const runtime = getRuntime();
|
|
81
|
+
const previous = runtime.translationsEnabled;
|
|
82
|
+
runtime.translationsEnabled = false;
|
|
83
|
+
try {
|
|
84
|
+
return fn();
|
|
85
|
+
} finally {
|
|
86
|
+
runtime.translationsEnabled = previous;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function createTagHandler(handler) {
|
|
90
|
+
Object.defineProperty(handler, "__fluenticTagHandler", { value: true });
|
|
91
|
+
return handler;
|
|
92
|
+
}
|
|
93
|
+
/** Synchronous binding only: async callers must keep and use their own instance. */
|
|
94
|
+
function withRuntime(runtime, fn) {
|
|
95
|
+
const previous = resolver;
|
|
96
|
+
resolver = () => runtime;
|
|
97
|
+
try {
|
|
98
|
+
return fn();
|
|
99
|
+
} finally {
|
|
100
|
+
resolver = previous;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// src/translation.ts
|
|
104
|
+
function parseCatalog(data) {
|
|
105
|
+
const catalog = Object.create(null);
|
|
106
|
+
const rows = parseRows(data);
|
|
107
|
+
const header = rows[0];
|
|
108
|
+
const hasHeader = isCompiledHeader(header);
|
|
109
|
+
const compiled = hasHeader || Array.isArray(data) && rows.some((row) => Array.isArray(row) && row.length === 2 && Array.isArray(row[1]));
|
|
110
|
+
if (hasHeader) rows.shift();
|
|
111
|
+
if (Array.isArray(data) && rows.at(-1) === null) rows.pop();
|
|
112
|
+
for (const row of rows) if (Array.isArray(row)) {
|
|
113
|
+
const [id, message] = compiled ? parseCompiledRow(row) : parseTranslationRow(row, Array.isArray(data));
|
|
114
|
+
if (compiled || message !== "") catalog[id] = message;
|
|
115
|
+
} else {
|
|
116
|
+
if (compiled) throw new TypeError("[i18n] invalid compiled catalog row");
|
|
117
|
+
const item = parseTranslationItem(row);
|
|
118
|
+
if (item.translation !== "") catalog[item.id] = item.translation;
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
catalog,
|
|
122
|
+
compiled
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function parseRows(data) {
|
|
126
|
+
if (Array.isArray(data)) return [...data];
|
|
127
|
+
return (typeof data === "string" ? data : data.jsonl).split(/\r?\n/).filter((line) => line.trim()).map((line) => JSON.parse(line));
|
|
128
|
+
}
|
|
129
|
+
function isCompiledHeader(row) {
|
|
130
|
+
return typeof row === "object" && row !== null && !Array.isArray(row) && "compiled" in row && row.compiled === true;
|
|
131
|
+
}
|
|
132
|
+
function parseCompiledRow(row) {
|
|
133
|
+
if (row.length !== 2 || typeof row[0] !== "string") throw new TypeError("[i18n] invalid compiled catalog row");
|
|
134
|
+
validateCompact(row[1]);
|
|
135
|
+
return [row[0], row[1]];
|
|
136
|
+
}
|
|
137
|
+
function parseTranslationRow(row, allowCompact) {
|
|
138
|
+
if (row.length === 2 && typeof row[0] === "string") {
|
|
139
|
+
if (allowCompact) validateCompact(row[1]);
|
|
140
|
+
else if (typeof row[1] !== "string") throw new TypeError("[i18n] invalid translation row");
|
|
141
|
+
return [row[0], row[1]];
|
|
142
|
+
}
|
|
143
|
+
if (row.length === 3 && row.every((value) => typeof value === "string")) return [`${row[0]}\0${row[1]}`, row[2]];
|
|
144
|
+
throw new TypeError("[i18n] invalid translation row");
|
|
145
|
+
}
|
|
146
|
+
function parseTranslationItem(row) {
|
|
147
|
+
const item = row;
|
|
148
|
+
if (!item || typeof item.translation !== "string") throw new TypeError("[i18n] invalid translation item");
|
|
149
|
+
const source = item.source ?? (Array.isArray(item.message) ? item.message[1] : item.message);
|
|
150
|
+
const context = Array.isArray(item.message) ? item.message[0] : item.context;
|
|
151
|
+
const id = item.id ?? (source === void 0 ? void 0 : context ? `${context}\0${source}` : source);
|
|
152
|
+
if (typeof id !== "string") throw new TypeError("[i18n] translation id is missing");
|
|
153
|
+
return {
|
|
154
|
+
id,
|
|
155
|
+
translation: item.translation
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
// src/translation-manager.ts
|
|
159
|
+
function defineTranslationLoader(loader) {
|
|
160
|
+
if (typeof loader === "function") return loader;
|
|
161
|
+
return async ({ languageId }) => {
|
|
162
|
+
if (!Object.prototype.hasOwnProperty.call(loader, languageId)) return [];
|
|
163
|
+
const entry = loader[languageId];
|
|
164
|
+
const source = await (typeof entry === "function" ? entry() : entry);
|
|
165
|
+
return typeof source === "object" && source !== null && "default" in source ? source.default : source;
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
function createTranslationManager() {
|
|
169
|
+
const catalogs = Object.create(null);
|
|
170
|
+
const pending = /* @__PURE__ */ new Map();
|
|
171
|
+
const compiledCatalogs = Object.create(null);
|
|
172
|
+
let loader = null;
|
|
173
|
+
let generation = 0;
|
|
174
|
+
const setTranslationLoader = (next) => {
|
|
175
|
+
loader = next;
|
|
176
|
+
generation++;
|
|
177
|
+
pending.clear();
|
|
178
|
+
for (const key of Object.keys(catalogs)) delete catalogs[key];
|
|
179
|
+
for (const key of Object.keys(compiledCatalogs)) delete compiledCatalogs[key];
|
|
180
|
+
};
|
|
181
|
+
const loadTranslations = async (languageId) => {
|
|
182
|
+
if (!loader || Object.prototype.hasOwnProperty.call(catalogs, languageId)) return;
|
|
183
|
+
if (pending.has(languageId)) return pending.get(languageId);
|
|
184
|
+
const version = generation;
|
|
185
|
+
const currentLoader = loader;
|
|
186
|
+
const task = Promise.resolve().then(() => currentLoader({ languageId })).then((data) => {
|
|
187
|
+
if (version === generation) {
|
|
188
|
+
const result = parseCatalog(data);
|
|
189
|
+
catalogs[languageId] = result.catalog;
|
|
190
|
+
compiledCatalogs[languageId] = result.compiled;
|
|
191
|
+
}
|
|
192
|
+
}).finally(() => {
|
|
193
|
+
if (version === generation) pending.delete(languageId);
|
|
194
|
+
});
|
|
195
|
+
pending.set(languageId, task);
|
|
196
|
+
return task;
|
|
197
|
+
};
|
|
198
|
+
const getTranslation = (args) => {
|
|
199
|
+
const catalog = catalogs[args.languageId];
|
|
200
|
+
const id = args.context ? `${args.context}\0${args.message}` : args.message;
|
|
201
|
+
return catalog?.[id] ?? catalog?.[args.message] ?? null;
|
|
202
|
+
};
|
|
203
|
+
return {
|
|
204
|
+
catalogs,
|
|
205
|
+
compiledCatalogs,
|
|
206
|
+
setTranslationLoader,
|
|
207
|
+
loadTranslations,
|
|
208
|
+
getTranslation
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
export { createTagHandler as a, getRuntime as c, setGetJsxHandler as d, setLanguageId as f, withRuntime as h, createRuntime as i, noTranslate as l, setTranslationGetter as m, defineTranslationLoader as n, defaultRuntime as o, setRuntimeResolver as p, I18nRegistry as r, getFormats as s, createTranslationManager as t, setFormats as u };
|
|
212
|
+
|
|
213
|
+
//# sourceMappingURL=translation-manager-C79eKu2K.js.map
|