@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,186 @@
|
|
|
1
|
+
import { c as getRuntime, s as getFormats } from "./translation-manager-C79eKu2K.js";
|
|
2
|
+
import { n as isMessageSchemaValue, r as isMessageTagHandler, t as isMessageDescriptor } from "./types-BkDsJfVD.js";
|
|
3
|
+
// src/shared/formatting.ts
|
|
4
|
+
function createFormatting(engine, compiled = false, getCompiledId) {
|
|
5
|
+
const getMessageParts = (options, runtime = getRuntime(), jsx = false) => {
|
|
6
|
+
if (!runtime.ready) throw new Error("[i18n] runtime is not ready; await adapter initialization before rendering");
|
|
7
|
+
const languageId = options.languageId ?? runtime.languageId;
|
|
8
|
+
const descriptor = isMessageDescriptor(options.msg) ? options.msg : {
|
|
9
|
+
text: options.msg,
|
|
10
|
+
context: null,
|
|
11
|
+
schema: {},
|
|
12
|
+
compiled: void 0
|
|
13
|
+
};
|
|
14
|
+
if (compiled && !descriptor.compiled) throw new TypeError("[i18n] compiled runtime requires compiled messages; enable the Fluentic plugin");
|
|
15
|
+
const key = descriptor.compiled?.[0] ?? descriptor.text;
|
|
16
|
+
const translated = runtime.translationsEnabled ? runtime.translationGetter?.({
|
|
17
|
+
languageId,
|
|
18
|
+
context: descriptor.compiled ? null : descriptor.context,
|
|
19
|
+
message: key,
|
|
20
|
+
getCompiledId: getCompiledId && !descriptor.compiled ? () => getCompiledId(descriptor.context, descriptor.text) : void 0
|
|
21
|
+
}) : null;
|
|
22
|
+
const compiledTranslation = typeof translated === "object" && translated !== null && !Array.isArray(translated);
|
|
23
|
+
if (compiled && typeof translated === "string") throw new TypeError("[i18n] compiled runtime requires compiled translations");
|
|
24
|
+
const message = compiledTranslation ? translated.compiled : translated ?? descriptor.compiled?.[1] ?? descriptor.text;
|
|
25
|
+
const isCompiled = compiled || compiledTranslation || Array.isArray(message);
|
|
26
|
+
const values = {};
|
|
27
|
+
const resolve = (value) => {
|
|
28
|
+
if (isMessageTagHandler(value)) return value;
|
|
29
|
+
if (isMessageSchemaValue(value)) throw new Error("[i18n] Message parameter schemas are only valid in t.msg(); pass a real value");
|
|
30
|
+
if (typeof value === "function") return resolve(value({ languageId }));
|
|
31
|
+
if (isMessageDescriptor(value)) return getText({
|
|
32
|
+
msg: value,
|
|
33
|
+
languageId
|
|
34
|
+
}, runtime);
|
|
35
|
+
return value;
|
|
36
|
+
};
|
|
37
|
+
for (const [key, value] of Object.entries(options.params ?? {})) values[key] = resolve(value);
|
|
38
|
+
values.break = jsx ? runtime.jsxLineBreak : "\n";
|
|
39
|
+
for (const [key, expected] of Object.entries(descriptor.schema)) {
|
|
40
|
+
if (key !== "break" && (!options.params || options.params[key] === void 0)) throw new Error("[i18n] Missing required message parameter: " + key);
|
|
41
|
+
const value = values[key];
|
|
42
|
+
if (!(expected === String ? typeof value === "string" : expected === Number ? typeof value === "number" : expected === Boolean ? typeof value === "boolean" : expected === Date ? value instanceof Date && !Number.isNaN(value.getTime()) : true)) throw new Error("[i18n] Message parameter \"" + key + "\" must be a " + expected.name);
|
|
43
|
+
}
|
|
44
|
+
return engine(message, languageId, getFormats(runtime, languageId), values, isCompiled);
|
|
45
|
+
};
|
|
46
|
+
const getText = (options, runtime = getRuntime()) => {
|
|
47
|
+
const parts = getMessageParts(options, runtime);
|
|
48
|
+
if (parts.some((part) => typeof part !== "string")) throw new Error("[i18n] t.text() received a non-text rich value; use t.jsx() instead");
|
|
49
|
+
return parts.join("");
|
|
50
|
+
};
|
|
51
|
+
const getJsx = (options) => {
|
|
52
|
+
const runtime = getRuntime();
|
|
53
|
+
if (!runtime.jsxHandler) throw new Error("[i18n] JSX renderer is not configured; initialize a platform adapter");
|
|
54
|
+
const enabled = runtime.translationsEnabled;
|
|
55
|
+
return runtime.jsxHandler(options, (current = getRuntime()) => getMessageParts(options, enabled ? current : {
|
|
56
|
+
...current,
|
|
57
|
+
translationsEnabled: false
|
|
58
|
+
}, true));
|
|
59
|
+
};
|
|
60
|
+
return {
|
|
61
|
+
getText,
|
|
62
|
+
getJsx,
|
|
63
|
+
getMessageParts,
|
|
64
|
+
formatMessage: getText
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
// src/shared/interpreter.ts
|
|
68
|
+
const numberCache = /* @__PURE__ */ new Map();
|
|
69
|
+
const dateCache = /* @__PURE__ */ new Map();
|
|
70
|
+
const pluralCache = /* @__PURE__ */ new Map();
|
|
71
|
+
function cached(map, key, create) {
|
|
72
|
+
let value = map.get(key);
|
|
73
|
+
if (!value) {
|
|
74
|
+
value = create();
|
|
75
|
+
if (map.size >= 100) map.delete(map.keys().next().value);
|
|
76
|
+
map.set(key, value);
|
|
77
|
+
}
|
|
78
|
+
return value;
|
|
79
|
+
}
|
|
80
|
+
const numberDefaults = {
|
|
81
|
+
integer: { maximumFractionDigits: 0 },
|
|
82
|
+
currency: { style: "currency" },
|
|
83
|
+
percent: { style: "percent" }
|
|
84
|
+
};
|
|
85
|
+
const dateDefaults = {
|
|
86
|
+
short: {
|
|
87
|
+
month: "numeric",
|
|
88
|
+
day: "numeric",
|
|
89
|
+
year: "2-digit"
|
|
90
|
+
},
|
|
91
|
+
medium: {
|
|
92
|
+
month: "short",
|
|
93
|
+
day: "numeric",
|
|
94
|
+
year: "numeric"
|
|
95
|
+
},
|
|
96
|
+
long: {
|
|
97
|
+
month: "long",
|
|
98
|
+
day: "numeric",
|
|
99
|
+
year: "numeric"
|
|
100
|
+
},
|
|
101
|
+
full: {
|
|
102
|
+
weekday: "long",
|
|
103
|
+
month: "long",
|
|
104
|
+
day: "numeric",
|
|
105
|
+
year: "numeric"
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
const timeDefaults = {
|
|
109
|
+
short: {
|
|
110
|
+
hour: "numeric",
|
|
111
|
+
minute: "numeric"
|
|
112
|
+
},
|
|
113
|
+
medium: {
|
|
114
|
+
hour: "numeric",
|
|
115
|
+
minute: "numeric",
|
|
116
|
+
second: "numeric"
|
|
117
|
+
},
|
|
118
|
+
long: {
|
|
119
|
+
hour: "numeric",
|
|
120
|
+
minute: "numeric",
|
|
121
|
+
second: "numeric",
|
|
122
|
+
timeZoneName: "short"
|
|
123
|
+
},
|
|
124
|
+
full: {
|
|
125
|
+
hour: "numeric",
|
|
126
|
+
minute: "numeric",
|
|
127
|
+
second: "numeric",
|
|
128
|
+
timeZoneName: "long"
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
const interpret = (message, locale, formats, values) => {
|
|
132
|
+
const number = (value, options) => cached(numberCache, JSON.stringify([locale, options]), () => new Intl.NumberFormat(locale, options)).format(value);
|
|
133
|
+
const render = (message, pound) => {
|
|
134
|
+
if (typeof message === "string") return [message];
|
|
135
|
+
const output = [];
|
|
136
|
+
const append = (value) => {
|
|
137
|
+
if (Array.isArray(value)) for (const part of value) append(part);
|
|
138
|
+
else if (typeof value === "string" && typeof output[output.length - 1] === "string") output[output.length - 1] += value;
|
|
139
|
+
else output.push(value);
|
|
140
|
+
};
|
|
141
|
+
for (const part of message) {
|
|
142
|
+
if (typeof part === "string") {
|
|
143
|
+
append(part);
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
const op = part[0];
|
|
147
|
+
if (op === 6) {
|
|
148
|
+
if (pound !== void 0) append(number(pound));
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const name = part[1];
|
|
152
|
+
if (!(name in values)) throw new Error("[i18n] Missing required message parameter: " + name);
|
|
153
|
+
const value = values[name];
|
|
154
|
+
if (op === 0) {
|
|
155
|
+
append(typeof value === "string" || typeof value === "number" ? String(value) : value || "");
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (op >= 1 && op <= 3) {
|
|
159
|
+
const style = part[2];
|
|
160
|
+
const kind = op === 1 ? "number" : op === 2 ? "date" : "time";
|
|
161
|
+
const defaults = op === 1 ? numberDefaults : op === 2 ? dateDefaults : timeDefaults;
|
|
162
|
+
const options = typeof style === "string" ? formats[kind]?.[style] ?? defaults[style] : style ?? (op === 3 ? timeDefaults.medium : void 0);
|
|
163
|
+
if (op === 1) append(number(Number(value) * Number(options?.scale ?? 1), options));
|
|
164
|
+
else append(cached(dateCache, JSON.stringify([locale, options]), () => new Intl.DateTimeFormat(locale, options)).format(value));
|
|
165
|
+
} else if (op === 4) {
|
|
166
|
+
const branches = part[2];
|
|
167
|
+
append(render(Object.prototype.hasOwnProperty.call(branches, String(value)) ? branches[String(value)] : branches.other));
|
|
168
|
+
} else if (op === 5) {
|
|
169
|
+
const count = Number(value);
|
|
170
|
+
const adjusted = count - part[2];
|
|
171
|
+
const branches = part[4];
|
|
172
|
+
const exact = "=" + count;
|
|
173
|
+
const key = Object.prototype.hasOwnProperty.call(branches, exact) ? exact : cached(pluralCache, locale + ":" + part[3], () => new Intl.PluralRules(locale, { type: part[3] ? "ordinal" : "cardinal" })).select(adjusted);
|
|
174
|
+
append(render(branches[key] ?? branches.other, adjusted));
|
|
175
|
+
} else if (op === 7) {
|
|
176
|
+
if (typeof value !== "function") throw new TypeError("[i18n] rich tag " + name + " requires a handler");
|
|
177
|
+
append(value(render(part[2], pound)));
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return output;
|
|
181
|
+
};
|
|
182
|
+
return render(message);
|
|
183
|
+
};
|
|
184
|
+
export { createFormatting as n, interpret as t };
|
|
185
|
+
|
|
186
|
+
//# sourceMappingURL=interpreter-Dld8QyOI.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interpreter-Dld8QyOI.js","names":[],"sources":["../src/shared/formatting.ts","../src/shared/interpreter.ts"],"sourcesContent":["import { isMessageDescriptor, isMessageSchemaValue, isMessageTagHandler } from '../core/types';\nimport type { MessageFormatOptions, MessageParam } from '../core/types';\nimport type { CompactMessage } from './compact';\nimport { type Formats, getFormats, getRuntime, type Runtime } from './runtime';\n\nexport type Engine = (\n message: CompactMessage,\n languageId: string,\n formats: Partial<Formats>,\n values: Record<string, unknown>,\n compiled: boolean,\n) => unknown[];\n\nexport type Formatting = ReturnType<typeof createFormatting>;\n\nexport function createFormatting(\n engine: Engine,\n compiled = false,\n getCompiledId?: (context: string | null, message: string) => string,\n) {\n const getMessageParts = (options: MessageFormatOptions, runtime = getRuntime(), jsx = false): unknown[] => {\n if (!runtime.ready) throw new Error('[i18n] runtime is not ready; await adapter initialization before rendering');\n\n const languageId = options.languageId ?? runtime.languageId;\n\n const descriptor = isMessageDescriptor(options.msg)\n ? options.msg\n : { text: options.msg, context: null, schema: {}, compiled: undefined };\n\n if (compiled && !descriptor.compiled) {\n throw new TypeError('[i18n] compiled runtime requires compiled messages; enable the Fluentic plugin');\n }\n\n const key = descriptor.compiled?.[0] ?? descriptor.text;\n\n const translated = runtime.translationsEnabled\n ? runtime.translationGetter?.({\n languageId,\n context: descriptor.compiled ? null : descriptor.context,\n message: key,\n getCompiledId: getCompiledId && !descriptor.compiled\n ? () => getCompiledId(descriptor.context, descriptor.text)\n : undefined,\n })\n : null;\n const compiledTranslation = typeof translated === 'object' && translated !== null && !Array.isArray(translated);\n if (compiled && typeof translated === 'string') {\n throw new TypeError('[i18n] compiled runtime requires compiled translations');\n }\n\n const message = compiledTranslation\n ? translated.compiled\n : translated ?? descriptor.compiled?.[1] ?? descriptor.text;\n const isCompiled = compiled || compiledTranslation || Array.isArray(message);\n const values: Record<string, unknown> = {};\n\n const resolve = (value: MessageParam): unknown => {\n if (isMessageTagHandler(value)) return value;\n if (isMessageSchemaValue(value)) {\n throw new Error('[i18n] Message parameter schemas are only valid in t.msg(); pass a real value');\n }\n if (typeof value === 'function') return resolve(value({ languageId }) as MessageParam);\n if (isMessageDescriptor(value)) return getText({ msg: value, languageId }, runtime);\n\n return value;\n };\n\n for (const [key, value] of Object.entries(options.params ?? {})) values[key] = resolve(value);\n values.break = jsx ? runtime.jsxLineBreak : '\\n';\n\n for (const [key, expected] of Object.entries(descriptor.schema)) {\n if (key !== 'break' && (!options.params || options.params[key] === undefined)) {\n throw new Error('[i18n] Missing required message parameter: ' + key);\n }\n\n const value = values[key];\n\n const valid = expected === String\n ? typeof value === 'string'\n : expected === Number\n ? typeof value === 'number'\n : expected === Boolean\n ? typeof value === 'boolean'\n : expected === Date\n ? value instanceof Date && !Number.isNaN(value.getTime())\n : true;\n\n if (!valid) {\n throw new Error('[i18n] Message parameter \"' + key + '\" must be a ' + (expected as { name: string; }).name);\n }\n }\n\n return engine(message, languageId, getFormats(runtime, languageId), values, isCompiled);\n };\n\n const getText = (options: MessageFormatOptions, runtime = getRuntime()): string => {\n const parts = getMessageParts(options, runtime);\n\n if (parts.some((part) => typeof part !== 'string')) {\n throw new Error('[i18n] t.text() received a non-text rich value; use t.jsx() instead');\n }\n\n return parts.join('');\n };\n\n const getJsx = (options: MessageFormatOptions): unknown => {\n const runtime = getRuntime();\n\n if (!runtime.jsxHandler) throw new Error('[i18n] JSX renderer is not configured; initialize a platform adapter');\n // Capture notranslate at element creation, not at React's later render.\n const enabled = runtime.translationsEnabled;\n\n return runtime.jsxHandler(\n options,\n (current = getRuntime()) =>\n getMessageParts(options, enabled ? current : { ...current, translationsEnabled: false }, true),\n );\n };\n\n return { getText, getJsx, getMessageParts, formatMessage: getText };\n}\n","import type { CompactMessage } from './compact';\nimport type { Engine } from './formatting';\n\nconst numberCache = new Map<string, Intl.NumberFormat>();\nconst dateCache = new Map<string, Intl.DateTimeFormat>();\nconst pluralCache = new Map<string, Intl.PluralRules>();\n\nfunction cached<T>(map: Map<string, T>, key: string, create: () => T): T {\n let value = map.get(key);\n\n if (!value) {\n value = create();\n if (map.size >= 100) map.delete(map.keys().next().value!);\n map.set(key, value);\n }\n\n return value;\n}\n\nconst numberDefaults = {\n integer: { maximumFractionDigits: 0 },\n currency: { style: 'currency' },\n percent: { style: 'percent' },\n};\nconst dateDefaults = {\n short: { month: 'numeric', day: 'numeric', year: '2-digit' },\n medium: { month: 'short', day: 'numeric', year: 'numeric' },\n long: { month: 'long', day: 'numeric', year: 'numeric' },\n full: { weekday: 'long', month: 'long', day: 'numeric', year: 'numeric' },\n};\nconst timeDefaults = {\n short: { hour: 'numeric', minute: 'numeric' },\n medium: { hour: 'numeric', minute: 'numeric', second: 'numeric' },\n long: { hour: 'numeric', minute: 'numeric', second: 'numeric', timeZoneName: 'short' },\n full: { hour: 'numeric', minute: 'numeric', second: 'numeric', timeZoneName: 'long' },\n};\nexport const interpret: Engine = (message, locale, formats, values) => {\n const number = (value: number, options?: Intl.NumberFormatOptions) =>\n cached(numberCache, JSON.stringify([locale, options]), () => new Intl.NumberFormat(locale, options)).format(value);\n\n const render = (message: CompactMessage, pound?: number): unknown[] => {\n if (typeof message === 'string') return [message];\n\n const output: unknown[] = [];\n\n const append = (value: unknown) => {\n if (Array.isArray(value)) {\n for (const part of value) append(part);\n } else if (typeof value === 'string' && typeof output[output.length - 1] === 'string') {\n output[output.length - 1] += value;\n } else output.push(value);\n };\n\n for (const part of message) {\n if (typeof part === 'string') {\n append(part);\n continue;\n }\n\n const op = part[0];\n\n if (op === 6) {\n if (pound !== undefined) append(number(pound));\n continue;\n }\n\n const name = part[1];\n\n if (!(name in values)) throw new Error('[i18n] Missing required message parameter: ' + name);\n\n const value = values[name];\n\n if (op === 0) {\n append(typeof value === 'string' || typeof value === 'number' ? String(value) : value || '');\n continue;\n }\n if (op >= 1 && op <= 3) {\n const style = part[2] as string | Record<string, unknown> | null;\n const kind = op === 1 ? 'number' : op === 2 ? 'date' : 'time';\n const defaults = op === 1 ? numberDefaults : op === 2 ? dateDefaults : timeDefaults;\n\n const options = (typeof style === 'string'\n ? formats[kind]?.[style] ?? (defaults as Record<string, object>)[style]\n : style ?? (op === 3 ? timeDefaults.medium : undefined)) as Record<string, unknown> | undefined;\n\n if (op === 1) {\n append(number(Number(value) * (Number(options?.scale ?? 1)), options));\n } else {\n append(\n cached(\n dateCache,\n JSON.stringify([locale, options]),\n () => new Intl.DateTimeFormat(locale, options as Intl.DateTimeFormatOptions),\n ).format(value as Date),\n );\n }\n } else if (op === 4) {\n const branches = part[2];\n append(\n render(\n Object.prototype.hasOwnProperty.call(branches, String(value)) ? branches[String(value)] : branches.other,\n ),\n );\n } else if (op === 5) {\n const count = Number(value);\n const adjusted = count - part[2];\n const branches = part[4];\n const exact = '=' + count;\n\n const key = Object.prototype.hasOwnProperty.call(branches, exact)\n ? exact\n : cached(\n pluralCache,\n locale + ':' + part[3],\n () => new Intl.PluralRules(locale, { type: part[3] ? 'ordinal' : 'cardinal' }),\n ).select(adjusted);\n append(render(branches[key] ?? branches.other, adjusted));\n } else if (op === 7) {\n if (typeof value !== 'function') throw new TypeError('[i18n] rich tag ' + name + ' requires a handler');\n append(value(render(part[2], pound)));\n }\n }\n\n return output;\n };\n\n return render(message);\n};\n"],"mappings":";;;AAeA,SAAgB,iBACd,QACA,WAAW,OACX,eACA;CACA,MAAM,mBAAmB,SAA+B,UAAU,WAAW,GAAG,MAAM,UAAqB;EACzG,IAAI,CAAC,QAAQ,OAAO,MAAM,IAAI,MAAM,4EAA4E;EAEhH,MAAM,aAAa,QAAQ,cAAc,QAAQ;EAEjD,MAAM,aAAa,oBAAoB,QAAQ,GAAG,IAC9C,QAAQ,MACR;GAAE,MAAM,QAAQ;GAAK,SAAS;GAAM,QAAQ,CAAC;GAAG,UAAU,KAAA;EAAU;EAExE,IAAI,YAAY,CAAC,WAAW,UAC1B,MAAM,IAAI,UAAU,gFAAgF;EAGtG,MAAM,MAAM,WAAW,WAAW,MAAM,WAAW;EAEnD,MAAM,aAAa,QAAQ,sBACvB,QAAQ,oBAAoB;GAC5B;GACA,SAAS,WAAW,WAAW,OAAO,WAAW;GACjD,SAAS;GACT,eAAe,iBAAiB,CAAC,WAAW,iBAClC,cAAc,WAAW,SAAS,WAAW,IAAI,IACvD,KAAA;EACN,CAAC,IACC;EACJ,MAAM,sBAAsB,OAAO,eAAe,YAAY,eAAe,QAAQ,CAAC,MAAM,QAAQ,UAAU;EAC9G,IAAI,YAAY,OAAO,eAAe,UACpC,MAAM,IAAI,UAAU,wDAAwD;EAG9E,MAAM,UAAU,sBACZ,WAAW,WACX,cAAc,WAAW,WAAW,MAAM,WAAW;EACzD,MAAM,aAAa,YAAY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|
+
// src/state-manager.ts
|
|
3
|
+
function createStateManager(initial) {
|
|
4
|
+
let current = initial;
|
|
5
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
6
|
+
return {
|
|
7
|
+
get: () => current,
|
|
8
|
+
subscribe: (listener) => {
|
|
9
|
+
listeners.add(listener);
|
|
10
|
+
return () => listeners.delete(listener);
|
|
11
|
+
},
|
|
12
|
+
set(partial) {
|
|
13
|
+
const next = {
|
|
14
|
+
...current,
|
|
15
|
+
...partial
|
|
16
|
+
};
|
|
17
|
+
if (!Object.keys(next).some((key) => current[key] !== next[key])) return;
|
|
18
|
+
current = next;
|
|
19
|
+
for (const listener of listeners) listener();
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
// src/storage-manager.ts
|
|
24
|
+
function createStorageManager(initial = null) {
|
|
25
|
+
let storage = initial;
|
|
26
|
+
return {
|
|
27
|
+
setStorage(next) {
|
|
28
|
+
storage = next;
|
|
29
|
+
},
|
|
30
|
+
getStorage() {
|
|
31
|
+
if (!storage) throw new Error("[i18n] storage is not configured");
|
|
32
|
+
return storage;
|
|
33
|
+
},
|
|
34
|
+
hasStorage() {
|
|
35
|
+
return storage !== null;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function createLocalStorage() {
|
|
40
|
+
if (typeof globalThis.localStorage === "undefined") return null;
|
|
41
|
+
return {
|
|
42
|
+
read() {
|
|
43
|
+
const value = globalThis.localStorage.getItem("fluentic:i18n:state");
|
|
44
|
+
if (!value) return null;
|
|
45
|
+
try {
|
|
46
|
+
return JSON.parse(value);
|
|
47
|
+
} catch {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
write(state) {
|
|
52
|
+
globalThis.localStorage.setItem("fluentic:i18n:state", JSON.stringify(state));
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
// src/manager.ts
|
|
57
|
+
function createI18nManager(options = {}) {
|
|
58
|
+
const runtime = options.runtime ?? createRuntime();
|
|
59
|
+
const state = createStateManager({
|
|
60
|
+
languageId: runtime.languageId,
|
|
61
|
+
isLoading: false,
|
|
62
|
+
ready: runtime.ready
|
|
63
|
+
});
|
|
64
|
+
const storage = createStorageManager(options.storage ?? null);
|
|
65
|
+
const translations = createTranslationManager();
|
|
66
|
+
let fallbackLanguageId;
|
|
67
|
+
let version = 0;
|
|
68
|
+
runtime.subscribe = state.subscribe;
|
|
69
|
+
runtime.getSnapshot = state.get;
|
|
70
|
+
const configure = (config) => {
|
|
71
|
+
runtime.languageId = config.languageId;
|
|
72
|
+
runtime.ready = false;
|
|
73
|
+
fallbackLanguageId = config.fallbackLanguageId;
|
|
74
|
+
version++;
|
|
75
|
+
state.set({
|
|
76
|
+
languageId: config.languageId,
|
|
77
|
+
ready: false
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
const setTranslationLoader = (loader) => {
|
|
81
|
+
translations.setTranslationLoader(loader);
|
|
82
|
+
runtime.ready = false;
|
|
83
|
+
state.set({ ready: false });
|
|
84
|
+
};
|
|
85
|
+
const init = async () => {
|
|
86
|
+
const saved = storage.hasStorage() ? await storage.getStorage().read() : null;
|
|
87
|
+
await loadTranslations(saved?.languageId ?? runtime.languageId);
|
|
88
|
+
};
|
|
89
|
+
const setLanguageId = async (languageId) => {
|
|
90
|
+
if (await loadTranslations(languageId) && storage.hasStorage()) await storage.getStorage().write({ languageId });
|
|
91
|
+
};
|
|
92
|
+
const getLanguageId = () => {
|
|
93
|
+
return runtime.languageId;
|
|
94
|
+
};
|
|
95
|
+
const getTranslation = (args) => {
|
|
96
|
+
for (const languageId of getLanguageIds(args.languageId ?? runtime.languageId)) {
|
|
97
|
+
const value = translations.getTranslation({
|
|
98
|
+
...args,
|
|
99
|
+
languageId,
|
|
100
|
+
context: args.context ?? null
|
|
101
|
+
});
|
|
102
|
+
if (value !== null) return value;
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
};
|
|
106
|
+
const getSnapshot = () => {
|
|
107
|
+
return {
|
|
108
|
+
languageId: runtime.languageId,
|
|
109
|
+
fallbackLanguageId,
|
|
110
|
+
translations: Object.fromEntries(Object.entries(translations.catalogs).map(([id, catalog]) => [id, { ...catalog }])),
|
|
111
|
+
formats: getFormats(runtime, runtime.languageId),
|
|
112
|
+
compiledLanguages: Object.keys(translations.compiledCatalogs).filter((id) => translations.compiledCatalogs[id])
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
const hydrate = (snapshot) => {
|
|
116
|
+
version++;
|
|
117
|
+
for (const id of Object.keys(translations.catalogs)) delete translations.catalogs[id];
|
|
118
|
+
for (const id of Object.keys(translations.compiledCatalogs)) delete translations.compiledCatalogs[id];
|
|
119
|
+
for (const [id, catalog] of Object.entries(snapshot.translations)) translations.catalogs[id] = Object.assign(Object.create(null), catalog);
|
|
120
|
+
for (const id of snapshot.compiledLanguages ?? []) translations.compiledCatalogs[id] = true;
|
|
121
|
+
runtime.formats = snapshot.formats ?? {};
|
|
122
|
+
runtime.languageId = snapshot.languageId;
|
|
123
|
+
fallbackLanguageId = snapshot.fallbackLanguageId;
|
|
124
|
+
runtime.ready = true;
|
|
125
|
+
state.set({
|
|
126
|
+
languageId: snapshot.languageId,
|
|
127
|
+
ready: true,
|
|
128
|
+
isLoading: false
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
const getLanguageIds = (languageId) => {
|
|
132
|
+
const ids = fallbackLanguageId === void 0 ? [languageId, languageId.split("-")[0]] : [
|
|
133
|
+
languageId,
|
|
134
|
+
languageId.split("-")[0],
|
|
135
|
+
fallbackLanguageId
|
|
136
|
+
];
|
|
137
|
+
return [...new Set(ids)];
|
|
138
|
+
};
|
|
139
|
+
const loadTranslations = async (languageId) => {
|
|
140
|
+
const current = ++version;
|
|
141
|
+
state.set({ isLoading: true });
|
|
142
|
+
try {
|
|
143
|
+
await Promise.all(getLanguageIds(languageId).map(translations.loadTranslations));
|
|
144
|
+
if (version !== current) return false;
|
|
145
|
+
runtime.languageId = languageId;
|
|
146
|
+
runtime.ready = true;
|
|
147
|
+
state.set({
|
|
148
|
+
languageId,
|
|
149
|
+
ready: true,
|
|
150
|
+
isLoading: false
|
|
151
|
+
});
|
|
152
|
+
return true;
|
|
153
|
+
} finally {
|
|
154
|
+
if (current === version) state.set({ isLoading: false });
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
const manager = {
|
|
158
|
+
configure,
|
|
159
|
+
setStorage: storage.setStorage,
|
|
160
|
+
setTranslationLoader,
|
|
161
|
+
init,
|
|
162
|
+
setLanguageId,
|
|
163
|
+
getLanguageId,
|
|
164
|
+
getTranslation,
|
|
165
|
+
getSnapshot,
|
|
166
|
+
hydrate
|
|
167
|
+
};
|
|
168
|
+
runtime.translationGetter = (args) => {
|
|
169
|
+
for (const languageId of getLanguageIds(args.languageId)) {
|
|
170
|
+
const compiled = translations.compiledCatalogs[languageId];
|
|
171
|
+
const message = compiled && args.getCompiledId ? args.getCompiledId() : args.message;
|
|
172
|
+
const context = compiled && args.getCompiledId ? null : args.context;
|
|
173
|
+
const value = translations.getTranslation({
|
|
174
|
+
languageId,
|
|
175
|
+
context,
|
|
176
|
+
message
|
|
177
|
+
});
|
|
178
|
+
if (value !== null) return compiled ? { compiled: value } : value;
|
|
179
|
+
}
|
|
180
|
+
return null;
|
|
181
|
+
};
|
|
182
|
+
return {
|
|
183
|
+
manager,
|
|
184
|
+
state,
|
|
185
|
+
runtime
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
const defaultManaged = createI18nManager({ runtime: defaultRuntime });
|
|
189
|
+
const I18nManager = defaultManaged.manager;
|
|
190
|
+
const defaultI18nState = defaultManaged.state;
|
|
191
|
+
export { createLocalStorage as a, defaultManaged as i, createI18nManager as n, createStorageManager as o, defaultI18nState as r, createStateManager as s, I18nManager as t };
|
|
192
|
+
|
|
193
|
+
//# sourceMappingURL=manager-BW778X1Y.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manager-BW778X1Y.js","names":[],"sources":["../src/state-manager.ts","../src/storage-manager.ts","../src/manager.ts"],"sourcesContent":["export type StateManager<StateData> = {\n get(): StateData;\n subscribe(listener: () => void): () => void;\n set(partial: Partial<StateData>): void;\n};\n\nexport function createStateManager<StateData extends object>(initial: StateData): StateManager<StateData> {\n let current = initial;\n\n const listeners = new Set<() => void>();\n\n return {\n get: () => current,\n subscribe: (listener) => {\n listeners.add(listener);\n\n return () => listeners.delete(listener);\n },\n set(partial) {\n const next = { ...current, ...partial };\n const changed = Object.keys(next).some((key) => current[key as keyof StateData] !== next[key as keyof StateData]);\n\n if (!changed) return;\n current = next;\n\n for (const listener of listeners) listener();\n },\n };\n}\n","import type { LanguageId } from './core/types';\n\nexport type StoredState = { languageId: LanguageId; };\n\nexport type I18nStorage = {\n read(): StoredState | null | Promise<StoredState | null>;\n write(state: StoredState): void | Promise<void>;\n};\n\nexport function createStorageManager(initial: I18nStorage | null = null) {\n let storage = initial;\n\n return {\n setStorage(next: I18nStorage | null): void {\n storage = next;\n },\n getStorage(): I18nStorage {\n if (!storage) throw new Error('[i18n] storage is not configured');\n\n return storage;\n },\n hasStorage(): boolean {\n return storage !== null;\n },\n };\n}\n\nexport function createLocalStorage(): I18nStorage | null {\n if (typeof globalThis.localStorage === 'undefined') return null;\n\n return {\n read() {\n const value = globalThis.localStorage.getItem('fluentic:i18n:state');\n\n if (!value) return null;\n\n try {\n return JSON.parse(value) as StoredState;\n } catch {\n return null;\n }\n },\n write(state) {\n globalThis.localStorage.setItem('fluentic:i18n:state', JSON.stringify(state));\n },\n };\n}\n","import { createRuntime, defaultRuntime, type Formats, getFormats, type Runtime } from './shared/runtime';\nimport { createStateManager } from './state-manager';\nimport { createStorageManager, type I18nStorage } from './storage-manager';\nimport { type Catalog, createTranslationManager, type TranslationLoader } from './translation-manager';\n\nexport type { I18nStorage, StoredState } from './storage-manager';\nexport { defineTranslationLoader } from './translation-manager';\nexport type * from './translation-manager';\n\nexport type StateData = { languageId: string; isLoading: boolean; ready: boolean; };\nexport type I18nManagerOptions = { languageId: string; fallbackLanguageId?: string; };\nexport type CreateI18nManagerOptions = { storage?: I18nStorage | null; runtime?: Runtime; };\nexport type GetTranslationArgs = { languageId?: string; context?: string | null; message: string; };\n\nexport type I18nSnapshot = {\n languageId: string;\n fallbackLanguageId?: string;\n translations: Record<string, Catalog>;\n formats?: Partial<Formats>;\n compiledLanguages?: string[];\n};\n\nexport function createI18nManager(options: CreateI18nManagerOptions = {}) {\n const runtime = options.runtime ?? createRuntime();\n\n const state = createStateManager<StateData>({\n languageId: runtime.languageId,\n isLoading: false,\n ready: runtime.ready,\n });\n const storage = createStorageManager(options.storage ?? null);\n const translations = createTranslationManager();\n\n let fallbackLanguageId: string | undefined;\n let version = 0;\n\n runtime.subscribe = state.subscribe;\n runtime.getSnapshot = state.get;\n\n /* configure */\n\n const configure = (config: I18nManagerOptions) => {\n runtime.languageId = config.languageId;\n runtime.ready = false;\n fallbackLanguageId = config.fallbackLanguageId;\n version++;\n state.set({ languageId: config.languageId, ready: false });\n };\n\n const setTranslationLoader = (loader: TranslationLoader | null) => {\n translations.setTranslationLoader(loader);\n runtime.ready = false;\n state.set({ ready: false });\n };\n\n /* language and translations */\n\n const init = async () => {\n const saved = storage.hasStorage() ? await storage.getStorage().read() : null;\n\n await loadTranslations(saved?.languageId ?? runtime.languageId);\n };\n\n const setLanguageId = async (languageId: string) => {\n const committed = await loadTranslations(languageId);\n\n if (committed && storage.hasStorage()) await storage.getStorage().write({ languageId });\n };\n\n const getLanguageId = () => {\n return runtime.languageId;\n };\n\n const getTranslation = (args: GetTranslationArgs) => {\n for (const languageId of getLanguageIds(args.languageId ?? runtime.languageId)) {\n const value = translations.getTranslation({ ...args, languageId, context: args.context ?? null });\n\n if (value !== null) return value;\n }\n\n return null;\n };\n\n /* snapshots */\n\n const getSnapshot = (): I18nSnapshot => {\n return {\n languageId: runtime.languageId,\n fallbackLanguageId,\n translations: Object.fromEntries(\n Object.entries(translations.catalogs).map(([id, catalog]) => [id, { ...catalog }]),\n ),\n formats: getFormats(runtime, runtime.languageId),\n compiledLanguages: Object.keys(translations.compiledCatalogs).filter((id) => translations.compiledCatalogs[id]),\n };\n };\n\n const hydrate = (snapshot: I18nSnapshot) => {\n version++;\n\n for (const id of Object.keys(translations.catalogs)) delete translations.catalogs[id];\n for (const id of Object.keys(translations.compiledCatalogs)) delete translations.compiledCatalogs[id];\n for (const [id, catalog] of Object.entries(snapshot.translations)) {\n translations.catalogs[id] = Object.assign(Object.create(null), catalog);\n }\n\n for (const id of snapshot.compiledLanguages ?? []) translations.compiledCatalogs[id] = true;\n\n runtime.formats = snapshot.formats ?? {};\n runtime.languageId = snapshot.languageId;\n fallbackLanguageId = snapshot.fallbackLanguageId;\n runtime.ready = true;\n state.set({ languageId: snapshot.languageId, ready: true, isLoading: false });\n };\n\n /* utils */\n\n const getLanguageIds = (languageId: string) => {\n const ids = fallbackLanguageId === undefined\n ? [languageId, languageId.split('-')[0]]\n : [languageId, languageId.split('-')[0], fallbackLanguageId];\n\n return [...new Set(ids)];\n };\n\n const loadTranslations = async (languageId: string) => {\n const current = ++version;\n state.set({ isLoading: true });\n\n try {\n await Promise.all(getLanguageIds(languageId).map(translations.loadTranslations));\n if (version !== current) return false;\n runtime.languageId = languageId;\n runtime.ready = true;\n state.set({ languageId, ready: true, isLoading: false });\n\n return true;\n } finally {\n if (current === version) state.set({ isLoading: false });\n }\n };\n\n const manager = {\n configure,\n setStorage: storage.setStorage,\n setTranslationLoader,\n init,\n setLanguageId,\n getLanguageId,\n getTranslation,\n getSnapshot,\n hydrate,\n };\n\n runtime.translationGetter = (args) => {\n for (const languageId of getLanguageIds(args.languageId)) {\n const compiled = translations.compiledCatalogs[languageId];\n const message = compiled && args.getCompiledId ? args.getCompiledId() : args.message;\n const context = compiled && args.getCompiledId ? null : args.context;\n const value = translations.getTranslation({ languageId, context, message });\n\n if (value !== null) return compiled ? { compiled: value } : value;\n }\n\n return null;\n };\n\n return { manager, state, runtime };\n}\n\nexport type ManagedI18n = ReturnType<typeof createI18nManager>;\nexport type I18nManager = ManagedI18n['manager'];\nexport const defaultManaged = createI18nManager({ runtime: defaultRuntime });\nexport const I18nManager = defaultManaged.manager;\nexport const defaultI18nState = defaultManaged.state;\n"],"mappings":";;AAMA,SAAgB,mBAA6C,SAA6C;CACxG,IAAI,UAAU;CAEd,MAAM,4BAAY,IAAI,IAAgB;CAEtC,OAAO;EACL,WAAW;EACX,YAAY,aAAa;GACvB,UAAU,IAAI,QAAQ;GAEtB,aAAa,UAAU,OAAO,QAAQ;EACxC;EACA,IAAI,SAAS;GACX,MAAM,OAAO;IAAE,GAAG;IAAS,GAAG;GAAQ;GAGtC,IAAI,CAFY,OAAO,KAAK,IAAI,CAAC,CAAC,MAAM,QAAQ,QAAQ,SAA4B,KAAK,IAE9E,GAAG;GACd,UAAU;GAEV,KAAK,MAAM,YAAY,WAAW,SAAS;EAC7C;CACF;AACF;;ACnBA,SAAgB,qBAAqB,UAA8B,MAAM;CACvE,IAAI,UAAU;CAEd,OAAO;EACL,WAAW,MAAgC;GACzC,UAAU;EACZ;EACA,aAA0B;GACxB,IAAI,CAAC,SAAS,MAAM,IAAI,MAAM,kCAAkC;GAEhE,OAAO;EACT;EACA,aAAsB;GACpB,OAAO,YAAY;EACrB;CACF;AACF;AAEA,SAAgB,qBAAyC;CACvD,IAAI,OAAO,WAAW,iBAAiB,aAAa,OAAO;CAE3D,OAAO;EACL,OAAO;GACL,MAAM,QAAQ,WAAW,aAAa,QAAQ,qBAAqB;GAEnE,IAAI,CAAC,OAAO,OAAO;GAEnB,IAAI;IACF,OAAO,KAAK,MAAM,KAAK;GACzB,QAAQ;IACN,OAAO;GACT;EACF;EACA,MAAM,OAAO;GACX,WAAW,aAAa,QAAQ,uBAAuB,KAAK,UAAU,KAAK,CAAC;EAC9E;CACF;AACF;;ACxBA,SAAgB,kBAAkB,UAAoC,CAAC,GAAG;CACxE,MA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AAgB,CAAC;GAC/E,IAAI,YAAY,SAAS,OAAO;GAChC,QAAQ,aAAa;GACrB,QAAQ,QAAQ;GAChB,MAAM,IAAI;IAAE;IAAY,OAAO;IAAM,WAAW;GAAM,CAAC;GAEvD,OAAO;EACT,UAAU;GACR,IAAI,YAAY,SAAS,MAAM,IAAI,EAAE,WAAW,MAAM,CAAC;EACzD;CACF;CAEA,MAAM,UAAU;EACd;EACA,YAAY,QAAQ;EACpB;EACA;EACA;EACA;EACA;EACA;EACA;CACF;CAEA,QAAQ,qBAAqB,SAAS;EACpC,KAAK,MAAM,cAAc,eAAe,KAAK,UAAU,GAAG;GACxD,MAAM,WAAW,aAAa,iBAAiB;GAC/C,MAAM,UAAU,YAAY,KAAK,gBAAgB,KAAK,cAAc,IAAI,KAAK;GAC7E,MAAM,UAAU,YAAY,KAAK,gBAAgB,OAAO,KAAK;GAC7D,MAAM,QAAQ,aAAa,eAAe;IAAE;IAAY;IAAS;GAAQ,CAAC;GAE1E,IAAI,UAAU,MAAM,OAAO,WAAW,EAAE,UAAU,MAAM,IAAI;EAC9D;EAEA,OAAO;CACT;CAEA,OAAO;EAAE;EAAS;EAAO;CAAQ;AACnC;AAIA,MAAa,iBAAiB,kBAAkB,EAAE,SAAS,eAAe,CAAC;AAC3E,MAAa,cAAc,eAAe;AAC1C,MAAa,mBAAmB,eAAe"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { createJiti } from "jiti";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
// src/plugin/options.ts
|
|
5
|
+
function loadOptions(options = {}, root = process.cwd()) {
|
|
6
|
+
const file = options.config ? resolve(root, options.config) : [
|
|
7
|
+
"i18n.config.ts",
|
|
8
|
+
"i18n.config.js",
|
|
9
|
+
"i18n.config.mjs"
|
|
10
|
+
].map((name) => resolve(root, name)).find(existsSync);
|
|
11
|
+
const config = file ? createJiti(import.meta.url, { moduleCache: false })(file) : {};
|
|
12
|
+
return {
|
|
13
|
+
...config.default ?? config,
|
|
14
|
+
...options,
|
|
15
|
+
configFile: file
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function accepts(id, options) {
|
|
19
|
+
return /\.[cm]?[jt]sx?$/.test(id.split("?")[0]) && (options.includeDependencies || !id.includes("/node_modules/"));
|
|
20
|
+
}
|
|
21
|
+
export { loadOptions as n, accepts as t };
|
|
22
|
+
|
|
23
|
+
//# sourceMappingURL=options-Dxxx0aOz.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options-Dxxx0aOz.js","names":[],"sources":["../src/plugin/options.ts"],"sourcesContent":["import { createJiti } from 'jiti';\nimport { existsSync } from 'node:fs';\nimport { resolve } from 'node:path';\n\nexport type PluginOptions = { compiled?: boolean; config?: string; includeDependencies?: boolean; };\n\nexport function loadOptions(\n options: PluginOptions = {},\n root = process.cwd(),\n): PluginOptions & { configFile?: string; } {\n const file = options.config\n ? resolve(root, options.config)\n : ['i18n.config.ts', 'i18n.config.js', 'i18n.config.mjs'].map((name) => resolve(root, name)).find(existsSync);\n\n const config = file\n ? createJiti(import.meta.url, { moduleCache: false })(file) as { default?: PluginOptions; } & PluginOptions\n : {};\n\n return { ...((config.default ?? config) as PluginOptions), ...options, configFile: file };\n}\n\nexport function accepts(id: string, options: PluginOptions) {\n return /\\.[cm]?[jt]sx?$/.test(id.split('?')[0]) && (options.includeDependencies || !id.includes('/node_modules/'));\n}\n"],"mappings":";;;;AAMA,SAAgB,YACd,UAAyB,CAAC,GAC1B,OAAO,QAAQ,IAAI,GACuB;CAC1C,MAAM,OAAO,QAAQ,SACjB,QAAQ,MAAM,QAAQ,MAAM,IAC5B;EAAC;EAAkB;EAAkB;CAAiB,CAAC,CAAC,KAAK,SAAS,QAAQ,MAAM,IAAI,CAAC,CAAC,CAAC,KAAK,UAAU;CAE9G,MAAM,SAAS,OACX,WAAW,YAAY,KAAK,EAAE,aAAa,MAAM,CAAC,CAAC,CAAC,IAAI,IACxD,CAAC;CAEL,OAAO;EAAE,GAAK,OAAO,WAAW;EAA2B,GAAG;EAAS,YAAY;CAAK;AAC1F;AAEA,SAAgB,QAAQ,IAAY,SAAwB;CAC1D,OAAO,kBAAkB,KAAK,GAAG,MAAM,GAAG,CAAC,CAAC,EAAE,MAAM,QAAQ,uBAAuB,CAAC,GAAG,SAAS,gBAAgB;AAClH"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { t as PluginOptions } from "../options-DE7vYj0e.js";
|
|
2
|
+
/** Owns parsing even when a host (e.g. Metro) passes an already parsed AST. */
|
|
3
|
+
declare function fluenticBabel(_api: unknown, options?: PluginOptions): {
|
|
4
|
+
name: string;
|
|
5
|
+
parserOverride(code: string, parserOptions: {
|
|
6
|
+
sourceFilename?: string;
|
|
7
|
+
}): import("@babel/parser").ParseResult<import("@babel/types").File> | undefined;
|
|
8
|
+
pre(file: any): void;
|
|
9
|
+
};
|
|
10
|
+
export { fluenticBabel as default };
|
|
11
|
+
//# sourceMappingURL=babel.d.ts.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { c as traverse, t as compileProgram } from "../compiler-BZtB9WLi.js";
|
|
2
|
+
import { n as loadOptions } from "../options-Dxxx0aOz.js";
|
|
3
|
+
// src/plugin/babel.ts
|
|
4
|
+
/** Owns parsing even when a host (e.g. Metro) passes an already parsed AST. */
|
|
5
|
+
function fluenticBabel(_api, options = {}) {
|
|
6
|
+
const config = loadOptions(options);
|
|
7
|
+
const handled = /* @__PURE__ */ new WeakSet();
|
|
8
|
+
const enabled = (code) => config.compiled && code.includes("@fluentic/i18n");
|
|
9
|
+
const compile = (code, filename) => {
|
|
10
|
+
const { ast } = compileProgram(code, filename, config);
|
|
11
|
+
traverse.cache.clear();
|
|
12
|
+
handled.add(ast.program);
|
|
13
|
+
return ast;
|
|
14
|
+
};
|
|
15
|
+
return {
|
|
16
|
+
name: "fluentic-i18n",
|
|
17
|
+
parserOverride(code, parserOptions) {
|
|
18
|
+
if (!enabled(code)) return void 0;
|
|
19
|
+
return compile(code, parserOptions.sourceFilename ?? "source.tsx");
|
|
20
|
+
},
|
|
21
|
+
pre(file) {
|
|
22
|
+
if (!enabled(file.code) || handled.has(file.path.node)) return;
|
|
23
|
+
const ast = compile(file.code, file.opts.filename ?? "source.tsx");
|
|
24
|
+
file.path.replaceWith(ast.program);
|
|
25
|
+
file.scope.crawl();
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export { fluenticBabel as default };
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=babel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"babel.js","names":[],"sources":["../../src/plugin/babel.ts"],"sourcesContent":["import { compileProgram } from '../compiler';\nimport { traverse } from '../compiler/analyze';\nimport { loadOptions, type PluginOptions } from './options';\n/** Owns parsing even when a host (e.g. Metro) passes an already parsed AST. */\nexport default function fluenticBabel(_api: unknown, options: PluginOptions = {}) {\n const config = loadOptions(options);\n const handled = new WeakSet<object>();\n const enabled = (code: string) => config.compiled && code.includes('@fluentic/i18n');\n\n const compile = (code: string, filename: string) => {\n const { ast } = compileProgram(code, filename, config);\n // Standalone paths have a default Hub. Babel must create its own paths for presets.\n traverse.cache.clear();\n handled.add(ast.program);\n\n return ast;\n };\n\n return {\n name: 'fluentic-i18n',\n parserOverride(code: string, parserOptions: { sourceFilename?: string; }) {\n if (!enabled(code)) return undefined;\n\n return compile(code, parserOptions.sourceFilename ?? 'source.tsx');\n },\n pre(file: any) {\n if (!enabled(file.code) || handled.has(file.path.node)) return;\n\n const ast = compile(file.code, file.opts.filename ?? 'source.tsx');\n file.path.replaceWith(ast.program);\n file.scope.crawl();\n },\n };\n}\n"],"mappings":";;;;AAIA,SAAwB,cAAc,MAAe,UAAyB,CAAC,GAAG;CAChF,MAAM,SAAS,YAAY,OAAO;CAClC,MAAM,0BAAU,IAAI,QAAgB;CACpC,MAAM,WAAW,SAAiB,OAAO,YAAY,KAAK,SAAS,gBAAgB;CAEnF,MAAM,WAAW,MAAc,aAAqB;EAClD,MAAM,EAAE,QAAQ,eAAe,MAAM,UAAU,MAAM;EAErD,SAAS,MAAM,MAAM;EACrB,QAAQ,IAAI,IAAI,OAAO;EAEvB,OAAO;CACT;CAEA,OAAO;EACL,MAAM;EACN,eAAe,MAAc,eAA6C;GACxE,IAAI,CAAC,QAAQ,IAAI,GAAG,OAAO,KAAA;GAE3B,OAAO,QAAQ,MAAM,cAAc,kBAAkB,YAAY;EACnE;EACA,IAAI,MAAW;GACb,IAAI,CAAC,QAAQ,KAAK,IAAI,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,GAAG;GAExD,MAAM,MAAM,QAAQ,KAAK,MAAM,KAAK,KAAK,YAAY,YAAY;GACjE,KAAK,KAAK,YAAY,IAAI,OAAO;GACjC,KAAK,MAAM,MAAM;EACnB;CACF;AACF"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { n as transform } from "../compiler-BZtB9WLi.js";
|
|
2
|
+
import { n as loadOptions, t as accepts } from "../options-Dxxx0aOz.js";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
// src/plugin/esbuild.ts
|
|
5
|
+
function fluentic(options = {}) {
|
|
6
|
+
return {
|
|
7
|
+
name: "fluentic-i18n",
|
|
8
|
+
setup(build) {
|
|
9
|
+
let config = loadOptions(options, build.initialOptions.absWorkingDir);
|
|
10
|
+
build.onStart(() => {
|
|
11
|
+
config = loadOptions(options, build.initialOptions.absWorkingDir);
|
|
12
|
+
});
|
|
13
|
+
build.onLoad({ filter: /\.jsonl$/ }, async ({ path }) => ({
|
|
14
|
+
contents: await readFile(path, "utf8"),
|
|
15
|
+
loader: "text",
|
|
16
|
+
watchFiles: [path]
|
|
17
|
+
}));
|
|
18
|
+
build.onLoad({ filter: /\.[cm]?[jt]sx?$/ }, async ({ path }) => {
|
|
19
|
+
if (!config.compiled || !accepts(path, config)) return;
|
|
20
|
+
const result = transform(await readFile(path, "utf8"), {
|
|
21
|
+
...config,
|
|
22
|
+
filename: path
|
|
23
|
+
});
|
|
24
|
+
if (!result) return;
|
|
25
|
+
const loader = /\.tsx$/.test(path) ? "tsx" : /\.[cm]?ts$/.test(path) ? "ts" : /\.jsx$/.test(path) ? "jsx" : "js";
|
|
26
|
+
return {
|
|
27
|
+
contents: result.code + "\n//# sourceMappingURL=data:application/json;base64," + Buffer.from(JSON.stringify(result.map)).toString("base64"),
|
|
28
|
+
loader,
|
|
29
|
+
watchFiles: [path, ...config.configFile ? [config.configFile] : []]
|
|
30
|
+
};
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export { fluentic as default };
|
|
36
|
+
|
|
37
|
+
//# sourceMappingURL=esbuild.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"esbuild.js","names":[],"sources":["../../src/plugin/esbuild.ts"],"sourcesContent":["import { readFile } from 'node:fs/promises';\nimport { transform } from '../compiler';\nimport { accepts, loadOptions, type PluginOptions } from './options';\n\nexport default function fluentic(options: PluginOptions = {}) {\n return {\n name: 'fluentic-i18n',\n setup(build: any) {\n let config = loadOptions(options, build.initialOptions.absWorkingDir);\n build.onStart(() => {\n config = loadOptions(options, build.initialOptions.absWorkingDir);\n });\n build.onLoad(\n { filter: /\\.jsonl$/ },\n async ({ path }: { path: string; }) => ({\n contents: await readFile(path, 'utf8'),\n loader: 'text',\n watchFiles: [path],\n }),\n );\n build.onLoad({ filter: /\\.[cm]?[jt]sx?$/ }, async ({ path }: { path: string; }) => {\n if (!config.compiled || !accepts(path, config)) return;\n\n const result = transform(await readFile(path, 'utf8'), { ...config, filename: path });\n\n if (!result) return;\n\n const loader = /\\.tsx$/.test(path)\n ? 'tsx'\n : /\\.[cm]?ts$/.test(path)\n ? 'ts'\n : /\\.jsx$/.test(path)\n ? 'jsx'\n : 'js';\n\n return {\n contents: result.code + '\\n//# sourceMappingURL=data:application/json;base64,' +\n Buffer.from(JSON.stringify(result.map)).toString('base64'),\n loader,\n watchFiles: [path, ...(config.configFile ? [config.configFile] : [])],\n };\n });\n },\n };\n}\n"],"mappings":";;;;AAIA,SAAwB,SAAS,UAAyB,CAAC,GAAG;CAC5D,OAAO;EACL,MAAM;EACN,MAAM,OAAY;GAChB,IAAI,SAAS,YAAY,SAAS,MAAM,eAAe,aAAa;GACpE,MAAM,cAAc;IAClB,SAAS,YAAY,SAAS,MAAM,eAAe,aAAa;GAClE,CAAC;GACD,MAAM,OACJ,EAAE,QAAQ,WAAW,GACrB,OAAO,EAAE,YAA+B;IACtC,UAAU,MAAM,SAAS,MAAM,MAAM;IACrC,QAAQ;IACR,YAAY,CAAC,IAAI;GACnB,EACF;GACA,MAAM,OAAO,EAAE,QAAQ,kBAAkB,GAAG,OAAO,EAAE,WAA8B;IACjF,IAAI,CAAC,OAAO,YAAY,CAAC,QAAQ,MAAM,MAAM,GAAG;IAEhD,MAAM,SAAS,UAAU,MAAM,SAAS,MAAM,MAAM,GAAG;KAAE,GAAG;KAAQ,UAAU;IAAK,CAAC;IAEpF,IAAI,CAAC,QAAQ;IAEb,MAAM,SAAS,SAAS,KAAK,IAAI,IAC7B,QACA,aAAa,KAAK,IAAI,IACtB,OACA,SAAS,KAAK,IAAI,IAClB,QACA;IAEJ,OAAO;KACL,UAAU,OAAO,OAAO,yDACtB,OAAO,KAAK,KAAK,UAAU,OAAO,GAAG,CAAC,CAAC,CAAC,SAAS,QAAQ;KAC3D;KACA,YAAY,CAAC,MAAM,GAAI,OAAO,aAAa,CAAC,OAAO,UAAU,IAAI,CAAC,CAAE;IACtE;GACF,CAAC;EACH;CACF;AACF"}
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
name: string;
|
|
5
|
+
buildStart(this: {
|
|
6
|
+
addWatchFile(file: string): void;
|
|
7
|
+
}): void;
|
|
8
|
+
watchChange(id: string): void;
|
|
9
|
+
load(id: string): Promise<{
|
|
10
|
+
code: string;
|
|
11
|
+
map: {
|
|
12
|
+
mappings: string;
|
|
13
|
+
};
|
|
14
|
+
} | null>;
|
|
15
|
+
transform(code: string, id: string): {
|
|
16
|
+
code: string;
|
|
17
|
+
map: {
|
|
18
|
+
version: number;
|
|
19
|
+
sources: string[];
|
|
20
|
+
names: string[];
|
|
21
|
+
sourceRoot?: string | undefined;
|
|
22
|
+
sourcesContent?: string[] | undefined;
|
|
23
|
+
mappings: string;
|
|
24
|
+
file: string;
|
|
25
|
+
} | null;
|
|
26
|
+
messages: ExtractedMessage[];
|
|
27
|
+
} | null;
|
|
28
|
+
};
|
|
29
|
+
export { fluentic as default };
|
|
30
|
+
//# sourceMappingURL=rollup.d.ts.map
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { n as transform } from "../compiler-BZtB9WLi.js";
|
|
2
|
+
import { n as loadOptions, t as accepts } from "../options-Dxxx0aOz.js";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
// src/plugin/rollup.ts
|
|
5
|
+
function fluentic(options = {}) {
|
|
6
|
+
let config = loadOptions(options);
|
|
7
|
+
return {
|
|
8
|
+
name: "fluentic-i18n",
|
|
9
|
+
buildStart() {
|
|
10
|
+
if (config.configFile) this.addWatchFile(config.configFile);
|
|
11
|
+
},
|
|
12
|
+
watchChange(id) {
|
|
13
|
+
if (id === config.configFile) config = loadOptions(options);
|
|
14
|
+
},
|
|
15
|
+
async load(id) {
|
|
16
|
+
if (!id.endsWith(".jsonl")) return null;
|
|
17
|
+
const content = await readFile(id, "utf8");
|
|
18
|
+
return {
|
|
19
|
+
code: "export default " + JSON.stringify(content),
|
|
20
|
+
map: { mappings: "" }
|
|
21
|
+
};
|
|
22
|
+
},
|
|
23
|
+
transform(code, id) {
|
|
24
|
+
if (!config.compiled || !accepts(id, config)) return null;
|
|
25
|
+
return transform(code, {
|
|
26
|
+
...config,
|
|
27
|
+
filename: id.split("?")[0]
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export { fluentic as default };
|
|
33
|
+
|
|
34
|
+
//# sourceMappingURL=rollup.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rollup.js","names":[],"sources":["../../src/plugin/rollup.ts"],"sourcesContent":["import { readFile } from 'node:fs/promises';\nimport { transform } from '../compiler';\nimport { accepts, loadOptions, type PluginOptions } from './options';\n\nexport default function fluentic(options: PluginOptions = {}) {\n let config = loadOptions(options);\n\n return {\n name: 'fluentic-i18n',\n buildStart(this: { addWatchFile(file: string): void; }) {\n if (config.configFile) this.addWatchFile(config.configFile);\n },\n watchChange(id: string) {\n if (id === config.configFile) config = loadOptions(options);\n },\n async load(id: string) {\n if (!id.endsWith('.jsonl')) return null;\n\n const content = await readFile(id, 'utf8');\n\n return { code: 'export default ' + JSON.stringify(content), map: { mappings: '' } };\n },\n transform(code: string, id: string) {\n if (!config.compiled || !accepts(id, config)) return null;\n\n return transform(code, { ...config, filename: id.split('?')[0] });\n },\n };\n}\n"],"mappings":";;;;AAIA,SAAwB,SAAS,UAAyB,CAAC,GAAG;CAC5D,IAAI,SAAS,YAAY,OAAO;CAEhC,OAAO;EACL,MAAM;EACN,aAAwD;GACtD,IAAI,OAAO,YAAY,KAAK,aAAa,OAAO,UAAU;EAC5D;EACA,YAAY,IAAY;GACtB,IAAI,OAAO,OAAO,YAAY,SAAS,YAAY,OAAO;EAC5D;EACA,MAAM,KAAK,IAAY;GACrB,IAAI,CAAC,GAAG,SAAS,QAAQ,GAAG,OAAO;GAEnC,MAAM,UAAU,MAAM,SAAS,IAAI,MAAM;GAEzC,OAAO;IAAE,MAAM,oBAAoB,KAAK,UAAU,OAAO;IAAG,KAAK,EAAE,UAAU,GAAG;GAAE;EACpF;EACA,UAAU,MAAc,IAAY;GAClC,IAAI,CAAC,OAAO,YAAY,CAAC,QAAQ,IAAI,MAAM,GAAG,OAAO;GAErD,OAAO,UAAU,MAAM;IAAE,GAAG;IAAQ,UAAU,GAAG,MAAM,GAAG,CAAC,CAAC;GAAG,CAAC;EAClE;CACF;AACF"}
|