@dokebi/i18n 0.2.8 → 0.2.10
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/AI.md +2 -0
- package/dist/catalogs.d.ts +2 -1
- package/dist/catalogs.js +77 -39
- package/dist/contract.d.ts +26 -0
- package/dist/layers.d.ts +11 -0
- package/dist/layers.js +28 -2
- package/dist/page.d.ts +5 -1
- package/dist/page.js +88 -3
- package/dist/plugin.d.ts +5 -0
- package/dist/plugin.js +131 -6
- package/dist/service.d.ts +14 -1
- package/dist/service.js +68 -0
- package/package.json +3 -2
package/AI.md
CHANGED
|
@@ -81,6 +81,8 @@ export const layers = await loadLayers(new URL('../../', import.meta.url));
|
|
|
81
81
|
|
|
82
82
|
- **The server:** `request.i18n` translates in the scope of the request's app — its layer, the modules, the shared catalogs. The app comes from @dokebi/web's `appOf(request)`, which @dokebi/app answers from the host; an app without a layer (or no @dokebi/app) gets the modules and the shared catalogs. Outside a request, `i18n.forLocale(locale, { app: 'admin' })` (a job, a mail); `i18n.apps` lists the apps with a layer. With app layers and nothing that tells a request's app, the start warns `I18N_APP_LAYERS_UNUSED`.
|
|
83
83
|
- **The page:** the `i18n` prop carries the bundles of the request's app scope only (another app's layer never reaches the page), merged per language, so `useI18n()` in the browser gives exactly what `request.i18n.t()` gives on the server. A namespace no app layer changed has the same bundle token in every app (the browser keeps it once).
|
|
84
|
+
- **The words of a domain template (0.2.9, [D-0282](https://git.nyx-zone.com/dokebi/framework/-/blob/main/docs/decisions/D-0282-i18n-template-layers.md)).** An @dokebi/app domain draws an app with a template (`domains.<host>.template`, the views folder). `src/apps/<app>/views/<template>/locales/<lang>/<ns>.json` and `src/shared/modules/<module>/views/<template>/locales/<lang>/<ns>.json` replace keys **in the page bundles of that template's domains only**: the same keys, other words (`common.money`: "Holdings" on the elite domains, "Money" elsewhere), so shared components keep their keys and need no template branch. `loadLayers` reads them as `layers.templates` (folders without `locales/` skipped; template names `^[a-z][a-z0-9-]{0,31}$`). Within one language: the app's template words, then the modules' template words (by name), then that app's scope (app > module > shared); the language still comes first (no Korean template words: the Korean page keeps its Korean wording). **Server `t()` never reads them** (errors, notices and mail keep the app's words). At start a template key that the catalogs under it do not have in that language (the app's scope for an app's template; the modules, the apps and the shared ones for a module's), or with other placeholders, fails with `INVALID_CATALOG` naming the file and the key: a template layer only replaces words, and a typo would otherwise stay silent. A template layer adds no language. Only the namespaces a template layer touches get bundles of their own (their own tokens), so a browser that goes between a default and an elite domain never takes one's bundle for the other's (claims, the pageCache cookie and the stored bundles of D-0126 all go by token); the version covers the template layers. The template of a request comes from @dokebi/web's `templateOf(request)` (web 0.4.25), which @dokebi/app 0.4.18 answers from the host; without it the start warns `I18N_TEMPLATE_LAYERS_UNUSED`. `dokebi describe` lists them (section `i18n`: `templates[]` with `kind`, `name`, `template`, `keys`), and `i18n.templates` (the plugin's provider) has the same.
|
|
85
|
+
- **The words of a template bundle (0.2.10, [D-0291](https://git.nyx-zone.com/dokebi/framework/-/blob/main/docs/decisions/D-0291-template-bundles-at-runtime.md)).** A template built apart from the app (@dokebi/template-inertia template bundles) may carry `locales/<locale>/<namespace>.json`; template-inertia hands them over while the app runs with `i18n.templateBundle(app, template, directory)`, and `templateBundle(app, template, undefined)` takes them out again. They go over the app's template layer of that template (the bundle's word wins key by key), into the page bundles of that template's domains only (server `t()` never reads them), under the rules of the template layers: keys the catalogs under them have, the same placeholders, the app's locales, the bounds — the same checks as the start, run again. A refused set changes nothing and answers `{ ok: false, reason }`; an applied one `{ ok: true, keys, locales }` and info `I18N_TEMPLATE_BUNDLE_WORDS`. The page bundles are rebuilt for the next page (new tokens for the touched namespaces). Without `http.enabled` there are no page bundles: `{ ok: false }`. 0.2.10 also asks a page's app whenever template layers exist: before, the template words of an app with no layer of its own (and no `http.apps` entry) never reached its pages.
|
|
84
86
|
- **Intended overrides:** a namespace file of an app or module may start with `"$overrides": ["title", "nav.home"]` — the keys (inside the namespace) it overrides on purpose. The loader checks its shape and leaves it out of the messages; `dokebi check` stops reporting those keys (`i18n-layer-override`, @dokebi/cli AI.md).
|
|
85
87
|
|
|
86
88
|
Use one folder per locale and one JSON file per namespace: `locales/en/auth.json`, `locales/ko/auth.json`, `locales/en/posts.json`, etc. A nested value `{"password":{"minimum":"At least {count} characters"}}` in `auth.json` becomes `auth.password.minimum`. Adding a namespace needs no import list. Files are discovered once when loading the entry, never per request. Restart after changes. `loadCatalogs(directory: string | URL)` is a Node-only subpath export; it accepts regular JSON files, safe ASCII path segments and at most 16 nested levels (`maxDepth`, which bounds the loader's own recursion), and only reads: the counts and sizes (`maxLocales`, `maxMessages`, `maxCatalogBytes`, `maxCatalogCharacters`, `maxKeyLength`) are the plugin options, checked at setup where dokebi.yaml is known (D-0118), so an entry file needs no limits. `loadCatalogs(directory, { maxMessages, maxLocales, maxBytes, maxKeyLength, maxDepth })` refuses earlier, while loading, when a caller asks (a test that loads catalogs by itself). An object is a plural message only when its keys are `other` and other cardinal categories (`zero`, `one`, `two`, `few`, `many`) and every value is a string; an object with any other key is nested, so `{"regions":{"kr":"Korea","other":"Other"}}` gives `regions.kr` and `regions.other`. It returns flat catalogs for constant-time lookup; inline catalogs remain supported. Invalid files throw `INVALID_CATALOG` with the original cause. Entries whose name starts with a dot (`.claude/`, `.DS_Store`, `.draft.json`) are never locales, namespaces, apps or modules: `loadCatalogs` and `loadLayers` skip them at every level (0.2.8; before, a `.claude/` folder an editor tool left in a `locales/` folder failed the start with `Invalid BCP 47 locale`), `catalogSource(catalogs).skipped` names them (paths relative to the folder read, at most 100), and the plugin logs them once at start at `debug` (`I18N_ENTRIES_SKIPPED`).
|
package/dist/catalogs.d.ts
CHANGED
|
@@ -32,7 +32,8 @@ export declare function catalogSource(catalogs: unknown): CatalogSource | undefi
|
|
|
32
32
|
* D-0181: the app and module layers under `src` (`src/apps/<app>/locales/` and
|
|
33
33
|
* `src/shared/modules/<module>/locales/`, each read like `loadCatalogs`). An entry exports it as
|
|
34
34
|
* `layers` next to `catalogs` (the shared layer, `src/shared/locales/`). Folders without a
|
|
35
|
-
* `locales` folder are skipped.
|
|
35
|
+
* `locales` folder are skipped. D-0282: also the template layers, `views/<template>/locales/` of
|
|
36
|
+
* each app and module (`layers.templates`, only when there is one).
|
|
36
37
|
*/
|
|
37
38
|
export declare function loadLayers(src: string | URL, limits?: CatalogLimits): Promise<CatalogLayers>;
|
|
38
39
|
/** Load once in an entry: locales/<locale>/<namespace>.json -> namespace.nested.key. */
|
package/dist/catalogs.js
CHANGED
|
@@ -27,73 +27,111 @@ export function catalogSource(catalogs) {
|
|
|
27
27
|
}
|
|
28
28
|
/** App and module names: the folders of src/apps and src/shared/modules (D-0181). */
|
|
29
29
|
const LAYER_NAME = /^[a-z][a-z0-9-]{0,63}$/;
|
|
30
|
+
/** D-0282: template names, as @dokebi/app's `domains.<host>.template` takes them. */
|
|
31
|
+
const TEMPLATE_NAME = /^[a-z][a-z0-9-]{0,31}$/;
|
|
30
32
|
/**
|
|
31
33
|
* D-0181: the app and module layers under `src` (`src/apps/<app>/locales/` and
|
|
32
34
|
* `src/shared/modules/<module>/locales/`, each read like `loadCatalogs`). An entry exports it as
|
|
33
35
|
* `layers` next to `catalogs` (the shared layer, `src/shared/locales/`). Folders without a
|
|
34
|
-
* `locales` folder are skipped.
|
|
36
|
+
* `locales` folder are skipped. D-0282: also the template layers, `views/<template>/locales/` of
|
|
37
|
+
* each app and module (`layers.templates`, only when there is one).
|
|
35
38
|
*/
|
|
36
39
|
export async function loadLayers(src, limits = {}) {
|
|
37
40
|
const root = src instanceof URL ? fileURLToPath(src) : src;
|
|
38
41
|
let bytes = 0;
|
|
39
42
|
let files = 0;
|
|
40
43
|
const skipped = [];
|
|
41
|
-
|
|
42
|
-
|
|
44
|
+
/** Whether `path` (under root) is a folder; a missing one is not. */
|
|
45
|
+
const folderAt = async (path) => {
|
|
46
|
+
try {
|
|
47
|
+
return (await stat(join(root, path))).isDirectory();
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
if (error.code === 'ENOENT')
|
|
51
|
+
return false;
|
|
52
|
+
throw new I18nError('INVALID_CATALOG', `Cannot read ${path}`, error);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
/** The sub-folders of `path` (under root), sorted, dot entries left out (and noted). */
|
|
56
|
+
const folders = async (path) => {
|
|
43
57
|
let entries;
|
|
44
58
|
try {
|
|
45
|
-
entries = await readdir(join(root,
|
|
59
|
+
entries = await readdir(join(root, path), { withFileTypes: true });
|
|
46
60
|
}
|
|
47
61
|
catch (error) {
|
|
48
|
-
// No apps or
|
|
62
|
+
// No apps, modules or views folder: nothing there.
|
|
49
63
|
if (error.code === 'ENOENT')
|
|
50
|
-
return
|
|
51
|
-
throw new I18nError('INVALID_CATALOG', `Cannot read ${
|
|
64
|
+
return [];
|
|
65
|
+
throw new I18nError('INVALID_CATALOG', `Cannot read ${path}`, error);
|
|
52
66
|
}
|
|
67
|
+
const out = [];
|
|
53
68
|
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
54
69
|
if (!entry.isDirectory())
|
|
55
70
|
continue;
|
|
56
71
|
if (hidden(entry.name)) {
|
|
57
72
|
if (skipped.length < MAX_SKIPPED)
|
|
58
|
-
skipped.push(`${
|
|
73
|
+
skipped.push(`${path}/${entry.name}`);
|
|
59
74
|
continue;
|
|
60
75
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
76
|
+
out.push(entry.name);
|
|
77
|
+
}
|
|
78
|
+
return out;
|
|
79
|
+
};
|
|
80
|
+
/** One `locales` folder, read like `loadCatalogs` within what the others left of maxBytes. */
|
|
81
|
+
const load = async (path, what) => {
|
|
82
|
+
let catalogs;
|
|
83
|
+
try {
|
|
84
|
+
catalogs = await loadCatalogs(join(root, path), {
|
|
85
|
+
...limits,
|
|
86
|
+
...(limits.maxBytes === undefined ? {} : { maxBytes: limits.maxBytes - bytes }),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
throw new I18nError('INVALID_CATALOG', `Cannot load the ${what} ${path}`, error);
|
|
91
|
+
}
|
|
92
|
+
const source = SOURCES.get(catalogs);
|
|
93
|
+
bytes += source.bytes;
|
|
94
|
+
files += source.files;
|
|
95
|
+
for (const skippedPath of source.skipped)
|
|
96
|
+
if (skipped.length < MAX_SKIPPED)
|
|
97
|
+
skipped.push(`${path}/${skippedPath}`);
|
|
98
|
+
return catalogs;
|
|
99
|
+
};
|
|
100
|
+
const read = async (parent, kind) => {
|
|
101
|
+
const layers = Object.create(null);
|
|
102
|
+
const templates = Object.create(null);
|
|
103
|
+
for (const name of await folders(parent)) {
|
|
104
|
+
const own = await folderAt(`${parent}/${name}/locales`);
|
|
105
|
+
// D-0282: views/<template>/locales of the app or module, the words of a domain template.
|
|
106
|
+
const byTemplate = Object.create(null);
|
|
107
|
+
for (const template of await folders(`${parent}/${name}/views`)) {
|
|
108
|
+
const path = `${parent}/${name}/views/${template}/locales`;
|
|
109
|
+
if (!(await folderAt(path)))
|
|
69
110
|
continue;
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
throw new I18nError('INVALID_CATALOG', `The ${kind} folder ${JSON.stringify(entry.name)} is not a ${kind} name`);
|
|
74
|
-
let catalogs;
|
|
75
|
-
try {
|
|
76
|
-
catalogs = await loadCatalogs(folder, {
|
|
77
|
-
...limits,
|
|
78
|
-
...(limits.maxBytes === undefined ? {} : { maxBytes: limits.maxBytes - bytes }),
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
catch (error) {
|
|
82
|
-
throw new I18nError('INVALID_CATALOG', `Cannot load the ${kind} layer ${parent}/${entry.name}/locales`, error);
|
|
111
|
+
if (!TEMPLATE_NAME.test(template))
|
|
112
|
+
throw new I18nError('INVALID_CATALOG', `The template folder ${JSON.stringify(template)} of ${parent}/${name}/views is not a template name (a-z, 0-9, -, at most 32)`);
|
|
113
|
+
byTemplate[template] = await load(path, `template layer`);
|
|
83
114
|
}
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
115
|
+
const hasTemplates = Object.keys(byTemplate).length > 0;
|
|
116
|
+
if (!own && !hasTemplates)
|
|
117
|
+
continue;
|
|
118
|
+
if (!LAYER_NAME.test(name))
|
|
119
|
+
throw new I18nError('INVALID_CATALOG', `The ${kind} folder ${JSON.stringify(name)} is not a ${kind} name`);
|
|
120
|
+
if (own)
|
|
121
|
+
layers[name] = await load(`${parent}/${name}/locales`, `${kind} layer`);
|
|
122
|
+
if (hasTemplates)
|
|
123
|
+
templates[name] = Object.freeze(byTemplate);
|
|
91
124
|
}
|
|
92
|
-
return Object.freeze(
|
|
125
|
+
return { layers: Object.freeze(layers), templates: Object.freeze(templates) };
|
|
93
126
|
};
|
|
127
|
+
const apps = await read('apps', 'app');
|
|
128
|
+
const modules = await read('shared/modules', 'module');
|
|
94
129
|
const layers = Object.freeze({
|
|
95
|
-
apps:
|
|
96
|
-
modules:
|
|
130
|
+
apps: apps.layers,
|
|
131
|
+
modules: modules.layers,
|
|
132
|
+
...(Object.keys(apps.templates).length || Object.keys(modules.templates).length
|
|
133
|
+
? { templates: Object.freeze({ apps: apps.templates, modules: modules.templates }) }
|
|
134
|
+
: {}),
|
|
97
135
|
});
|
|
98
136
|
SOURCES.set(layers, Object.freeze({ bytes, files, skipped: Object.freeze(skipped) }));
|
|
99
137
|
return layers;
|
package/dist/contract.d.ts
CHANGED
|
@@ -7,6 +7,15 @@ export type Catalogs = Readonly<Record<string, Readonly<Record<string, Message>>
|
|
|
7
7
|
export interface CatalogLayers {
|
|
8
8
|
readonly apps?: Readonly<Record<string, Catalogs>>;
|
|
9
9
|
readonly modules?: Readonly<Record<string, Catalogs>>;
|
|
10
|
+
/**
|
|
11
|
+
* D-0282: the screen words of a domain template (`views/<template>/locales/` of an app or a
|
|
12
|
+
* module), by app or module, then by template. They replace keys of that app's page bundles on
|
|
13
|
+
* the domains of that template only; server `t()` never reads them.
|
|
14
|
+
*/
|
|
15
|
+
readonly templates?: {
|
|
16
|
+
readonly apps?: Readonly<Record<string, Readonly<Record<string, Catalogs>>>>;
|
|
17
|
+
readonly modules?: Readonly<Record<string, Readonly<Record<string, Catalogs>>>>;
|
|
18
|
+
};
|
|
10
19
|
}
|
|
11
20
|
export interface I18nEntry {
|
|
12
21
|
readonly catalogs: Catalogs;
|
|
@@ -44,4 +53,21 @@ export interface I18nApi {
|
|
|
44
53
|
* that is not a time zone. Call it in every worker (runtime-settings onChange).
|
|
45
54
|
*/
|
|
46
55
|
setTimeZone(zone: string): void;
|
|
56
|
+
/**
|
|
57
|
+
* D-0291: the screen words of a template bundle (template-inertia loads it while the app runs):
|
|
58
|
+
* the `locales/<locale>/<namespace>.json` under `directory` become the words of `template` on
|
|
59
|
+
* `app`'s pages, over its template layer of D-0282, checked by the same rules (keys the catalogs
|
|
60
|
+
* under them have, the same placeholders, the bounds). The page bundles change for the next
|
|
61
|
+
* page; a refused set changes nothing. `undefined` removes the bundle's words.
|
|
62
|
+
*/
|
|
63
|
+
templateBundle(app: string, template: string, directory: string | undefined): Promise<TemplateBundleWords>;
|
|
47
64
|
}
|
|
65
|
+
/** What {@link I18nApi.templateBundle} answers. */
|
|
66
|
+
export type TemplateBundleWords = {
|
|
67
|
+
readonly ok: true;
|
|
68
|
+
readonly keys: number;
|
|
69
|
+
readonly locales: readonly string[];
|
|
70
|
+
} | {
|
|
71
|
+
readonly ok: false;
|
|
72
|
+
readonly reason: string;
|
|
73
|
+
};
|
package/dist/layers.d.ts
CHANGED
|
@@ -5,6 +5,16 @@ export interface Layer<T> {
|
|
|
5
5
|
readonly name: string;
|
|
6
6
|
readonly catalogs: ReadonlyMap<string, ReadonlyMap<string, T>>;
|
|
7
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* D-0282: the screen words of a domain template: an app's or a module's
|
|
10
|
+
* `views/<template>/locales/`. They replace keys in the page bundles of that template's domains.
|
|
11
|
+
*/
|
|
12
|
+
export interface TemplateLayer<T> {
|
|
13
|
+
readonly kind: 'app' | 'module';
|
|
14
|
+
readonly name: string;
|
|
15
|
+
readonly template: string;
|
|
16
|
+
readonly catalogs: ReadonlyMap<string, ReadonlyMap<string, T>>;
|
|
17
|
+
}
|
|
8
18
|
/** The base scope (no app). */
|
|
9
19
|
export declare const BASE = "";
|
|
10
20
|
/** The raw messages of an entry: the shared layer, the module layers and the app layers. */
|
|
@@ -12,6 +22,7 @@ export declare function entryLayers(entry: Record<string, unknown>): {
|
|
|
12
22
|
shared: Layer<Message>;
|
|
13
23
|
modules: Layer<Message>[];
|
|
14
24
|
apps: Layer<Message>[];
|
|
25
|
+
templates: TemplateLayer<Message>[];
|
|
15
26
|
};
|
|
16
27
|
/** Every locale any layer has. */
|
|
17
28
|
export declare function localesOf<T>(layers: Iterable<Layer<T>>): string[];
|
package/dist/layers.js
CHANGED
|
@@ -3,6 +3,7 @@ import { canonical, record } from './values.js';
|
|
|
3
3
|
/** The base scope (no app). */
|
|
4
4
|
export const BASE = '';
|
|
5
5
|
const LAYER_NAME = /^[a-z][a-z0-9-]{0,63}$/;
|
|
6
|
+
const TEMPLATE_NAME = /^[a-z][a-z0-9-]{0,31}$/;
|
|
6
7
|
/** The raw messages of an entry: the shared layer, the module layers and the app layers. */
|
|
7
8
|
export function entryLayers(entry) {
|
|
8
9
|
const catalogsOf = (input) => {
|
|
@@ -19,8 +20,8 @@ export function entryLayers(entry) {
|
|
|
19
20
|
return out;
|
|
20
21
|
};
|
|
21
22
|
const layers = entry.layers === undefined ? {} : record(entry.layers);
|
|
22
|
-
if (Object.keys(layers).some((key) => key !== 'apps' && key !== 'modules'))
|
|
23
|
-
throw new I18nError('INVALID_CATALOG', 'layers holds apps and
|
|
23
|
+
if (Object.keys(layers).some((key) => key !== 'apps' && key !== 'modules' && key !== 'templates'))
|
|
24
|
+
throw new I18nError('INVALID_CATALOG', 'layers holds apps, modules and templates (loadLayers)');
|
|
24
25
|
const named = (kind, input) => Object.entries(input === undefined ? {} : record(input))
|
|
25
26
|
.map(([name, catalogs]) => {
|
|
26
27
|
if (!LAYER_NAME.test(name))
|
|
@@ -28,10 +29,35 @@ export function entryLayers(entry) {
|
|
|
28
29
|
return { kind, name, catalogs: catalogsOf(catalogs) };
|
|
29
30
|
})
|
|
30
31
|
.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
32
|
+
// D-0282: { apps: { <app>: { <template>: catalogs } }, modules: { … } }, apps first, by name.
|
|
33
|
+
const templatesIn = layers.templates === undefined ? {} : record(layers.templates);
|
|
34
|
+
if (Object.keys(templatesIn).some((key) => key !== 'apps' && key !== 'modules'))
|
|
35
|
+
throw new I18nError('INVALID_CATALOG', 'layers.templates holds apps and modules (loadLayers)');
|
|
36
|
+
const templates = [];
|
|
37
|
+
for (const kind of ['app', 'module']) {
|
|
38
|
+
const owners = templatesIn[kind === 'app' ? 'apps' : 'modules'];
|
|
39
|
+
for (const [name, value] of Object.entries(owners === undefined ? {} : record(owners)).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) {
|
|
40
|
+
if (!LAYER_NAME.test(name))
|
|
41
|
+
throw new I18nError('INVALID_CATALOG', `The ${kind} of a template layer ${JSON.stringify(name)} is not a ${kind} name`);
|
|
42
|
+
const owner = { name };
|
|
43
|
+
const byTemplate = record(value);
|
|
44
|
+
for (const template of Object.keys(byTemplate).sort()) {
|
|
45
|
+
if (!TEMPLATE_NAME.test(template))
|
|
46
|
+
throw new I18nError('INVALID_CATALOG', `The template ${JSON.stringify(template)} of the ${kind} ${owner.name} is not a template name`);
|
|
47
|
+
templates.push({
|
|
48
|
+
kind,
|
|
49
|
+
name: owner.name,
|
|
50
|
+
template,
|
|
51
|
+
catalogs: catalogsOf(byTemplate[template]),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
31
56
|
return {
|
|
32
57
|
shared: { kind: 'shared', name: 'shared', catalogs: catalogsOf(entry.catalogs) },
|
|
33
58
|
modules: named('module', layers.modules),
|
|
34
59
|
apps: named('app', layers.apps),
|
|
60
|
+
templates,
|
|
35
61
|
};
|
|
36
62
|
}
|
|
37
63
|
/** Every locale any layer has. */
|
package/dist/page.d.ts
CHANGED
|
@@ -73,6 +73,8 @@ export declare class PageMessages {
|
|
|
73
73
|
#private;
|
|
74
74
|
/** Changes with the time zone (D-0178): the browser then takes the new settings. */
|
|
75
75
|
version: string;
|
|
76
|
+
/** D-0282: whether any template layer exists (the plugin then asks a request's template). */
|
|
77
|
+
readonly templates: boolean;
|
|
76
78
|
constructor(options: I18nOptions, input: unknown, logger: Logger, apps?: ReadonlyMap<string, AppLocales>);
|
|
77
79
|
/**
|
|
78
80
|
* D-0178: the application's time zone changed. The settings carry it and the version changes,
|
|
@@ -95,5 +97,7 @@ export declare class PageMessages {
|
|
|
95
97
|
* D-0195: the first HTML of a public page, the same for every visitor: the claim cookie is
|
|
96
98
|
* not read, so every bundle is sent.
|
|
97
99
|
*/
|
|
98
|
-
publicPage?: boolean
|
|
100
|
+
publicPage?: boolean,
|
|
101
|
+
/** D-0282: the domain template of the request (@dokebi/app), whose words replace keys. */
|
|
102
|
+
template?: string): PageI18nPayload;
|
|
99
103
|
}
|
package/dist/page.js
CHANGED
|
@@ -84,6 +84,13 @@ export class PageMessages {
|
|
|
84
84
|
#catalogHash;
|
|
85
85
|
/** D-0181: the scopes with bundles (BASE and the apps with a layer); the maps below are by slot. */
|
|
86
86
|
#scopes;
|
|
87
|
+
/**
|
|
88
|
+
* D-0282: the template scopes (`<app or BASE>\u0001<template>`): a scope with the template
|
|
89
|
+
* layers of that template over it. Only the namespaces they touch have bundles of their own.
|
|
90
|
+
*/
|
|
91
|
+
#templateScopes = new Set();
|
|
92
|
+
/** D-0282: whether any template layer exists (the plugin then asks a request's template). */
|
|
93
|
+
templates;
|
|
87
94
|
#chains = new Map();
|
|
88
95
|
/** locale → namespace → { chain locale → { key → message } } (each key under its source). */
|
|
89
96
|
#fragments = new Map();
|
|
@@ -239,6 +246,56 @@ export class PageMessages {
|
|
|
239
246
|
}
|
|
240
247
|
store(at, fragments, tokens, this.#baseNamespaces.some((namespace) => touched.has(namespace)) ? undefined : base);
|
|
241
248
|
}
|
|
249
|
+
// D-0282: per template, a scope for each app scope (BASE, the apps with a layer, the apps
|
|
250
|
+
// with that template's own words): the app's template layer, then the modules' template
|
|
251
|
+
// layers (name order), then the scope it sits on. Only the namespaces they touch differ.
|
|
252
|
+
const appSplitOf = new Map(appSplits);
|
|
253
|
+
const templateNames = [...new Set(layers.templates.map((layer) => layer.template))].sort();
|
|
254
|
+
for (const template of templateNames) {
|
|
255
|
+
const moduleSplits = layers.templates
|
|
256
|
+
.filter((layer) => layer.kind === 'module' && layer.template === template)
|
|
257
|
+
.map((layer) => byNamespace(layer.catalogs));
|
|
258
|
+
const appTemplateSplits = new Map(layers.templates
|
|
259
|
+
.filter((layer) => layer.kind === 'app' && layer.template === template)
|
|
260
|
+
.map((layer) => [layer.name, byNamespace(layer.catalogs)]));
|
|
261
|
+
const owners = new Set([BASE, ...appSplitOf.keys(), ...appTemplateSplits.keys()]);
|
|
262
|
+
for (const owner of owners) {
|
|
263
|
+
const own = appTemplateSplits.get(owner);
|
|
264
|
+
const over = [...(own ? [own] : []), ...moduleSplits];
|
|
265
|
+
if (!over.length)
|
|
266
|
+
continue;
|
|
267
|
+
const under = appSplitOf.get(owner);
|
|
268
|
+
const scope = templateScope(owner, template);
|
|
269
|
+
this.#templateScopes.add(scope);
|
|
270
|
+
for (const locale of baseCatalogs.keys()) {
|
|
271
|
+
const below = slot(under ? owner : BASE, locale);
|
|
272
|
+
const at = slot(scope, locale);
|
|
273
|
+
const chain = this.#chains.get(below);
|
|
274
|
+
this.#chains.set(at, chain);
|
|
275
|
+
const touched = new Set();
|
|
276
|
+
for (const source of chain)
|
|
277
|
+
for (const split of over)
|
|
278
|
+
for (const namespace of split.get(source)?.keys() ?? [])
|
|
279
|
+
touched.add(namespace);
|
|
280
|
+
const belowFragments = this.#fragments.get(below);
|
|
281
|
+
const belowTokens = this.#tokens.get(below);
|
|
282
|
+
if (!touched.size) {
|
|
283
|
+
store(at, belowFragments, belowTokens, below);
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
const fragments = new Map(belowFragments);
|
|
287
|
+
const tokens = new Map(belowTokens);
|
|
288
|
+
const splits = [...over, ...(under ? [under] : []), baseSplit];
|
|
289
|
+
for (const namespace of touched) {
|
|
290
|
+
const fragment = fragmentOf(chain, namespace, splits);
|
|
291
|
+
fragments.set(namespace, fragment);
|
|
292
|
+
tokens.set(namespace, tokenOf(locale, namespace, fragment));
|
|
293
|
+
}
|
|
294
|
+
store(at, fragments, tokens, this.#baseNamespaces.some((namespace) => touched.has(namespace)) ? undefined : below);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
this.templates = this.#templateScopes.size > 0;
|
|
242
299
|
// A token two bundles share (48 bits: not expected in practice) is never honored in a claim.
|
|
243
300
|
const byTokens = new Map();
|
|
244
301
|
for (const [at, tokens] of this.#tokens) {
|
|
@@ -291,11 +348,25 @@ export class PageMessages {
|
|
|
291
348
|
.map(([locale, messages]) => [locale, [...messages].sort(([a], [b]) => (a < b ? -1 : 1))]);
|
|
292
349
|
// Without layers the version is what it was (one catalog); with them the base scope and
|
|
293
350
|
// each app's own layer (which, over the base, is that app's scope).
|
|
294
|
-
|
|
351
|
+
// D-0282: template layers join the version only when there are some (it stays otherwise).
|
|
352
|
+
const canonicalJson = JSON.stringify(!layers.apps.length && !layers.modules.length && !layers.templates.length
|
|
295
353
|
? sortedCatalogs(baseCatalogs)
|
|
296
354
|
: [
|
|
297
355
|
[BASE, sortedCatalogs(baseCatalogs)],
|
|
298
356
|
...layers.apps.map((app) => [app.name, sortedCatalogs(app.catalogs)]),
|
|
357
|
+
...(layers.templates.length
|
|
358
|
+
? [
|
|
359
|
+
[
|
|
360
|
+
'templates',
|
|
361
|
+
layers.templates.map((layer) => [
|
|
362
|
+
layer.kind,
|
|
363
|
+
layer.name,
|
|
364
|
+
layer.template,
|
|
365
|
+
sortedCatalogs(layer.catalogs),
|
|
366
|
+
]),
|
|
367
|
+
],
|
|
368
|
+
]
|
|
369
|
+
: []),
|
|
299
370
|
]);
|
|
300
371
|
this.#catalogHash = createHash('sha256').update(canonicalJson);
|
|
301
372
|
this.version = this.#versionOf(this.#settings);
|
|
@@ -387,8 +458,20 @@ export class PageMessages {
|
|
|
387
458
|
* D-0195: the first HTML of a public page, the same for every visitor: the claim cookie is
|
|
388
459
|
* not read, so every bundle is sent.
|
|
389
460
|
*/
|
|
390
|
-
publicPage = false
|
|
391
|
-
|
|
461
|
+
publicPage = false,
|
|
462
|
+
/** D-0282: the domain template of the request (@dokebi/app), whose words replace keys. */
|
|
463
|
+
template) {
|
|
464
|
+
const owner = app !== undefined && this.#scopes.has(app) ? app : BASE;
|
|
465
|
+
let scope = owner;
|
|
466
|
+
if (template !== undefined && this.#templateScopes.size) {
|
|
467
|
+
// The app's own template words (it may have no layer of its own), else its scope's.
|
|
468
|
+
const own = app !== undefined ? templateScope(app, template) : undefined;
|
|
469
|
+
if (own !== undefined && this.#templateScopes.has(own))
|
|
470
|
+
scope = own;
|
|
471
|
+
else if (this.#templateScopes.has(templateScope(owner, template)))
|
|
472
|
+
scope = templateScope(owner, template);
|
|
473
|
+
}
|
|
474
|
+
const at = slot(scope, locale);
|
|
392
475
|
const fragments = this.#fragments.get(at);
|
|
393
476
|
const chain = this.#chains.get(at);
|
|
394
477
|
const tokens = this.#tokens.get(at);
|
|
@@ -572,6 +655,8 @@ export class PageMessages {
|
|
|
572
655
|
/** The key of one scope's locale in the maps of PageMessages. */
|
|
573
656
|
const SLOT = '\u0000';
|
|
574
657
|
const slot = (scope, locale) => `${scope}${SLOT}${locale}`;
|
|
658
|
+
/** D-0282: the scope of an app scope (or BASE) under one domain template. */
|
|
659
|
+
const templateScope = (owner, template) => `${owner}\u0001${template}`;
|
|
575
660
|
function pick(tokens, namespaces) {
|
|
576
661
|
const result = object();
|
|
577
662
|
for (const name of namespaces) {
|
package/dist/plugin.d.ts
CHANGED
|
@@ -5,6 +5,11 @@ import type { I18nApi } from './contract.js';
|
|
|
5
5
|
import { type I18nOptions } from './options.js';
|
|
6
6
|
export declare const web: import("@dokebi/core").PluginDependency<WebApi>;
|
|
7
7
|
export declare const localeData: import("@dokebi/core").ContributionPoint<WebViewData>;
|
|
8
|
+
/** The `core:describe` point (core 0.4.2), declared by name. */
|
|
9
|
+
export declare const describePoint: import("@dokebi/core").ContributionPoint<{
|
|
10
|
+
readonly section: string;
|
|
11
|
+
describe(): unknown;
|
|
12
|
+
}>;
|
|
8
13
|
export declare class I18nRuntime implements PluginRuntime<I18nApi> {
|
|
9
14
|
#private;
|
|
10
15
|
constructor(options: I18nOptions, entry: unknown);
|
package/dist/plugin.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { definePlugin, definePoint, optionalDependency, } from '@dokebi/core';
|
|
1
|
+
import { ConsoleLogger, definePlugin, definePoint, optionalDependency, } from '@dokebi/core';
|
|
2
2
|
import { diagnostics } from '@dokebi/core/libraries';
|
|
3
3
|
import { appLocales } from './app-locales.js';
|
|
4
|
+
import { loadCatalogs } from './catalogs.js';
|
|
4
5
|
import { I18nError } from './errors.js';
|
|
5
6
|
import { translationHttp } from './http.js';
|
|
6
7
|
import { options } from './options.js';
|
|
@@ -9,12 +10,16 @@ import { I18n, negotiationLimits } from './service.js';
|
|
|
9
10
|
// Optional by name: server-only translators do not import or install the web runtime.
|
|
10
11
|
export const web = optionalDependency({ name: 'dokebi-web', version: '^0.4.3' });
|
|
11
12
|
export const localeData = definePoint('dokebi-web:view-data');
|
|
13
|
+
/** The `core:describe` point (core 0.4.2), declared by name. */
|
|
14
|
+
export const describePoint = definePoint('core:describe');
|
|
12
15
|
export class I18nRuntime {
|
|
13
16
|
#options;
|
|
14
17
|
#entry;
|
|
15
18
|
#web;
|
|
16
19
|
#apps = [];
|
|
17
20
|
#pageApps = [];
|
|
21
|
+
/** D-0282: the template layers whose words the page bundles use (unused without templateOf). */
|
|
22
|
+
#templates = [];
|
|
18
23
|
constructor(options, entry) {
|
|
19
24
|
this.#options = options;
|
|
20
25
|
this.#entry = entry;
|
|
@@ -24,6 +29,16 @@ export class I18nRuntime {
|
|
|
24
29
|
try {
|
|
25
30
|
const api = new I18n(this.#options, this.#entry, context.logger);
|
|
26
31
|
this.#apps = api.apps;
|
|
32
|
+
// D-0282: what `dokebi describe` shows of the layers: the template layers and their keys.
|
|
33
|
+
context.contribute(describePoint, {
|
|
34
|
+
section: 'i18n',
|
|
35
|
+
describe: () => ({
|
|
36
|
+
locales: api.locales,
|
|
37
|
+
defaultLocale: api.defaultLocale,
|
|
38
|
+
apps: api.apps,
|
|
39
|
+
templates: api.templates,
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
27
42
|
if (this.#options.http.enabled) {
|
|
28
43
|
const server = context.getOptionalDependency(web);
|
|
29
44
|
if (!server)
|
|
@@ -32,7 +47,8 @@ export class I18nRuntime {
|
|
|
32
47
|
// same catalogs. An empty pageMessages list keeps the prop: routes can add namespaces.
|
|
33
48
|
// D-0241: apps that offer only some locales (checked against the catalogs' locales).
|
|
34
49
|
const offered = appLocales(this.#options, api.locales, api.defaultLocale);
|
|
35
|
-
|
|
50
|
+
// D-0291: replaced whole when a template bundle's words change (templateBundle).
|
|
51
|
+
let page = new PageMessages(this.#options, this.#entry, context.logger, offered);
|
|
36
52
|
// D-0181: with app layers each request translates in its app's scope; web knows the app
|
|
37
53
|
// (dokebi-app registers who it is), asked per request, so no dependency on dokebi-app.
|
|
38
54
|
const appOf = api.apps.length || offered.size
|
|
@@ -40,9 +56,22 @@ export class I18nRuntime {
|
|
|
40
56
|
: undefined;
|
|
41
57
|
// D-0196: apps with their own page messages need the app of a page too, layers or not.
|
|
42
58
|
this.#pageApps = Object.keys(this.#options.http.apps);
|
|
59
|
+
// D-0291: template words (D-0282 layers, bundle words added while the app runs) are found
|
|
60
|
+
// by the page's app too: asked whenever template layers exist.
|
|
43
61
|
const pageAppOf = appOf ??
|
|
44
|
-
(this.#pageApps.length
|
|
62
|
+
(this.#pageApps.length
|
|
63
|
+
? (request) => server.appOf(request)
|
|
64
|
+
: (request) => (page.templates ? server.appOf(request) : undefined));
|
|
45
65
|
this.#web = server;
|
|
66
|
+
// D-0282: the template of a page's domain (dokebi-app tells web), asked only when a
|
|
67
|
+
// template layer exists; web before 0.4.25 has no templateOf (the layers then go unused).
|
|
68
|
+
const askTemplate = server.templateOf;
|
|
69
|
+
// Read per request: bundle words (D-0291) may add template scopes while the app runs.
|
|
70
|
+
const templateOf = typeof askTemplate === 'function'
|
|
71
|
+
? (request) => (page.templates ? askTemplate(request) : undefined)
|
|
72
|
+
: undefined;
|
|
73
|
+
if (page.templates)
|
|
74
|
+
this.#templates = api.templates.map((layer) => `${layer.kind}:${layer.name}@${layer.template}`);
|
|
46
75
|
// D-0178: a time zone set at run time reaches the page settings and their version.
|
|
47
76
|
api.onTimeZone((zone) => page.setTimeZone(zone));
|
|
48
77
|
server.register(translationHttp(api, this.#options.http.cookieName, context.logger, {
|
|
@@ -65,9 +94,73 @@ export class I18nRuntime {
|
|
|
65
94
|
// D-0126: a sealed answer's browser keeps no bundles (the page leaves through the seal).
|
|
66
95
|
// D-0195: a public page's first HTML sends every bundle (the claim cookie of this
|
|
67
96
|
// browser is not read), so one HTML serves every visitor of the URL and language.
|
|
68
|
-
value: (request, view) => page.value(request, request.i18n?.locale ?? api.defaultLocale, server.sealOf(request) !== undefined, pageAppOf?.(request), view?.public === true),
|
|
97
|
+
value: (request, view) => page.value(request, request.i18n?.locale ?? api.defaultLocale, server.sealOf(request) !== undefined, pageAppOf?.(request), view?.public === true, templateOf?.(request)),
|
|
69
98
|
});
|
|
70
99
|
context.defer(() => page.close());
|
|
100
|
+
// D-0291: template bundle words, by `<app>\u0001<template>`, over the entry's layers.
|
|
101
|
+
let words = new Map();
|
|
102
|
+
let queue = Promise.resolve();
|
|
103
|
+
const quiet = new ConsoleLogger({ level: 'silent' }, { plugin: 'dokebi-i18n' });
|
|
104
|
+
const apply = async (app, template, directory) => {
|
|
105
|
+
const key = `${app}\u0001${template}`;
|
|
106
|
+
try {
|
|
107
|
+
const next = new Map(words);
|
|
108
|
+
if (directory === undefined) {
|
|
109
|
+
if (!next.delete(key))
|
|
110
|
+
return { ok: true, keys: 0, locales: [] };
|
|
111
|
+
}
|
|
112
|
+
else
|
|
113
|
+
next.set(key, await loadCatalogs(directory, {
|
|
114
|
+
maxMessages: this.#options.maxMessages,
|
|
115
|
+
maxLocales: this.#options.maxLocales,
|
|
116
|
+
maxBytes: this.#options.maxCatalogBytes,
|
|
117
|
+
maxKeyLength: this.#options.maxKeyLength,
|
|
118
|
+
}));
|
|
119
|
+
const entry = withBundleWords(this.#entry, next);
|
|
120
|
+
// The same checks as the start (keys under them, placeholders, bounds): it throws.
|
|
121
|
+
new I18n(this.#options, entry, quiet);
|
|
122
|
+
const built = new PageMessages(this.#options, entry, context.logger, offered);
|
|
123
|
+
built.setTimeZone(api.timeZone);
|
|
124
|
+
const old = page;
|
|
125
|
+
page = built;
|
|
126
|
+
words = next;
|
|
127
|
+
old.close();
|
|
128
|
+
const set = next.get(key);
|
|
129
|
+
const locales = set ? Object.keys(set) : [];
|
|
130
|
+
let keys = 0;
|
|
131
|
+
for (const own of Object.values(set ?? {}))
|
|
132
|
+
keys += Object.keys(own).length;
|
|
133
|
+
context.logger.log('info', 'Template bundle words applied to the page bundles', {
|
|
134
|
+
code: 'I18N_TEMPLATE_BUNDLE_WORDS',
|
|
135
|
+
app,
|
|
136
|
+
template,
|
|
137
|
+
keys,
|
|
138
|
+
locales,
|
|
139
|
+
removed: directory === undefined,
|
|
140
|
+
});
|
|
141
|
+
return { ok: true, keys, locales };
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
context.logger.log('debug', 'Template bundle words refused', {
|
|
145
|
+
app,
|
|
146
|
+
template,
|
|
147
|
+
error: diagnostics.describeError(error),
|
|
148
|
+
});
|
|
149
|
+
return { ok: false, reason: error.message };
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
api.useTemplateBundles((app, template, directory) => {
|
|
153
|
+
// One at a time: each starts from the words the one before left.
|
|
154
|
+
const run = queue.then(() => apply(app, template, directory));
|
|
155
|
+
// apply answers every failure itself; anything else is said here, and the next goes on.
|
|
156
|
+
queue = run.catch((error) => context.logger.log('error', 'Template bundle words failed', {
|
|
157
|
+
code: 'I18N_TEMPLATE_BUNDLE_WORDS_FAILED',
|
|
158
|
+
app,
|
|
159
|
+
template,
|
|
160
|
+
error: diagnostics.describeError(error),
|
|
161
|
+
}));
|
|
162
|
+
return run;
|
|
163
|
+
});
|
|
71
164
|
context.logger.log('debug', 'Translation HTTP integration registered', {
|
|
72
165
|
code: 'I18N_HTTP_READY',
|
|
73
166
|
});
|
|
@@ -90,6 +183,14 @@ export class I18nRuntime {
|
|
|
90
183
|
* tells a request's app are unused.
|
|
91
184
|
*/
|
|
92
185
|
start(context) {
|
|
186
|
+
if (this.#templates.length &&
|
|
187
|
+
this.#web &&
|
|
188
|
+
(!this.#web.templateOf || !this.#web.resolvesApps))
|
|
189
|
+
context.logger.log('warn', 'Translation template layers are not used: dokebi-web tells no template', {
|
|
190
|
+
code: 'I18N_TEMPLATE_LAYERS_UNUSED',
|
|
191
|
+
templates: this.#templates,
|
|
192
|
+
fix: 'Upgrade @dokebi/web to 0.4.25 or later and @dokebi/app to 0.4.18 or later (it tells web the template of each host)',
|
|
193
|
+
});
|
|
93
194
|
if (this.#apps.length && this.#web && !this.#web.resolvesApps)
|
|
94
195
|
context.logger.log('warn', 'Translation app layers are not used: nothing tells the app of a request', {
|
|
95
196
|
code: 'I18N_APP_LAYERS_UNUSED',
|
|
@@ -106,11 +207,35 @@ export class I18nRuntime {
|
|
|
106
207
|
}
|
|
107
208
|
export const createPlugin = definePlugin({
|
|
108
209
|
name: 'dokebi-i18n',
|
|
109
|
-
version: '0.2.
|
|
210
|
+
version: '0.2.10',
|
|
110
211
|
core: '^0.4.0',
|
|
111
212
|
options,
|
|
112
|
-
contributes: [localeData],
|
|
213
|
+
contributes: [localeData, describePoint],
|
|
113
214
|
dependencies: ({ options }) => (options.http.enabled ? [web] : []),
|
|
114
215
|
entry: (input) => input,
|
|
115
216
|
instance: ({ options, entry }) => new I18nRuntime(options, entry),
|
|
116
217
|
});
|
|
218
|
+
/**
|
|
219
|
+
* D-0291: the entry with template bundle words over its template layers: each bundle's words go
|
|
220
|
+
* over the app's own template layer of that template (D-0282), key by key per locale.
|
|
221
|
+
*/
|
|
222
|
+
function withBundleWords(entry, words) {
|
|
223
|
+
if (!words.size)
|
|
224
|
+
return entry;
|
|
225
|
+
const base = (entry ?? {});
|
|
226
|
+
const layers = (base.layers ?? {});
|
|
227
|
+
const templates = (layers.templates ?? {});
|
|
228
|
+
const apps = { ...(templates.apps ?? {}) };
|
|
229
|
+
for (const [key, catalogs] of words) {
|
|
230
|
+
const [app, template] = key.split('\u0001');
|
|
231
|
+
const own = apps[app]?.[template] ?? {};
|
|
232
|
+
const merged = {};
|
|
233
|
+
for (const locale of new Set([...Object.keys(own), ...Object.keys(catalogs)]))
|
|
234
|
+
merged[locale] = { ...(own[locale] ?? {}), ...(catalogs[locale] ?? {}) };
|
|
235
|
+
apps[app] = { ...(apps[app] ?? {}), [template]: merged };
|
|
236
|
+
}
|
|
237
|
+
return {
|
|
238
|
+
...base,
|
|
239
|
+
layers: { ...layers, templates: { apps, modules: templates.modules ?? {} } },
|
|
240
|
+
};
|
|
241
|
+
}
|
package/dist/service.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Logger } from '@dokebi/core';
|
|
2
|
-
import type { I18nApi, Translator } from './contract.js';
|
|
2
|
+
import type { I18nApi, TemplateBundleWords, Translator } from './contract.js';
|
|
3
3
|
import { type NegotiationLimits } from './negotiation.js';
|
|
4
4
|
import { type I18nOptions } from './options.js';
|
|
5
5
|
export declare const NUMBER_OPTIONS: Set<string>;
|
|
@@ -15,6 +15,16 @@ export declare class I18n implements I18nApi {
|
|
|
15
15
|
readonly defaultLocale: string;
|
|
16
16
|
/** D-0181: the apps with a layer of their own. */
|
|
17
17
|
readonly apps: readonly string[];
|
|
18
|
+
/**
|
|
19
|
+
* D-0282: the template layers (the screen words of a domain template) and how many keys each
|
|
20
|
+
* replaces; checked here at start, used by the page bundles only.
|
|
21
|
+
*/
|
|
22
|
+
readonly templates: readonly {
|
|
23
|
+
readonly kind: 'app' | 'module';
|
|
24
|
+
readonly name: string;
|
|
25
|
+
readonly template: string;
|
|
26
|
+
readonly keys: number;
|
|
27
|
+
}[];
|
|
18
28
|
constructor(options: I18nOptions, input: unknown, logger: Logger);
|
|
19
29
|
forLocale(locale?: string, options?: {
|
|
20
30
|
readonly app?: string;
|
|
@@ -23,5 +33,8 @@ export declare class I18n implements I18nApi {
|
|
|
23
33
|
get timeZone(): string;
|
|
24
34
|
setTimeZone(zone: string): void;
|
|
25
35
|
/** The page messages follow the zone (their settings and version). */
|
|
36
|
+
/** D-0291: set by the plugin when the page bundles exist (http.enabled). */
|
|
37
|
+
useTemplateBundles(apply: (app: string, template: string, directory: string | undefined) => Promise<TemplateBundleWords>): void;
|
|
38
|
+
templateBundle(app: string, template: string, directory: string | undefined): Promise<TemplateBundleWords>;
|
|
26
39
|
onTimeZone(listener: (zone: string) => void): void;
|
|
27
40
|
}
|
package/dist/service.js
CHANGED
|
@@ -186,12 +186,19 @@ export class I18n {
|
|
|
186
186
|
defaultLocale;
|
|
187
187
|
/** D-0181: the apps with a layer of their own. */
|
|
188
188
|
apps;
|
|
189
|
+
/**
|
|
190
|
+
* D-0282: the template layers (the screen words of a domain template) and how many keys each
|
|
191
|
+
* replaces; checked here at start, used by the page bundles only.
|
|
192
|
+
*/
|
|
193
|
+
templates;
|
|
189
194
|
/** Translators by scope (BASE, or an app with a layer), then by locale. */
|
|
190
195
|
#translators = new Map();
|
|
191
196
|
#negotiator;
|
|
192
197
|
#logger;
|
|
193
198
|
#zone;
|
|
194
199
|
#zoneListeners = [];
|
|
200
|
+
/** D-0291: what applies a template bundle's words to the page bundles (the HTTP integration). */
|
|
201
|
+
#bundleWords;
|
|
195
202
|
constructor(options, input, logger) {
|
|
196
203
|
this.#logger = logger;
|
|
197
204
|
this.#zone = { current: zoneOf(options.timeZone) };
|
|
@@ -219,6 +226,7 @@ export class I18n {
|
|
|
219
226
|
: `Catalog files exceed ${options.maxCatalogBytes} bytes (maxCatalogBytes)`);
|
|
220
227
|
// D-0181: the shared catalogs, the module layers and the app layers, each compiled once.
|
|
221
228
|
const raw = entryLayers(entry);
|
|
229
|
+
// D-0282: template layers add no locale (a locale only they have fails their check below).
|
|
222
230
|
const all = [raw.shared, ...raw.modules, ...raw.apps];
|
|
223
231
|
const locales = localesOf(all);
|
|
224
232
|
if (raw.shared.catalogs.size === 0 || locales.length > options.maxLocales)
|
|
@@ -261,6 +269,15 @@ export class I18n {
|
|
|
261
269
|
}
|
|
262
270
|
return { kind: layer.kind, name: layer.name, catalogs };
|
|
263
271
|
};
|
|
272
|
+
// D-0282: template layers count toward the same bounds; their words are checked below.
|
|
273
|
+
const templateLayers = raw.templates.map((layer) => ({
|
|
274
|
+
layer,
|
|
275
|
+
compiled: compile({
|
|
276
|
+
kind: layer.kind,
|
|
277
|
+
name: `${layer.name} (template ${layer.template})`,
|
|
278
|
+
catalogs: layer.catalogs,
|
|
279
|
+
}),
|
|
280
|
+
}));
|
|
264
281
|
const shared = compile(raw.shared);
|
|
265
282
|
const modules = raw.modules.map(compile);
|
|
266
283
|
const apps = raw.apps.map(compile);
|
|
@@ -292,6 +309,40 @@ export class I18n {
|
|
|
292
309
|
baseTranslators.set(locale, translator(locale, messages, own));
|
|
293
310
|
}
|
|
294
311
|
this.#translators.set(BASE, baseTranslators);
|
|
312
|
+
// D-0282: a template layer only replaces words. A key the catalogs under it do not have in
|
|
313
|
+
// that locale is a typo, and other placeholders would break the page: both fail the start.
|
|
314
|
+
const placeholders = (resolved) => {
|
|
315
|
+
const names = new Set();
|
|
316
|
+
for (const parts of resolved.message.branches.values())
|
|
317
|
+
for (const part of parts)
|
|
318
|
+
if (typeof part !== 'string')
|
|
319
|
+
names.add(part.name);
|
|
320
|
+
return [...names].sort();
|
|
321
|
+
};
|
|
322
|
+
for (const { layer, compiled } of templateLayers)
|
|
323
|
+
for (const [locale, entries] of compiled.catalogs)
|
|
324
|
+
for (const [key, resolved] of entries) {
|
|
325
|
+
const under = (layer.kind === 'app'
|
|
326
|
+
? apps
|
|
327
|
+
.find((app) => app.name === layer.name)
|
|
328
|
+
?.catalogs.get(locale)
|
|
329
|
+
?.get(key)
|
|
330
|
+
: apps.map((app) => app.catalogs.get(locale)?.get(key)).find(Boolean)) ??
|
|
331
|
+
base.get(locale)?.get(key);
|
|
332
|
+
const where = `${layer.kind === 'app' ? 'src/apps' : 'src/shared/modules'}/${layer.name}/views/${layer.template}/locales/${locale}`;
|
|
333
|
+
if (!under)
|
|
334
|
+
throw new I18nError('INVALID_CATALOG', `${where} replaces ${JSON.stringify(key)}, which the catalogs under it (${layer.kind === 'app' ? 'the app, the modules and the shared ones' : 'the modules, the apps and the shared ones'}) do not have in ${locale}: a template layer only replaces words`);
|
|
335
|
+
const own = placeholders(resolved);
|
|
336
|
+
const theirs = placeholders(under);
|
|
337
|
+
if (own.join(',') !== theirs.join(','))
|
|
338
|
+
throw new I18nError('INVALID_CATALOG', `${where} replaces ${JSON.stringify(key)} with other placeholders (${own.map((name) => `{${name}}`).join(' ') || 'none'} for ${theirs.map((name) => `{${name}}`).join(' ') || 'none'})`);
|
|
339
|
+
}
|
|
340
|
+
this.templates = Object.freeze(raw.templates.map((layer) => Object.freeze({
|
|
341
|
+
kind: layer.kind,
|
|
342
|
+
name: layer.name,
|
|
343
|
+
template: layer.template,
|
|
344
|
+
keys: [...layer.catalogs.values()].reduce((sum, entries) => sum + entries.size, 0),
|
|
345
|
+
})));
|
|
295
346
|
// An app scope is its layer over the base: only the keys the layer has are resolved again
|
|
296
347
|
// (per chain locale, the app's message before the base's: a locale still comes before a
|
|
297
348
|
// layer); every other key is the base's, so an app costs its own messages, not a copy.
|
|
@@ -322,6 +373,11 @@ export class I18n {
|
|
|
322
373
|
messages: messageCount,
|
|
323
374
|
modules: modules.length,
|
|
324
375
|
apps: this.apps,
|
|
376
|
+
...(this.templates.length
|
|
377
|
+
? {
|
|
378
|
+
templates: this.templates.map((layer) => `${layer.kind}:${layer.name}@${layer.template}=${layer.keys}`),
|
|
379
|
+
}
|
|
380
|
+
: {}),
|
|
325
381
|
});
|
|
326
382
|
Object.freeze(this);
|
|
327
383
|
}
|
|
@@ -363,6 +419,18 @@ export class I18n {
|
|
|
363
419
|
});
|
|
364
420
|
}
|
|
365
421
|
/** The page messages follow the zone (their settings and version). */
|
|
422
|
+
/** D-0291: set by the plugin when the page bundles exist (http.enabled). */
|
|
423
|
+
useTemplateBundles(apply) {
|
|
424
|
+
this.#bundleWords = apply;
|
|
425
|
+
}
|
|
426
|
+
templateBundle(app, template, directory) {
|
|
427
|
+
if (!this.#bundleWords)
|
|
428
|
+
return Promise.resolve({
|
|
429
|
+
ok: false,
|
|
430
|
+
reason: 'dokebi-i18n http is off: there are no page bundles for template words',
|
|
431
|
+
});
|
|
432
|
+
return this.#bundleWords(app, template, directory);
|
|
433
|
+
}
|
|
366
434
|
onTimeZone(listener) {
|
|
367
435
|
this.#zoneListeners.push(listener);
|
|
368
436
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dokebi/i18n",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.10",
|
|
4
4
|
"description": "Compiled translation catalogs, locale negotiation, cardinal plurals and named Intl formats.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dokebi",
|
|
@@ -66,6 +66,7 @@
|
|
|
66
66
|
}
|
|
67
67
|
],
|
|
68
68
|
"contributes": [
|
|
69
|
+
"core:describe",
|
|
69
70
|
"dokebi-web:view-data"
|
|
70
71
|
],
|
|
71
72
|
"installModule": "dist/install.js"
|
|
@@ -97,6 +98,6 @@
|
|
|
97
98
|
}
|
|
98
99
|
},
|
|
99
100
|
"devDependencies": {
|
|
100
|
-
"@dokebi/web": "0.4.
|
|
101
|
+
"@dokebi/web": "0.4.27"
|
|
101
102
|
}
|
|
102
103
|
}
|