@dokebi/template-inertia 0.4.1 → 0.4.4

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/dist/i18n.d.ts ADDED
@@ -0,0 +1,216 @@
1
+ import { type App, type ComputedRef } from 'vue';
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';
4
+ /** A coded error of the browser runtime; the codes are the server translator's. */
5
+ export declare class PageI18nError extends Error {
6
+ readonly code: PageI18nErrorCode;
7
+ readonly plugin = "dokebi-template-inertia";
8
+ readonly fix: string;
9
+ constructor(code: PageI18nErrorCode, message: string, fix?: string, cause?: unknown);
10
+ }
11
+ export type PageMessage = string | Readonly<Partial<Record<Intl.LDMLPluralRule, string>> & {
12
+ other: string;
13
+ }>;
14
+ export type PageParameters = Readonly<Record<string, string | number>>;
15
+ /** How the browser keeps bundles (dokebi-i18n http.pageCache); absent on seal hosts and when off. */
16
+ export interface PageI18nCache {
17
+ readonly cookie: string;
18
+ readonly maxCookieBytes: number;
19
+ readonly maxAgeSeconds: number;
20
+ readonly maxBundles: number;
21
+ readonly maxStoreCharacters: number;
22
+ }
23
+ /** Settings the server sends once per catalog version (dokebi-i18n options and formats). */
24
+ export interface PageI18nSettings {
25
+ readonly locales: readonly string[];
26
+ readonly defaultLocale: string;
27
+ readonly missing: 'error' | 'key';
28
+ readonly timeZone: string;
29
+ readonly maxOutputLength: number;
30
+ /** Longest message key (dokebi-i18n maxKeyLength). Default 256. */
31
+ readonly maxKeyLength?: number;
32
+ /** Longest message or plural branch, a `t()` default included (maxBranchLength). Default 16384. */
33
+ readonly maxBranchLength?: number;
34
+ /** The preference cookie dokebi-i18n reads (http.cookieName). */
35
+ readonly cookie: string;
36
+ readonly numberFormats: Readonly<Record<string, Intl.NumberFormatOptions>>;
37
+ readonly dateFormats: Readonly<Record<string, Intl.DateTimeFormatOptions>>;
38
+ /** Longest X-Dokebi-I18n header the server reads (http.maxPageHeaderBytes). Default 4096. */
39
+ readonly maxHeaderBytes?: number;
40
+ readonly cache?: PageI18nCache;
41
+ }
42
+ /** The shared page prop `i18n` of dokebi-i18n (D-0099, D-0126). */
43
+ export interface PageI18nPayload {
44
+ readonly locale: string;
45
+ readonly version: string;
46
+ /** Message lookup chain of `locale`, `locale` first. */
47
+ readonly fallback: readonly string[];
48
+ /** Namespaces the page needs. */
49
+ readonly namespaces: readonly string[];
50
+ /** locale → flat key → message: the messages of the bundles in `bundles`. */
51
+ readonly messages?: Readonly<Record<string, Readonly<Record<string, PageMessage>>>>;
52
+ /** namespace → token of every bundle this answer carries. */
53
+ readonly bundles?: Readonly<Record<string, string>>;
54
+ /** namespace → token of the bundles the server left out because the cookie named them. */
55
+ readonly held?: Readonly<Record<string, string>>;
56
+ readonly settings?: PageI18nSettings;
57
+ }
58
+ /** The debug log of the runtime (D-0047); the core Logger and a console adapter fit. */
59
+ export interface PageI18nLogger {
60
+ isEnabled?(level: 'debug' | 'warn' | 'error'): boolean;
61
+ log(level: 'debug' | 'warn' | 'error', message: string, fields?: Record<string, unknown>): void;
62
+ }
63
+ /** Messages of one locale: flat keys (`auth.password.minimum`) or nested namespace objects. */
64
+ export interface MessageTree {
65
+ readonly [key: string]: PageMessage | MessageTree;
66
+ }
67
+ export interface CreateI18nOptions {
68
+ readonly locale: string;
69
+ /** locale → messages (flat dot keys or nested objects, as in locales/<locale>/<ns>.json). */
70
+ readonly messages: Readonly<Record<string, MessageTree>>;
71
+ /** Lookup chain of `locale` (default: `locale`, its parent tags with messages, `defaultLocale`). */
72
+ readonly fallback?: readonly string[];
73
+ /** Default: `locale`. */
74
+ readonly defaultLocale?: string;
75
+ readonly missing?: 'error' | 'key';
76
+ readonly timeZone?: string;
77
+ readonly maxOutputLength?: number;
78
+ /** Longest message key (dokebi-i18n maxKeyLength). Default 256. */
79
+ readonly maxKeyLength?: number;
80
+ /** Longest message or plural branch (dokebi-i18n maxBranchLength). Default 16384. */
81
+ readonly maxBranchLength?: number;
82
+ readonly numberFormats?: Readonly<Record<string, Intl.NumberFormatOptions>>;
83
+ readonly dateFormats?: Readonly<Record<string, Intl.DateTimeFormatOptions>>;
84
+ readonly logger?: PageI18nLogger;
85
+ }
86
+ /** The part of Inertia's `router` the page connector uses (`import { router } from '@inertiajs/vue3'`). */
87
+ export interface PageI18nRouter {
88
+ on(event: 'before', callback: (event: {
89
+ detail: {
90
+ visit: {
91
+ headers: Record<string, string>;
92
+ };
93
+ };
94
+ }) => void): unknown;
95
+ reload(options?: {
96
+ only?: readonly string[];
97
+ onFinish?: () => void;
98
+ }): void;
99
+ }
100
+ export interface PageI18nOptions {
101
+ /** Inertia's router: adds the held-messages header to visits, follows new pages, switches. */
102
+ readonly router?: PageI18nRouter;
103
+ readonly logger?: PageI18nLogger;
104
+ /**
105
+ * Where bundles are kept between page loads (D-0126). Default: the browser's localStorage when
106
+ * the server allows it (`settings.cache`); `false` keeps nothing (and names nothing).
107
+ */
108
+ readonly storage?: StoreHost['storage'] | false;
109
+ }
110
+ /** Request header naming what this browser holds: `v=<version>; h=<token><token>…`. */
111
+ export declare const PAGE_I18N_HEADER = "X-Dokebi-I18n";
112
+ /**
113
+ * @internal How pageI18n feeds an I18n: the payload of the page being shown (reactive in an
114
+ * Inertia app, read where it is used, so a new page's messages are there before it renders).
115
+ */
116
+ interface PageFeed {
117
+ attach(app: App): void;
118
+ current(): PageI18nPayload | undefined;
119
+ /** A payload was taken in; true when its missing messages are being fetched. */
120
+ absorbed(payload: PageI18nPayload, complete: boolean): boolean;
121
+ }
122
+ /**
123
+ * One app's translations: `t`, `n`, `d` and `has` are bound (destructure them from useI18n()),
124
+ * `locale` and `locales` are read-only refs, so templates re-render when the language changes.
125
+ */
126
+ export declare class I18n {
127
+ #private;
128
+ readonly locale: ComputedRef<string>;
129
+ readonly locales: ComputedRef<readonly string[]>;
130
+ constructor(options: CreateI18nOptions);
131
+ /** Translates `key` with the lookup chain; with `defaultValue`, only the locale's own message wins. */
132
+ readonly t: {
133
+ (key: string, parameters?: PageParameters): string;
134
+ (key: string, defaultValue: string, parameters?: PageParameters): string;
135
+ };
136
+ /** True when the lookup chain of the current locale has `key`. */
137
+ readonly has: (key: string) => boolean;
138
+ /** A finite number with a named format of the dokebi-i18n entry (`default`: Intl's). */
139
+ readonly n: (value: number, format?: string) => string;
140
+ /**
141
+ * The application's time zone of this page's settings (D-0178: it follows i18n setTimeZone on
142
+ * the server): pass it to the @dokebi/i18n/calendar helpers so pages count days as the server does.
143
+ */
144
+ readonly timeZone: () => string;
145
+ /** A Date or epoch milliseconds with a named format, in the configured time zone. */
146
+ readonly d: (value: Date | number, format?: string) => string;
147
+ /** The language's own name (`English`, `Français`) for a language switch. */
148
+ readonly localeName: (locale: string) => string;
149
+ /**
150
+ * Shows another language. On an Inertia page (pageI18n) it writes the preference cookie and
151
+ * reloads the page's props from the server (no document reload); otherwise the messages of
152
+ * `locale` must be loaded already.
153
+ */
154
+ readonly switchLocale: (locale: string) => Promise<void>;
155
+ /** Vue plugin: provides this instance to useI18n() and `$t`, `$n`, `$d` to templates. */
156
+ install(app: App): void;
157
+ /** @internal pageI18n's connection: the page payloads, the language switch and the store. */
158
+ connect(feed: PageFeed, switcher: ((locale: string) => Promise<void>) | undefined, store: StoreHost | undefined): void;
159
+ /**
160
+ * @internal The value of X-Dokebi-I18n: the catalog version and the tokens of the bundles this
161
+ * document holds (the shown locale's first), within the server's header limit.
162
+ */
163
+ heldHeader(): string;
164
+ /** @internal */
165
+ get cookie(): string | undefined;
166
+ /** @internal The claim cookie of the store (settings.cache.cookie), if bundles are kept. */
167
+ get claimCookie(): string | undefined;
168
+ /** @internal The store, for pageI18n and tests (undefined when nothing is kept). */
169
+ get store(): BundleStore | undefined;
170
+ /**
171
+ * @internal Takes in the `i18n` of a recovery answer (pageI18n's fetch) and checks the shown
172
+ * page again; true when nothing is missing now.
173
+ */
174
+ recovered(payload: PageI18nPayload): boolean;
175
+ }
176
+ /** An I18n for plain Vue apps and tests: the messages are given, nothing is fetched. */
177
+ export declare function createI18n(options: CreateI18nOptions): I18n;
178
+ /**
179
+ * The page's I18n (from a component's setup). Without the `i18n` prop (no @dokebi/i18n, or
180
+ * http integration off) it throws UNAVAILABLE; `{ optional: true }` returns undefined instead.
181
+ */
182
+ export declare function useI18n(): I18n;
183
+ export declare function useI18n(options: {
184
+ optional: true;
185
+ }): I18n | undefined;
186
+ interface PageLike {
187
+ readonly props?: Readonly<Record<string, unknown>>;
188
+ readonly component?: string;
189
+ readonly version?: string | null;
190
+ readonly url?: string;
191
+ }
192
+ /**
193
+ * The Vue plugin of an Inertia app (views/app.js and views/ssr.js install it): translations from
194
+ * the `i18n` prop of the page being shown (Inertia's `$page`, from `page` until Inertia's plugin
195
+ * is installed). A new page's messages are taken in where the page first translates, before it
196
+ * is drawn. With `router` (the browser), every visit names what this document holds (header
197
+ * X-Dokebi-I18n) so the server sends only what is new, a page whose messages this document never
198
+ * received is completed with `router.reload({ only: ['i18n'] })`, and switchLocale writes the
199
+ * preference cookie and reloads the props. In a browser it keeps the bundles it received
200
+ * (D-0126) when the server allows it, and when a first page lacks some (the server trusted a
201
+ * cookie the store does not match) it fetches them before `app.mount` mounts the app. Without
202
+ * the prop it installs nothing, so the same entry works before @dokebi/i18n is added.
203
+ */
204
+ export declare function pageI18n(page: PageLike | undefined, options?: PageI18nOptions): I18n | {
205
+ install(_app: App): void;
206
+ };
207
+ declare module 'vue' {
208
+ interface ComponentCustomProperties {
209
+ /** `t` of the page's I18n (@dokebi/template-inertia/i18n). */
210
+ $t: I18n['t'];
211
+ $n: I18n['n'];
212
+ $d: I18n['d'];
213
+ $i18n: I18n;
214
+ }
215
+ }
216
+ export {};