lino-i18n 0.3.0 → 0.4.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 +40 -0
- package/README.md +45 -1
- package/bin/lino-i18n.js +67 -34
- package/package.json +218 -22
- package/src/browser.d.ts +1 -0
- package/src/browser.js +1 -0
- package/src/catalogs.js +56 -26
- package/src/compiler.d.ts +53 -0
- package/src/compiler.js +72 -0
- package/src/content.js +135 -0
- package/src/converters/react-intl.js +10 -26
- package/src/dictionary.js +169 -0
- package/src/eslint.d.ts +15 -0
- package/src/eslint.js +244 -0
- package/src/extract-bindings.js +41 -0
- package/src/extract-derived.js +85 -0
- package/src/extract-dictionary.js +122 -0
- package/src/extract-jsx.js +176 -0
- package/src/extract-project.js +314 -0
- package/src/extract.js +365 -0
- package/src/format.js +23 -23
- package/src/i18n.js +2 -1
- package/src/index.d.ts +19 -4
- package/src/index.js +12 -1
- package/src/intl.d.ts +176 -0
- package/src/intl.js +407 -0
- package/src/message-schema.js +26 -0
- package/src/messages.d.ts +187 -0
- package/src/messages.js +397 -0
- package/src/native-state.js +72 -0
- package/src/next/client.d.ts +62 -0
- package/src/next/client.js +159 -0
- package/src/next/proxy.d.ts +17 -0
- package/src/next/proxy.js +39 -0
- package/src/next/server.d.ts +28 -0
- package/src/next/server.js +101 -0
- package/src/node.d.ts +37 -0
- package/src/node.js +61 -0
- package/src/providers/gt-locales.d.ts +2 -0
- package/src/providers/gt-locales.js +5 -0
- package/src/providers/gt.d.ts +75 -0
- package/src/providers/gt.js +158 -0
- package/src/python-extract.d.ts +16 -0
- package/src/python-extract.js +134 -0
- package/src/python-extract.py +300 -0
- package/src/react-content.js +69 -0
- package/src/react-native.d.ts +92 -0
- package/src/react-native.js +110 -0
- package/src/react-server.d.ts +14 -0
- package/src/react-server.js +28 -0
- package/src/react.d.ts +98 -4
- package/src/react.js +162 -8
- package/src/remark.d.ts +11 -0
- package/src/remark.js +35 -0
- package/src/rrweb/catalog.d.ts +29 -0
- package/src/rrweb/catalog.js +158 -0
- package/src/rrweb/harvest.d.ts +24 -0
- package/src/rrweb/harvest.js +102 -0
- package/src/rrweb/index.d.ts +36 -0
- package/src/rrweb/index.js +135 -0
- package/src/rrweb/replay.d.ts +1 -0
- package/src/rrweb/replay.js +3 -0
- package/src/server.d.ts +26 -0
- package/src/server.js +137 -0
- package/src/source-project-files.js +170 -0
- package/src/static-values.js +122 -0
- package/src/tanstack-start/client.d.ts +20 -0
- package/src/tanstack-start/client.js +97 -0
- package/src/tanstack-start/server.d.ts +19 -0
- package/src/tanstack-start/server.js +41 -0
- package/src/tooling-files.js +134 -0
- package/src/tooling.d.ts +56 -0
- package/src/tooling.js +90 -0
- package/src/transform-jsx.js +236 -0
- package/src/vue-content.js +82 -0
- package/src/vue-extract.d.ts +8 -0
- package/src/vue-extract.js +303 -0
- package/src/vue.d.ts +120 -0
- package/src/vue.js +309 -0
package/src/catalogs.js
CHANGED
|
@@ -20,6 +20,15 @@ const SELECTOR_SUFFIXES = new Set([
|
|
|
20
20
|
|
|
21
21
|
const LABEL_ALIAS_KEY = 'label';
|
|
22
22
|
|
|
23
|
+
function setEntry(object, key, value) {
|
|
24
|
+
Object.defineProperty(object, key, {
|
|
25
|
+
value,
|
|
26
|
+
enumerable: true,
|
|
27
|
+
writable: true,
|
|
28
|
+
configurable: true,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
23
32
|
function unescapeValue(value, quote = '"') {
|
|
24
33
|
let result = '';
|
|
25
34
|
for (let index = 0; index < value.length; index += 1) {
|
|
@@ -59,6 +68,35 @@ function escapeValue(value) {
|
|
|
59
68
|
.join('\\t');
|
|
60
69
|
}
|
|
61
70
|
|
|
71
|
+
function formatKey(key) {
|
|
72
|
+
return !key || /[\s"'\\:#]/.test(key)
|
|
73
|
+
? `"${escapeValue(key).replaceAll('\n', '\\n')}"`
|
|
74
|
+
: key;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function splitKey(line) {
|
|
78
|
+
if (line.startsWith('"') || line.startsWith("'")) {
|
|
79
|
+
const quote = line[0];
|
|
80
|
+
const closing = findClosingQuote(line, quote);
|
|
81
|
+
if (
|
|
82
|
+
closing === -1 ||
|
|
83
|
+
(line[closing + 1] && !/\s/.test(line[closing + 1]))
|
|
84
|
+
) {
|
|
85
|
+
throw new Error('Malformed quoted catalog key');
|
|
86
|
+
}
|
|
87
|
+
return [
|
|
88
|
+
unescapeValue(line.slice(1, closing), quote),
|
|
89
|
+
line.slice(closing + 1).trimStart() || undefined,
|
|
90
|
+
];
|
|
91
|
+
}
|
|
92
|
+
const match = /^(\S+)(?:\s+(.*))?$/.exec(line);
|
|
93
|
+
const key =
|
|
94
|
+
match[2] === undefined && match[1].endsWith(':')
|
|
95
|
+
? match[1].slice(0, -1)
|
|
96
|
+
: match[1];
|
|
97
|
+
return [key, match[2]?.trimStart()];
|
|
98
|
+
}
|
|
99
|
+
|
|
62
100
|
function countIndent(line) {
|
|
63
101
|
let count = 0;
|
|
64
102
|
for (const char of line) {
|
|
@@ -172,7 +210,7 @@ function parseLogicalLines(text) {
|
|
|
172
210
|
.replace(/\r\n/g, '\n')
|
|
173
211
|
.split('\n');
|
|
174
212
|
const entries = [];
|
|
175
|
-
for (let index = 0; index < lines.length;
|
|
213
|
+
for (let index = 0; index < lines.length;) {
|
|
176
214
|
const raw = lines[index].replace(/\r$/, '');
|
|
177
215
|
const content = raw.trimEnd();
|
|
178
216
|
if (!content.trim() || content.trimStart().startsWith('#')) {
|
|
@@ -182,17 +220,7 @@ function parseLogicalLines(text) {
|
|
|
182
220
|
|
|
183
221
|
const indent = countIndent(content);
|
|
184
222
|
const trimmed = content.trimStart();
|
|
185
|
-
const
|
|
186
|
-
if (!match) {
|
|
187
|
-
index += 1;
|
|
188
|
-
continue;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
let key = match[1];
|
|
192
|
-
const rest = match[2]?.trimStart();
|
|
193
|
-
if (rest === undefined && key.endsWith(':')) {
|
|
194
|
-
key = key.slice(0, -1);
|
|
195
|
-
}
|
|
223
|
+
const [key, rest] = splitKey(trimmed);
|
|
196
224
|
|
|
197
225
|
if (rest === undefined) {
|
|
198
226
|
entries.push({ indent, key, value: null });
|
|
@@ -228,16 +256,16 @@ function parseEntriesAt(lines, start, indent) {
|
|
|
228
256
|
|
|
229
257
|
index += 1;
|
|
230
258
|
if (line.value !== null) {
|
|
231
|
-
tree
|
|
259
|
+
setEntry(tree, line.key, line.value);
|
|
232
260
|
continue;
|
|
233
261
|
}
|
|
234
262
|
|
|
235
263
|
if (index < lines.length && lines[index].indent > line.indent) {
|
|
236
264
|
const parsed = parseEntriesAt(lines, index, lines[index].indent);
|
|
237
|
-
tree
|
|
265
|
+
setEntry(tree, line.key, parsed.tree);
|
|
238
266
|
index = parsed.index;
|
|
239
267
|
} else {
|
|
240
|
-
tree
|
|
268
|
+
setEntry(tree, line.key, {});
|
|
241
269
|
}
|
|
242
270
|
}
|
|
243
271
|
|
|
@@ -295,14 +323,14 @@ function labelAliasValue(value) {
|
|
|
295
323
|
|
|
296
324
|
function addLabelAlias(out, base, value) {
|
|
297
325
|
if (!Object.prototype.hasOwnProperty.call(out, base)) {
|
|
298
|
-
out
|
|
326
|
+
setEntry(out, base, value);
|
|
299
327
|
}
|
|
300
328
|
}
|
|
301
329
|
|
|
302
330
|
function flattenTree(tree, pathParts = [], out = {}) {
|
|
303
331
|
for (const [key, value] of Object.entries(tree)) {
|
|
304
332
|
if (typeof value === 'string') {
|
|
305
|
-
out[
|
|
333
|
+
setEntry(out, [...pathParts, key].join('.'), value);
|
|
306
334
|
continue;
|
|
307
335
|
}
|
|
308
336
|
if (!isPlainObject(value)) {
|
|
@@ -314,14 +342,14 @@ function flattenTree(tree, pathParts = [], out = {}) {
|
|
|
314
342
|
const labelValue = labelAliasValue(value);
|
|
315
343
|
if (isSelectorGroup(value)) {
|
|
316
344
|
if (labelValue !== undefined) {
|
|
317
|
-
out
|
|
345
|
+
setEntry(out, `${base}.${LABEL_ALIAS_KEY}`, labelValue);
|
|
318
346
|
addLabelAlias(out, base, labelValue);
|
|
319
347
|
}
|
|
320
348
|
for (const [suffix, child] of Object.entries(value)) {
|
|
321
349
|
if (suffix === LABEL_ALIAS_KEY) {
|
|
322
350
|
continue;
|
|
323
351
|
}
|
|
324
|
-
out
|
|
352
|
+
setEntry(out, `${base}_${suffix}`, child);
|
|
325
353
|
}
|
|
326
354
|
continue;
|
|
327
355
|
}
|
|
@@ -348,9 +376,9 @@ function splitSelectorSuffix(key) {
|
|
|
348
376
|
function setNestedValue(tree, parts, value) {
|
|
349
377
|
let node = tree;
|
|
350
378
|
for (const part of parts.slice(0, -1)) {
|
|
351
|
-
const current = node[part];
|
|
379
|
+
const current = Object.hasOwn(node, part) ? node[part] : undefined;
|
|
352
380
|
if (!isPlainObject(current)) {
|
|
353
|
-
node
|
|
381
|
+
setEntry(node, part, {});
|
|
354
382
|
if (typeof current === 'string') {
|
|
355
383
|
node[part][LABEL_ALIAS_KEY] = current;
|
|
356
384
|
}
|
|
@@ -358,13 +386,13 @@ function setNestedValue(tree, parts, value) {
|
|
|
358
386
|
node = node[part];
|
|
359
387
|
}
|
|
360
388
|
const leaf = parts[parts.length - 1];
|
|
361
|
-
if (isPlainObject(node[leaf])) {
|
|
389
|
+
if (Object.hasOwn(node, leaf) && isPlainObject(node[leaf])) {
|
|
362
390
|
if (!Object.prototype.hasOwnProperty.call(node[leaf], LABEL_ALIAS_KEY)) {
|
|
363
391
|
node[leaf][LABEL_ALIAS_KEY] = value;
|
|
364
392
|
}
|
|
365
393
|
return;
|
|
366
394
|
}
|
|
367
|
-
node
|
|
395
|
+
setEntry(node, leaf, value);
|
|
368
396
|
}
|
|
369
397
|
|
|
370
398
|
function translationsToTree(translations) {
|
|
@@ -404,13 +432,13 @@ function formatTreeLines(tree, indent = ' ') {
|
|
|
404
432
|
: entries;
|
|
405
433
|
for (const [key, value] of orderedEntries) {
|
|
406
434
|
if (typeof value === 'string') {
|
|
407
|
-
lines.push(`${indent}${key} ${formatValue(value, indent)}`);
|
|
435
|
+
lines.push(`${indent}${formatKey(key)} ${formatValue(value, indent)}`);
|
|
408
436
|
continue;
|
|
409
437
|
}
|
|
410
438
|
if (!isPlainObject(value)) {
|
|
411
439
|
continue;
|
|
412
440
|
}
|
|
413
|
-
lines.push(`${indent}${key}`);
|
|
441
|
+
lines.push(`${indent}${formatKey(key)}`);
|
|
414
442
|
lines.push(...formatTreeLines(value, `${indent} `));
|
|
415
443
|
}
|
|
416
444
|
return lines;
|
|
@@ -420,7 +448,7 @@ function formatFlatCatalog(locale, translations) {
|
|
|
420
448
|
const lines = [String(locale)];
|
|
421
449
|
for (const [key, value] of Object.entries(translations || {})) {
|
|
422
450
|
const safe = typeof value === 'string' ? value : String(value);
|
|
423
|
-
lines.push(` ${key} ${formatValue(safe, ' ')}`);
|
|
451
|
+
lines.push(` ${formatKey(key)} ${formatValue(safe, ' ')}`);
|
|
424
452
|
}
|
|
425
453
|
return lines.join('\n');
|
|
426
454
|
}
|
|
@@ -469,6 +497,8 @@ export function formatLinoCatalogs(catalogues, options = {}) {
|
|
|
469
497
|
.join('\n\n');
|
|
470
498
|
}
|
|
471
499
|
|
|
500
|
+
// The public async API converts parse errors to Promise rejections.
|
|
501
|
+
// eslint-disable-next-line require-await
|
|
472
502
|
export async function loadLocaleFromString(locale, text, options = {}) {
|
|
473
503
|
const parsedCatalogues = parseLinoCatalogs(text, options);
|
|
474
504
|
const parsed = parsedCatalogues.find(
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export interface JSXTransformOptions {
|
|
2
|
+
file?: string;
|
|
3
|
+
autoText?: boolean;
|
|
4
|
+
attributes?: string[];
|
|
5
|
+
components?: Record<string, string[]>;
|
|
6
|
+
attributeTranslator?: string;
|
|
7
|
+
serverTranslator?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface TransformedSource {
|
|
10
|
+
code: string;
|
|
11
|
+
map: {
|
|
12
|
+
version: number;
|
|
13
|
+
sources: string[];
|
|
14
|
+
sourcesContent?: Array<string | null>;
|
|
15
|
+
names: string[];
|
|
16
|
+
mappings: string;
|
|
17
|
+
} | null;
|
|
18
|
+
}
|
|
19
|
+
export declare function transformJSX(
|
|
20
|
+
code: string,
|
|
21
|
+
options?: JSXTransformOptions
|
|
22
|
+
): TransformedSource;
|
|
23
|
+
export declare function createExtractionPlugin(options?: {
|
|
24
|
+
locale?: string;
|
|
25
|
+
catalogFile?: string;
|
|
26
|
+
manifestFile?: string;
|
|
27
|
+
transform?: JSXTransformOptions;
|
|
28
|
+
}): {
|
|
29
|
+
name: string;
|
|
30
|
+
enforce: 'pre';
|
|
31
|
+
buildStart(): void;
|
|
32
|
+
transform(
|
|
33
|
+
this: { error(message: string): never },
|
|
34
|
+
code: string,
|
|
35
|
+
id: string
|
|
36
|
+
): TransformedSource | null;
|
|
37
|
+
generateBundle(this: {
|
|
38
|
+
error(message: string): never;
|
|
39
|
+
emitFile(asset: {
|
|
40
|
+
type: 'asset';
|
|
41
|
+
fileName: string;
|
|
42
|
+
source: string;
|
|
43
|
+
}): unknown;
|
|
44
|
+
resolve?(
|
|
45
|
+
source: string,
|
|
46
|
+
importer: string,
|
|
47
|
+
options: { skipSelf: true }
|
|
48
|
+
): Promise<{
|
|
49
|
+
id: string;
|
|
50
|
+
external?: boolean | 'absolute' | 'relative';
|
|
51
|
+
} | null>;
|
|
52
|
+
}): Promise<void>;
|
|
53
|
+
};
|
package/src/compiler.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Vite/Rollup plugin emits reviewable catalogs, with optional JSX translation.
|
|
2
|
+
import { parseSource } from './extract.js';
|
|
3
|
+
import { extractProject } from './extract-project.js';
|
|
4
|
+
import { formatLinoCatalog } from './catalogs.js';
|
|
5
|
+
import { transformJSX } from './transform-jsx.js';
|
|
6
|
+
|
|
7
|
+
export { transformJSX };
|
|
8
|
+
|
|
9
|
+
export function createExtractionPlugin({
|
|
10
|
+
locale = 'en',
|
|
11
|
+
catalogFile = `locales/${locale}.lino`,
|
|
12
|
+
manifestFile = 'messages.json',
|
|
13
|
+
transform,
|
|
14
|
+
} = {}) {
|
|
15
|
+
const files = new Map();
|
|
16
|
+
return {
|
|
17
|
+
name: 'lino-i18n-extract',
|
|
18
|
+
enforce: 'pre',
|
|
19
|
+
buildStart() {
|
|
20
|
+
files.clear();
|
|
21
|
+
},
|
|
22
|
+
transform(code, id) {
|
|
23
|
+
if (!/\.[cm]?[jt]sx?(?:\?|$)/.test(id) || id.includes('/node_modules/')) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
const output = transform
|
|
27
|
+
? transformJSX(code, { ...transform, file: id })
|
|
28
|
+
: null;
|
|
29
|
+
files.set(id, output?.code || code);
|
|
30
|
+
return output ? { code: output.code, map: output.map } : null;
|
|
31
|
+
},
|
|
32
|
+
async generateBundle() {
|
|
33
|
+
const resolveImport = await bundlerImports(this, files);
|
|
34
|
+
const manifest = extractProject(Object.fromEntries(files), {
|
|
35
|
+
resolveImport,
|
|
36
|
+
});
|
|
37
|
+
if (manifest.diagnostics.length) {
|
|
38
|
+
this.error(JSON.stringify(manifest.diagnostics));
|
|
39
|
+
}
|
|
40
|
+
const { messages } = manifest;
|
|
41
|
+
this.emitFile({
|
|
42
|
+
type: 'asset',
|
|
43
|
+
fileName: catalogFile,
|
|
44
|
+
source: `${formatLinoCatalog(locale, Object.fromEntries(messages.map(({ id, source }) => [id, source])))}\n`,
|
|
45
|
+
});
|
|
46
|
+
this.emitFile({
|
|
47
|
+
type: 'asset',
|
|
48
|
+
fileName: manifestFile,
|
|
49
|
+
source: `${JSON.stringify({ version: 1, messages }, null, 2)}\n`,
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function bundlerImports(context, files) {
|
|
56
|
+
const resolved = new Map();
|
|
57
|
+
if (context.resolve) {
|
|
58
|
+
for (const [file, code] of files) {
|
|
59
|
+
for (const statement of parseSource(code, file).program.body) {
|
|
60
|
+
const source = statement.source?.value;
|
|
61
|
+
if (!source) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const target = await context.resolve(source, file, { skipSelf: true });
|
|
65
|
+
if (target && !target.external && files.has(target.id)) {
|
|
66
|
+
resolved.set(`${file}\0${source}`, target.id);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return (source, file) => resolved.get(`${file}\0${source}`);
|
|
72
|
+
}
|
package/src/content.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { formatMessage } from './messages.js';
|
|
2
|
+
import { escapeMessageText } from './message-schema.js';
|
|
3
|
+
|
|
4
|
+
export function pluralCases(props) {
|
|
5
|
+
const cases = { ...props.cases };
|
|
6
|
+
for (const name of ['zero', 'one', 'two', 'few', 'many', 'other']) {
|
|
7
|
+
if (props[name] !== undefined) {
|
|
8
|
+
cases[name === 'zero' ? '=0' : name] = props[name];
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
return cases;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// The same structural message is used by T and the build-time extractor.
|
|
15
|
+
// Only code-owned elements can be restored. Translations never create props.
|
|
16
|
+
export function prepareContent(children, adapter) {
|
|
17
|
+
const values = Object.create(null);
|
|
18
|
+
let nodeId = 0;
|
|
19
|
+
let branchId = 0;
|
|
20
|
+
let renderedId = 0;
|
|
21
|
+
let derived = false;
|
|
22
|
+
|
|
23
|
+
function serializeInline(node) {
|
|
24
|
+
derived ||= node.derived;
|
|
25
|
+
return serialize(node.children);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function register(name, value) {
|
|
29
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) {
|
|
30
|
+
throw new Error(`Invalid variable name ${name}`);
|
|
31
|
+
}
|
|
32
|
+
if (Object.hasOwn(values, name) && values[name] !== value) {
|
|
33
|
+
throw new Error(`Duplicate content variable ${name}`);
|
|
34
|
+
}
|
|
35
|
+
values[name] = value;
|
|
36
|
+
return `{${name}}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function serializeBranch(element) {
|
|
40
|
+
const plural = element.plural;
|
|
41
|
+
const name = element.props.name || `n${branchId++}`;
|
|
42
|
+
register(name, plural ? element.props.count : String(element.props.value));
|
|
43
|
+
const cases = plural ? pluralCases(element.props) : element.props.cases;
|
|
44
|
+
if (!cases || !Object.hasOwn(cases, 'other')) {
|
|
45
|
+
throw new Error('Branch and Plural require an other case');
|
|
46
|
+
}
|
|
47
|
+
const kind = plural
|
|
48
|
+
? element.props.ordinal
|
|
49
|
+
? 'selectordinal'
|
|
50
|
+
: 'plural'
|
|
51
|
+
: 'select';
|
|
52
|
+
const variants = Object.entries(cases)
|
|
53
|
+
.map(([key, content]) => {
|
|
54
|
+
if (!/^(?:[A-Za-z_][A-Za-z0-9_]*|=-?\d+(?:\.\d+)?)$/.test(key)) {
|
|
55
|
+
throw new Error(`Invalid branch case ${key}`);
|
|
56
|
+
}
|
|
57
|
+
return `${key} {${serialize(content)}}`;
|
|
58
|
+
})
|
|
59
|
+
.join(' ');
|
|
60
|
+
return `{${name}, ${kind}, ${variants}}`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function serializeNode(original) {
|
|
64
|
+
const node = adapter.describe(original);
|
|
65
|
+
if (!node) {
|
|
66
|
+
return '';
|
|
67
|
+
}
|
|
68
|
+
if (node.kind === 'text') {
|
|
69
|
+
return escapeMessageText(node.value);
|
|
70
|
+
}
|
|
71
|
+
if (node.kind === 'inline') {
|
|
72
|
+
return serializeInline(node);
|
|
73
|
+
}
|
|
74
|
+
if (node.kind === 'variable') {
|
|
75
|
+
return register(node.name, node.value);
|
|
76
|
+
}
|
|
77
|
+
if (node.kind === 'branch') {
|
|
78
|
+
return serializeBranch(node);
|
|
79
|
+
}
|
|
80
|
+
const name = `c${nodeId++}`;
|
|
81
|
+
if (Object.hasOwn(values, name)) {
|
|
82
|
+
throw new Error(`Reserved content variable ${name}`);
|
|
83
|
+
}
|
|
84
|
+
if (node.opaque) {
|
|
85
|
+
values[name] = original;
|
|
86
|
+
return `{${name}}`;
|
|
87
|
+
}
|
|
88
|
+
values[name] = (chunks) =>
|
|
89
|
+
adapter.clone(original, chunks, `${name}-${renderedId++}`);
|
|
90
|
+
return `<${name}>${serialize(node.children)}</${name}>`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function serialize(nodes) {
|
|
94
|
+
return adapter.nodes(nodes).map(serializeNode).join('');
|
|
95
|
+
}
|
|
96
|
+
const source = serialize(children);
|
|
97
|
+
return { source, values, derived };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function renderContent(
|
|
101
|
+
i18n,
|
|
102
|
+
{ children, id, source, values = {}, locale },
|
|
103
|
+
prepare
|
|
104
|
+
) {
|
|
105
|
+
const prepared =
|
|
106
|
+
source === undefined ? prepare(children) : { source, values: {} };
|
|
107
|
+
if (prepared.derived && id) {
|
|
108
|
+
throw new Error(
|
|
109
|
+
'Derived messages use source identities; omit an explicit id'
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
const key = id || prepared.source;
|
|
113
|
+
const message =
|
|
114
|
+
i18n.getEnabled?.() === false
|
|
115
|
+
? prepared.source
|
|
116
|
+
: i18n.t(key, {}, { locale, defaultValue: prepared.source });
|
|
117
|
+
return formatMessage(
|
|
118
|
+
message,
|
|
119
|
+
{ ...prepared.values, ...values },
|
|
120
|
+
i18n
|
|
121
|
+
.getLocaleConfig?.()
|
|
122
|
+
.resolveCanonicalLocale(locale || i18n.getLocale()) ||
|
|
123
|
+
locale ||
|
|
124
|
+
i18n.getLocale()
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function selectPlural(locale, props) {
|
|
129
|
+
const cases = pluralCases(props);
|
|
130
|
+
const exact = `=${props.count}`;
|
|
131
|
+
const category = new Intl.PluralRules(locale, {
|
|
132
|
+
type: props.ordinal ? 'ordinal' : 'cardinal',
|
|
133
|
+
}).select(props.count);
|
|
134
|
+
return cases[exact] ?? cases[category] ?? cases.other ?? null;
|
|
135
|
+
}
|
|
@@ -13,35 +13,19 @@
|
|
|
13
13
|
// takes `defaultMessage`.
|
|
14
14
|
// - Compiled AST messages are decompiled back to ICU when possible.
|
|
15
15
|
|
|
16
|
+
import { printAST } from '@formatjs/icu-messageformat-parser/printer.js';
|
|
17
|
+
|
|
16
18
|
function decompileAst(ast) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
try {
|
|
20
|
+
// Historical bundles may contain bare strings alongside AST nodes.
|
|
21
|
+
return printAST(
|
|
22
|
+
ast.map((node) =>
|
|
23
|
+
typeof node === 'string' ? { type: 0, value: node } : node
|
|
24
|
+
)
|
|
25
|
+
);
|
|
26
|
+
} catch {
|
|
21
27
|
return null;
|
|
22
28
|
}
|
|
23
|
-
let out = '';
|
|
24
|
-
for (const node of ast) {
|
|
25
|
-
if (typeof node === 'string') {
|
|
26
|
-
out += node;
|
|
27
|
-
continue;
|
|
28
|
-
}
|
|
29
|
-
if (!node || typeof node !== 'object') {
|
|
30
|
-
continue;
|
|
31
|
-
}
|
|
32
|
-
// FormatJS node shapes: { type: 0, value: 'text' } (literal)
|
|
33
|
-
// { type: 1, value: 'name' } (argument), etc.
|
|
34
|
-
if (node.type === 0 && typeof node.value === 'string') {
|
|
35
|
-
out += node.value;
|
|
36
|
-
} else if (node.type === 1 && typeof node.value === 'string') {
|
|
37
|
-
out += `{${node.value}}`;
|
|
38
|
-
} else if (typeof node.value === 'string') {
|
|
39
|
-
out += `{${node.value}}`;
|
|
40
|
-
} else {
|
|
41
|
-
return null; // cannot represent reliably
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
return out;
|
|
45
29
|
}
|
|
46
30
|
|
|
47
31
|
function normaliseMessage(entry) {
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
function put(object, parts, value) {
|
|
2
|
+
const [key, ...remaining] = parts;
|
|
3
|
+
if (remaining.length) {
|
|
4
|
+
if (!Object.hasOwn(object, key) || typeof object[key] !== 'object') {
|
|
5
|
+
Object.defineProperty(object, key, {
|
|
6
|
+
value: {},
|
|
7
|
+
enumerable: true,
|
|
8
|
+
writable: true,
|
|
9
|
+
configurable: true,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
put(object[key], remaining, value);
|
|
13
|
+
} else {
|
|
14
|
+
Object.defineProperty(object, key, {
|
|
15
|
+
value,
|
|
16
|
+
enumerable: true,
|
|
17
|
+
writable: true,
|
|
18
|
+
configurable: true,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function dictionarySchema(schema) {
|
|
24
|
+
const ancestors = new Set();
|
|
25
|
+
const sources = [];
|
|
26
|
+
let visited = 0;
|
|
27
|
+
function clone(node, path, depth) {
|
|
28
|
+
visited += 1;
|
|
29
|
+
if (depth > 20 || visited > 10000) {
|
|
30
|
+
throw new TypeError('Dictionary depth/node limit exceeded');
|
|
31
|
+
}
|
|
32
|
+
if (typeof node === 'string') {
|
|
33
|
+
sources.push([path.join('.'), node]);
|
|
34
|
+
return node;
|
|
35
|
+
}
|
|
36
|
+
if (!node || typeof node !== 'object') {
|
|
37
|
+
throw new TypeError('Dictionary leaves must be source strings');
|
|
38
|
+
}
|
|
39
|
+
if (ancestors.has(node)) {
|
|
40
|
+
throw new TypeError('Dictionary cycle detected');
|
|
41
|
+
}
|
|
42
|
+
ancestors.add(node);
|
|
43
|
+
const keys = Object.keys(node);
|
|
44
|
+
if (keys.length > 10000 || (Array.isArray(node) && node.length > 10000)) {
|
|
45
|
+
throw new TypeError('Dictionary node limit exceeded');
|
|
46
|
+
}
|
|
47
|
+
if (
|
|
48
|
+
Array.isArray(node) &&
|
|
49
|
+
(keys.length !== node.length ||
|
|
50
|
+
keys.some((key, index) => key !== String(index)))
|
|
51
|
+
) {
|
|
52
|
+
throw new TypeError(
|
|
53
|
+
'Dictionary arrays cannot be sparse or have named properties'
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
const entries = keys.map((key) => {
|
|
57
|
+
if (!key || key.includes('.')) {
|
|
58
|
+
throw new TypeError(`Invalid dictionary key ${key}`);
|
|
59
|
+
}
|
|
60
|
+
const descriptor = Object.getOwnPropertyDescriptor(node, key);
|
|
61
|
+
if (!Object.hasOwn(descriptor, 'value')) {
|
|
62
|
+
throw new TypeError('Dictionary accessors are not supported');
|
|
63
|
+
}
|
|
64
|
+
return [key, clone(descriptor.value, [...path, key], depth + 1)];
|
|
65
|
+
});
|
|
66
|
+
ancestors.delete(node);
|
|
67
|
+
return Object.freeze(
|
|
68
|
+
Array.isArray(node)
|
|
69
|
+
? entries.map(([, value]) => value)
|
|
70
|
+
: Object.fromEntries(entries)
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
if (!schema || typeof schema !== 'object') {
|
|
74
|
+
throw new TypeError('A dictionary schema must be an object or array');
|
|
75
|
+
}
|
|
76
|
+
return { schema: clone(schema, [], 0), sources: Object.fromEntries(sources) };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function defineDictionary(schema) {
|
|
80
|
+
return dictionarySchema(schema).schema;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function scopedDictionary(getTranslator, prefix = '') {
|
|
84
|
+
const path = (key) => (prefix ? (key ? `${prefix}.${key}` : prefix) : key);
|
|
85
|
+
const translate = (key, values, options) => {
|
|
86
|
+
const i18n = getTranslator();
|
|
87
|
+
if (!i18n.dictionary) {
|
|
88
|
+
throw new Error('Dictionary helpers require a createTranslator instance');
|
|
89
|
+
}
|
|
90
|
+
return i18n.dictionary(path(key), values, options);
|
|
91
|
+
};
|
|
92
|
+
translate.obj = (key = '', values, options) =>
|
|
93
|
+
getTranslator().dictionaryTree(path(key), values, options);
|
|
94
|
+
return translate;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function withDictionary(options) {
|
|
98
|
+
if (options.dictionary === undefined) {
|
|
99
|
+
return options;
|
|
100
|
+
}
|
|
101
|
+
const { schema, sources } = dictionarySchema(options.dictionary);
|
|
102
|
+
const locale = options.sourceLocale || 'en';
|
|
103
|
+
return {
|
|
104
|
+
...options,
|
|
105
|
+
dictionary: schema,
|
|
106
|
+
locales: {
|
|
107
|
+
...options.locales,
|
|
108
|
+
[locale]: { ...sources, ...options.locales?.[locale] },
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function renderDictionary(node, path, translate) {
|
|
114
|
+
if (typeof node === 'string') {
|
|
115
|
+
return translate(path);
|
|
116
|
+
}
|
|
117
|
+
const entries = Object.entries(node).map(([key, value]) => [
|
|
118
|
+
key,
|
|
119
|
+
renderDictionary(value, path ? `${path}.${key}` : key, translate),
|
|
120
|
+
]);
|
|
121
|
+
return Array.isArray(node)
|
|
122
|
+
? entries.map(([, value]) => value)
|
|
123
|
+
: Object.fromEntries(entries);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function createDictionaryApi(
|
|
127
|
+
core,
|
|
128
|
+
gt,
|
|
129
|
+
tables,
|
|
130
|
+
{ sourceLocale = 'en', dictionary: schema } = {}
|
|
131
|
+
) {
|
|
132
|
+
function dictionary(key, values, options) {
|
|
133
|
+
const source = core.t(key, {}, { locale: sourceLocale });
|
|
134
|
+
return gt({ source, id: key }, values, options);
|
|
135
|
+
}
|
|
136
|
+
function dictionaryObject(prefix = '', values, options) {
|
|
137
|
+
const keys = new Set(
|
|
138
|
+
[...tables.values()].flatMap((table) => Object.keys(table))
|
|
139
|
+
);
|
|
140
|
+
const object = {};
|
|
141
|
+
for (const key of [...keys].sort()) {
|
|
142
|
+
if (!prefix || key.startsWith(`${prefix}.`)) {
|
|
143
|
+
put(
|
|
144
|
+
object,
|
|
145
|
+
(prefix ? key.slice(prefix.length + 1) : key).split('.'),
|
|
146
|
+
dictionary(key, values, options)
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return object;
|
|
151
|
+
}
|
|
152
|
+
function dictionaryTree(prefix = '', values, options) {
|
|
153
|
+
if (schema === undefined) {
|
|
154
|
+
return dictionaryObject(prefix, values, options);
|
|
155
|
+
}
|
|
156
|
+
let node = schema;
|
|
157
|
+
for (const key of prefix ? prefix.split('.') : []) {
|
|
158
|
+
if (!node || typeof node !== 'object' || !Object.hasOwn(node, key)) {
|
|
159
|
+
throw new Error(`Unknown dictionary path ${prefix}`);
|
|
160
|
+
}
|
|
161
|
+
node = node[key];
|
|
162
|
+
}
|
|
163
|
+
return renderDictionary(node, prefix, (key) =>
|
|
164
|
+
dictionary(key, values, options)
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
dictionary.obj = dictionaryTree;
|
|
168
|
+
return { dictionary, dictionaryObject, dictionaryTree };
|
|
169
|
+
}
|
package/src/eslint.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ESLint, Linter } from 'eslint';
|
|
2
|
+
|
|
3
|
+
export interface LinoLintOptions {
|
|
4
|
+
/** UTF-8 source bytes. Default 1 MiB, maximum 10 MiB. */
|
|
5
|
+
maxBytes?: number;
|
|
6
|
+
/** Default 100,000, maximum 200,000 AST nodes. */
|
|
7
|
+
maxNodes?: number;
|
|
8
|
+
/** Default 100, maximum 200 AST levels. */
|
|
9
|
+
maxDepth?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare const plugin: ESLint.Plugin & {
|
|
13
|
+
configs: { recommended: Linter.Config };
|
|
14
|
+
};
|
|
15
|
+
export default plugin;
|