@dokebi/i18n 0.2.2 → 0.2.3
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/options.d.ts +3 -0
- package/dist/options.js +17 -0
- package/dist/page.js +11 -1
- package/dist/plugin.d.ts +4 -1
- package/dist/plugin.js +17 -3
- package/options.schema.json +30 -0
- package/package.json +1 -1
package/AI.md
CHANGED
|
@@ -14,6 +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: [...] } }` replaces `http.pageMessages` on that app's pages (D-0196); unknown names fail the start (`UNKNOWN_NAMESPACE`). |
|
|
17
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. |
|
|
18
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). |
|
|
19
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). |
|
|
@@ -105,7 +106,7 @@ options:
|
|
|
105
106
|
|
|
106
107
|
With web registered, the plugin attaches `request.i18n`, selects a configured locale from the preference cookie, then Accept-Language, then the default locale. It contributes the `locale` and `i18n` page props through the existing web view-data point (`i18n`: §Vue pages). HTML, JSON (any JSON media type, `application/problem+json` included; the type is compared without case; also a JSON answer @dokebi/web sealed on a seal host, `x-dokebi-sealed-path`) and Inertia responses preserve existing Vary fields and include Accept-Language and Cookie. Other response types have no added language header (the adapter removes only the label it wrote; one the answer set itself stays, also when it equals the selected locale). A Content-Language the handler or an error handler sets on a document is the body's real language and is kept (an untranslated English error answers `en` on a French request). The adapter never rewrites HTML. Inertia 0.4.4 renders a single Content-Language tag as the document's `lang` attribute; EJS templates use the contributed locale explicitly.
|
|
107
108
|
|
|
108
|
-
**Locales in the URL** (`/ja/p/1`, `/products/:id?lang=`… as a route parameter): the answer varies by its URL only, so it must not say `Vary: Accept-Language, Cookie` (a shared cache or CDN could not keep a public page) and its `Content-Language` is the URL's locale. With `http.pathLocale: true` a first path segment that is a configured locale decides by itself (no cookie, no Accept-Language; `/fr/nowhere` too, so the app's own 404 page under `/fr/` says `fr`); a path without such a segment (`/`, a redirect page) negotiates as above and varies by it. For a route parameter call `useUrlLocale(request, reply, locale)` before rendering: it switches `request.i18n` (and the page props `locale`/`i18n`), relabels `Content-Language` unless the answer set its own, and drops the Vary; a locale without a catalog is `INVALID_LOCALE` (answer your 404). An Inertia visit still varies by `X-Dokebi-I18n`, and a full page load whose bundles the `http.pageCache` cookie left out (`held`) says `Vary: Cookie`: that HTML is right only for the browser that keeps those bundles, so a shared cache must not hand it to another one. The first HTML of a template-inertia **public page** (`render(…, { public: true })`, 0.2.2, D-0195) reads no such cookie: `locale` and `i18n` are view data marked `public`, and asked for a public page they send every bundle, so that HTML is the same for every browser of the URL and language (no `Vary: Cookie`). An answer's own `Content-Language` stays (web's untranslated error pages say `en`). The browser runtime's `switchLocale` writes the preference cookie; with URL locales navigate to the other URL instead.
|
|
109
|
+
**Locales in the URL** (`/ja/p/1`, `/products/:id?lang=`… as a route parameter): the answer varies by its URL only, so it must not say `Vary: Accept-Language, Cookie` (a shared cache or CDN could not keep a public page) and its `Content-Language` is the URL's locale. With `http.pathLocale: true` a first path segment that is a configured locale decides by itself (no cookie, no Accept-Language; `/fr/nowhere` too, so the app's own 404 page under `/fr/` says `fr`); a path without such a segment (`/`, a redirect page) negotiates as above and varies by it. For a route parameter call `useUrlLocale(request, reply, locale)` before rendering: it switches `request.i18n` (and the page props `locale`/`i18n`), relabels `Content-Language` unless the answer set its own, and drops the Vary; a locale without a catalog is `INVALID_LOCALE` (answer your 404). An Inertia visit still varies by `X-Dokebi-I18n`, and a full page load whose bundles the `http.pageCache` cookie left out (`held`) says `Vary: Cookie`: that HTML is right only for the browser that keeps those bundles, so a shared cache must not hand it to another one. The first HTML of a template-inertia **public page** (`render(…, { public: true })`, 0.2.2, D-0195) reads no such cookie: `locale` and `i18n` are view data marked `public`, and asked for a public page they send every bundle of the page's list, so that HTML is the same for every browser of the URL and language (no `Vary: Cookie`). An answer's own `Content-Language` stays (web's untranslated error pages say `en`). The browser runtime's `switchLocale` writes the preference cookie; with URL locales navigate to the other URL instead.
|
|
109
110
|
|
|
110
111
|
The cookie is a client preference, not authentication. Only known locale values are accepted; a Cookie header above `http.maxCookieHeaderBytes` (16 KiB) is ignored. This plugin reads but does not write the preference cookie. Browser locale selection can continue to write it. All-excluded Accept-Language preferences fall back to the configured default in the automatic adapter, and so does a header past the `negotiate` input limits (it is client input, not a server error: the request continues, with a debug log `I18N_HTTP_HEADER_IGNORED`). An endpoint that requires 406 explicitly checks `negotiate` as in example 42.
|
|
111
112
|
|
|
@@ -162,6 +163,7 @@ const { t, n, d, locale, switchLocale } = useI18n();
|
|
|
162
163
|
```
|
|
163
164
|
|
|
164
165
|
- **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).
|
|
166
|
+
- **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`).
|
|
165
167
|
- **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. 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`.
|
|
166
168
|
- **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.
|
|
167
169
|
- **Payload** (D-0099, measured on the example 37 blog catalogs: 166 messages per locale in five namespaces): the first Korean page with every namespace carries 10.7 KB (3.6 KB gzip) once; a later visit's prop is about 125 bytes. With `pageMessages: ['common']` the first page carries 1.3 KB, and the first visit that adds `posts` 3.1 KB. Choosing the prop costs well under a microsecond per page (fragments are joined at startup).
|
package/dist/options.d.ts
CHANGED
|
@@ -2,6 +2,9 @@ import { type OptionsOf } from '@dokebi/core';
|
|
|
2
2
|
export declare const options: import("@dokebi/core").OptionsDeclaration<{
|
|
3
3
|
readonly defaultLocale: string;
|
|
4
4
|
readonly http: {
|
|
5
|
+
readonly apps: Readonly<Record<string, {
|
|
6
|
+
readonly pageMessages: readonly string[];
|
|
7
|
+
}>>;
|
|
5
8
|
readonly cookieName: string;
|
|
6
9
|
readonly enabled: boolean;
|
|
7
10
|
readonly maxCookieHeaderBytes: number;
|
package/dist/options.js
CHANGED
|
@@ -21,6 +21,22 @@ export const options = defineOptions({
|
|
|
21
21
|
maxItems: 64,
|
|
22
22
|
description: 'Namespaces every rendered page receives in the `i18n` prop for the browser runtime (@dokebi/template-inertia/i18n); a route adds more with pageMessages(request, ...).',
|
|
23
23
|
}),
|
|
24
|
+
apps: o.record(o.object({
|
|
25
|
+
pageMessages: o.array(o.string({
|
|
26
|
+
pattern: '^(?:\\*|[a-zA-Z][a-zA-Z0-9_-]*)$',
|
|
27
|
+
maxLength: 128,
|
|
28
|
+
description: 'A catalog namespace, or * for every namespace.',
|
|
29
|
+
}), {
|
|
30
|
+
required: true,
|
|
31
|
+
maxItems: 64,
|
|
32
|
+
description: 'Namespaces every page of this app receives in place of http.pageMessages (D-0196); a route still adds more with pageMessages(request, ...).',
|
|
33
|
+
}),
|
|
34
|
+
}), {
|
|
35
|
+
default: {},
|
|
36
|
+
keys: { pattern: '^[a-z][a-z0-9-]{0,31}$' },
|
|
37
|
+
maxProperties: 256,
|
|
38
|
+
description: 'Page messages per @dokebi/app app (the app name of domains.<host>.app), D-0196: an app not listed gets http.pageMessages.',
|
|
39
|
+
}),
|
|
24
40
|
maxPageHeaderBytes: o.integer({
|
|
25
41
|
default: 4096,
|
|
26
42
|
minimum: 256,
|
|
@@ -95,6 +111,7 @@ export const options = defineOptions({
|
|
|
95
111
|
enabled: false,
|
|
96
112
|
cookieName: 'dokebi-lang',
|
|
97
113
|
pageMessages: ['*'],
|
|
114
|
+
apps: {},
|
|
98
115
|
maxPageHeaderBytes: 4096,
|
|
99
116
|
pathLocale: false,
|
|
100
117
|
maxCookieHeaderBytes: 16384,
|
package/dist/page.js
CHANGED
|
@@ -94,6 +94,8 @@ export class PageMessages {
|
|
|
94
94
|
#base = new Map();
|
|
95
95
|
#baseBundles = new Map();
|
|
96
96
|
#baseNamespaces;
|
|
97
|
+
/** D-0196: an app's own list (http.apps.<app>.pageMessages), in place of the configured one. */
|
|
98
|
+
#appNamespaces = new Map();
|
|
97
99
|
#namespaces;
|
|
98
100
|
#logger;
|
|
99
101
|
#maxHeader;
|
|
@@ -144,6 +146,12 @@ export class PageMessages {
|
|
|
144
146
|
this.#maxHeader = options.http.maxPageHeaderBytes;
|
|
145
147
|
this.#maxJoined = options.http.maxJoinedSets;
|
|
146
148
|
this.#baseNamespaces = Object.freeze(configured.includes('*') ? [...namespaces].sort() : [...new Set(configured)]);
|
|
149
|
+
for (const [app, own] of Object.entries(options.http.apps)) {
|
|
150
|
+
for (const name of own.pageMessages)
|
|
151
|
+
if (name !== '*' && !namespaces.has(name))
|
|
152
|
+
throw new I18nError('UNKNOWN_NAMESPACE', `http.apps.${app}.pageMessages names ${JSON.stringify(name)}, which no catalog has`);
|
|
153
|
+
this.#appNamespaces.set(app, Object.freeze(own.pageMessages.includes('*') ? [...namespaces].sort() : [...new Set(own.pageMessages)]));
|
|
154
|
+
}
|
|
147
155
|
const owners = new Map();
|
|
148
156
|
const ambiguous = new Set();
|
|
149
157
|
const ordered = [...namespaces].sort();
|
|
@@ -303,6 +311,7 @@ export class PageMessages {
|
|
|
303
311
|
code: 'I18N_PAGE_READY',
|
|
304
312
|
version: this.version,
|
|
305
313
|
namespaces: this.#baseNamespaces,
|
|
314
|
+
...(this.#appNamespaces.size ? { apps: Object.fromEntries(this.#appNamespaces) } : {}),
|
|
306
315
|
bundles: this.#registered.size,
|
|
307
316
|
cache: cache.enabled,
|
|
308
317
|
});
|
|
@@ -358,7 +367,8 @@ export class PageMessages {
|
|
|
358
367
|
if (!fragments || !chain || !tokens)
|
|
359
368
|
throw new I18nError('INVALID_LOCALE', 'Page messages for an unregistered locale');
|
|
360
369
|
const extra = request[EXTRA];
|
|
361
|
-
|
|
370
|
+
// D-0196: an app with its own list gets it; its bundles are joined like a route's table.
|
|
371
|
+
let wanted = (app !== undefined ? this.#appNamespaces.get(app) : undefined) ?? this.#baseNamespaces;
|
|
362
372
|
if (extra?.length) {
|
|
363
373
|
const all = new Set(wanted);
|
|
364
374
|
for (const name of extra) {
|
package/dist/plugin.d.ts
CHANGED
|
@@ -9,7 +9,10 @@ export declare class I18nRuntime implements PluginRuntime<I18nApi> {
|
|
|
9
9
|
#private;
|
|
10
10
|
constructor(options: I18nOptions, entry: unknown);
|
|
11
11
|
setup(context: PluginContext): I18nApi;
|
|
12
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* D-0181 · D-0196: after every setup, app layers and per-app page messages with nothing that
|
|
14
|
+
* tells a request's app are unused.
|
|
15
|
+
*/
|
|
13
16
|
start(context: PluginContext): void;
|
|
14
17
|
}
|
|
15
18
|
export declare const createPlugin: import("@dokebi/core").PluginFactory<I18nApi>;
|
package/dist/plugin.js
CHANGED
|
@@ -13,6 +13,7 @@ export class I18nRuntime {
|
|
|
13
13
|
#entry;
|
|
14
14
|
#web;
|
|
15
15
|
#apps = [];
|
|
16
|
+
#pageApps = [];
|
|
16
17
|
constructor(options, entry) {
|
|
17
18
|
this.#options = options;
|
|
18
19
|
this.#entry = entry;
|
|
@@ -34,6 +35,10 @@ export class I18nRuntime {
|
|
|
34
35
|
const appOf = api.apps.length
|
|
35
36
|
? (request) => server.appOf(request)
|
|
36
37
|
: undefined;
|
|
38
|
+
// D-0196: apps with their own page messages need the app of a page too, layers or not.
|
|
39
|
+
this.#pageApps = Object.keys(this.#options.http.apps);
|
|
40
|
+
const pageAppOf = appOf ??
|
|
41
|
+
(this.#pageApps.length ? (request) => server.appOf(request) : undefined);
|
|
37
42
|
this.#web = server;
|
|
38
43
|
// D-0178: a time zone set at run time reaches the page settings and their version.
|
|
39
44
|
api.onTimeZone((zone) => page.setTimeZone(zone));
|
|
@@ -56,7 +61,7 @@ export class I18nRuntime {
|
|
|
56
61
|
// D-0126: a sealed answer's browser keeps no bundles (the page leaves through the seal).
|
|
57
62
|
// D-0195: a public page's first HTML sends every bundle (the claim cookie of this
|
|
58
63
|
// browser is not read), so one HTML serves every visitor of the URL and language.
|
|
59
|
-
value: (request, view) => page.value(request, request.i18n?.locale ?? api.defaultLocale, server.sealOf(request) !== undefined,
|
|
64
|
+
value: (request, view) => page.value(request, request.i18n?.locale ?? api.defaultLocale, server.sealOf(request) !== undefined, pageAppOf?.(request), view?.public === true),
|
|
60
65
|
});
|
|
61
66
|
context.defer(() => page.close());
|
|
62
67
|
context.logger.log('debug', 'Translation HTTP integration registered', {
|
|
@@ -76,7 +81,10 @@ export class I18nRuntime {
|
|
|
76
81
|
throw error;
|
|
77
82
|
}
|
|
78
83
|
}
|
|
79
|
-
/**
|
|
84
|
+
/**
|
|
85
|
+
* D-0181 · D-0196: after every setup, app layers and per-app page messages with nothing that
|
|
86
|
+
* tells a request's app are unused.
|
|
87
|
+
*/
|
|
80
88
|
start(context) {
|
|
81
89
|
if (this.#apps.length && this.#web && !this.#web.resolvesApps)
|
|
82
90
|
context.logger.log('warn', 'Translation app layers are not used: nothing tells the app of a request', {
|
|
@@ -84,11 +92,17 @@ export class I18nRuntime {
|
|
|
84
92
|
apps: this.#apps,
|
|
85
93
|
fix: 'Register @dokebi/app (it tells dokebi-web the app of each host), or move the app layers to src/shared/locales',
|
|
86
94
|
});
|
|
95
|
+
if (this.#pageApps.length && this.#web && !this.#web.resolvesApps)
|
|
96
|
+
context.logger.log('warn', 'Page messages per app are not used: nothing tells the app of a request', {
|
|
97
|
+
code: 'I18N_PAGE_APPS_UNUSED',
|
|
98
|
+
apps: this.#pageApps,
|
|
99
|
+
fix: 'Register @dokebi/app (it tells dokebi-web the app of each host), or remove http.apps',
|
|
100
|
+
});
|
|
87
101
|
}
|
|
88
102
|
}
|
|
89
103
|
export const createPlugin = definePlugin({
|
|
90
104
|
name: 'dokebi-i18n',
|
|
91
|
-
version: '0.2.
|
|
105
|
+
version: '0.2.3',
|
|
92
106
|
core: '^0.4.0',
|
|
93
107
|
options,
|
|
94
108
|
contributes: [localeData],
|
package/options.schema.json
CHANGED
|
@@ -34,6 +34,35 @@
|
|
|
34
34
|
],
|
|
35
35
|
"description": "Namespaces every rendered page receives in the `i18n` prop for the browser runtime (@dokebi/template-inertia/i18n); a route adds more with pageMessages(request, ...)."
|
|
36
36
|
},
|
|
37
|
+
"apps": {
|
|
38
|
+
"type": "object",
|
|
39
|
+
"propertyNames": {
|
|
40
|
+
"pattern": "^[a-z][a-z0-9-]{0,31}$"
|
|
41
|
+
},
|
|
42
|
+
"maxProperties": 256,
|
|
43
|
+
"additionalProperties": {
|
|
44
|
+
"type": "object",
|
|
45
|
+
"additionalProperties": false,
|
|
46
|
+
"properties": {
|
|
47
|
+
"pageMessages": {
|
|
48
|
+
"type": "array",
|
|
49
|
+
"items": {
|
|
50
|
+
"type": "string",
|
|
51
|
+
"maxLength": 128,
|
|
52
|
+
"pattern": "^(?:\\*|[a-zA-Z][a-zA-Z0-9_-]*)$",
|
|
53
|
+
"description": "A catalog namespace, or * for every namespace."
|
|
54
|
+
},
|
|
55
|
+
"maxItems": 64,
|
|
56
|
+
"description": "Namespaces every page of this app receives in place of http.pageMessages (D-0196); a route still adds more with pageMessages(request, ...)."
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
"required": [
|
|
60
|
+
"pageMessages"
|
|
61
|
+
]
|
|
62
|
+
},
|
|
63
|
+
"default": {},
|
|
64
|
+
"description": "Page messages per @dokebi/app app (the app name of domains.<host>.app), D-0196: an app not listed gets http.pageMessages."
|
|
65
|
+
},
|
|
37
66
|
"maxPageHeaderBytes": {
|
|
38
67
|
"type": "integer",
|
|
39
68
|
"minimum": 256,
|
|
@@ -123,6 +152,7 @@
|
|
|
123
152
|
"pageMessages": [
|
|
124
153
|
"*"
|
|
125
154
|
],
|
|
155
|
+
"apps": {},
|
|
126
156
|
"maxPageHeaderBytes": 4096,
|
|
127
157
|
"pathLocale": false,
|
|
128
158
|
"maxCookieHeaderBytes": 16384,
|