lino-i18n 0.4.1 → 0.5.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 +12 -0
- package/README.md +36 -0
- package/bin/lino-i18n.js +11 -4
- package/package.json +1 -1
- package/src/index.d.ts +6 -1
- package/src/loaders.d.ts +15 -0
- package/src/loaders.js +19 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# lino-i18n Changelog
|
|
2
2
|
|
|
3
|
+
## 0.5.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 7be5c34: Fix the `lino-i18n` CLI doing nothing and exiting with code 0 when run through the npm bin symlink (`npx lino-i18n`, `node_modules/.bin/lino-i18n`, npm scripts). The entry-point check now compares real paths, so `lino-i18n check` reports out-of-sync catalogs again.
|
|
8
|
+
|
|
9
|
+
## 0.5.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- 38fa1d4: Add opt-in recursive directory loading with deterministic depth-first merging of nested locale catalogs, preserving compatibility aliases after merging. Publish types for the loaders entry point and the recursive option.
|
|
14
|
+
|
|
3
15
|
## 0.4.1
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -66,6 +66,42 @@ i18n.t('role', { context: 'female' }); // → "She is a developer"
|
|
|
66
66
|
i18n.t('telegram.help.solve.alias.detail'); // → "Tool aliases imply `--tool <tool>`"
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
+
### Nested locale directories
|
|
70
|
+
|
|
71
|
+
Directory loading is shallow by default. Use `recursive: true` to merge UI text,
|
|
72
|
+
user prompts and system prompts into the same locale tables:
|
|
73
|
+
|
|
74
|
+
```text
|
|
75
|
+
locales/
|
|
76
|
+
en.lino
|
|
77
|
+
prompts/
|
|
78
|
+
messages.lino
|
|
79
|
+
system/
|
|
80
|
+
messages.lino
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
const catalogues = await loadLocalesFromDirectory('./locales', {
|
|
85
|
+
recursive: true,
|
|
86
|
+
compatibilityAliases: ['collapseTail', 'parentLabel'], // Optional migration aliases
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Each file can declare multiple locales; locale roots inside files determine the
|
|
91
|
+
catalogue language, regardless of filenames or directory names. Directory names
|
|
92
|
+
add no prefix to translation keys. Recursive traversal sorts entries by name in
|
|
93
|
+
UTF-16 code-unit order (case-sensitive and independent of the host locale), then
|
|
94
|
+
visits them depth first: a directory's contents are processed at that directory's
|
|
95
|
+
position among its siblings. Later files and later locale blocks overwrite earlier
|
|
96
|
+
values for duplicate keys in the same locale. For example, `en.lino` is read before
|
|
97
|
+
`prompts/`, and `z.lino` after it. Other keys are preserved.
|
|
98
|
+
|
|
99
|
+
Only regular `.lino` files are loaded; symbolic links and other files are skipped.
|
|
100
|
+
Read and parse errors reject the load. Compatibility aliases (including the
|
|
101
|
+
`mode` shortcut) are expanded once after the entire tree is merged, preserving
|
|
102
|
+
explicit keys. The returned catalogues work with the usual `createI18n` fallback,
|
|
103
|
+
interpolation and plural handling.
|
|
104
|
+
|
|
69
105
|
## Browser
|
|
70
106
|
|
|
71
107
|
Import `lino-i18n/browser` for native ES modules or browser bundles. This
|
package/bin/lino-i18n.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
//
|
|
5
5
|
// Configuration priority: CLI flags > JSON `--config` file > defaults.
|
|
6
6
|
|
|
7
|
-
import { promises as fs } from 'node:fs';
|
|
7
|
+
import { promises as fs, realpathSync } from 'node:fs';
|
|
8
8
|
import path from 'node:path';
|
|
9
9
|
import { fileURLToPath, URL } from 'node:url';
|
|
10
10
|
|
|
@@ -386,12 +386,19 @@ function unknownCommand(command, flags, rest, log, err) {
|
|
|
386
386
|
return 1;
|
|
387
387
|
}
|
|
388
388
|
|
|
389
|
-
|
|
390
|
-
|
|
389
|
+
// npm installs the bin as a symlink (`node_modules/.bin/lino-i18n ->
|
|
390
|
+
// ../lino-i18n/bin/lino-i18n.js`). Node keeps the symlink path in
|
|
391
|
+
// `process.argv[1]` while `import.meta.url` is the resolved real path, so both
|
|
392
|
+
// sides must be compared after resolving symlinks.
|
|
393
|
+
export function isCliEntryPoint(
|
|
394
|
+
argvPath = process.argv[1],
|
|
395
|
+
modulePath = fileURLToPath(import.meta.url)
|
|
396
|
+
) {
|
|
397
|
+
if (!argvPath) {
|
|
391
398
|
return false;
|
|
392
399
|
}
|
|
393
400
|
try {
|
|
394
|
-
return
|
|
401
|
+
return realpathSync(argvPath) === realpathSync(modulePath);
|
|
395
402
|
} catch {
|
|
396
403
|
return false;
|
|
397
404
|
}
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -142,9 +142,14 @@ export declare function loadLocalesFromFile(
|
|
|
142
142
|
options?: CompatibilityAliasOptions
|
|
143
143
|
): Promise<Array<{ locale: string; translations: Record<string, string> }>>;
|
|
144
144
|
|
|
145
|
+
export interface DirectoryLoaderOptions extends CompatibilityAliasOptions {
|
|
146
|
+
/** Walk subdirectories in sorted depth-first order. Defaults to false. */
|
|
147
|
+
recursive?: boolean;
|
|
148
|
+
}
|
|
149
|
+
|
|
145
150
|
export declare function loadLocalesFromDirectory(
|
|
146
151
|
directory: string,
|
|
147
|
-
options?:
|
|
152
|
+
options?: DirectoryLoaderOptions
|
|
148
153
|
): Promise<Record<string, Record<string, string>>>;
|
|
149
154
|
|
|
150
155
|
export declare function interpolate(
|
package/src/loaders.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export {
|
|
2
|
+
parseLinoCatalog,
|
|
3
|
+
parseLinoCatalogs,
|
|
4
|
+
formatLinoCatalog,
|
|
5
|
+
formatLinoCatalogs,
|
|
6
|
+
loadLocaleFromString,
|
|
7
|
+
loadLocaleFromFile,
|
|
8
|
+
loadLocalesFromFile,
|
|
9
|
+
loadLocalesFromDirectory,
|
|
10
|
+
} from './index.js';
|
|
11
|
+
export type {
|
|
12
|
+
CompatibilityAlias,
|
|
13
|
+
CompatibilityAliasOptions,
|
|
14
|
+
DirectoryLoaderOptions,
|
|
15
|
+
} from './index.js';
|
package/src/loaders.js
CHANGED
|
@@ -34,19 +34,29 @@ export async function loadLocalesFromFile(filePath, options = {}) {
|
|
|
34
34
|
];
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
// Yield files in traversal order; merge once so aliases never mask explicit keys.
|
|
38
|
+
async function* localeFiles(directory, recursive) {
|
|
38
39
|
const entries = (await fs.readdir(directory, { withFileTypes: true })).sort(
|
|
39
|
-
(left, right) =>
|
|
40
|
+
(left, right) => {
|
|
41
|
+
if (!recursive) {
|
|
42
|
+
return left.name.localeCompare(right.name);
|
|
43
|
+
}
|
|
44
|
+
return left.name < right.name ? -1 : left.name > right.name ? 1 : 0;
|
|
45
|
+
}
|
|
40
46
|
);
|
|
41
|
-
const catalogues = {};
|
|
42
47
|
for (const entry of entries) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
+
const entryPath = path.join(directory, entry.name);
|
|
49
|
+
if (recursive && entry.isDirectory()) {
|
|
50
|
+
yield* localeFiles(entryPath, recursive);
|
|
51
|
+
} else if (entry.isFile() && entry.name.endsWith('.lino')) {
|
|
52
|
+
yield entryPath;
|
|
48
53
|
}
|
|
49
|
-
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export async function loadLocalesFromDirectory(directory, options = {}) {
|
|
58
|
+
const catalogues = {};
|
|
59
|
+
for await (const filePath of localeFiles(directory, options.recursive)) {
|
|
50
60
|
for (const { locale, translations } of await loadLocalesFromFile(
|
|
51
61
|
filePath
|
|
52
62
|
)) {
|