@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,82 @@
|
|
|
1
|
+
import type { LanguageId, MessageContext } from './core/types';
|
|
2
|
+
import { type Catalog, parseCatalog, type TranslationData } from './translation';
|
|
3
|
+
|
|
4
|
+
export type * from './translation';
|
|
5
|
+
export { normalizeCatalog } from './translation';
|
|
6
|
+
|
|
7
|
+
export type TranslationLoaderArgs = { languageId: LanguageId; };
|
|
8
|
+
export type TranslationLookupArgs = { languageId: LanguageId; context: MessageContext | null; message: string; };
|
|
9
|
+
export type TranslationLoader = (args: TranslationLoaderArgs) => TranslationData | Promise<TranslationData>;
|
|
10
|
+
export type TranslationModule = { default: TranslationData; };
|
|
11
|
+
export type TranslationSource = TranslationData | TranslationModule;
|
|
12
|
+
|
|
13
|
+
export type TranslationLoaderEntry =
|
|
14
|
+
| TranslationSource
|
|
15
|
+
| Promise<TranslationSource>
|
|
16
|
+
| (() => TranslationSource | Promise<TranslationSource>);
|
|
17
|
+
|
|
18
|
+
export type TranslationLoaderMap = Record<string, TranslationLoaderEntry>;
|
|
19
|
+
|
|
20
|
+
export function defineTranslationLoader(loader: TranslationLoader | TranslationLoaderMap): TranslationLoader {
|
|
21
|
+
if (typeof loader === 'function') return loader;
|
|
22
|
+
|
|
23
|
+
return async ({ languageId }) => {
|
|
24
|
+
if (!Object.prototype.hasOwnProperty.call(loader, languageId)) return [];
|
|
25
|
+
|
|
26
|
+
const entry = loader[languageId];
|
|
27
|
+
const source = await (typeof entry === 'function' ? entry() : entry);
|
|
28
|
+
|
|
29
|
+
return typeof source === 'object' && source !== null && 'default' in source
|
|
30
|
+
? source.default
|
|
31
|
+
: source as TranslationData;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function createTranslationManager() {
|
|
36
|
+
const catalogs: Record<string, Catalog> = Object.create(null);
|
|
37
|
+
const pending = new Map<string, Promise<void>>();
|
|
38
|
+
const compiledCatalogs: Record<string, boolean> = Object.create(null);
|
|
39
|
+
|
|
40
|
+
let loader: TranslationLoader | null = null;
|
|
41
|
+
let generation = 0;
|
|
42
|
+
|
|
43
|
+
const setTranslationLoader = (next: TranslationLoader | null) => {
|
|
44
|
+
loader = next;
|
|
45
|
+
generation++;
|
|
46
|
+
pending.clear();
|
|
47
|
+
|
|
48
|
+
for (const key of Object.keys(catalogs)) delete catalogs[key];
|
|
49
|
+
for (const key of Object.keys(compiledCatalogs)) delete compiledCatalogs[key];
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const loadTranslations = async (languageId: string) => {
|
|
53
|
+
if (!loader || Object.prototype.hasOwnProperty.call(catalogs, languageId)) return;
|
|
54
|
+
if (pending.has(languageId)) return pending.get(languageId);
|
|
55
|
+
|
|
56
|
+
const version = generation;
|
|
57
|
+
const currentLoader = loader;
|
|
58
|
+
|
|
59
|
+
const task = Promise.resolve().then(() => currentLoader({ languageId })).then((data) => {
|
|
60
|
+
if (version === generation) {
|
|
61
|
+
const result = parseCatalog(data);
|
|
62
|
+
catalogs[languageId] = result.catalog;
|
|
63
|
+
compiledCatalogs[languageId] = result.compiled;
|
|
64
|
+
}
|
|
65
|
+
}).finally(() => {
|
|
66
|
+
if (version === generation) pending.delete(languageId);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
pending.set(languageId, task);
|
|
70
|
+
|
|
71
|
+
return task;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const getTranslation = (args: TranslationLookupArgs) => {
|
|
75
|
+
const catalog = catalogs[args.languageId];
|
|
76
|
+
const id = args.context ? `${args.context}\0${args.message}` : args.message;
|
|
77
|
+
|
|
78
|
+
return catalog?.[id] ?? catalog?.[args.message] ?? null;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
return { catalogs, compiledCatalogs, setTranslationLoader, loadTranslations, getTranslation };
|
|
82
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { type CompactMessage, validateCompact } from './shared/compact';
|
|
2
|
+
|
|
3
|
+
export type TranslationItem = { id: string; translation: string; };
|
|
4
|
+
export type TranslationRow = [id: string, translation: string] | [context: string, id: string, translation: string];
|
|
5
|
+
export type CompiledTranslationItem = [id: string, message: CompactMessage];
|
|
6
|
+
export type CompiledCatalogHeader = { compiled: true; };
|
|
7
|
+
export type TranslationJsonl = string;
|
|
8
|
+
|
|
9
|
+
export type TranslationData =
|
|
10
|
+
| unknown[]
|
|
11
|
+
| TranslationJsonl
|
|
12
|
+
| { jsonl: TranslationJsonl; };
|
|
13
|
+
|
|
14
|
+
export type Catalog = Record<string, CompactMessage>;
|
|
15
|
+
|
|
16
|
+
type TranslationItemRaw = {
|
|
17
|
+
id?: string;
|
|
18
|
+
source?: string;
|
|
19
|
+
context?: string;
|
|
20
|
+
message?: string | [string, string];
|
|
21
|
+
translation?: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export function normalizeCatalog(data: TranslationData): Catalog {
|
|
25
|
+
return parseCatalog(data).catalog;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function parseCatalog(data: TranslationData): { catalog: Catalog; compiled: boolean; } {
|
|
29
|
+
const catalog: Catalog = Object.create(null);
|
|
30
|
+
const rows = parseRows(data);
|
|
31
|
+
const header = rows[0];
|
|
32
|
+
const hasHeader = isCompiledHeader(header);
|
|
33
|
+
const compiled = hasHeader ||
|
|
34
|
+
(Array.isArray(data) && rows.some((row) => Array.isArray(row) && row.length === 2 && Array.isArray(row[1])));
|
|
35
|
+
|
|
36
|
+
if (hasHeader) rows.shift();
|
|
37
|
+
if (Array.isArray(data) && rows.at(-1) === null) rows.pop();
|
|
38
|
+
|
|
39
|
+
for (const row of rows) {
|
|
40
|
+
if (Array.isArray(row)) {
|
|
41
|
+
const [id, message] = compiled ? parseCompiledRow(row) : parseTranslationRow(row, Array.isArray(data));
|
|
42
|
+
|
|
43
|
+
if (compiled || message !== '') catalog[id] = message;
|
|
44
|
+
} else {
|
|
45
|
+
if (compiled) throw new TypeError('[i18n] invalid compiled catalog row');
|
|
46
|
+
|
|
47
|
+
const item = parseTranslationItem(row);
|
|
48
|
+
|
|
49
|
+
if (item.translation !== '') catalog[item.id] = item.translation;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return { catalog, compiled };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function parseRows(data: TranslationData): unknown[] {
|
|
57
|
+
if (Array.isArray(data)) return [...data];
|
|
58
|
+
|
|
59
|
+
const jsonl = typeof data === 'string' ? data : data.jsonl;
|
|
60
|
+
|
|
61
|
+
return jsonl.split(/\r?\n/).filter((line) => line.trim()).map((line) => JSON.parse(line));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function isCompiledHeader(row: unknown): row is CompiledCatalogHeader {
|
|
65
|
+
return typeof row === 'object' && row !== null && !Array.isArray(row) &&
|
|
66
|
+
'compiled' in row && row.compiled === true;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function parseCompiledRow(row: unknown[]): CompiledTranslationItem {
|
|
70
|
+
if (row.length !== 2 || typeof row[0] !== 'string') {
|
|
71
|
+
throw new TypeError('[i18n] invalid compiled catalog row');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
validateCompact(row[1]);
|
|
75
|
+
|
|
76
|
+
return [row[0], row[1]];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function parseTranslationRow(row: unknown[], allowCompact: boolean): [string, CompactMessage] {
|
|
80
|
+
if (row.length === 2 && typeof row[0] === 'string') {
|
|
81
|
+
// Programmatic compileCatalog() results can be passed directly as arrays.
|
|
82
|
+
if (allowCompact) validateCompact(row[1]);
|
|
83
|
+
else if (typeof row[1] !== 'string') throw new TypeError('[i18n] invalid translation row');
|
|
84
|
+
|
|
85
|
+
return [row[0], row[1]];
|
|
86
|
+
}
|
|
87
|
+
if (row.length === 3 && row.every((value) => typeof value === 'string')) {
|
|
88
|
+
return [`${row[0]}\0${row[1]}`, row[2] as string];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
throw new TypeError('[i18n] invalid translation row');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function parseTranslationItem(row: unknown): TranslationItem {
|
|
95
|
+
const item = row as TranslationItemRaw;
|
|
96
|
+
|
|
97
|
+
if (!item || typeof item.translation !== 'string') throw new TypeError('[i18n] invalid translation item');
|
|
98
|
+
|
|
99
|
+
const source = item.source ?? (Array.isArray(item.message) ? item.message[1] : item.message);
|
|
100
|
+
const context = Array.isArray(item.message) ? item.message[0] : item.context;
|
|
101
|
+
const id = item.id ?? (source === undefined ? undefined : context ? `${context}\0${source}` : source);
|
|
102
|
+
|
|
103
|
+
if (typeof id !== 'string') throw new TypeError('[i18n] translation id is missing');
|
|
104
|
+
|
|
105
|
+
return { id, translation: item.translation };
|
|
106
|
+
}
|
package/src/types.ts
ADDED