@dokebi/i18n 0.2.5 → 0.2.7

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 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: [...] } }` replaces `http.pageMessages` on that app's pages (D-0196); 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`). |
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). |
@@ -118,7 +118,7 @@ Disabled integration installs no request/response hooks and contributes no page
118
118
 
119
119
  ## Upgrading catalogs
120
120
 
121
- Use CLI 0.4.6 or newer for the `src/shared/locales` folder contract and preservation of retired user files. An upgrade never automatically merges a generated sample into user translations. The upgrade entry keeps existing `src/shared/views/locales` catalogs authoritative while that legacy directory exists. Move all language folders to `src/shared/locales`, resolve any generated-sample collision explicitly, remove the old empty directory, then use the canonical entry shown above. Edited entries are preserved by the CLI; update their path explicitly. New installations only create the canonical folder.
121
+ Use CLI 0.4.6 or newer for the `src/shared/locales` folder contract and preservation of retired user files. An upgrade never automatically merges a generated sample into user translations. An upgrade from 0.1.x (whose entry read `src/shared/views/locales`) gets a compatibility entry that keeps those catalogs authoritative while that legacy directory exists; an upgrade from 0.2.0 or later names the canonical entry, so an entry you never edited stays the canonical one (0.2.7; 0.2.6 and earlier put the compatibility entry in its place). Move all language folders to `src/shared/locales`, resolve any generated-sample collision explicitly, remove the old empty directory, then use the canonical entry shown above. Edited entries are preserved by the CLI; update their path explicitly. New installations only create the canonical folder.
122
122
 
123
123
  ## Runtime API
124
124
 
@@ -164,6 +164,7 @@ const { t, n, d, locale, switchLocale } = useI18n();
164
164
 
165
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
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`).
167
+ - **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.
167
168
  - **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`.
168
169
  - **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.
169
170
  - **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).
@@ -0,0 +1,13 @@
1
+ import type { I18nOptions } from './options.js';
2
+ /** D-0241: the locales one @dokebi/app app offers, and the one it answers when nothing matches. */
3
+ export interface AppLocales {
4
+ readonly locales: readonly string[];
5
+ readonly defaultLocale: string;
6
+ }
7
+ /**
8
+ * `http.apps.<app>.locales` and `defaultLocale`, checked against the locales the catalogs have:
9
+ * every one must be one of them (INVALID_OPTIONS otherwise). An app without `locales` is not in
10
+ * the map (it offers every locale). Its default: `defaultLocale`, else the plugin's when the app
11
+ * offers it, else the app's first locale.
12
+ */
13
+ export declare function appLocales(options: I18nOptions, locales: readonly string[], defaultLocale: string): ReadonlyMap<string, AppLocales>;
@@ -0,0 +1,39 @@
1
+ import { I18nError } from './errors.js';
2
+ import { canonical } from './values.js';
3
+ /**
4
+ * `http.apps.<app>.locales` and `defaultLocale`, checked against the locales the catalogs have:
5
+ * every one must be one of them (INVALID_OPTIONS otherwise). An app without `locales` is not in
6
+ * the map (it offers every locale). Its default: `defaultLocale`, else the plugin's when the app
7
+ * offers it, else the app's first locale.
8
+ */
9
+ export function appLocales(options, locales, defaultLocale) {
10
+ const known = new Map(locales.map((locale) => [locale.toLowerCase(), locale]));
11
+ const out = new Map();
12
+ for (const [app, settings] of Object.entries(options.http.apps)) {
13
+ const listed = settings.locales;
14
+ if (listed === undefined) {
15
+ if (settings.defaultLocale !== undefined)
16
+ throw new I18nError('INVALID_OPTIONS', `http.apps.${app}.defaultLocale needs http.apps.${app}.locales (the locales it chooses from)`);
17
+ continue;
18
+ }
19
+ const chosen = [];
20
+ for (const given of listed) {
21
+ const locale = known.get(canonical(given).toLowerCase());
22
+ if (locale === undefined)
23
+ throw new I18nError('INVALID_OPTIONS', `http.apps.${app}.locales names ${JSON.stringify(given)}, which no catalog has (catalogs: ${locales.join(', ')})`);
24
+ if (!chosen.includes(locale))
25
+ chosen.push(locale);
26
+ }
27
+ let fallback;
28
+ if (settings.defaultLocale !== undefined) {
29
+ const locale = known.get(canonical(settings.defaultLocale).toLowerCase());
30
+ if (locale === undefined || !chosen.includes(locale))
31
+ throw new I18nError('INVALID_OPTIONS', `http.apps.${app}.defaultLocale ${JSON.stringify(settings.defaultLocale)} is not one of http.apps.${app}.locales (${chosen.join(', ')})`);
32
+ fallback = locale;
33
+ }
34
+ else
35
+ fallback = chosen.includes(defaultLocale) ? defaultLocale : chosen[0];
36
+ out.set(app, Object.freeze({ locales: Object.freeze(chosen), defaultLocale: fallback }));
37
+ }
38
+ return out;
39
+ }
package/dist/http.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { Logger } from '@dokebi/core';
2
2
  import type { FastifyPluginCallback, FastifyReply, FastifyRequest } from 'fastify';
