lino-i18n 0.4.1 → 0.5.0
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 +6 -0
- package/README.md +36 -0
- 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,11 @@
|
|
|
1
1
|
# lino-i18n Changelog
|
|
2
2
|
|
|
3
|
+
## 0.5.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 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.
|
|
8
|
+
|
|
3
9
|
## 0.4.1
|
|
4
10
|
|
|
5
11
|
### 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/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
|
)) {
|