@dokebi/i18n 0.0.0-stage → 0.2.1
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 +236 -0
- package/LICENSE +21 -0
- package/README.md +18 -2
- package/dist/calendar.d.ts +78 -0
- package/dist/calendar.js +318 -0
- package/dist/catalogs.d.ts +33 -0
- package/dist/catalogs.js +165 -0
- package/dist/contract.d.ts +47 -0
- package/dist/contract.js +1 -0
- package/dist/dependency.d.ts +3 -0
- package/dist/dependency.js +8 -0
- package/dist/errors.d.ts +5 -0
- package/dist/errors.js +17 -0
- package/dist/http.d.ts +31 -0
- package/dist/http.js +206 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +6 -0
- package/dist/install.d.ts +31 -0
- package/dist/install.js +29 -0
- package/dist/layers.d.ts +19 -0
- package/dist/layers.js +57 -0
- package/dist/message.d.ts +23 -0
- package/dist/message.js +103 -0
- package/dist/negotiation.d.ts +23 -0
- package/dist/negotiation.js +165 -0
- package/dist/options.d.ts +42 -0
- package/dist/options.js +211 -0
- package/dist/page.d.ts +93 -0
- package/dist/page.js +572 -0
- package/dist/plugin.d.ts +15 -0
- package/dist/plugin.js +92 -0
- package/dist/service.d.ts +27 -0
- package/dist/service.js +359 -0
- package/dist/values.d.ts +12 -0
- package/dist/values.js +65 -0
- package/options.schema.json +250 -0
- package/package.json +100 -4
- package/templates/extension.ts +7 -0
- package/templates/locales/en/common.json +7 -0
- package/templates/upgrade-extension.ts +12 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { I18nError } from './errors.js';
|
|
2
|
+
// RFC 4647 language range with an optional RFC 9110 weight; other parameters make a field malformed.
|
|
3
|
+
const RANGE = /^\s*(\*|[a-z]{1,8}(?:-[a-z0-9]{1,8})*)\s*(?:;\s*q=(0(?:\.\d{0,3})?|1(?:\.0{0,3})?))?\s*$/i;
|
|
4
|
+
/** The option defaults, for a Negotiator made without options. */
|
|
5
|
+
export const NEGOTIATION_LIMITS = Object.freeze({
|
|
6
|
+
maxLength: 4096,
|
|
7
|
+
maxRanges: 32,
|
|
8
|
+
});
|
|
9
|
+
const byQuality = (a, b) => b.quality - a.quality;
|
|
10
|
+
/** A range matches its own tag, its descendants (`en` matches `en-us`) and `*` matches all. */
|
|
11
|
+
function matches(language, tag) {
|
|
12
|
+
return (language === '*' ||
|
|
13
|
+
tag === language ||
|
|
14
|
+
(tag.length > language.length &&
|
|
15
|
+
tag.charCodeAt(language.length) === 45 &&
|
|
16
|
+
tag.startsWith(language)));
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Accept-Language negotiation over one fixed locale list. Lowercased tags are prepared once; a call
|
|
20
|
+
* allocates only its parsed ranges. Calls are synchronous, so the per-locale scratch is reused.
|
|
21
|
+
*/
|
|
22
|
+
export class Negotiator {
|
|
23
|
+
#locales;
|
|
24
|
+
#tags;
|
|
25
|
+
#fallback;
|
|
26
|
+
/** Per call: the most specific range matching each locale. */
|
|
27
|
+
#best;
|
|
28
|
+
/** Per call: the locale was excluded by its most specific range (q=0). */
|
|
29
|
+
#excluded;
|
|
30
|
+
/** Per range: locales already tried for the current range. */
|
|
31
|
+
#tried;
|
|
32
|
+
#limits;
|
|
33
|
+
constructor(locales, fallback, limits = NEGOTIATION_LIMITS) {
|
|
34
|
+
this.#limits = limits;
|
|
35
|
+
this.#locales = locales;
|
|
36
|
+
this.#tags = locales.map((locale) => locale.toLowerCase());
|
|
37
|
+
this.#fallback = locales.indexOf(fallback);
|
|
38
|
+
this.#best = new Array(locales.length).fill(undefined);
|
|
39
|
+
this.#excluded = new Uint8Array(locales.length);
|
|
40
|
+
this.#tried = new Uint8Array(locales.length);
|
|
41
|
+
}
|
|
42
|
+
/** The public contract: input past the limits is a coded error; undefined when all are excluded. */
|
|
43
|
+
negotiate(header) {
|
|
44
|
+
if (header === undefined || header === '')
|
|
45
|
+
return this.#locales[this.#fallback];
|
|
46
|
+
const ranges = this.#parse(header);
|
|
47
|
+
if (!ranges)
|
|
48
|
+
throw new I18nError('INVALID_HEADER', `Accept-Language must be text of at most ${this.#limits.maxLength} characters and ${this.#limits.maxRanges} ranges (maxAcceptLanguageLength, maxAcceptLanguageRanges)`);
|
|
49
|
+
return this.#select(ranges);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* For the automatic HTTP adapter, where a page always gets a language: an all-excluded header
|
|
53
|
+
* selects the fallback. undefined means the client-controlled header is past the limits and
|
|
54
|
+
* carries no usable preference; the caller decides (and logs) what to use instead.
|
|
55
|
+
*/
|
|
56
|
+
prefer(header) {
|
|
57
|
+
if (header === undefined || header === '')
|
|
58
|
+
return this.#locales[this.#fallback];
|
|
59
|
+
const ranges = this.#parse(header);
|
|
60
|
+
if (!ranges)
|
|
61
|
+
return undefined;
|
|
62
|
+
return this.#select(ranges) ?? this.#locales[this.#fallback];
|
|
63
|
+
}
|
|
64
|
+
#parse(header) {
|
|
65
|
+
if (typeof header !== 'string' || header.length > this.#limits.maxLength)
|
|
66
|
+
return undefined;
|
|
67
|
+
const fields = header.split(',', this.#limits.maxRanges + 1);
|
|
68
|
+
if (fields.length > this.#limits.maxRanges)
|
|
69
|
+
return undefined;
|
|
70
|
+
const ranges = [];
|
|
71
|
+
for (const field of fields) {
|
|
72
|
+
const match = RANGE.exec(field);
|
|
73
|
+
// Malformed fields are ignored.
|
|
74
|
+
if (match?.[1])
|
|
75
|
+
ranges.push({
|
|
76
|
+
language: match[1].toLowerCase(),
|
|
77
|
+
quality: match[2] === undefined ? 1 : Number(match[2]),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return ranges;
|
|
81
|
+
}
|
|
82
|
+
#select(ranges) {
|
|
83
|
+
const tags = this.#tags;
|
|
84
|
+
const best = this.#best;
|
|
85
|
+
const excluded = this.#excluded;
|
|
86
|
+
// A more specific range overrides an exclusion (or the weight) of its parent/wildcard.
|
|
87
|
+
for (let index = 0; index < tags.length; index++) {
|
|
88
|
+
const tag = tags[index];
|
|
89
|
+
let chosen;
|
|
90
|
+
for (const range of ranges) {
|
|
91
|
+
if (!matches(range.language, tag))
|
|
92
|
+
continue;
|
|
93
|
+
if (!chosen ||
|
|
94
|
+
range.language.length > chosen.language.length ||
|
|
95
|
+
(range.language.length === chosen.language.length && range.quality > chosen.quality))
|
|
96
|
+
chosen = range;
|
|
97
|
+
}
|
|
98
|
+
best[index] = chosen;
|
|
99
|
+
excluded[index] = chosen?.quality === 0 ? 1 : 0;
|
|
100
|
+
}
|
|
101
|
+
let selected = -1;
|
|
102
|
+
let quality = 0;
|
|
103
|
+
// Stable: equal weights keep the header order.
|
|
104
|
+
for (const range of ranges.sort(byQuality)) {
|
|
105
|
+
// Sorted by weight: no later range can beat the current selection.
|
|
106
|
+
if (range.quality <= quality)
|
|
107
|
+
break;
|
|
108
|
+
this.#tried.fill(0);
|
|
109
|
+
for (;;) {
|
|
110
|
+
const match = range.language === '*' ? this.#any() : this.#lookup(range.language);
|
|
111
|
+
if (match < 0)
|
|
112
|
+
break;
|
|
113
|
+
const explicit = best[match];
|
|
114
|
+
const score = explicit && explicit.language.length > range.language.length
|
|
115
|
+
? Math.min(range.quality, explicit.quality)
|
|
116
|
+
: range.quality;
|
|
117
|
+
if (score > quality) {
|
|
118
|
+
selected = match;
|
|
119
|
+
quality = score;
|
|
120
|
+
}
|
|
121
|
+
// A more specific low weight must not be promoted by a broad range.
|
|
122
|
+
// Other descendants may still have the full weight of this preference.
|
|
123
|
+
if (score === range.quality)
|
|
124
|
+
break;
|
|
125
|
+
this.#tried[match] = 1;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (selected >= 0)
|
|
129
|
+
return this.#locales[selected];
|
|
130
|
+
// Unmatched preferences: the fallback or the first locale that is not excluded.
|
|
131
|
+
this.#tried.fill(0);
|
|
132
|
+
const fallback = this.#any();
|
|
133
|
+
return fallback < 0 ? undefined : this.#locales[fallback];
|
|
134
|
+
}
|
|
135
|
+
#open(index) {
|
|
136
|
+
return this.#excluded[index] === 0 && this.#tried[index] === 0;
|
|
137
|
+
}
|
|
138
|
+
/** The fallback when still open, else the first open locale in registration order. */
|
|
139
|
+
#any() {
|
|
140
|
+
if (this.#fallback >= 0 && this.#open(this.#fallback))
|
|
141
|
+
return this.#fallback;
|
|
142
|
+
for (let index = 0; index < this.#tags.length; index++)
|
|
143
|
+
if (this.#open(index))
|
|
144
|
+
return index;
|
|
145
|
+
return -1;
|
|
146
|
+
}
|
|
147
|
+
/** RFC 4647 lookup over open locales: exact tag, then a descendant, then shorter ranges. */
|
|
148
|
+
#lookup(range) {
|
|
149
|
+
const tags = this.#tags;
|
|
150
|
+
let candidate = range;
|
|
151
|
+
while (candidate) {
|
|
152
|
+
for (let index = 0; index < tags.length; index++)
|
|
153
|
+
if (tags[index] === candidate && this.#open(index))
|
|
154
|
+
return index;
|
|
155
|
+
for (let index = 0; index < tags.length; index++)
|
|
156
|
+
if (this.#open(index) && matches(candidate, tags[index]))
|
|
157
|
+
return index;
|
|
158
|
+
candidate = candidate.slice(0, Math.max(0, candidate.lastIndexOf('-')));
|
|
159
|
+
// A trailing singleton (`x`, `u`) never stands alone.
|
|
160
|
+
if (candidate.length - candidate.lastIndexOf('-') - 1 === 1)
|
|
161
|
+
candidate = candidate.slice(0, Math.max(0, candidate.lastIndexOf('-')));
|
|
162
|
+
}
|
|
163
|
+
return -1;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type OptionsOf } from '@dokebi/core';
|
|
2
|
+
export declare const options: import("@dokebi/core").OptionsDeclaration<{
|
|
3
|
+
readonly defaultLocale: string;
|
|
4
|
+
readonly http: {
|
|
5
|
+
readonly cookieName: string;
|
|
6
|
+
readonly enabled: boolean;
|
|
7
|
+
readonly maxCookieHeaderBytes: number;
|
|
8
|
+
readonly maxJoinedSets: number;
|
|
9
|
+
readonly maxPageHeaderBytes: number;
|
|
10
|
+
readonly pageCache: {
|
|
11
|
+
readonly cookieName: string;
|
|
12
|
+
readonly enabled: boolean;
|
|
13
|
+
readonly maxAgeSeconds: number;
|
|
14
|
+
readonly maxBundles: number;
|
|
15
|
+
readonly maxCookieBytes: number;
|
|
16
|
+
readonly maxStoreCharacters: number;
|
|
17
|
+
};
|
|
18
|
+
readonly pageMessages: readonly string[];
|
|
19
|
+
readonly pathLocale: boolean;
|
|
20
|
+
};
|
|
21
|
+
readonly maxAcceptLanguageLength: number;
|
|
22
|
+
readonly maxAcceptLanguageRanges: number;
|
|
23
|
+
readonly maxBranchLength: number;
|
|
24
|
+
readonly maxCatalogBytes: number;
|
|
25
|
+
readonly maxCatalogCharacters: number;
|
|
26
|
+
readonly maxDefaultCharacters: number;
|
|
27
|
+
readonly maxDefaultTemplates: number;
|
|
28
|
+
readonly maxKeyLength: number;
|
|
29
|
+
readonly maxLocales: number;
|
|
30
|
+
readonly maxMessages: number;
|
|
31
|
+
readonly maxNamedFormats: number;
|
|
32
|
+
readonly maxOutputLength: number;
|
|
33
|
+
readonly missing: "auto" | "error" | "key";
|
|
34
|
+
readonly timeZone: string;
|
|
35
|
+
}>;
|
|
36
|
+
export type I18nOptions = OptionsOf<typeof options>;
|
|
37
|
+
/**
|
|
38
|
+
* The missing-message policy in effect: `auto` is `key` in an app `npx dokebi dev` runs
|
|
39
|
+
* (DOKEBI_DEV=1), so a missing translation shows its key instead of breaking the page while it is
|
|
40
|
+
* being written (community request A2), and `error` everywhere else, tests and production included.
|
|
41
|
+
*/
|
|
42
|
+
export declare function missingPolicy(missing: 'auto' | 'error' | 'key'): 'error' | 'key';
|
package/dist/options.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { defineOptions, option as o } from '@dokebi/core';
|
|
2
|
+
export const options = defineOptions({
|
|
3
|
+
http: o.object({
|
|
4
|
+
enabled: o.boolean({
|
|
5
|
+
default: false,
|
|
6
|
+
description: 'Attach request translators and page locale when dokebi-web is installed.',
|
|
7
|
+
}),
|
|
8
|
+
cookieName: o.string({
|
|
9
|
+
default: 'dokebi-lang',
|
|
10
|
+
minLength: 1,
|
|
11
|
+
maxLength: 128,
|
|
12
|
+
pattern: '^[A-Za-z0-9_-]+$',
|
|
13
|
+
description: 'Locale preference cookie; only configured locale values are accepted.',
|
|
14
|
+
}),
|
|
15
|
+
pageMessages: o.array(o.string({
|
|
16
|
+
pattern: '^(?:\\*|[a-zA-Z][a-zA-Z0-9_-]*)$',
|
|
17
|
+
maxLength: 128,
|
|
18
|
+
description: 'A catalog namespace, or * for every namespace.',
|
|
19
|
+
}), {
|
|
20
|
+
default: ['*'],
|
|
21
|
+
maxItems: 64,
|
|
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
|
+
}),
|
|
24
|
+
maxPageHeaderBytes: o.integer({
|
|
25
|
+
default: 4096,
|
|
26
|
+
minimum: 256,
|
|
27
|
+
maximum: 16384,
|
|
28
|
+
description: 'Longest X-Dokebi-I18n request header (what an Inertia visit holds) that is read; a longer one counts as holding nothing (D-0126).',
|
|
29
|
+
}),
|
|
30
|
+
pathLocale: o.boolean({
|
|
31
|
+
default: false,
|
|
32
|
+
description: 'A first path segment that is a configured locale (/ja/p/1) decides the request locale: Content-Language says it and the answer varies by its URL only (no Vary: Accept-Language, Cookie), so shared caches keep it. A route parameter does the same with useUrlLocale(request, reply, locale).',
|
|
33
|
+
}),
|
|
34
|
+
maxCookieHeaderBytes: o.integer({
|
|
35
|
+
default: 16384,
|
|
36
|
+
minimum: 256,
|
|
37
|
+
maximum: 65536,
|
|
38
|
+
description: 'Longest Cookie request header read for the preference cookie and the page cache claim; a longer one counts as holding neither (the request goes on).',
|
|
39
|
+
}),
|
|
40
|
+
maxJoinedSets: o.integer({
|
|
41
|
+
default: 256,
|
|
42
|
+
minimum: 0,
|
|
43
|
+
maximum: 65536,
|
|
44
|
+
description: 'Namespace sets (per locale) whose joined page messages are kept for later pages, the least recently used dropped first; 0 joins on every page (D-0126).',
|
|
45
|
+
}),
|
|
46
|
+
pageCache: o.object({
|
|
47
|
+
enabled: o.boolean({
|
|
48
|
+
default: true,
|
|
49
|
+
description: 'The browser runtime keeps the message bundles it received (localStorage) and names them in a cookie, so a full page load omits them from the HTML (D-0126). Never on seal hosts.',
|
|
50
|
+
}),
|
|
51
|
+
cookieName: o.string({
|
|
52
|
+
default: 'dokebi-i18n-held',
|
|
53
|
+
minLength: 1,
|
|
54
|
+
maxLength: 128,
|
|
55
|
+
pattern: '^[A-Za-z0-9_-]+$',
|
|
56
|
+
description: 'Cookie that names the kept bundles by content hash (no message text).',
|
|
57
|
+
}),
|
|
58
|
+
maxCookieBytes: o.integer({
|
|
59
|
+
default: 1024,
|
|
60
|
+
minimum: 64,
|
|
61
|
+
maximum: 3800,
|
|
62
|
+
description: 'Longest value of that cookie (8 characters per bundle); the browser names its newest bundles of the current locale first. A longer cookie is ignored.',
|
|
63
|
+
}),
|
|
64
|
+
maxAgeSeconds: o.integer({
|
|
65
|
+
default: 31_536_000,
|
|
66
|
+
minimum: 60,
|
|
67
|
+
maximum: 34_560_000,
|
|
68
|
+
description: 'Max-Age of that cookie.',
|
|
69
|
+
}),
|
|
70
|
+
maxBundles: o.integer({
|
|
71
|
+
default: 512,
|
|
72
|
+
minimum: 1,
|
|
73
|
+
maximum: 4096,
|
|
74
|
+
description: 'Bundles (locale × namespace, one version each) the browser keeps at most; the oldest of other locales go first.',
|
|
75
|
+
}),
|
|
76
|
+
maxStoreCharacters: o.integer({
|
|
77
|
+
default: 1_048_576,
|
|
78
|
+
minimum: 65_536,
|
|
79
|
+
maximum: 4_194_304,
|
|
80
|
+
description: 'UTF-16 code units of kept bundles the browser stores at most (localStorage holds about 5 MB per origin).',
|
|
81
|
+
}),
|
|
82
|
+
}, {
|
|
83
|
+
default: {
|
|
84
|
+
enabled: true,
|
|
85
|
+
cookieName: 'dokebi-i18n-held',
|
|
86
|
+
maxCookieBytes: 1024,
|
|
87
|
+
maxAgeSeconds: 31_536_000,
|
|
88
|
+
maxBundles: 512,
|
|
89
|
+
maxStoreCharacters: 1_048_576,
|
|
90
|
+
},
|
|
91
|
+
description: 'Browser-kept message bundles (D-0126): what a full page load can leave out of the HTML.',
|
|
92
|
+
}),
|
|
93
|
+
}, {
|
|
94
|
+
default: {
|
|
95
|
+
enabled: false,
|
|
96
|
+
cookieName: 'dokebi-lang',
|
|
97
|
+
pageMessages: ['*'],
|
|
98
|
+
maxPageHeaderBytes: 4096,
|
|
99
|
+
pathLocale: false,
|
|
100
|
+
maxCookieHeaderBytes: 16384,
|
|
101
|
+
maxJoinedSets: 256,
|
|
102
|
+
pageCache: {
|
|
103
|
+
enabled: true,
|
|
104
|
+
cookieName: 'dokebi-i18n-held',
|
|
105
|
+
maxCookieBytes: 1024,
|
|
106
|
+
maxAgeSeconds: 31_536_000,
|
|
107
|
+
maxBundles: 512,
|
|
108
|
+
maxStoreCharacters: 1_048_576,
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
description: 'Optional HTTP integration. No hooks are installed when disabled.',
|
|
112
|
+
}),
|
|
113
|
+
defaultLocale: o.string({
|
|
114
|
+
default: 'en',
|
|
115
|
+
minLength: 2,
|
|
116
|
+
maxLength: 128,
|
|
117
|
+
description: 'Default BCP 47 locale; its catalog must exist.',
|
|
118
|
+
}),
|
|
119
|
+
missing: o.enum(['auto', 'error', 'key'], {
|
|
120
|
+
default: 'auto',
|
|
121
|
+
description: 'Behavior when a key is absent from the entire fallback chain: error (MISSING_MESSAGE), key (the key is shown), or auto: key with a warning under npx dokebi dev, error everywhere else (tests, start, production).',
|
|
122
|
+
}),
|
|
123
|
+
timeZone: o.string({
|
|
124
|
+
default: 'UTC',
|
|
125
|
+
minLength: 1,
|
|
126
|
+
maxLength: 128,
|
|
127
|
+
description: 'Default Intl time zone for named date formats.',
|
|
128
|
+
}),
|
|
129
|
+
maxOutputLength: o.integer({
|
|
130
|
+
default: 65536,
|
|
131
|
+
minimum: 1,
|
|
132
|
+
maximum: 1048576,
|
|
133
|
+
description: 'Maximum rendered message length in UTF-16 code units.',
|
|
134
|
+
}),
|
|
135
|
+
maxMessages: o.integer({
|
|
136
|
+
default: 10000,
|
|
137
|
+
minimum: 1,
|
|
138
|
+
maximum: 1_000_000,
|
|
139
|
+
description: 'Messages of every locale catalog together at most (D-0114); more fails the start with INVALID_CATALOG. Applies to catalogs from loadCatalogs too (D-0118).',
|
|
140
|
+
}),
|
|
141
|
+
maxCatalogCharacters: o.integer({
|
|
142
|
+
default: 4_194_304,
|
|
143
|
+
minimum: 1024,
|
|
144
|
+
maximum: 268_435_456,
|
|
145
|
+
description: 'Compiled message text of every catalog together at most, in UTF-16 code units (D-0114); more fails the start with INVALID_CATALOG.',
|
|
146
|
+
}),
|
|
147
|
+
maxCatalogBytes: o.integer({
|
|
148
|
+
default: 4_194_304,
|
|
149
|
+
minimum: 1024,
|
|
150
|
+
maximum: 1_073_741_824,
|
|
151
|
+
description: 'Bytes of the namespace files loadCatalogs read, every locale together, at most; more fails the start with INVALID_CATALOG (D-0118). Inline catalogs have no files.',
|
|
152
|
+
}),
|
|
153
|
+
maxBranchLength: o.integer({
|
|
154
|
+
default: 16384,
|
|
155
|
+
minimum: 16,
|
|
156
|
+
maximum: 1_048_576,
|
|
157
|
+
description: 'One message (or one plural branch) of a catalog or an explicit default at most, in UTF-16 code units; longer fails the start (INVALID_CATALOG).',
|
|
158
|
+
}),
|
|
159
|
+
maxKeyLength: o.integer({
|
|
160
|
+
default: 256,
|
|
161
|
+
minimum: 16,
|
|
162
|
+
maximum: 4096,
|
|
163
|
+
description: 'Message keys (flat dot paths) at most, in characters; a longer catalog key fails the start, a longer key asked of t() is MISSING_MESSAGE.',
|
|
164
|
+
}),
|
|
165
|
+
maxNamedFormats: o.integer({
|
|
166
|
+
default: 31,
|
|
167
|
+
minimum: 0,
|
|
168
|
+
maximum: 1024,
|
|
169
|
+
description: 'Named number formats, and named date formats, of the entry at most (each; default is reserved); more fails the start (INVALID_FORMAT).',
|
|
170
|
+
}),
|
|
171
|
+
maxDefaultTemplates: o.integer({
|
|
172
|
+
default: 64,
|
|
173
|
+
minimum: 0,
|
|
174
|
+
maximum: 65_536,
|
|
175
|
+
description: 'Explicit t() default texts kept compiled per locale; more are compiled on every call without being kept.',
|
|
176
|
+
}),
|
|
177
|
+
maxDefaultCharacters: o.integer({
|
|
178
|
+
default: 65_536,
|
|
179
|
+
minimum: 0,
|
|
180
|
+
maximum: 16_777_216,
|
|
181
|
+
description: 'UTF-16 code units of the explicit t() default texts kept compiled per locale, at most.',
|
|
182
|
+
}),
|
|
183
|
+
maxAcceptLanguageLength: o.integer({
|
|
184
|
+
default: 4096,
|
|
185
|
+
minimum: 64,
|
|
186
|
+
maximum: 65_536,
|
|
187
|
+
description: 'Longest Accept-Language value negotiate() reads, in characters; a longer one is INVALID_HEADER (the HTTP adapter uses the default locale instead).',
|
|
188
|
+
}),
|
|
189
|
+
maxAcceptLanguageRanges: o.integer({
|
|
190
|
+
default: 32,
|
|
191
|
+
minimum: 1,
|
|
192
|
+
maximum: 1024,
|
|
193
|
+
description: 'Comma-separated ranges of an Accept-Language value negotiate() reads at most; more is INVALID_HEADER (the HTTP adapter uses the default locale instead).',
|
|
194
|
+
}),
|
|
195
|
+
maxLocales: o.integer({
|
|
196
|
+
default: 32,
|
|
197
|
+
minimum: 1,
|
|
198
|
+
maximum: 1024,
|
|
199
|
+
description: 'Locale catalogs at most (D-0114); more fails the start with INVALID_CATALOG. Applies to catalogs from loadCatalogs too (D-0118).',
|
|
200
|
+
}),
|
|
201
|
+
});
|
|
202
|
+
/**
|
|
203
|
+
* The missing-message policy in effect: `auto` is `key` in an app `npx dokebi dev` runs
|
|
204
|
+
* (DOKEBI_DEV=1), so a missing translation shows its key instead of breaking the page while it is
|
|
205
|
+
* being written (community request A2), and `error` everywhere else, tests and production included.
|
|
206
|
+
*/
|
|
207
|
+
export function missingPolicy(missing) {
|
|
208
|
+
if (missing !== 'auto')
|
|
209
|
+
return missing;
|
|
210
|
+
return process.env.DOKEBI_DEV === '1' ? 'key' : 'error';
|
|
211
|
+
}
|
package/dist/page.d.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { Logger } from '@dokebi/core';
|
|
2
|
+
import type { Message } from './contract.js';
|
|
3
|
+
import { type I18nOptions } from './options.js';
|
|
4
|
+
/** Request header of the browser runtime: what it holds, `v=<version>; h=<token><token>…`. */
|
|
5
|
+
export declare const PAGE_HEADER = "x-dokebi-i18n";
|
|
6
|
+
/** Characters of a bundle token (base64url of a SHA-256 prefix, 48 bits). */
|
|
7
|
+
export declare const PAGE_TOKEN_LENGTH = 8;
|
|
8
|
+
/**
|
|
9
|
+
* Where SSR finds the bundles a full load leaves out (a process-wide map, also across package
|
|
10
|
+
* copies): `<token>:<locale>:<namespace>` → messages. The runtime reads it when it renders on the
|
|
11
|
+
* server; a browser never has it.
|
|
12
|
+
*/
|
|
13
|
+
export declare const PAGE_BUNDLES: unique symbol;
|
|
14
|
+
/**
|
|
15
|
+
* Set on a request whose page left bundles out because the pageCache cookie named them: that
|
|
16
|
+
* answer depends on the Cookie header, also when its URL names the locale (http.ts says
|
|
17
|
+
* `Vary: Cookie` then, so a shared cache never hands it to a browser without those bundles).
|
|
18
|
+
*/
|
|
19
|
+
export declare const COOKIE_CLAIMED: unique symbol;
|
|
20
|
+
/** How the browser keeps bundles (http.pageCache); absent when it must not (off, or a seal host). */
|
|
21
|
+
export interface PageI18nCache {
|
|
22
|
+
/** Cookie that names the kept bundles by token. */
|
|
23
|
+
readonly cookie: string;
|
|
24
|
+
readonly maxCookieBytes: number;
|
|
25
|
+
readonly maxAgeSeconds: number;
|
|
26
|
+
readonly maxBundles: number;
|
|
27
|
+
readonly maxStoreCharacters: number;
|
|
28
|
+
}
|
|
29
|
+
/** Settings the browser needs to translate as the server does; sent once per catalog version. */
|
|
30
|
+
export interface PageI18nSettings {
|
|
31
|
+
readonly locales: readonly string[];
|
|
32
|
+
readonly defaultLocale: string;
|
|
33
|
+
readonly missing: 'error' | 'key';
|
|
34
|
+
readonly timeZone: string;
|
|
35
|
+
readonly maxOutputLength: number;
|
|
36
|
+
/** Longest message key `t()` takes (maxKeyLength): the browser refuses past it as the server. */
|
|
37
|
+
readonly maxKeyLength: number;
|
|
38
|
+
/** Longest message or plural branch, a `t()` default included (maxBranchLength). */
|
|
39
|
+
readonly maxBranchLength: number;
|
|
40
|
+
readonly cookie: string;
|
|
41
|
+
readonly numberFormats: Readonly<Record<string, Readonly<Record<string, unknown>>>>;
|
|
42
|
+
readonly dateFormats: Readonly<Record<string, Readonly<Record<string, unknown>>>>;
|
|
43
|
+
/** Longest X-Dokebi-I18n header the server reads (http.maxPageHeaderBytes). */
|
|
44
|
+
readonly maxHeaderBytes: number;
|
|
45
|
+
readonly cache?: PageI18nCache;
|
|
46
|
+
}
|
|
47
|
+
/** The `i18n` page prop (D-0099, D-0126). Field names are the protocol both packages document. */
|
|
48
|
+
export interface PageI18nPayload {
|
|
49
|
+
readonly locale: string;
|
|
50
|
+
/** Catalog version: a hash of every catalog, format and setting; a new one brings `settings`. */
|
|
51
|
+
readonly version: string;
|
|
52
|
+
/** The message lookup chain of `locale`, `locale` first. */
|
|
53
|
+
readonly fallback: readonly string[];
|
|
54
|
+
/** The namespaces this page needs; the browser holds them after this page. */
|
|
55
|
+
readonly namespaces: readonly string[];
|
|
56
|
+
/** Locale → flat key → message: the messages of the bundles in `bundles`. */
|
|
57
|
+
readonly messages?: Readonly<Record<string, Readonly<Record<string, Message>>>>;
|
|
58
|
+
/** Namespace → token of every bundle this answer carries (an empty bundle has no messages). */
|
|
59
|
+
readonly bundles?: Readonly<Record<string, string>>;
|
|
60
|
+
/** Namespace → token of the bundles left out because the cookie named them (full loads). */
|
|
61
|
+
readonly held?: Readonly<Record<string, string>>;
|
|
62
|
+
/** Present when the browser did not name this catalog version (always on a full load). */
|
|
63
|
+
readonly settings?: PageI18nSettings;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Names namespaces a page rendered for this request needs in the browser, beyond
|
|
67
|
+
* `options.dokebi-i18n.http.pageMessages`. Call before `render`; unknown names fail the render.
|
|
68
|
+
*/
|
|
69
|
+
export declare function pageMessages(request: object, ...namespaces: string[]): void;
|
|
70
|
+
/** Precomputed per locale and namespace at setup; a request only picks and joins fragments. */
|
|
71
|
+
export declare class PageMessages {
|
|
72
|
+
#private;
|
|
73
|
+
/** Changes with the time zone (D-0178): the browser then takes the new settings. */
|
|
74
|
+
version: string;
|
|
75
|
+
constructor(options: I18nOptions, input: unknown, logger: Logger);
|
|
76
|
+
/**
|
|
77
|
+
* D-0178: the application's time zone changed. The settings carry it and the version changes,
|
|
78
|
+
* so every browser takes the new settings on its next page; the bundles and their tokens stay.
|
|
79
|
+
*/
|
|
80
|
+
setTimeZone(zone: string): void;
|
|
81
|
+
/** Takes this catalog's bundles out of the SSR registry (the plugin's stop). */
|
|
82
|
+
close(): void;
|
|
83
|
+
/**
|
|
84
|
+
* The `i18n` prop of one rendered page. Unknown pageMessages() names throw UNKNOWN_NAMESPACE.
|
|
85
|
+
* `sealed`: the answer leaves through the seal (D-0062). Its browser must not keep bundles, so
|
|
86
|
+
* the cookie is not read and the settings carry no `cache`.
|
|
87
|
+
*/
|
|
88
|
+
value(request: {
|
|
89
|
+
readonly headers: Record<string, unknown>;
|
|
90
|
+
}, locale: string, sealed?: boolean,
|
|
91
|
+
/** D-0181: the request's app; its scope's bundles (the base for an app without a layer). */
|
|
92
|
+
app?: string): PageI18nPayload;
|
|
93
|
+
}
|