@dokebi/i18n 0.2.9 → 0.2.11
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 +3 -1
- package/dist/contract.d.ts +17 -0
- package/dist/options.d.ts +1 -0
- package/dist/options.js +10 -2
- package/dist/page.d.ts +5 -0
- package/dist/page.js +112 -16
- package/dist/plugin.js +102 -6
- package/dist/service.d.ts +4 -1
- package/dist/service.js +14 -0
- package/options.schema.json +12 -1
- package/package.json +2 -2
package/AI.md
CHANGED
|
@@ -14,7 +14,7 @@ Private `dokebi.yaml`, under `options.dokebi-i18n`:
|
|
|
14
14
|
| `http.enabled` | boolean | false | Attach request translators and contribute page locale; requires web. |
|
|
15
15
|
| `http.cookieName` | string | dokebi-lang | Preference cookie name, 1–128 ASCII letters, digits, underscores or hyphens. |
|
|
16
16
|
| `http.pageMessages` | string[] | `['*']` | Namespaces every rendered page receives in the `i18n` prop (`*`: all); at most 64. Unknown names fail the start (`UNKNOWN_NAMESPACE`). |
|
|
17
|
-
| `http.apps` | object | `{}` | Per @dokebi/app app (the name of `domains.<host>.app`, at most 256): `{ <app>: { pageMessages?, locales?, defaultLocale? } }` (at least one). `pageMessages` replaces `http.pageMessages` on that app's pages (D-0196; unknown names fail the start, `UNKNOWN_NAMESPACE`); `locales` and `defaultLocale` are the locales that app offers and its answer when none matches (0.2.6, D-0241; outside the catalogs' locales, or a default outside the list, fail the start, `INVALID_OPTIONS`). |
|
|
17
|
+
| `http.apps` | object | `{}` | Per @dokebi/app app (the name of `domains.<host>.app`, at most 256): `{ <app>: { pageMessages?, pageExclude?, locales?, defaultLocale? } }` (at least one). `pageMessages` replaces `http.pageMessages` on that app's pages (D-0196; unknown names fail the start, `UNKNOWN_NAMESPACE`); `pageExclude` lists key prefixes (on a dot boundary) or whole namespaces that app's pages never receive (0.2.11, D-0312, §Vue pages; at most 4096; a prefix whose namespace no catalog has fails the start, `UNKNOWN_NAMESPACE`); `locales` and `defaultLocale` are the locales that app offers and its answer when none matches (0.2.6, D-0241; outside the catalogs' locales, or a default outside the list, fail the start, `INVALID_OPTIONS`). |
|
|
18
18
|
| `http.maxPageHeaderBytes` | integer | 4096 | Longest `X-Dokebi-I18n` visit header read, 256–16384; a longer one counts as holding nothing (D-0126). The browser runtime stays inside it. |
|
|
19
19
|
| `http.pathLocale` | boolean | false | A first path segment that is a configured locale (`/ja/p/1`, any case) decides the request locale; see §HTTP integration (URL locales). |
|
|
20
20
|
| `http.maxCookieHeaderBytes` | integer | 16384 | Longest Cookie request header read, 256–65536; a longer one counts as naming neither the preference cookie nor the page cache claim (the request goes on). |
|
|
@@ -82,6 +82,7 @@ export const layers = await loadLayers(new URL('../../', import.meta.url));
|
|
|
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
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.
|
|
85
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).
|
|
86
87
|
|
|
87
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`).
|
|
@@ -165,6 +166,7 @@ const { t, n, d, locale, switchLocale } = useI18n();
|
|
|
165
166
|
|
|
166
167
|
- **Which namespaces.** `http.pageMessages` (default `['*']`, every namespace) names what every page gets; a route adds more with `pageMessages(request, 'posts', 'editor')` before `render` (an unknown name fails that render with `UNKNOWN_NAMESPACE`). With `[]` pages get only what routes add (`locale`, `version` and `settings` still come).
|
|
167
168
|
- **Per app (D-0196).** Several apps on one server (a member site and an admin) share one catalog, so `['*']` sends the admin's wording in the member site's first HTML too, and a public page (D-0195) sends every bundle on every load. `http.apps.<app>.pageMessages` gives that app's pages their own list instead (`apps: { frontend: { pageMessages: [front, site, common] } }`); a route of the app still adds more with `pageMessages(request, …)`, and apps not listed get `http.pageMessages`. The app is @dokebi/web's `appOf(request)` (@dokebi/app answers it from the host); with `http.apps` and nothing that tells a request's app, the start warns `I18N_PAGE_APPS_UNUSED` and every page gets `http.pageMessages`. An app's list is joined like a route's table (`http.maxJoinedSets`).
|
|
169
|
+
- **Keys an app's pages never receive (0.2.11, [D-0312](https://git.nyx-zone.com/dokebi/framework/-/blob/main/docs/decisions/D-0312-i18n-page-exclude.md)).** A shared namespace often mixes the member site's words with staff-only ones (`game.title` next to `game.settings.*`), and renaming the staff keys breaks every page that uses them. `http.apps.<app>.pageExclude` leaves key prefixes out of every bundle that app's pages receive — its first HTML, its visits, its list and a route's `pageMessages(request, …)` alike, also on its template domains: `apps: { site: { pageMessages: [game, community, …], pageExclude: [game.settings, community.manage, logs] } }`. A prefix ends at a dot (`game.settings` covers `game.settings` and `game.settings.*`, not `game.settingsBar`); a bare namespace (`logs`) leaves that namespace's bundle empty. The filtered bundles have their own tokens (a browser that holds another app's bundle gets the app's again), and the app's settings name the prefixes (`settings.excluded`, sorted), so a page of that app that calls one gets `EXCLUDED_MESSAGE` from the browser runtime under `missing: error` — in SSR and in the browser — instead of `MISSING_MESSAGE` (with `missing: key` the key is shown and the runtime warns `I18N_EXCLUDED_KEY` once per prefix; @dokebi/template-inertia 0.4.31 or later). The server's `t()` (jobs, mail, other requests) and the other apps keep every key. A prefix that covers no key of any catalog warns `I18N_PAGE_EXCLUDE_UNMATCHED` (`app`, `prefixes`) at the start (a typo or a stale entry). Template words (`views/<template>/locales`, a template bundle's `locales/`) for a key the app leaves out are no error — the catalogs still have the key — and stay out of that app's pages while other apps on the template get them. Long lists are fine: a key costs one set lookup per dot in it, whatever the list's length, and the list rides in the app's settings (sent with the first HTML and when the version changes). Measured on 26,400 keys (22 namespaces, three locales, eight prefixes): the start builds the filtered bundles in about 8 ms more, choosing a page's prop costs the same, and the first HTML's messages shrank from 447 KB to 356 KB.
|
|
168
170
|
- **Locales per app (0.2.6, [D-0241](https://git.nyx-zone.com/dokebi/framework/-/blob/main/docs/decisions/D-0241-i18n-app-locales.md)).** A locale folder (`locales/en`) makes that locale every app's: an English browser gets English on every app, also on apps whose `en` catalogs are not written yet (half Korean, half English). `http.apps.<app>.locales` lists what that app offers (`apps: { admin: { locales: ['ko'] }, site: { locales: ['ko', 'en'] } }`): its negotiation — a path locale, the `dokebi-lang` cookie, `Accept-Language`, then the default — picks only from that list (a saved `en` cookie answers `ko` on the admin app), and the page's settings (`settings.locales`, the switcher's list, and `settings.defaultLocale`) are the app's, with their own catalog version. `defaultLocale` is that app's answer when nothing matches: unset, the plugin's `defaultLocale` when the app offers it, else the app's first locale. Apps not listed offer every locale. Ship `en` on the member site first and translate the staff apps later, or keep an app Korean-only for good.
|
|
169
171
|
- **Sent once per browser.** The runtime names what the document holds on every Inertia visit (request header `X-Dokebi-I18n: v=<version>; h=<token><token>…`, `PAGE_HEADER`, at most `http.maxPageHeaderBytes`); the prop then carries only bundles it lacks, and no `settings` while the version matches. The header is read **only on an Inertia visit** (`X-Inertia: true`): a full page request that carries it (any client can send it) gets the whole payload, so HTML that a cache keeps never lacks bundles. Tokens address content, so after a deploy a visit gets the settings again but only the bundles whose messages changed. Unknown tokens, another locale's tokens and a malformed or over-long header mean "nothing held" (never an error). Inertia answers name `X-Dokebi-I18n` in `Vary`.
|
|
170
172
|
- **Kept between page loads (D-0126, `http.pageCache`, on by default).** The runtime keeps the bundles it receives in localStorage (one item per bundle, bounded by `maxBundles` and `maxStoreCharacters`) and writes the cookie `dokebi-i18n-held` with their tokens (`maxCookieBytes`; 61 bundles take 488 bytes). On a full page load (no header) the server leaves the bundles the cookie names out of the HTML and lists them in `held`; the settings always come. Measured with 85 bundles (125 KB Korean, example 48): a home page that needs 60 is 93 KB of HTML (44 KB gzip) on the first load and 2.4 KB (1.4 KB gzip) on a reload; a visit is unchanged. The cookie is no proof: when the browser lacks a named bundle (storage cleared, the cookie changed, the quota full) the runtime fetches the missing messages (an Inertia partial reload of `i18n`) **before it mounts** the app, so no screen is drawn without them; SSR renders the left-out bundles from the server's own catalogs. A seal host never reads the cookie and tells the browser to keep nothing. Turning `pageCache.enabled` off makes the server ignore the cookie (it expires by itself). The cookie and the store are browser data like a cache: an app with a strict cookie policy can turn it off.
|
package/dist/contract.d.ts
CHANGED
|
@@ -53,4 +53,21 @@ export interface I18nApi {
|
|
|
53
53
|
* that is not a time zone. Call it in every worker (runtime-settings onChange).
|
|
54
54
|
*/
|
|
55
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>;
|
|
56
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/options.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export declare const options: import("@dokebi/core").OptionsDeclaration<{
|
|
|
5
5
|
readonly apps: Readonly<Record<string, {
|
|
6
6
|
readonly defaultLocale?: string;
|
|
7
7
|
readonly locales?: readonly string[];
|
|
8
|
+
readonly pageExclude?: readonly string[];
|
|
8
9
|
readonly pageMessages?: readonly string[];
|
|
9
10
|
}>>;
|
|
10
11
|
readonly cookieName: string;
|
package/dist/options.js
CHANGED
|
@@ -30,6 +30,14 @@ export const options = defineOptions({
|
|
|
30
30
|
maxItems: 64,
|
|
31
31
|
description: 'Namespaces every page of this app receives in place of http.pageMessages (D-0196); a route still adds more with pageMessages(request, ...). Unset: http.pageMessages.',
|
|
32
32
|
}),
|
|
33
|
+
pageExclude: o.array(o.string({
|
|
34
|
+
pattern: '^[a-zA-Z][a-zA-Z0-9_-]*(?:\\.[a-zA-Z][a-zA-Z0-9_-]*)*$',
|
|
35
|
+
maxLength: 256,
|
|
36
|
+
description: 'A key prefix on a dot boundary (game.settings leaves out game.settings and game.settings.*, not game.settingsX), or a whole namespace.',
|
|
37
|
+
}), {
|
|
38
|
+
maxItems: 4096,
|
|
39
|
+
description: "Keys this app's pages never receive (D-0312): left out of every bundle the app's first HTML and visits carry, its own and a route's pageMessages() alike. A page of the app that calls one gets EXCLUDED_MESSAGE (missing: error) instead of MISSING_MESSAGE. The server's t() (jobs, mail, other requests) and the other apps keep them. Unset: nothing left out.",
|
|
40
|
+
}),
|
|
33
41
|
locales: o.array(o.string({
|
|
34
42
|
minLength: 2,
|
|
35
43
|
maxLength: 35,
|
|
@@ -50,14 +58,14 @@ export const options = defineOptions({
|
|
|
50
58
|
refine: [
|
|
51
59
|
{
|
|
52
60
|
test: (value) => Object.values(value).some((field) => field !== undefined),
|
|
53
|
-
message: 'sets pageMessages, locales or defaultLocale',
|
|
61
|
+
message: 'sets pageMessages, pageExclude, locales or defaultLocale',
|
|
54
62
|
},
|
|
55
63
|
],
|
|
56
64
|
}), {
|
|
57
65
|
default: {},
|
|
58
66
|
keys: { pattern: '^[a-z][a-z0-9-]{0,31}$' },
|
|
59
67
|
maxProperties: 256,
|
|
60
|
-
description: 'Per @dokebi/app app (the app name of domains.<host>.app): its page messages (D-0196) and the locales it offers (D-0241). An app not listed gets http.pageMessages and every locale.',
|
|
68
|
+
description: 'Per @dokebi/app app (the app name of domains.<host>.app): its page messages (D-0196), the keys its pages never receive (D-0312) and the locales it offers (D-0241). An app not listed gets http.pageMessages and every locale.',
|
|
61
69
|
}),
|
|
62
70
|
maxPageHeaderBytes: o.integer({
|
|
63
71
|
default: 4096,
|
package/dist/page.d.ts
CHANGED
|
@@ -44,6 +44,11 @@ export interface PageI18nSettings {
|
|
|
44
44
|
/** Longest X-Dokebi-I18n header the server reads (http.maxPageHeaderBytes). */
|
|
45
45
|
readonly maxHeaderBytes: number;
|
|
46
46
|
readonly cache?: PageI18nCache;
|
|
47
|
+
/**
|
|
48
|
+
* D-0312: the key prefixes this app's pages never receive (http.apps.<app>.pageExclude), sorted.
|
|
49
|
+
* The runtime answers a call of one with EXCLUDED_MESSAGE, not MISSING_MESSAGE.
|
|
50
|
+
*/
|
|
51
|
+
readonly excluded?: readonly string[];
|
|
47
52
|
}
|
|
48
53
|
/** The `i18n` page prop (D-0099, D-0126). Field names are the protocol both packages document. */
|
|
49
54
|
export interface PageI18nPayload {
|
package/dist/page.js
CHANGED
|
@@ -124,6 +124,8 @@ export class PageMessages {
|
|
|
124
124
|
* their version: a browser on that app compares its claim with that version.
|
|
125
125
|
*/
|
|
126
126
|
#appSettings = new Map();
|
|
127
|
+
/** D-0312: app → the key prefixes its pages never receive (http.apps.<app>.pageExclude). */
|
|
128
|
+
#excluded = new Map();
|
|
127
129
|
constructor(options, input, logger, apps = new Map()) {
|
|
128
130
|
this.#logger = logger;
|
|
129
131
|
// The entry was validated by the provider (service.ts) already; this reads the same data.
|
|
@@ -159,6 +161,12 @@ export class PageMessages {
|
|
|
159
161
|
this.#maxJoined = options.http.maxJoinedSets;
|
|
160
162
|
this.#baseNamespaces = Object.freeze(configured.includes('*') ? [...namespaces].sort() : [...new Set(configured)]);
|
|
161
163
|
for (const [app, own] of Object.entries(options.http.apps)) {
|
|
164
|
+
if (own.pageExclude?.length) {
|
|
165
|
+
for (const prefix of own.pageExclude)
|
|
166
|
+
if (!namespaces.has(namespaceOf(prefix)))
|
|
167
|
+
throw new I18nError('UNKNOWN_NAMESPACE', `http.apps.${app}.pageExclude names ${JSON.stringify(prefix)}, whose namespace no catalog has`);
|
|
168
|
+
this.#excluded.set(app, Object.freeze([...new Set(own.pageExclude)].sort()));
|
|
169
|
+
}
|
|
162
170
|
// D-0241: an app entry may set only its locales; its page messages are then the base's.
|
|
163
171
|
if (own.pageMessages === undefined)
|
|
164
172
|
continue;
|
|
@@ -296,6 +304,53 @@ export class PageMessages {
|
|
|
296
304
|
}
|
|
297
305
|
}
|
|
298
306
|
this.templates = this.#templateScopes.size > 0;
|
|
307
|
+
// D-0312: for an app with pageExclude, each scope its requests resolve to (its own or the
|
|
308
|
+
// base's, and the template scopes over them) gets a twin without those keys. Only the
|
|
309
|
+
// namespaces the prefixes name are filtered and get new tokens; the others stay the scope's.
|
|
310
|
+
for (const [app, prefixes] of this.#excluded) {
|
|
311
|
+
const owner = this.#scopes.has(app) ? app : BASE;
|
|
312
|
+
const sources = new Set([owner]);
|
|
313
|
+
for (const scope of this.#templateScopes)
|
|
314
|
+
if (scope.startsWith(`${app}\u0001`) || scope.startsWith(`${owner}\u0001`))
|
|
315
|
+
sources.add(scope);
|
|
316
|
+
// A set, so a key costs one lookup per dot in it however long the list is (shop: 411).
|
|
317
|
+
const listed = new Set(prefixes);
|
|
318
|
+
const filtered = [...new Set(prefixes.map(namespaceOf))];
|
|
319
|
+
const matched = new Set();
|
|
320
|
+
for (const source of sources)
|
|
321
|
+
for (const locale of baseCatalogs.keys()) {
|
|
322
|
+
const from = slot(source, locale);
|
|
323
|
+
const at = slot(excludedScope(source, app), locale);
|
|
324
|
+
this.#chains.set(at, this.#chains.get(from));
|
|
325
|
+
const fromFragments = this.#fragments.get(from);
|
|
326
|
+
const fromTokens = this.#tokens.get(from);
|
|
327
|
+
const fragments = new Map(fromFragments);
|
|
328
|
+
const tokens = new Map(fromTokens);
|
|
329
|
+
const touched = new Set();
|
|
330
|
+
for (const namespace of filtered) {
|
|
331
|
+
const fragment = fromFragments.get(namespace);
|
|
332
|
+
if (!fragment)
|
|
333
|
+
continue;
|
|
334
|
+
const kept = without(fragment, listed, matched);
|
|
335
|
+
if (kept === fragment)
|
|
336
|
+
continue;
|
|
337
|
+
fragments.set(namespace, kept);
|
|
338
|
+
tokens.set(namespace, tokenOf(locale, namespace, kept));
|
|
339
|
+
touched.add(namespace);
|
|
340
|
+
}
|
|
341
|
+
if (!touched.size)
|
|
342
|
+
store(at, fromFragments, fromTokens, from);
|
|
343
|
+
else
|
|
344
|
+
store(at, fragments, tokens, this.#baseNamespaces.some((namespace) => touched.has(namespace)) ? undefined : from);
|
|
345
|
+
}
|
|
346
|
+
const unmatched = prefixes.filter((prefix) => !matched.has(prefix));
|
|
347
|
+
if (unmatched.length)
|
|
348
|
+
logger.log('warn', 'pageExclude names keys no catalog has', {
|
|
349
|
+
code: 'I18N_PAGE_EXCLUDE_UNMATCHED',
|
|
350
|
+
app,
|
|
351
|
+
prefixes: unmatched,
|
|
352
|
+
});
|
|
353
|
+
}
|
|
299
354
|
// A token two bundles share (48 bits: not expected in practice) is never honored in a claim.
|
|
300
355
|
const byTokens = new Map();
|
|
301
356
|
for (const [at, tokens] of this.#tokens) {
|
|
@@ -370,8 +425,8 @@ export class PageMessages {
|
|
|
370
425
|
]);
|
|
371
426
|
this.#catalogHash = createHash('sha256').update(canonicalJson);
|
|
372
427
|
this.version = this.#versionOf(this.#settings);
|
|
373
|
-
for (const
|
|
374
|
-
this.#appSettings.set(app, this.#appScope(
|
|
428
|
+
for (const app of new Set([...apps.keys(), ...this.#excluded.keys()]))
|
|
429
|
+
this.#appSettings.set(app, this.#appScope(apps.get(app), this.#excluded.get(app)));
|
|
375
430
|
// SSR renders the bundles a full load leaves out from here (the runtime, in this process).
|
|
376
431
|
const shared = registry();
|
|
377
432
|
for (const [at, byNamespace] of this.#fragments)
|
|
@@ -393,25 +448,25 @@ export class PageMessages {
|
|
|
393
448
|
version: this.version,
|
|
394
449
|
namespaces: this.#baseNamespaces,
|
|
395
450
|
...(this.#appNamespaces.size ? { apps: Object.fromEntries(this.#appNamespaces) } : {}),
|
|
451
|
+
...(this.#excluded.size ? { excluded: Object.fromEntries(this.#excluded) } : {}),
|
|
396
452
|
bundles: this.#registered.size,
|
|
397
453
|
cache: cache.enabled,
|
|
398
454
|
});
|
|
399
455
|
}
|
|
400
|
-
/**
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
456
|
+
/**
|
|
457
|
+
* D-0241: the settings of an app with its own locales, from the current ones; D-0312: of an app
|
|
458
|
+
* with pageExclude, which its runtime reads to tell an excluded key from a missing one.
|
|
459
|
+
*/
|
|
460
|
+
#appScope(own, excluded) {
|
|
461
|
+
const extra = {
|
|
462
|
+
...(own ? { locales: own.locales, defaultLocale: own.defaultLocale } : {}),
|
|
463
|
+
...(excluded ? { excluded } : {}),
|
|
464
|
+
};
|
|
465
|
+
const sealed = Object.freeze({ ...this.#sealedSettings, ...extra });
|
|
407
466
|
const settings = this.#settings === this.#sealedSettings
|
|
408
467
|
? sealed
|
|
409
|
-
: Object.freeze({
|
|
410
|
-
|
|
411
|
-
locales: own.locales,
|
|
412
|
-
defaultLocale: own.defaultLocale,
|
|
413
|
-
});
|
|
414
|
-
return { settings, sealed, version: this.#versionOf(settings), own };
|
|
468
|
+
: Object.freeze({ ...this.#settings, ...extra });
|
|
469
|
+
return { settings, sealed, version: this.#versionOf(settings), own, excluded };
|
|
415
470
|
}
|
|
416
471
|
#versionOf(settings) {
|
|
417
472
|
return this.#catalogHash
|
|
@@ -434,7 +489,7 @@ export class PageMessages {
|
|
|
434
489
|
this.#settings = settings;
|
|
435
490
|
this.version = version;
|
|
436
491
|
for (const [app, scope] of this.#appSettings)
|
|
437
|
-
this.#appSettings.set(app, this.#appScope(scope.own));
|
|
492
|
+
this.#appSettings.set(app, this.#appScope(scope.own, scope.excluded));
|
|
438
493
|
}
|
|
439
494
|
/** Takes this catalog's bundles out of the SSR registry (the plugin's stop). */
|
|
440
495
|
close() {
|
|
@@ -471,6 +526,9 @@ export class PageMessages {
|
|
|
471
526
|
else if (this.#templateScopes.has(templateScope(owner, template)))
|
|
472
527
|
scope = templateScope(owner, template);
|
|
473
528
|
}
|
|
529
|
+
// D-0312: the app's twin of that scope, without its pageExclude keys.
|
|
530
|
+
if (app !== undefined && this.#excluded.has(app))
|
|
531
|
+
scope = excludedScope(scope, app);
|
|
474
532
|
const at = slot(scope, locale);
|
|
475
533
|
const fragments = this.#fragments.get(at);
|
|
476
534
|
const chain = this.#chains.get(at);
|
|
@@ -657,6 +715,44 @@ const SLOT = '\u0000';
|
|
|
657
715
|
const slot = (scope, locale) => `${scope}${SLOT}${locale}`;
|
|
658
716
|
/** D-0282: the scope of an app scope (or BASE) under one domain template. */
|
|
659
717
|
const templateScope = (owner, template) => `${owner}\u0001${template}`;
|
|
718
|
+
/** D-0312: a scope as one app with pageExclude sees it (app names never hold the separators). */
|
|
719
|
+
const excludedScope = (scope, app) => `${scope}\u0002${app}`;
|
|
720
|
+
/**
|
|
721
|
+
* D-0312: whether a listed prefix covers `key` (the key itself, or a part of it that ends at a
|
|
722
|
+
* dot); every covering prefix goes into `matched`. One set lookup per dot of the key.
|
|
723
|
+
*/
|
|
724
|
+
function excludedBy(key, prefixes, matched) {
|
|
725
|
+
let covered = false;
|
|
726
|
+
for (let dot = key.indexOf('.');; dot = key.indexOf('.', dot + 1)) {
|
|
727
|
+
const part = dot < 0 ? key : key.slice(0, dot);
|
|
728
|
+
if (prefixes.has(part)) {
|
|
729
|
+
matched.add(part);
|
|
730
|
+
covered = true;
|
|
731
|
+
}
|
|
732
|
+
if (dot < 0)
|
|
733
|
+
return covered;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
/** A bundle without the keys `prefixes` cover; the same object when none is in it. */
|
|
737
|
+
function without(fragment, prefixes, matched) {
|
|
738
|
+
const result = object();
|
|
739
|
+
let removed = false;
|
|
740
|
+
for (const [source, messages] of Object.entries(fragment)) {
|
|
741
|
+
const kept = object();
|
|
742
|
+
let any = false;
|
|
743
|
+
for (const [key, message] of Object.entries(messages)) {
|
|
744
|
+
if (excludedBy(key, prefixes, matched)) {
|
|
745
|
+
removed = true;
|
|
746
|
+
continue;
|
|
747
|
+
}
|
|
748
|
+
kept[key] = message;
|
|
749
|
+
any = true;
|
|
750
|
+
}
|
|
751
|
+
if (any)
|
|
752
|
+
result[source] = Object.freeze(kept);
|
|
753
|
+
}
|
|
754
|
+
return removed ? Object.freeze(result) : fragment;
|
|
755
|
+
}
|
|
660
756
|
function pick(tokens, namespaces) {
|
|
661
757
|
const result = object();
|
|
662
758
|
for (const name of namespaces) {
|
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';
|
|
@@ -46,7 +47,8 @@ export class I18nRuntime {
|
|
|
46
47
|
// same catalogs. An empty pageMessages list keeps the prop: routes can add namespaces.
|
|
47
48
|
// D-0241: apps that offer only some locales (checked against the catalogs' locales).
|
|
48
49
|
const offered = appLocales(this.#options, api.locales, api.defaultLocale);
|
|
49
|
-
|
|
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);
|
|
50
52
|
// D-0181: with app layers each request translates in its app's scope; web knows the app
|
|
51
53
|
// (dokebi-app registers who it is), asked per request, so no dependency on dokebi-app.
|
|
52
54
|
const appOf = api.apps.length || offered.size
|
|
@@ -54,13 +56,19 @@ export class I18nRuntime {
|
|
|
54
56
|
: undefined;
|
|
55
57
|
// D-0196: apps with their own page messages need the app of a page too, layers or not.
|
|
56
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.
|
|
57
61
|
const pageAppOf = appOf ??
|
|
58
|
-
(this.#pageApps.length
|
|
62
|
+
(this.#pageApps.length
|
|
63
|
+
? (request) => server.appOf(request)
|
|
64
|
+
: (request) => (page.templates ? server.appOf(request) : undefined));
|
|
59
65
|
this.#web = server;
|
|
60
66
|
// D-0282: the template of a page's domain (dokebi-app tells web), asked only when a
|
|
61
67
|
// template layer exists; web before 0.4.25 has no templateOf (the layers then go unused).
|
|
62
|
-
const
|
|
63
|
-
|
|
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)
|
|
64
72
|
: undefined;
|
|
65
73
|
if (page.templates)
|
|
66
74
|
this.#templates = api.templates.map((layer) => `${layer.kind}:${layer.name}@${layer.template}`);
|
|
@@ -89,6 +97,70 @@ export class I18nRuntime {
|
|
|
89
97
|
value: (request, view) => page.value(request, request.i18n?.locale ?? api.defaultLocale, server.sealOf(request) !== undefined, pageAppOf?.(request), view?.public === true, templateOf?.(request)),
|
|
90
98
|
});
|
|
91
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
|
+
});
|
|
92
164
|
context.logger.log('debug', 'Translation HTTP integration registered', {
|
|
93
165
|
code: 'I18N_HTTP_READY',
|
|
94
166
|
});
|
|
@@ -135,7 +207,7 @@ export class I18nRuntime {
|
|
|
135
207
|
}
|
|
136
208
|
export const createPlugin = definePlugin({
|
|
137
209
|
name: 'dokebi-i18n',
|
|
138
|
-
version: '0.2.
|
|
210
|
+
version: '0.2.11',
|
|
139
211
|
core: '^0.4.0',
|
|
140
212
|
options,
|
|
141
213
|
contributes: [localeData, describePoint],
|
|
@@ -143,3 +215,27 @@ export const createPlugin = definePlugin({
|
|
|
143
215
|
entry: (input) => input,
|
|
144
216
|
instance: ({ options, entry }) => new I18nRuntime(options, entry),
|
|
145
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>;
|
|
@@ -33,5 +33,8 @@ export declare class I18n implements I18nApi {
|
|
|
33
33
|
get timeZone(): string;
|
|
34
34
|
setTimeZone(zone: string): void;
|
|
35
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>;
|
|
36
39
|
onTimeZone(listener: (zone: string) => void): void;
|
|
37
40
|
}
|
package/dist/service.js
CHANGED
|
@@ -197,6 +197,8 @@ export class I18n {
|
|
|
197
197
|
#logger;
|
|
198
198
|
#zone;
|
|
199
199
|
#zoneListeners = [];
|
|
200
|
+
/** D-0291: what applies a template bundle's words to the page bundles (the HTTP integration). */
|
|
201
|
+
#bundleWords;
|
|
200
202
|
constructor(options, input, logger) {
|
|
201
203
|
this.#logger = logger;
|
|
202
204
|
this.#zone = { current: zoneOf(options.timeZone) };
|
|
@@ -417,6 +419,18 @@ export class I18n {
|
|
|
417
419
|
});
|
|
418
420
|
}
|
|
419
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
|
+
}
|
|
420
434
|
onTimeZone(listener) {
|
|
421
435
|
this.#zoneListeners.push(listener);
|
|
422
436
|
}
|
package/options.schema.json
CHANGED
|
@@ -55,6 +55,17 @@
|
|
|
55
55
|
"maxItems": 64,
|
|
56
56
|
"description": "Namespaces every page of this app receives in place of http.pageMessages (D-0196); a route still adds more with pageMessages(request, ...). Unset: http.pageMessages."
|
|
57
57
|
},
|
|
58
|
+
"pageExclude": {
|
|
59
|
+
"type": "array",
|
|
60
|
+
"items": {
|
|
61
|
+
"type": "string",
|
|
62
|
+
"maxLength": 256,
|
|
63
|
+
"pattern": "^[a-zA-Z][a-zA-Z0-9_-]*(?:\\.[a-zA-Z][a-zA-Z0-9_-]*)*$",
|
|
64
|
+
"description": "A key prefix on a dot boundary (game.settings leaves out game.settings and game.settings.*, not game.settingsX), or a whole namespace."
|
|
65
|
+
},
|
|
66
|
+
"maxItems": 4096,
|
|
67
|
+
"description": "Keys this app's pages never receive (D-0312): left out of every bundle the app's first HTML and visits carry, its own and a route's pageMessages() alike. A page of the app that calls one gets EXCLUDED_MESSAGE (missing: error) instead of MISSING_MESSAGE. The server's t() (jobs, mail, other requests) and the other apps keep them. Unset: nothing left out."
|
|
68
|
+
},
|
|
58
69
|
"locales": {
|
|
59
70
|
"type": "array",
|
|
60
71
|
"items": {
|
|
@@ -78,7 +89,7 @@
|
|
|
78
89
|
}
|
|
79
90
|
},
|
|
80
91
|
"default": {},
|
|
81
|
-
"description": "Per @dokebi/app app (the app name of domains.<host>.app): its page messages (D-0196) and the locales it offers (D-0241). An app not listed gets http.pageMessages and every locale."
|
|
92
|
+
"description": "Per @dokebi/app app (the app name of domains.<host>.app): its page messages (D-0196), the keys its pages never receive (D-0312) and the locales it offers (D-0241). An app not listed gets http.pageMessages and every locale."
|
|
82
93
|
},
|
|
83
94
|
"maxPageHeaderBytes": {
|
|
84
95
|
"type": "integer",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dokebi/i18n",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.11",
|
|
4
4
|
"description": "Compiled translation catalogs, locale negotiation, cardinal plurals and named Intl formats.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dokebi",
|
|
@@ -98,6 +98,6 @@
|
|
|
98
98
|
}
|
|
99
99
|
},
|
|
100
100
|
"devDependencies": {
|
|
101
|
-
"@dokebi/web": "0.4.
|
|
101
|
+
"@dokebi/web": "0.4.28"
|
|
102
102
|
}
|
|
103
103
|
}
|