@dokebi/template-inertia 0.4.30 → 0.4.31

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
@@ -148,7 +148,7 @@ const { t, n, d, locale, locales, switchLocale, localeName, timeZone } = useI18n
148
148
  </template>
149
149
  ```
150
150
 
151
- - `t(key, parameters?)`, `t(key, defaultValue, parameters?)`, `n(value, format?)`, `d(value, format?)`, `has(key)`: the server translator's rules — `{name}` placeholders, `{{`/`}}` literal braces, cardinal plurals chosen by `Intl.PluralRules` of the locale that gave the message, the lookup chain selected → registered parents → default, an explicit default that wins over the chain but not over the locale's own message, the `missing` policy in effect on the server (`error` throws `MISSING_MESSAGE` naming the namespace to send; `key` returns the key; dokebi-i18n's default `auto` is `key` under `npx dokebi dev` and `error` elsewhere), `maxOutputLength`, `maxKeyLength` and `maxBranchLength` of the server's settings (defaults 256 and 16384 when the server sends none), the named number/date formats and the application's time zone of the page's settings. A parity test runs the server translator and this runtime on one corpus.
151
+ - `t(key, parameters?)`, `t(key, defaultValue, parameters?)`, `n(value, format?)`, `d(value, format?)`, `has(key)`: the server translator's rules — `{name}` placeholders, `{{`/`}}` literal braces, cardinal plurals chosen by `Intl.PluralRules` of the locale that gave the message, the lookup chain selected → registered parents → default, an explicit default that wins over the chain but not over the locale's own message, the `missing` policy in effect on the server (`error` throws `MISSING_MESSAGE` naming the namespace to send; `key` returns the key; dokebi-i18n's default `auto` is `key` under `npx dokebi dev` and `error` elsewhere), the keys the app's pages never receive (0.4.31, D-0312: dokebi-i18n `http.apps.<app>.pageExclude` reaches the page as `settings.excluded`; a key under one throws `EXCLUDED_MESSAGE` naming the prefix with `missing: error`, and with `key` returns the key and warns `I18N_EXCLUDED_KEY` once per prefix — an explicit default still wins), `maxOutputLength`, `maxKeyLength` and `maxBranchLength` of the server's settings (defaults 256 and 16384 when the server sends none), the named number/date formats and the application's time zone of the page's settings. A parity test runs the server translator and this runtime on one corpus.
152
152
  - `timeZone()` is that zone: it follows `setTimeZone` on the server from the next page on (D-0178). Pass it to `@dokebi/i18n/calendar` so a page counts days as the server does (`dayOf(timeZone(), Date.now())` is the site's today; `presetRange(timeZone(), 'last7')` a filter's days).
153
153
  - `locale` and `locales` are read-only refs (destructure them; templates unwrap them). The functions are bound, so destructuring keeps them working. Templates also have `$t`, `$n`, `$d` and `$i18n`.
154
154
  - `switchLocale(locale)` writes the preference cookie of dokebi-i18n (`http.cookieName`) and reloads the page's props through Inertia's router (no document reload): every prop comes back translated by the server, plus the messages this browser does not hold. `<html lang>` follows. `localeName(code)` is the language's own name for a switch (`Intl.DisplayNames`). The generated `views/default/Layout.vue` shows such a switch when there are two or more locales.
@@ -157,7 +157,7 @@ const { t, n, d, locale, locales, switchLocale, localeName, timeZone } = useI18n
157
157
  - **Kept between page loads (D-0126).** In a browser, when the server allows it (`settings.cache`, dokebi-i18n `http.pageCache`), the bundles a page receives are written to localStorage when the page is idle (one item per bundle: `dokebi-i18n:1:<locale>:<namespace>:<token>:…`, one version per locale and namespace, within `maxBundles` and `maxStoreCharacters`, the oldest bundles of other locales evicted first, also when the quota is full) and the cookie `dokebi-i18n-held` names them (newest of the shown locale first, within `maxCookieBytes`). The next full page load leaves them out of the HTML (`held`) and the runtime reads them from the store before the first render; the kept bundles of the shown locale are loaded into memory when idle, so visits name them too, and `switchLocale` names the kept bundles of the target language. **When the store lacks a bundle the server left out** (storage cleared, a changed cookie, a full quota, another tab evicted it), the runtime rewrites the cookie from what is really kept and fetches the missing messages with an Inertia partial request for `i18n` **before `app.mount` runs** (pageI18n defers the app's `mount`, so neither a client render nor a hydration ever draws without them; an SSR page stays visible meanwhile). If that request gets no Inertia answer, the cookie is deleted and the document is loaded again (at most once a minute per tab; after that the page mounts and the failure is logged at `error` as `I18N_PAGE_RECOVERY_FAILED`). On the server, SSR takes the left-out bundles from the catalogs of @dokebi/i18n in the same process, so the first paint has them; a renderer in another process cannot, and `t()` throws `MISSING_MESSAGE` (the SSR failure policy applies). `storage: false` keeps nothing. No app code changes: the generated `views/app.js` and `views/ssr.js` are unchanged.
158
158
  - A project made before this entry existed: `views/app.js` and `views/ssr.js` are yours, so add the two lines by hand — `import { pageI18n } from '@dokebi/template-inertia/i18n'`, then `.use(pageI18n(props.initialPage, { router }))` after `.use(plugin)` in `setup` (`router` from `@inertiajs/vue3`), and `withApp: (vue) => vue.use(pageI18n(page))` in the `createInertiaApp` options of `views/ssr.js`.
159
159
  - `createI18n({ locale, messages, defaultLocale?, fallback?, missing?, timeZone?, maxOutputLength?, maxKeyLength?, maxBranchLength?, numberFormats?, dateFormats?, logger? })` makes an instance from given messages (flat dot keys or nested namespace objects), for plain Vue apps and unit tests: `app.use(createI18n(...))`.
160
- - Debug logs (D-0047) go to the optional `logger` (`{ isEnabled?, log(level, message, fields) }`): `Missing translation returned as key` (`I18N_MISSING_KEY`), `Page translations are off` (`I18N_PAGE_OFF`), `Page translations reloaded` (`I18N_PAGE_RELOAD`), `Page language switch` (`I18N_PAGE_SWITCH`); D-0126: `I18N_PAGE_INCOMPLETE` (a page lacks bundles), `I18N_PAGE_RECOVER`, `I18N_PAGE_RECOVERED`, `I18N_PAGE_RECOVERY_RELOAD`, `I18N_STORE_COOKIE`, `I18N_STORE_LOADED`, `I18N_STORE_FULL`, `I18N_STORE_TRIMMED`, `I18N_STORE_SKIPPED`, `I18N_STORE_INVALID` (an item that is not the server's shape is removed), `I18N_STORE_UNAVAILABLE` (storage refused: nothing is kept and the cookie is deleted), `I18N_STORE_OFF`.
160
+ - Debug logs (D-0047) go to the optional `logger` (`{ isEnabled?, log(level, message, fields) }`): `Missing translation returned as key` (`I18N_MISSING_KEY`), `Excluded translation returned as key` (`I18N_EXCLUDED_KEY`, `prefix`; the first one per prefix at `warn`), `Page translations are off` (`I18N_PAGE_OFF`), `Page translations reloaded` (`I18N_PAGE_RELOAD`), `Page language switch` (`I18N_PAGE_SWITCH`); D-0126: `I18N_PAGE_INCOMPLETE` (a page lacks bundles), `I18N_PAGE_RECOVER`, `I18N_PAGE_RECOVERED`, `I18N_PAGE_RECOVERY_RELOAD`, `I18N_STORE_COOKIE`, `I18N_STORE_LOADED`, `I18N_STORE_FULL`, `I18N_STORE_TRIMMED`, `I18N_STORE_SKIPPED`, `I18N_STORE_INVALID` (an item that is not the server's shape is removed), `I18N_STORE_UNAVAILABLE` (storage refused: nothing is kept and the cookie is deleted), `I18N_STORE_OFF`.
161
161
  - Types: `page.d.ts` of the scaffold types `$page.props.i18n` (`PageI18nPayload`) and `locale`; importing the module also types `$t`, `$n`, `$d` and `$i18n` for vue-tsc. Vue is an optional peer dependency (`^3.5.0`), used only by this entry.
162
162
  - Do not add `vue-i18n` or another translation runtime, import the catalogs into the browser bundle, cache messages yourself or build a route that serves them: the prop already carries what the page needs, once per browser and bundle version.
163
163
 
@@ -169,6 +169,7 @@ const { t, n, d, locale, locales, switchLocale, localeName, timeZone } = useI18n
169
169
  | `INVALID_LOCALE` | an invalid BCP 47 tag; `switchLocale` without a router to a locale without messages |
170
170
  | `INVALID_FORMAT` | an unknown or invalid named number/date format |
171
171
  | `MISSING_MESSAGE` | an invalid key, or no message in the lookup chain with `missing: error` |
172
+ | `EXCLUDED_MESSAGE` | a key under the app's `pageExclude` (`settings.excluded`) with `missing: error`: the server leaves it out of this app's pages on purpose (D-0312) |
172
173
  | `INVALID_PARAMETER` | a missing parameter, a non-finite number, an invalid date or plural count |
173
174
  | `OUTPUT_LIMIT` | the rendered message (or a returned missing key) exceeds `maxOutputLength` |
174
175
  | `RECOVERY_FAILED` | the fetch of a first page's missing bundles got no Inertia answer, or one without them (handled: the document is loaded again without the cookie; it is the `error` of `I18N_PAGE_RECOVERY_FAILED`) |
package/dist/i18n.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { type App, type ComputedRef } from 'vue';
2
2
  import { BundleStore, type StoreHost } from './i18n-store.js';
3
- export type PageI18nErrorCode = 'UNAVAILABLE' | 'INVALID_CATALOG' | 'INVALID_LOCALE' | 'INVALID_FORMAT' | 'MISSING_MESSAGE' | 'INVALID_PARAMETER' | 'OUTPUT_LIMIT' | 'RECOVERY_FAILED';
3
+ export type PageI18nErrorCode = 'UNAVAILABLE' | 'INVALID_CATALOG' | 'INVALID_LOCALE' | 'INVALID_FORMAT' | 'MISSING_MESSAGE' | 'EXCLUDED_MESSAGE' | 'INVALID_PARAMETER' | 'OUTPUT_LIMIT' | 'RECOVERY_FAILED';
4
4
  /** A coded error of the browser runtime; the codes are the server translator's. */
5
5
  export declare class PageI18nError extends Error {
6
6
  readonly code: PageI18nErrorCode;
@@ -38,6 +38,11 @@ export interface PageI18nSettings {
38
38
  /** Longest X-Dokebi-I18n header the server reads (http.maxPageHeaderBytes). Default 4096. */
39
39
  readonly maxHeaderBytes?: number;
40
40
  readonly cache?: PageI18nCache;
41
+ /**
42
+ * D-0312: key prefixes this app's pages never receive (dokebi-i18n http.apps.<app>.pageExclude).
43
+ * A call of one is EXCLUDED_MESSAGE, not MISSING_MESSAGE.
44
+ */
45
+ readonly excluded?: readonly string[];
41
46
  }
42
47
  /** The shared page prop `i18n` of dokebi-i18n (D-0099, D-0126). */
43
48
  export interface PageI18nPayload {
package/dist/i18n.js CHANGED
@@ -129,6 +129,19 @@ function chainOf(locale, known, defaultLocale) {
129
129
  chain.push(defaultLocale);
130
130
  return chain;
131
131
  }
132
+ /**
133
+ * D-0312: the shortest pageExclude prefix that covers `key` (the key itself, or a part of it that
134
+ * ends at a dot), if any: one set lookup per dot, however long the list is.
135
+ */
136
+ function excludedBy(key, prefixes) {
137
+ for (let dot = key.indexOf('.');; dot = key.indexOf('.', dot + 1)) {
138
+ const part = dot < 0 ? key : key.slice(0, dot);
139
+ if (prefixes.has(part))
140
+ return part;
141
+ if (dot < 0)
142
+ return undefined;
143
+ }
144
+ }
132
145
  function namespaceOf(key) {
133
146
  const dot = key.indexOf('.');
134
147
  return dot < 0 ? key : key.slice(0, dot);
@@ -168,6 +181,10 @@ export class I18n {
168
181
  #formats = new Map();
169
182
  #defaults = new Map();
170
183
  #defaultCharacters = 0;
184
+ /** D-0312: the pageExclude prefixes already warned about (bounded by the settings' list). */
185
+ #warnedExcluded = new Set();
186
+ /** D-0312: `settings.excluded` as a set (the page's settings). */
187
+ #excludedSet;
171
188
  #settings;
172
189
  #logger;
173
190
  #feed;
@@ -248,6 +265,28 @@ export class I18n {
248
265
  this.#debug('Translation pending, key shown', { code: 'I18N_PAGE_PENDING', locale });
249
266
  return key;
250
267
  }
268
+ // D-0312: a key the server leaves out of this app's pages on purpose says so.
269
+ const excluded = this.#excludedSet && excludedBy(key, this.#excludedSet);
270
+ if (excluded) {
271
+ if (this.#settings.missing === 'error')
272
+ throw new PageI18nError('EXCLUDED_MESSAGE', `Message ${JSON.stringify(key)} is left out of this app's pages (pageExclude ${JSON.stringify(excluded)})`, `This app's pages do not receive keys under ${JSON.stringify(excluded)} (options.dokebi-i18n.http.apps.<app>.pageExclude): use another key on this page, or take the prefix out of pageExclude.`);
273
+ if (key.length > this.#settings.maxOutputLength)
274
+ throw new PageI18nError('OUTPUT_LIMIT', 'Missing key exceeds maxOutputLength');
275
+ // A page that calls a key it can never receive is a defect: warned once per prefix (an
276
+ // I18n lives as long as the document), then at debug like a missing key.
277
+ const fields = { code: 'I18N_EXCLUDED_KEY', locale, prefix: excluded };
278
+ if (!this.#warnedExcluded.has(excluded)) {
279
+ this.#warnedExcluded.add(excluded);
280
+ if (this.#logger && (this.#logger.isEnabled?.('warn') ?? true))
281
+ this.#logger.log('warn', 'Excluded translation returned as key', {
282
+ ...fields,
283
+ fix: `This app's pages do not receive keys under ${JSON.stringify(excluded)} (options.dokebi-i18n.http.apps.<app>.pageExclude).`,
284
+ });
285
+ }
286
+ else
287
+ this.#debug('Excluded translation returned as key', fields);
288
+ return key;
289
+ }
251
290
  if (this.#settings.missing === 'error')
252
291
  throw new PageI18nError('MISSING_MESSAGE', `No message ${JSON.stringify(key)} in the locale fallback chain of ${locale}`, `Add the key to src/shared/locales/<locale>/${key.split('.', 1)[0]}.json, or send that namespace to the page (options.dokebi-i18n.http.pageMessages, or pageMessages(request, '${key.split('.', 1)[0]}') in the route).`);
253
292
  if (key.length > this.#settings.maxOutputLength)
@@ -512,6 +551,10 @@ export class I18n {
512
551
  ...(settings.cache ? { cache: settings.cache } : {}),
513
552
  };
514
553
  this.#locales = Object.freeze([...settings.locales]);
554
+ this.#excludedSet =
555
+ Array.isArray(settings.excluded) && settings.excluded.length
556
+ ? new Set(settings.excluded.filter((prefix) => typeof prefix === 'string'))
557
+ : undefined;
515
558
  // Formats and the time zone may have changed with the version.
516
559
  this.#formats.clear();
517
560
  const cache = settings.cache;
package/dist/index.js CHANGED
@@ -1252,7 +1252,7 @@ class InertiaRuntime {
1252
1252
  }
1253
1253
  export const createPlugin = definePlugin({
1254
1254
  name: 'dokebi-template-inertia',
1255
- version: '0.4.30',
1255
+ version: '0.4.31',
1256
1256
  core: '^0.4.2',
1257
1257
  options,
1258
1258
  entry: parseEntry,
@@ -15,7 +15,7 @@ import { checkTemplateBundle, cspDeclarationProblem, jsonValue, PAGE_META_MAX_BY
15
15
  import { packageVersion } from './template-shared.js';
16
16
  import { registerVueTypeScript } from './vue-typescript.js';
17
17
  /** The builder written into `builtWith` (scripts/testing/check-metadata.mjs keeps it the package version). */
18
- const BUILDER = { name: 'dokebi-template-inertia', version: '0.4.30' };
18
+ const BUILDER = { name: 'dokebi-template-inertia', version: '0.4.31' };
19
19
  /** D-0296: carried files go under this folder of the bundle (served like the pages' files). */
20
20
  export const TEMPLATE_CARRY_FOLDER = 'browser';
21
21
  /** The folders Vite writes under browser/: a carried file never takes them (any case). */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dokebi/template-inertia",
3
- "version": "0.4.30",
3
+ "version": "0.4.31",
4
4
  "description": "Inertia v3 server adapter for Dokebi: page JSON, Vite manifest assets and optional server-side rendering.",
5
5
  "keywords": [
6
6
  "dokebi",
@@ -152,7 +152,7 @@
152
152
  ]
153
153
  },
154
154
  "devDependencies": {
155
- "@dokebi/i18n": "0.2.10",
155
+ "@dokebi/i18n": "0.2.11",
156
156
  "@dokebi/session": "0.4.11",
157
157
  "@dokebi/web": "0.4.28",
158
158
  "@dokebi/websocket": "0.4.10",