3
+ import type { AppLocales } from './app-locales.js';
3
4
  import type { I18nApi, Translator } from './contract.js';
4
5
  import { type NegotiationLimits } from './negotiation.js';
5
6
  export interface I18nHttpRequest {
@@ -28,4 +29,6 @@ export declare function translationHttp(api: I18nApi, cookieName: string, logger
28
29
  readonly pathLocale?: boolean;
29
30
  /** D-0181: the request's app (@dokebi/app); an app with a layer translates in its scope. */
30
31
  readonly appOf?: (request: FastifyRequest) => string | undefined;
32
+ /** D-0241: apps that offer only some locales, and their default. */
33
+ readonly appLocales?: ReadonlyMap<string, AppLocales>;
31
34
  }, pageHeader?: string): FastifyPluginCallback;
package/dist/http.js CHANGED
@@ -86,20 +86,28 @@ export function translationHttp(api, cookieName, logger, limits, pageHeader) {
86
86
  // D-0099: an Inertia visit's `i18n` prop depends on what the browser runtime says it holds.
87
87
  const varyNames = pageHeader ? [...VARY_NAMES, pageHeader] : VARY_NAMES;
88
88
  const inertiaVary = varyNames.join(', ');
89
- // D-0181: the translators of each scope (the base, and each app with a layer), made once.
90
- const scoped = new Map(['', ...api.apps].map((app) => {
91
- const options = app ? { app } : undefined;
92
- const translators = new Map(api.locales.map((locale) => [locale.toLowerCase(), api.forLocale(locale, options)]));
89
+ // D-0181: the translators of each scope (the base, each app with a layer, and D-0241 each app
90
+ // with its own locales), made once. A scope negotiates only among its own locales.
91
+ const own = limits.appLocales ?? new Map();
92
+ const scoped = new Map([...new Set(['', ...api.apps, ...own.keys()])].map((app) => {
93
+ const options = app && api.apps.includes(app) ? { app } : undefined;
94
+ const offered = (app && own.get(app)) || undefined;
95
+ const locales = offered?.locales ?? api.locales;
96
+ const fallbackLocale = offered?.defaultLocale ?? api.defaultLocale;
97
+ const translators = new Map(locales.map((locale) => [locale.toLowerCase(), api.forLocale(locale, options)]));
93
98
  const scope = {
94
99
  options,
95
100
  translators,
96
101
  adapter: Object.freeze({ translators }),
97
- fallback: api.forLocale(api.defaultLocale, options),
102
+ fallback: api.forLocale(fallbackLocale, options),
103
+ negotiator: offered
104
+ ? new Negotiator(locales, fallbackLocale, limits.negotiation)
105
+ : undefined,
98
106
  };
99
107
  return [app, scope];
100
108
  }));
101
109
  const base = scoped.get('');
102
- const appOf = api.apps.length ? limits.appOf : undefined;
110
+ const appOf = api.apps.length || own.size ? limits.appOf : undefined;
103
111
  const longest = Math.max(...api.locales.map((locale) => locale.length));
104
112
  // A URL-decided answer varies by its URL only; an Inertia visit still by what the page holds.
105
113
  const urlVary = pageHeader ? [pageHeader] : [];
@@ -110,7 +118,8 @@ export function translationHttp(api, cookieName, logger, limits, pageHeader) {
110
118
  server.decorateRequest('i18n');
111
119
  server.addHook('onRequest', (request, reply, next) => {
112
120
  const app = appOf?.(request);
113
- const { translators, adapter, fallback, options } = (app !== undefined && scoped.get(app)) || base;
121
+ const scope = (app !== undefined && scoped.get(app)) || base;
122
+ const { translators, adapter, fallback, options } = scope;
114
123
  request[ADAPTER] = adapter;
115
124
  const fromPath = limits.pathLocale
116
125
  ? pathTranslator(request.url, translators, longest)
@@ -130,7 +139,7 @@ export function translationHttp(api, cookieName, logger, limits, pageHeader) {
130
139
  : undefined;
131
140
  let translator = saved ? translators.get(saved.toLowerCase()) : undefined;
132
141
  if (!translator) {
133
- const locale = negotiator.prefer(request.headers['accept-language']);
142
+ const locale = (scope.negotiator ?? negotiator).prefer(request.headers['accept-language']);
134
143
  // A header past the negotiation limits is client input, not a server failure.
135
144
  if (locale === undefined && logger.isEnabled('debug'))
136
145
  logger.log('debug', 'Oversized Accept-Language ignored', {
package/dist/install.d.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  interface InstallContext {
2
2
  readonly options: Readonly<Record<string, unknown>>;
3
3
  }
4
+ interface UpgradeContext extends InstallContext {
5
+ /** The installed version the upgrade starts from. */
6
+ readonly from: string;
7
+ }
4
8
  export declare function planInstall(context: InstallContext): {
5
9
  requires: string[];
6
10
  entry: {
@@ -13,7 +17,7 @@ export declare function planInstall(context: InstallContext): {
13
17
  owner: 'user';
14
18
  }[];
15
19
  };
16
- export declare function planUpgrade(context: InstallContext): {
20
+ export declare function planUpgrade(context: UpgradeContext): {
17
21
  requires: string[];
18
22
  entry: {
19
23
  source: string;
package/dist/install.js CHANGED
@@ -9,6 +9,14 @@ const files = [
9
9
  function requires(context) {
10
10
  return context.options.http?.enabled ? ['dokebi-web'] : [];
11
11
  }
12
+ /**
13
+ * Whether `from` is a 0.1.x install, whose entry read the catalogs from src/shared/views/locales/
14
+ * (0.2.0 moved them to src/shared/locales/). Only those get the compatibility entry.
15
+ */
16
+ function legacyLayout(from) {
17
+ const [major, minor] = from.split('.').map((part) => Number.parseInt(part, 10));
18
+ return major === 0 && minor !== undefined && minor < 2;
19
+ }
12
20
  export function planInstall(context) {
13
21
  return {
14
22
  requires: requires(context),
@@ -17,10 +25,18 @@ export function planInstall(context) {
17
25
  };
18
26
  }
19
27
  export function planUpgrade(context) {
20
- // CLI >=0.4.6 preserves retired user-owned files. No user translation is overwritten or deleted.
28
+ // The CLI rewrites an entry the project never edited with the template named here, so a project
29
+ // already on the 0.2 layout gets the current entry, never the 0.1 compatibility one (an upgrade
30
+ // to 0.2.6 put that in place of shop's entry). An edited entry is kept either way, and CLI
31
+ // >=0.4.6 preserves retired user-owned files: no translation is overwritten or deleted.
21
32
  return {
22
33
  requires: requires(context),
23
- entry: { source: 'templates/upgrade-extension.ts', target },
34
+ entry: {
35
+ source: legacyLayout(context.from)
36
+ ? 'templates/upgrade-extension.ts'
37
+ : 'templates/extension.ts',
38
+ target,
39
+ },
24
40
  files,
25
41
  };
26
42
  }
package/dist/options.d.ts CHANGED
@@ -3,7 +3,9 @@ export declare const options: import("@dokebi/core").OptionsDeclaration<{
3
3
  readonly defaultLocale: string;
4
4
  readonly http: {
5
5
  readonly apps: Readonly<Record<string, {
6
- readonly pageMessages: readonly string[];
6
+ readonly defaultLocale?: string;
7
+ readonly locales?: readonly string[];
8
+ readonly pageMessages?: readonly string[];
7
9
  }>>;
8
10
  readonly cookieName: string;
9
11
  readonly enabled: boolean;
package/dist/options.js CHANGED
@@ -27,15 +27,37 @@ export const options = defineOptions({
27
27
  maxLength: 128,
28
28
  description: 'A catalog namespace, or * for every namespace.',
29
29
  }), {
30
- required: true,
31
30
  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, ...).',
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.',
33
32
  }),
33
+ locales: o.array(o.string({
34
+ minLength: 2,
35
+ maxLength: 35,
36
+ pattern: '^[A-Za-z]{2,8}(?:-[A-Za-z0-9]{1,8})*$',
37
+ description: 'A locale of the catalogs (ko, en, en-US).',
38
+ }), {
39
+ minItems: 1,
40
+ maxItems: 32,
41
+ description: "The locales this app offers (D-0241): negotiation (path, cookie, Accept-Language, default) picks only from them and the page's locale switcher lists them. Each must be a locale the catalogs have. Unset: every locale of the catalogs.",
42
+ }),
43
+ defaultLocale: o.string({
44
+ minLength: 2,
45
+ maxLength: 35,
46
+ pattern: '^[A-Za-z]{2,8}(?:-[A-Za-z0-9]{1,8})*$',
47
+ description: "This app's answer when nothing the browser asks for is offered (D-0241): one of its locales. Unset: the plugin's defaultLocale when the app offers it, else the app's first locale.",
48
+ }),
49
+ }, {
50
+ refine: [
51
+ {
52
+ test: (value) => Object.values(value).some((field) => field !== undefined),
53
+ message: 'sets pageMessages, locales or defaultLocale',
54
+ },
55
+ ],
34
56
  }), {
35
57
  default: {},
36
58
  keys: { pattern: '^[a-z][a-z0-9-]{0,31}$' },
37
59
  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.',
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.',
39
61
  }),
40
62
  maxPageHeaderBytes: o.integer({
41
63
  default: 4096,
package/dist/page.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { Logger } from '@dokebi/core';
2
+ import type { AppLocales } from './app-locales.js';
2
3
  import type { Message } from './contract.js';
3
4
  import { type I18nOptions } from './options.js';
4
5
  /** Request header of the browser runtime: what it holds, `v=<version>; h=<token><token>…`. */
@@ -72,7 +73,7 @@ export declare class PageMessages {
72
73
  #private;
73
74
  /** Changes with the time zone (D-0178): the browser then takes the new settings. */
74
75
  version: string;
75
- constructor(options: I18nOptions, input: unknown, logger: Logger);
76
+ constructor(options: I18nOptions, input: unknown, logger: Logger, apps?: ReadonlyMap<string, AppLocales>);
76
77
  /**
77
78
  * D-0178: the application's time zone changed. The settings carry it and the version changes,
78
79
  * so every browser takes the new settings on its next page; the bundles and their tokens stay.
package/dist/page.js CHANGED
@@ -112,7 +112,12 @@ export class PageMessages {
112
112
  */
113
113
  #lastHeader = { text: '', locale: '', claim: NOTHING };
114
114
  #lastCookie = { text: '', locale: '', claim: NOTHING };
115
- constructor(options, input, logger) {
115
+ /**
116
+ * D-0241: the settings of each app with its own locales (the switcher's list and default), and
117
+ * their version: a browser on that app compares its claim with that version.
118
+ */
119
+ #appSettings = new Map();
120
+ constructor(options, input, logger, apps = new Map()) {
116
121
  this.#logger = logger;
117
122
  // The entry was validated by the provider (service.ts) already; this reads the same data.
118
123
  const entry = record(input ?? {});
@@ -147,6 +152,9 @@ export class PageMessages {
147
152
  this.#maxJoined = options.http.maxJoinedSets;
148
153
  this.#baseNamespaces = Object.freeze(configured.includes('*') ? [...namespaces].sort() : [...new Set(configured)]);
149
154
  for (const [app, own] of Object.entries(options.http.apps)) {
155
+ // D-0241: an app entry may set only its locales; its page messages are then the base's.
156
+ if (own.pageMessages === undefined)
157
+ continue;
150
158
  for (const name of own.pageMessages)
151
159
  if (name !== '*' && !namespaces.has(name))
152
160
  throw new I18nError('UNKNOWN_NAMESPACE', `http.apps.${app}.pageMessages names ${JSON.stringify(name)}, which no catalog has`);
@@ -291,6 +299,8 @@ export class PageMessages {
291
299
  ]);
292
300
  this.#catalogHash = createHash('sha256').update(canonicalJson);
293
301
  this.version = this.#versionOf(this.#settings);
302
+ for (const [app, own] of apps)
303
+ this.#appSettings.set(app, this.#appScope(own));
294
304
  // SSR renders the bundles a full load leaves out from here (the runtime, in this process).
295
305
  const shared = registry();
296
306
  for (const [at, byNamespace] of this.#fragments)
@@ -316,6 +326,22 @@ export class PageMessages {
316
326
  cache: cache.enabled,
317
327
  });
318
328
  }
329
+ /** D-0241: the settings of an app with its own locales, from the current ones. */
330
+ #appScope(own) {
331
+ const sealed = Object.freeze({
332
+ ...this.#sealedSettings,
333
+ locales: own.locales,
334
+ defaultLocale: own.defaultLocale,
335
+ });
336
+ const settings = this.#settings === this.#sealedSettings
337
+ ? sealed
338
+ : Object.freeze({
339
+ ...this.#settings,
340
+ locales: own.locales,
341
+ defaultLocale: own.defaultLocale,
342
+ });
343
+ return { settings, sealed, version: this.#versionOf(settings), own };
344
+ }
319
345
  #versionOf(settings) {
320
346
  return this.#catalogHash
321
347
  .copy()
@@ -336,6 +362,8 @@ export class PageMessages {
336
362
  this.#sealedSettings = sealed;
337
363
  this.#settings = settings;
338
364
  this.version = version;
365
+ for (const [app, scope] of this.#appSettings)
366
+ this.#appSettings.set(app, this.#appScope(scope.own));
339
367
  }
340
368
  /** Takes this catalog's bundles out of the SSR registry (the plugin's stop). */
341
369
  close() {
@@ -434,7 +462,9 @@ export class PageMessages {
434
462
  const omitted = fromCookie ? wanted.filter((name) => held.has(name)) : [];
435
463
  if (omitted.length)
436
464
  request[COOKIE_CLAIMED] = true;
437
- const settings = claim.version !== this.version;
465
+ const own = app !== undefined ? this.#appSettings.get(app) : undefined;
466
+ const version = own?.version ?? this.version;
467
+ const settings = claim.version !== version;
438
468
  if (this.#logger.isEnabled('debug'))
439
469
  this.#logger.log('debug', 'Page messages selected', {
440
470
  code: 'I18N_PAGE_MESSAGES',
@@ -448,13 +478,23 @@ export class PageMessages {
448
478
  });
449
479
  return {
450
480
  locale,
451
- version: this.version,
481
+ version,
452
482
  fallback: chain,
453
483
  namespaces: wanted,
454
484
  ...(messages && Object.keys(messages).length ? { messages } : {}),
455
485
  ...(bundles && missing.length ? { bundles } : {}),
456
486
  ...(omitted.length ? { held: pick(tokens, omitted) } : {}),
457
- ...(settings ? { settings: sealed ? this.#sealedSettings : this.#settings } : {}),
487
+ ...(settings
488
+ ? {
489
+ settings: own
490
+ ? sealed
491
+ ? own.sealed
492
+ : own.settings
493
+ : sealed
494
+ ? this.#sealedSettings
495
+ : this.#settings,
496
+ }
497
+ : {}),
458
498
  };
459
499
  }
460
500
  /**
package/dist/plugin.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { definePlugin, definePoint, optionalDependency, } from '@dokebi/core';
2
2
  import { diagnostics } from '@dokebi/core/libraries';
3
+ import { appLocales } from './app-locales.js';
3
4
  import { I18nError } from './errors.js';
4
5
  import { translationHttp } from './http.js';
5
6
  import { options } from './options.js';
@@ -29,10 +30,12 @@ export class I18nRuntime {
29
30
  throw new I18nError('HTTP_UNAVAILABLE', 'Enable the web plugin before enabling i18n HTTP integration');
30
31
  // D-0099: the messages of the browser runtime (@dokebi/template-inertia/i18n), from the
31
32
  // same catalogs. An empty pageMessages list keeps the prop: routes can add namespaces.
32
- const page = new PageMessages(this.#options, this.#entry, context.logger);
33
+ // D-0241: apps that offer only some locales (checked against the catalogs' locales).
34
+ const offered = appLocales(this.#options, api.locales, api.defaultLocale);
35
+ const page = new PageMessages(this.#options, this.#entry, context.logger, offered);
33
36
  // D-0181: with app layers each request translates in its app's scope; web knows the app
34
37
  // (dokebi-app registers who it is), asked per request, so no dependency on dokebi-app.
35
- const appOf = api.apps.length
38
+ const appOf = api.apps.length || offered.size
36
39
  ? (request) => server.appOf(request)
37
40
  : undefined;
38
41
  // D-0196: apps with their own page messages need the app of a page too, layers or not.
@@ -47,6 +50,7 @@ export class I18nRuntime {
47
50
  pathLocale: this.#options.http.pathLocale,
48
51
  negotiation: negotiationLimits(this.#options),
49
52
  ...(appOf ? { appOf } : {}),
53
+ ...(offered.size ? { appLocales: offered } : {}),
50
54
  }, 'X-Dokebi-I18n'), {});
51
55
  // D-0195: both are the same for every visitor of a URL and language, so they go into
52
56
  // the first HTML of a public page (template-inertia `render(…, { public: true })`).
@@ -102,7 +106,7 @@ export class I18nRuntime {
102
106
  }
103
107
  export const createPlugin = definePlugin({
104
108
  name: 'dokebi-i18n',
105
- version: '0.2.5',
109
+ version: '0.2.7',
106
110
  core: '^0.4.0',
107
111
  options,
108
112
  contributes: [localeData],
@@ -53,15 +53,32 @@
53
53
  "description": "A catalog namespace, or * for every namespace."
54
54
  },
55
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, ...)."
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
+ },
58
+ "locales": {
59
+ "type": "array",
60
+ "items": {
61
+ "type": "string",
62
+ "minLength": 2,
63
+ "maxLength": 35,
64
+ "pattern": "^[A-Za-z]{2,8}(?:-[A-Za-z0-9]{1,8})*$",
65
+ "description": "A locale of the catalogs (ko, en, en-US)."
66
+ },
67
+ "minItems": 1,
68
+ "maxItems": 32,
69
+ "description": "The locales this app offers (D-0241): negotiation (path, cookie, Accept-Language, default) picks only from them and the page's locale switcher lists them. Each must be a locale the catalogs have. Unset: every locale of the catalogs."
70
+ },
71
+ "defaultLocale": {
72
+ "type": "string",
73
+ "minLength": 2,
74
+ "maxLength": 35,
75
+ "pattern": "^[A-Za-z]{2,8}(?:-[A-Za-z0-9]{1,8})*$",
76
+ "description": "This app's answer when nothing the browser asks for is offered (D-0241): one of its locales. Unset: the plugin's defaultLocale when the app offers it, else the app's first locale."
57
77
  }
58
- },
59
- "required": [
60
- "pageMessages"
61
- ]
78
+ }
62
79
  },
63
80
  "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."
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."
65
82
  },
66
83
  "maxPageHeaderBytes": {
67
84
  "type": "integer",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dokebi/i18n",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "Compiled translation catalogs, locale negotiation, cardinal plurals and named Intl formats.",
5
5
  "keywords": [
6
6
  "dokebi",
@@ -97,6 +97,6 @@
97
97
  }
98
98
  },
99
99
  "devDependencies": {
100
- "@dokebi/web": "0.4.7"
100
+ "@dokebi/web": "0.4.12"
101
101
  }
102
102
  }