@k8ordo/i18n 2.3.1 → 4.0.0
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/README.md +1 -1
- package/dist/current.d.mts +5 -12
- package/dist/current.mjs +6 -7
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +1 -1
- package/dist/locales.d.mts +24 -28
- package/dist/locales.mjs +7 -17
- package/dist/message.d.mts +4 -1
- package/dist/message.mjs +4 -3
- package/docs/GUIDE.md +229 -66
- package/docs/llms.txt +23 -12
- package/package.json +5 -5
- package/dist/cookie.d.mts +0 -8
- package/dist/cookie.mjs +0 -19
package/README.md
CHANGED
|
@@ -125,7 +125,7 @@ Where a Server Component hands text to a Client Component as a prop, it calls
|
|
|
125
125
|
the message and passes the string: a function does not cross that boundary.
|
|
126
126
|
|
|
127
127
|
`/` negotiates and redirects; `<html lang>` and a language switcher read
|
|
128
|
-
`locales.getLocale()`, and `<html dir>` its `locales.definitions[…].dir`; a static build
|
|
128
|
+
`locales.getLocale()`, and `<html dir>` its `locales.definitions[…].dir`; a static build's `src/routes/[locale]/paths.ts` returns `locales.all`. Links
|
|
129
129
|
stay the router's: `bindParams(() => ({ locale: locales.getLocale() }))`
|
|
130
130
|
(imported from `@k8ordo/framework` in a framework application) gives an `href` that spells `/:locale/…` patterns without the locale.
|
|
131
131
|
|
package/dist/current.d.mts
CHANGED
|
@@ -32,20 +32,13 @@ export declare const register: (set: RegisteredSet) => void;
|
|
|
32
32
|
* a message read while hydrating has to agree with the text written there.
|
|
33
33
|
*/
|
|
34
34
|
export declare const browserPathname: () => string;
|
|
35
|
-
/**
|
|
36
|
-
* The locale named where this runs, or `null` when nothing names one: no
|
|
37
|
-
* segment in the URL, no request in progress, a segment that is not one of
|
|
38
|
-
* the set's (`/fr/…`), or no set defined in this environment to tell.
|
|
39
|
-
*/
|
|
40
|
-
export declare const namedLocale: () => string | null;
|
|
41
|
-
/** The registered default, when a set registered in this environment. */
|
|
42
|
-
export declare const registeredDefault: () => string | null;
|
|
43
35
|
/**
|
|
44
36
|
* The current locale as the application's set resolves it — the one named,
|
|
45
37
|
* else the set's default — or `null` when no set is defined in this
|
|
46
|
-
* environment.
|
|
47
|
-
* know (`@k8ordo/ui`'s built-in text)
|
|
48
|
-
*
|
|
49
|
-
* and the browser both answer
|
|
38
|
+
* environment. A message refuses the `null`; a library rendering inside an
|
|
39
|
+
* application it does not know (`@k8ordo/ui`'s built-in text) takes it as
|
|
40
|
+
* no locale: an application that never defined a set has none, whatever its
|
|
41
|
+
* URL happens to start with, so the server and the browser both answer
|
|
42
|
+
* `null` and agree.
|
|
50
43
|
*/
|
|
51
44
|
export declare const currentLocale: () => RegisteredLocale | null;
|
package/dist/current.mjs
CHANGED
|
@@ -60,19 +60,18 @@ const namedLocale = () => {
|
|
|
60
60
|
if (named === void 0 || !set.is(named)) return null;
|
|
61
61
|
return named;
|
|
62
62
|
};
|
|
63
|
-
/** The registered default, when a set registered in this environment. */
|
|
64
|
-
const registeredDefault = () => global[SET_KEY]?.default ?? null;
|
|
65
63
|
/**
|
|
66
64
|
* The current locale as the application's set resolves it — the one named,
|
|
67
65
|
* else the set's default — or `null` when no set is defined in this
|
|
68
|
-
* environment.
|
|
69
|
-
* know (`@k8ordo/ui`'s built-in text)
|
|
70
|
-
*
|
|
71
|
-
* and the browser both answer
|
|
66
|
+
* environment. A message refuses the `null`; a library rendering inside an
|
|
67
|
+
* application it does not know (`@k8ordo/ui`'s built-in text) takes it as
|
|
68
|
+
* no locale: an application that never defined a set has none, whatever its
|
|
69
|
+
* URL happens to start with, so the server and the browser both answer
|
|
70
|
+
* `null` and agree.
|
|
72
71
|
*/
|
|
73
72
|
const currentLocale = () => {
|
|
74
73
|
const set = global[SET_KEY];
|
|
75
74
|
if (set === void 0) return null;
|
|
76
75
|
return namedLocale() ?? set.default;
|
|
77
76
|
};
|
|
78
|
-
export { browserPathname, currentLocale, inBrowser, localeStorage,
|
|
77
|
+
export { browserPathname, currentLocale, inBrowser, localeStorage, register };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { parseAcceptLanguage } from "./accept-language.mjs";
|
|
2
|
-
import { Register, RegisteredLocale } from "./register.mjs";
|
|
3
1
|
import { currentLocale } from "./current.mjs";
|
|
4
2
|
import { Delocalized, LocaleDefinition, LocaleOf, LocaleParamsSchema, Locales, LocalesOptions, NegotiateRequestOptions, defineLocales } from "./locales.mjs";
|
|
5
3
|
import { IntlFormats, LocaleDateTimeFormatOptions } from "./format.mjs";
|
|
6
4
|
import { Message, Variants, message } from "./message.mjs";
|
|
5
|
+
import { Register, RegisteredLocale } from "./register.mjs";
|
|
6
|
+
import { parseAcceptLanguage } from "./accept-language.mjs";
|
|
7
7
|
export { type Delocalized, type IntlFormats, type LocaleDateTimeFormatOptions, type LocaleDefinition, type LocaleOf, type LocaleParamsSchema, type Locales, type LocalesOptions, type Message, type NegotiateRequestOptions, type Register, type RegisteredLocale, type Variants, currentLocale, defineLocales, message, parseAcceptLanguage };
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { parseAcceptLanguage } from "./accept-language.mjs";
|
|
2
1
|
import { currentLocale } from "./current.mjs";
|
|
3
2
|
import { defineLocales } from "./locales.mjs";
|
|
4
3
|
import { message } from "./message.mjs";
|
|
4
|
+
import { parseAcceptLanguage } from "./accept-language.mjs";
|
|
5
5
|
export { currentLocale, defineLocales, message, parseAcceptLanguage };
|
package/dist/locales.d.mts
CHANGED
|
@@ -71,38 +71,33 @@ export type Locales<L extends string = string, D extends L = L> = IntlFormats &
|
|
|
71
71
|
* tag is tried in order — the exact tag, then the first supported locale
|
|
72
72
|
* that speaks its language — and nothing matching is the default. A tag
|
|
73
73
|
* that is not BCP 47 is skipped, not thrown on: the list is user input.
|
|
74
|
+
* So are `null` and `undefined`, a choice not made yet:
|
|
75
|
+
* `negotiate([saved?.value, ...navigator.languages])`.
|
|
74
76
|
*/
|
|
75
|
-
readonly negotiate: (requested: Iterable<string>) => L;
|
|
77
|
+
readonly negotiate: (requested: Iterable<string | null | undefined>) => L;
|
|
76
78
|
/**
|
|
77
79
|
* The locale a request asks for, for a server that answers before any
|
|
78
|
-
* page renders — sending `/` to a locale.
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
80
|
+
* page renders — sending `/` to a locale. `options.chosen` comes first: the
|
|
81
|
+
* visitor's own choice, read from wherever the application keeps it. Then
|
|
82
|
+
* the `Accept-Language` header, in its order of preference. Both go
|
|
83
|
+
* through `negotiate`, so a choice holding a locale the set no longer has
|
|
84
|
+
* falls through to the header, and nothing matching is the default.
|
|
85
|
+
* `request` is anything with the request's `headers` — a `Request`, or
|
|
86
|
+
* `@k8ordo/framework`'s `request` prop.
|
|
84
87
|
*/
|
|
85
|
-
readonly negotiateRequest: (request:
|
|
88
|
+
readonly negotiateRequest: (request: {
|
|
89
|
+
readonly headers: Headers;
|
|
90
|
+
}, options?: NegotiateRequestOptions) => L;
|
|
86
91
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
* `
|
|
92
|
+
* The same pathname in `locale`: a leading segment that is one of the
|
|
93
|
+
* set's locales is replaced (`'/ja/ui'` → `'/en/ui'`), and one is put in
|
|
94
|
+
* front otherwise (`'/ui'` → `'/en/ui'`, `'/'` → `'/en'`, `'/fr/ui'` →
|
|
95
|
+
* `'/en/fr/ui'`). So the pathname `usePathname` returns takes the page on
|
|
96
|
+
* screen to another locale as it is.
|
|
90
97
|
*/
|
|
91
98
|
readonly localize: (pathname: string, locale: L) => string;
|
|
92
99
|
/** The inverse: `'/en/ui'` → `{ locale: 'en', pathname: '/ui' }`, `'/x'` → `{ locale: null, pathname: '/x' }`. */
|
|
93
100
|
readonly delocalize: (pathname: string) => Delocalized<L>;
|
|
94
|
-
/**
|
|
95
|
-
* The static build's `paths` option: every pattern that has a `/:locale`
|
|
96
|
-
* segment, once per locale, so
|
|
97
|
-
* `framework({ mode: 'static', paths: locales.paths })` is the whole
|
|
98
|
-
* answer for a site whose only parameter is the locale. A pattern with
|
|
99
|
-
* another parameter comes back still holding it (`/ja/blog/:slug`), which
|
|
100
|
-
* the build renders only as the location of a `fallback.tsx`'s shell, and
|
|
101
|
-
* otherwise refuses: expand the rest in the same function. A shell runs
|
|
102
|
-
* only its layouts' schemas, so it renders in its locale when this set's
|
|
103
|
-
* `paramsSchema` sits on `[locale]/layout.tsx`.
|
|
104
|
-
*/
|
|
105
|
-
readonly paths: (patterns: readonly string[]) => string[];
|
|
106
101
|
/**
|
|
107
102
|
* A params schema for a `[locale]` route segment, in the shape
|
|
108
103
|
* `@k8ordo/framework` runs: `export const paramsSchema =
|
|
@@ -129,12 +124,13 @@ export type Locales<L extends string = string, D extends L = L> = IntlFormats &
|
|
|
129
124
|
};
|
|
130
125
|
export type NegotiateRequestOptions = {
|
|
131
126
|
/**
|
|
132
|
-
* The
|
|
133
|
-
*
|
|
134
|
-
* `
|
|
135
|
-
* is
|
|
127
|
+
* The locale the visitor chose before, as the application read it — a
|
|
128
|
+
* cookie its language switcher wrote (`cookies().get('locale')` under
|
|
129
|
+
* `@k8ordo/framework`), the language of a signed-in visitor's profile.
|
|
130
|
+
* `null` and `undefined` are no choice, and only `Accept-Language` is
|
|
131
|
+
* read.
|
|
136
132
|
*/
|
|
137
|
-
readonly
|
|
133
|
+
readonly chosen?: string | null | undefined;
|
|
138
134
|
};
|
|
139
135
|
export type LocalesOptions<D extends string> = {
|
|
140
136
|
/** The default locale. Must be one of the list; the first entry when omitted. */
|
package/dist/locales.mjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { parseAcceptLanguage } from "./accept-language.mjs";
|
|
2
|
-
import { readCookie } from "./cookie.mjs";
|
|
3
2
|
import { browserPathname, inBrowser, localeStorage, register } from "./current.mjs";
|
|
4
3
|
import { intlFormats } from "./format.mjs";
|
|
5
4
|
// src/locales.ts
|
|
@@ -11,12 +10,6 @@ const parseTag = (tag) => {
|
|
|
11
10
|
return null;
|
|
12
11
|
}
|
|
13
12
|
};
|
|
14
|
-
/** `'/ui'` → `'/en/ui'`; `'/'` and `''` → `'/en'`. Pure over strings, so it lives outside the set. */
|
|
15
|
-
const localize = (pathname, locale) => {
|
|
16
|
-
const rest = pathname === "/" || pathname === "" ? "" : pathname;
|
|
17
|
-
if (rest !== "" && !rest.startsWith("/")) throw new TypeError(`localize: ${JSON.stringify(pathname)} is not a pathname — it does not start with "/"`);
|
|
18
|
-
return `/${locale}${rest}`;
|
|
19
|
-
};
|
|
20
13
|
/**
|
|
21
14
|
* Where a server keeps the locale. Without one this throws rather than
|
|
22
15
|
* falling back: the locale set would be ignored and the page rendered in the
|
|
@@ -79,6 +72,7 @@ const defineLocales = (definitions, options) => {
|
|
|
79
72
|
const is = (value) => typeof value === "string" && list.includes(value);
|
|
80
73
|
const negotiate = (requested) => {
|
|
81
74
|
for (const tag of requested) {
|
|
75
|
+
if (tag === null || tag === void 0) continue;
|
|
82
76
|
const exact = byExactTag.get(tag.toLowerCase());
|
|
83
77
|
if (exact !== void 0) return exact;
|
|
84
78
|
const parsed = parseTag(tag);
|
|
@@ -88,15 +82,7 @@ const defineLocales = (definitions, options) => {
|
|
|
88
82
|
}
|
|
89
83
|
return fallback;
|
|
90
84
|
};
|
|
91
|
-
const negotiateRequest = (request, {
|
|
92
|
-
const chosen = cookie === void 0 ? null : readCookie(request.headers.get("cookie"), cookie);
|
|
93
|
-
return negotiate([...chosen === null ? [] : [chosen], ...parseAcceptLanguage(request.headers.get("accept-language"))]);
|
|
94
|
-
};
|
|
95
|
-
const paths = (patterns) => patterns.flatMap((pattern) => {
|
|
96
|
-
const segments = pattern.split("/");
|
|
97
|
-
if (!segments.includes(":locale")) return [pattern];
|
|
98
|
-
return list.map((locale) => segments.map((segment) => segment === ":locale" ? locale : segment).join("/"));
|
|
99
|
-
});
|
|
85
|
+
const negotiateRequest = (request, { chosen } = {}) => negotiate([chosen, ...parseAcceptLanguage(request.headers.get("accept-language"))]);
|
|
100
86
|
const delocalize = (pathname) => {
|
|
101
87
|
const first = pathname.split("/")[1] ?? "";
|
|
102
88
|
if (!is(first)) return {
|
|
@@ -109,6 +95,11 @@ const defineLocales = (definitions, options) => {
|
|
|
109
95
|
pathname: rest === "" ? "/" : rest
|
|
110
96
|
};
|
|
111
97
|
};
|
|
98
|
+
const localize = (pathname, locale) => {
|
|
99
|
+
if (pathname !== "" && !pathname.startsWith("/")) throw new TypeError(`localize: ${JSON.stringify(pathname)} is not a pathname — it does not start with "/"`);
|
|
100
|
+
const rest = delocalize(pathname === "" ? "/" : pathname).pathname;
|
|
101
|
+
return rest === "/" ? `/${locale}` : `/${locale}${rest}`;
|
|
102
|
+
};
|
|
112
103
|
const getLocale = () => {
|
|
113
104
|
if (inBrowser) return delocalize(browserPathname()).locale ?? fallback;
|
|
114
105
|
const current = localeStorage()?.getStore();
|
|
@@ -140,7 +131,6 @@ const defineLocales = (definitions, options) => {
|
|
|
140
131
|
negotiateRequest,
|
|
141
132
|
localize,
|
|
142
133
|
delocalize,
|
|
143
|
-
paths,
|
|
144
134
|
paramsSchema,
|
|
145
135
|
getLocale,
|
|
146
136
|
run,
|
package/dist/message.d.mts
CHANGED
|
@@ -22,7 +22,10 @@ export type Variants<V> = Readonly<Record<RegisteredLocale, V>>;
|
|
|
22
22
|
* ```
|
|
23
23
|
*
|
|
24
24
|
* When nothing names a locale, the text is the default locale's — the one
|
|
25
|
-
* `defineLocales` registered
|
|
25
|
+
* `defineLocales` registered. Called where no set is defined, it throws:
|
|
26
|
+
* the module that calls `defineLocales` has to run first, on every side
|
|
27
|
+
* (`@k8ordo/framework` runs `src/i18n.ts` before anything else; a test or a
|
|
28
|
+
* Storybook preview imports it).
|
|
26
29
|
*/
|
|
27
30
|
export declare function message(variants: Variants<string>): Message;
|
|
28
31
|
export declare function message<A extends readonly unknown[]>(variants: Variants<(...args: A) => string>): Message<A>;
|
package/dist/message.mjs
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { currentLocale } from "./current.mjs";
|
|
2
2
|
// src/message.ts
|
|
3
3
|
function message(variants) {
|
|
4
4
|
return (...args) => {
|
|
5
|
-
const locale =
|
|
6
|
-
|
|
5
|
+
const locale = currentLocale();
|
|
6
|
+
if (locale === null) throw new TypeError("message: no locale set is defined here — the module that calls defineLocales has to run before any message, in the browser too (under @k8ordo/framework, export it as locales from src/i18n.ts; a test imports that file from its setupFiles, and Storybook from .storybook/preview.tsx)");
|
|
7
|
+
const variant = variants[locale];
|
|
7
8
|
if (variant === void 0) throw new TypeError(`message: no text for ${JSON.stringify(locale)} in ${JSON.stringify(Object.keys(variants))}`);
|
|
8
9
|
return typeof variant === "function" ? variant(...args) : variant;
|
|
9
10
|
};
|
package/docs/GUIDE.md
CHANGED
|
@@ -115,10 +115,9 @@ the definition, not later.
|
|
|
115
115
|
| `default` | The tag used when nothing names one. |
|
|
116
116
|
| `is(value)` | Membership as a type guard. |
|
|
117
117
|
| `negotiate(…)` | The best supported tag for a preference list. |
|
|
118
|
-
| `negotiateRequest(…)` | The best supported tag for a
|
|
119
|
-
| `localize` | `'/ui'` → `'/en/ui'`; `'/'` → `'/en'`.
|
|
118
|
+
| `negotiateRequest(…)` | The best supported tag for a request: the choice given, then `Accept-Language`. |
|
|
119
|
+
| `localize` | `'/ui'` → `'/en/ui'`; `'/'` → `'/en'`; `'/ja/ui'` → `'/en/ui'`. |
|
|
120
120
|
| `delocalize` | `'/en/ui'` → `{ locale: 'en', pathname: '/ui' }`; `'/x'` → `{ locale: null, … }`. |
|
|
121
|
-
| `paths` | The static build's `paths` option: `/:locale` expanded to every locale. |
|
|
122
121
|
| `paramsSchema` | The `[locale]` segment's schema (Standard Schema; no schema library). |
|
|
123
122
|
| `getLocale()` | The locale of the render in progress. Not a hook. |
|
|
124
123
|
| `run(locale, fn)` | Server only: runs `fn` with `locale` current. |
|
|
@@ -152,21 +151,25 @@ declared rather than derived.
|
|
|
152
151
|
each requested tag in order — the exact tag, then the first supported locale
|
|
153
152
|
that speaks its language — before falling back to the default. Nothing
|
|
154
153
|
matching across the whole list is the default; a tag that is not BCP 47 is
|
|
155
|
-
skipped, because the list is user input
|
|
154
|
+
skipped, because the list is user input, and so are `null` and `undefined`,
|
|
155
|
+
so a choice that may not have been made goes in front as it is:
|
|
156
|
+
`locales.negotiate([saved?.value, ...navigator.languages])`.
|
|
156
157
|
|
|
157
158
|
```ts
|
|
158
159
|
locales.negotiate(navigator.languages); // in the browser
|
|
159
|
-
locales.negotiateRequest(request, {
|
|
160
|
+
locales.negotiateRequest(request, { chosen: cookies().get('locale') }); // on a server
|
|
160
161
|
```
|
|
161
162
|
|
|
162
|
-
`negotiateRequest(request, {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
`
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
163
|
+
`negotiateRequest(request, { chosen? })` is the server's form. `chosen` comes
|
|
164
|
+
first — the locale the visitor chose before — then the `Accept-Language`
|
|
165
|
+
header in its order of preference. Both go through `negotiate`, so a choice
|
|
166
|
+
still holding a locale the set no longer lists falls through to the header,
|
|
167
|
+
and a `chosen` of `null` or `undefined` — no choice yet — leaves only the
|
|
168
|
+
header. Where the choice comes from is the application's: a cookie the
|
|
169
|
+
language switcher wrote, read with `@k8ordo/framework`'s `cookies().get()`,
|
|
170
|
+
or the language of a signed-in visitor's profile. The package reads no
|
|
171
|
+
cookie itself. `request` is anything with the request's `headers`: a
|
|
172
|
+
`Request`, or the `request` prop a page receives.
|
|
170
173
|
|
|
171
174
|
```ts
|
|
172
175
|
// the language switcher, in the browser
|
|
@@ -183,7 +186,10 @@ cookie under `SameSite=Strict`: the choice is gone once the browser closes,
|
|
|
183
186
|
and the first request arriving from a link on another site goes without it,
|
|
184
187
|
so that page is chosen by the header instead. `sameSite: 'lax'` and a far
|
|
185
188
|
`expires` — 400 days is the longest a browser keeps a cookie — are what make
|
|
186
|
-
the choice stick.
|
|
189
|
+
the choice stick. Why the choice is a plain cookie rather than a
|
|
190
|
+
`@k8ordo/state` cookie state, beside the application's other preferences, is
|
|
191
|
+
in `@k8ordo/framework`'s guide
|
|
192
|
+
(`node_modules/@k8ordo/framework/docs/GUIDE.md`, "Preferences").
|
|
187
193
|
|
|
188
194
|
`parseAcceptLanguage(header)` is the header half on its own: it turns an
|
|
189
195
|
`Accept-Language` header into a preference list for `negotiate`: `q` weights decide the order (a `q` that is not a number is ignored),
|
|
@@ -350,9 +356,17 @@ the page in the default.
|
|
|
350
356
|
|
|
351
357
|
**In the browser**, the URL is the locale: the first segment of
|
|
352
358
|
`location.pathname`, read when a message is called. Changing locale is a
|
|
353
|
-
navigation to the same pathname under the other segment
|
|
354
|
-
|
|
355
|
-
|
|
359
|
+
navigation to the same pathname under the other segment, which re-renders
|
|
360
|
+
the page; there is no state to keep in sync. `localize` replaces the
|
|
361
|
+
pathname's locale segment with the one it is given, so the pathname the page
|
|
362
|
+
is on goes in as it is:
|
|
363
|
+
|
|
364
|
+
```tsx
|
|
365
|
+
function ToEnglish() {
|
|
366
|
+
const pathname = usePathname();
|
|
367
|
+
return <a href={withBase(locales.localize(pathname, 'en'))}>English</a>;
|
|
368
|
+
}
|
|
369
|
+
```
|
|
356
370
|
|
|
357
371
|
Under Vite's `base` (`base: '/docs/'`), the segment read is the first one
|
|
358
372
|
below it — `/docs/en/ui` is in `en` — as the route table's `[locale]` sits
|
|
@@ -362,11 +376,17 @@ base back from the router's `withBase` (imported from `@k8ordo/framework` in
|
|
|
362
376
|
a framework application).
|
|
363
377
|
|
|
364
378
|
A first segment that is not one of the set's locales is no locale, and the
|
|
365
|
-
default applies, so a 404 page never throws on `/fr/…`.
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
379
|
+
default applies, so a 404 page never throws on `/fr/…`.
|
|
380
|
+
|
|
381
|
+
**On either side, the set has to be defined before a message is called.**
|
|
382
|
+
The set is what tells a locale from a path and gives the default, so a
|
|
383
|
+
message called where no set is defined throws, naming the module that has to
|
|
384
|
+
run first, rather than rendering some variant. `@k8ordo/framework` runs
|
|
385
|
+
`src/i18n.ts` before any route file, in the browser and on the server, so
|
|
386
|
+
nothing is left to wire there. Anywhere else, import the module that
|
|
387
|
+
defines the set from code that runs first on each side — in the browser, a
|
|
388
|
+
client module the root renders. A test or a Storybook preview does the same
|
|
389
|
+
(see [Testing](#testing)).
|
|
370
390
|
|
|
371
391
|
`locales.getLocale()` reads the same source for code that needs the tag
|
|
372
392
|
itself: `<html lang>`, a language switcher, an `Intl` API the set does not
|
|
@@ -378,21 +398,24 @@ draw.
|
|
|
378
398
|
|
|
379
399
|
In server mode a `guard.ts` answers it before anything renders. It
|
|
380
400
|
chooses from the request — the cookie holding the locale the visitor chose
|
|
381
|
-
before, then `Accept-Language` — and ends the request with
|
|
401
|
+
before, then `Accept-Language` — and ends the request with `redirect()`:
|
|
382
402
|
|
|
383
403
|
```ts
|
|
384
404
|
// src/routes/(home)/guard.ts
|
|
385
|
-
import {
|
|
405
|
+
import { href } from '@k8ordo/framework';
|
|
406
|
+
import { cookies, redirect } from '@k8ordo/framework/server';
|
|
386
407
|
import type { Guard } from '@k8ordo/framework/server';
|
|
387
408
|
|
|
388
409
|
import { locales } from '../../i18n';
|
|
389
410
|
|
|
390
411
|
const guard: Guard<'/'> = ({ request }) => {
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
412
|
+
redirect(
|
|
413
|
+
href('/:locale', {
|
|
414
|
+
locale: locales.negotiateRequest(request, {
|
|
415
|
+
chosen: cookies().get('locale'),
|
|
416
|
+
}),
|
|
417
|
+
}),
|
|
418
|
+
);
|
|
396
419
|
};
|
|
397
420
|
|
|
398
421
|
export default guard;
|
|
@@ -411,71 +434,102 @@ The route group is what keeps the guard to `/`: a `guard.ts` runs before
|
|
|
411
434
|
every URL below its directory, so one directly in `src/routes/` would send
|
|
412
435
|
`/en/…` away as well. The page still has to exist — a guard runs before a
|
|
413
436
|
URL a page declares, and without one `/` is a 404 — and it can return
|
|
414
|
-
`null`. The
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
navigation to `/`, whose payload request the guard answers too.
|
|
437
|
+
`null`. The URL is built by `href`, as a link's is, so it carries the Vite
|
|
438
|
+
`base`. A visitor without JavaScript is redirected all the same, and so is a
|
|
439
|
+
client navigation to `/`, whose payload request the guard answers too.
|
|
418
440
|
|
|
419
441
|
Static mode refuses a `guard.ts` — a file has no request to guard — so
|
|
420
|
-
there the page renders nothing and redirects from an effect:
|
|
421
|
-
|
|
442
|
+
there the page renders nothing and redirects from an effect: to the bound
|
|
443
|
+
`href` where the router is `@k8ordo/router`'s (below), or to
|
|
422
444
|
`locales.localize` otherwise:
|
|
423
445
|
|
|
424
446
|
```tsx
|
|
425
447
|
'use client';
|
|
426
448
|
useEffect(() => {
|
|
427
|
-
|
|
428
|
-
'/:locale',
|
|
429
|
-
{ locale: locales.negotiate(navigator.languages) },
|
|
449
|
+
navigation.navigate(
|
|
450
|
+
href('/:locale', { locale: locales.negotiate(navigator.languages) }),
|
|
430
451
|
{ history: 'replace' },
|
|
431
452
|
);
|
|
432
453
|
}, []);
|
|
433
454
|
```
|
|
434
455
|
|
|
456
|
+
Where the language switcher keeps the visitor's choice in a cookie, put it in
|
|
457
|
+
front of the browser's list — the order `negotiateRequest` reads on a
|
|
458
|
+
server. Without the cookie the value is `undefined`, which `negotiate` skips:
|
|
459
|
+
|
|
460
|
+
```tsx
|
|
461
|
+
useEffect(() => {
|
|
462
|
+
void cookieStore.get('locale').then((saved) => {
|
|
463
|
+
navigation.navigate(
|
|
464
|
+
href('/:locale', {
|
|
465
|
+
locale: locales.negotiate([saved?.value, ...navigator.languages]),
|
|
466
|
+
}),
|
|
467
|
+
{ history: 'replace' },
|
|
468
|
+
);
|
|
469
|
+
});
|
|
470
|
+
}, []);
|
|
471
|
+
```
|
|
472
|
+
|
|
435
473
|
### `<html lang>` and `dir`
|
|
436
474
|
|
|
437
|
-
The root layout sits above `[locale]` and
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
and the default where it does not, as `delocalize` reads it.
|
|
475
|
+
The root layout sits above `[locale]` and reads the locale the way a message
|
|
476
|
+
does, with `locales.getLocale()`. On a page the schema has already run for
|
|
477
|
+
that page, and a 404 runs the schemas of the layouts above its
|
|
478
|
+
`not-found.tsx`, so it is the locale the page's own messages render in — the
|
|
479
|
+
URL's where a schema accepted it, the default where none did:
|
|
443
480
|
|
|
444
481
|
```tsx
|
|
445
|
-
const locale = locales.
|
|
482
|
+
const locale = locales.getLocale();
|
|
446
483
|
|
|
447
484
|
<html dir={locales.definitions[locale].dir} lang={locale}>
|
|
448
485
|
```
|
|
449
486
|
|
|
450
487
|
## Static builds
|
|
451
488
|
|
|
452
|
-
Static mode asks for the
|
|
453
|
-
The
|
|
489
|
+
Static mode asks each parameter's directory for the values it takes, in a
|
|
490
|
+
`paths.ts`. The locale directory answers with the set:
|
|
491
|
+
|
|
492
|
+
```ts
|
|
493
|
+
// src/routes/[locale]/paths.ts
|
|
494
|
+
import type { Paths } from '@k8ordo/framework';
|
|
495
|
+
|
|
496
|
+
import { locales } from '../../i18n';
|
|
497
|
+
|
|
498
|
+
const paths: Paths<'/:locale'> = () => locales.all;
|
|
499
|
+
|
|
500
|
+
export default paths;
|
|
501
|
+
```
|
|
502
|
+
|
|
503
|
+
Every page, `route.ts` and `redirect.ts` below `[locale]/` is written once
|
|
504
|
+
per locale. A parameter further down has a `paths.ts` of its own, which
|
|
505
|
+
receives the locale it is asked for:
|
|
454
506
|
|
|
455
507
|
```ts
|
|
456
|
-
|
|
508
|
+
// src/routes/[locale]/blog/[slug]/paths.ts
|
|
509
|
+
import type { Paths } from '@k8ordo/framework';
|
|
510
|
+
|
|
511
|
+
import { slugsIn } from '../../../../lib/blog';
|
|
512
|
+
|
|
513
|
+
const paths: Paths<'/:locale/blog/:slug'> = ({ locale }) => slugsIn(locale);
|
|
514
|
+
|
|
515
|
+
export default paths;
|
|
457
516
|
```
|
|
458
517
|
|
|
459
|
-
|
|
460
|
-
another parameter comes back still holding it (`/ja/blog/:slug`). Only a
|
|
461
|
-
page with a `fallback.tsx` beside it takes such a pathname, as the location
|
|
462
|
-
of its shell: one per locale, answering in the browser the slugs the build
|
|
463
|
-
did not write. For any other route the build stops:
|
|
518
|
+
Without one the build stops:
|
|
464
519
|
|
|
465
520
|
```
|
|
466
|
-
|
|
521
|
+
static build needs the values of /:locale/blog/:slug — a paths.ts in each parameter's directory returns them, and a fallback.tsx beside a page answers the ones the build does not write
|
|
467
522
|
```
|
|
468
523
|
|
|
469
|
-
A
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
`fallback.tsx`.
|
|
524
|
+
A page with a `fallback.tsx` beside it is the exception: its shell stands
|
|
525
|
+
below each locale (`/ja/blog/:slug`, `/en/blog/:slug`), answering in the
|
|
526
|
+
browser the slugs the build did not write.
|
|
473
527
|
|
|
474
528
|
A shell runs the schemas of the layouts above it, never its page's: there
|
|
475
529
|
is no value for the page's to parse. With the schema on
|
|
476
530
|
`[locale]/layout.tsx`, as in [The shape of it](#the-shape-of-it), the
|
|
477
531
|
locale is set for every page below it, shells included, and a shell's
|
|
478
|
-
messages come out in the locale
|
|
532
|
+
messages come out in the locale it stands under.
|
|
479
533
|
|
|
480
534
|
Each path is rendered as its own request, so the schema names the locale
|
|
481
535
|
for each and the messages come out in that locale; the build renders several
|
|
@@ -489,6 +543,30 @@ there. Once it is hydrated, `@k8ordo/framework` renders it again where the
|
|
|
489
543
|
visitor is, so its client components come out in their locale once the
|
|
490
544
|
script runs, and a visitor without JavaScript keeps the default.
|
|
491
545
|
|
|
546
|
+
The root layout is a Server Component, so the `404.html`'s `<html lang>` and
|
|
547
|
+
`dir` stay the default's as well. A client component the not-found page
|
|
548
|
+
renders puts the visitor's on once the page is rendered where they are —
|
|
549
|
+
`getLocale()` reads the pathname the HTML was rendered for while it
|
|
550
|
+
hydrates, and the URL after, so the effect runs again then:
|
|
551
|
+
|
|
552
|
+
```tsx
|
|
553
|
+
'use client';
|
|
554
|
+
|
|
555
|
+
import { useEffect } from 'react';
|
|
556
|
+
|
|
557
|
+
import { locales } from '../i18n';
|
|
558
|
+
|
|
559
|
+
// rendered by not-found.tsx
|
|
560
|
+
export function DocumentLocale() {
|
|
561
|
+
const locale = locales.getLocale();
|
|
562
|
+
useEffect(() => {
|
|
563
|
+
document.documentElement.lang = locale;
|
|
564
|
+
document.documentElement.dir = locales.definitions[locale].dir;
|
|
565
|
+
}, [locale]);
|
|
566
|
+
return null;
|
|
567
|
+
}
|
|
568
|
+
```
|
|
569
|
+
|
|
492
570
|
## Alongside the rest of k8ordo
|
|
493
571
|
|
|
494
572
|
- **`@k8ordo/router`**: the locale is a param of every pattern
|
|
@@ -500,16 +578,18 @@ script runs, and a visitor without JavaScript keeps the default.
|
|
|
500
578
|
// src/lib/links.ts
|
|
501
579
|
import { bindParams } from '@k8ordo/framework'; // '@k8ordo/router' without the framework
|
|
502
580
|
|
|
503
|
-
export const
|
|
581
|
+
export const href = bindParams(() => ({
|
|
504
582
|
locale: locales.getLocale(),
|
|
505
583
|
}));
|
|
506
584
|
```
|
|
507
585
|
|
|
508
586
|
`href('/:locale/products/:id', { id })` then reads the current locale;
|
|
509
|
-
`
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
587
|
+
`href('/:locale', { locale: 'en' })` overrides it, and
|
|
588
|
+
`navigation.navigate(href(…))` goes there from code. Neither package
|
|
589
|
+
imports the other: the line above is the application's. `localize`
|
|
590
|
+
remains for a pathname in hand — the language switcher, which reads the
|
|
591
|
+
page it is on with `const pathname = usePathname()` and takes it to
|
|
592
|
+
another locale with `withBase(locales.localize(pathname, locale))`.
|
|
513
593
|
|
|
514
594
|
- **`@k8ordo/ui`**: its built-in strings read `currentLocale()` from this
|
|
515
595
|
package — the locale messages render in, or `null` (English there) when no
|
|
@@ -546,13 +626,78 @@ script runs, and a visitor without JavaScript keeps the default.
|
|
|
546
626
|
```
|
|
547
627
|
|
|
548
628
|
The definition stays at module scope; what has to happen per request is
|
|
549
|
-
calling it
|
|
550
|
-
|
|
629
|
+
calling it, and `formFields` is called during the page's render, as
|
|
630
|
+
`@k8ordo/form`'s guide writes it. A Server Action posted from a `[locale]` page runs in that page's
|
|
551
631
|
locale in server mode — the framework runs the page's params
|
|
552
632
|
schemas for the action's request too — so `parseForm` in it needs nothing
|
|
553
633
|
more. Anywhere else — an action run outside the framework, a job — call it
|
|
554
634
|
inside `locales.run`.
|
|
555
635
|
|
|
636
|
+
A check written without `{ error }` speaks zod's own wording, and zod
|
|
637
|
+
keeps one configuration for the whole process (`z.config`), so a fixed
|
|
638
|
+
`z.config(z.locales.en())` answers every request in English. To word
|
|
639
|
+
those checks in the request's locale as well, make the configuration read
|
|
640
|
+
the locale each time zod reports, from a module of its own:
|
|
641
|
+
|
|
642
|
+
```ts
|
|
643
|
+
// src/lib/zod-locale.ts
|
|
644
|
+
import type { Variants } from '@k8ordo/i18n';
|
|
645
|
+
import * as z from 'zod'; // or 'zod/mini'
|
|
646
|
+
|
|
647
|
+
import { locales } from '../i18n';
|
|
648
|
+
|
|
649
|
+
const zodLocales = {
|
|
650
|
+
ja: z.locales.ja(),
|
|
651
|
+
en: z.locales.en(),
|
|
652
|
+
} satisfies Variants<{ localeError: z.core.$ZodErrorMap }>;
|
|
653
|
+
|
|
654
|
+
z.config({
|
|
655
|
+
localeError: (issue) => zodLocales[locales.getLocale()].localeError(issue),
|
|
656
|
+
});
|
|
657
|
+
```
|
|
658
|
+
|
|
659
|
+
Import it first in every module that declares a schema
|
|
660
|
+
(`import './zod-locale';` at the top of `src/lib/talk-form.ts`). Keep it
|
|
661
|
+
out of `src/i18n.ts`: the framework loads that file in the browser too, and
|
|
662
|
+
zod would come with it. `satisfies Variants<…>` makes a locale added to the
|
|
663
|
+
set and missing here a type error. This is the fallback for the checks
|
|
664
|
+
left without a message; a message per check is still how to word the ones
|
|
665
|
+
a person reads.
|
|
666
|
+
|
|
667
|
+
### Adding a locale
|
|
668
|
+
|
|
669
|
+
Adding a locale is a handful of edits, and each one left out is a type error
|
|
670
|
+
or names itself:
|
|
671
|
+
|
|
672
|
+
1. Add it to the set in `src/i18n.ts`, with its time zone and direction:
|
|
673
|
+
`ar: { timeZone: 'Africa/Cairo', dir: 'rtl' }`. `RegisteredLocale` gains
|
|
674
|
+
it — through the generated `Register` under `@k8ordo/framework`, through
|
|
675
|
+
the hand-written one elsewhere.
|
|
676
|
+
2. Every `message()` without an `ar` variant is now a type error; the
|
|
677
|
+
typecheck lists them.
|
|
678
|
+
3. `@k8ordo/ui` ships text for `ja` and `en` only, and throws on a locale it
|
|
679
|
+
has none for, naming `registerMessages`. Register one next to the set.
|
|
680
|
+
Typed as `Messages`, a missing key is a type error; spreading `en` keeps
|
|
681
|
+
English for the keys not translated yet:
|
|
682
|
+
|
|
683
|
+
```ts
|
|
684
|
+
// src/i18n.ts
|
|
685
|
+
import { en, registerMessages } from '@k8ordo/ui/i18n';
|
|
686
|
+
import type { Messages } from '@k8ordo/ui/i18n';
|
|
687
|
+
|
|
688
|
+
const ar: Messages = { ...en, close: 'إغلاق' };
|
|
689
|
+
registerMessages('ar', ar);
|
|
690
|
+
```
|
|
691
|
+
|
|
692
|
+
4. If the application words zod's default messages per locale (the
|
|
693
|
+
`zod-locale.ts` above), its table is a type error until it has an `ar`
|
|
694
|
+
entry.
|
|
695
|
+
|
|
696
|
+
Nothing else is written per locale. The `[locale]` schema, negotiation,
|
|
697
|
+
`localize`, the static build's `[locale]/paths.ts` (it returns
|
|
698
|
+
`locales.all`) and `<html lang dir>` (it reads `definitions`) all follow the
|
|
699
|
+
set.
|
|
700
|
+
|
|
556
701
|
## What it guarantees
|
|
557
702
|
|
|
558
703
|
- A locale outside the list never reaches a page: the schema refuses it.
|
|
@@ -560,6 +705,8 @@ script runs, and a visitor without JavaScript keeps the default.
|
|
|
560
705
|
by `@k8ordo/framework`'s generated files, or by hand.
|
|
561
706
|
From JavaScript, or through `as`, it throws where it is read, naming the
|
|
562
707
|
locale and the variants present — never `undefined`.
|
|
708
|
+
- A message never guesses a language: read where no set is defined, it
|
|
709
|
+
throws, naming the module that has to run first.
|
|
563
710
|
- Arguments to a function message are typed by the function.
|
|
564
711
|
- A page under `[locale]` never renders in a locale its URL does not spell:
|
|
565
712
|
on the server its schema accepted that locale, and in the browser the URL
|
|
@@ -582,3 +729,19 @@ itself replaces the set messages read: the last one defined wins. In a
|
|
|
582
729
|
browser environment, `history.replaceState(null, '', '/en/…')` is the locale
|
|
583
730
|
and `run` throws; an environment that defines `document`, such as jsdom or
|
|
584
731
|
happy-dom, counts as one.
|
|
732
|
+
|
|
733
|
+
A test runner and Storybook run none of the framework's entries, so nothing
|
|
734
|
+
there has defined the set: a message called directly, or by a component a
|
|
735
|
+
test or a story renders, throws. Import `src/i18n.ts` once before them — in
|
|
736
|
+
a file the Vitest config lists in `setupFiles`, and at the top of
|
|
737
|
+
`.storybook/preview.tsx`:
|
|
738
|
+
|
|
739
|
+
```ts
|
|
740
|
+
// vitest.setup.ts, listed in test.setupFiles
|
|
741
|
+
import './src/i18n';
|
|
742
|
+
```
|
|
743
|
+
|
|
744
|
+
```ts
|
|
745
|
+
// .storybook/preview.tsx
|
|
746
|
+
import '../src/i18n';
|
|
747
|
+
```
|
package/docs/llms.txt
CHANGED
|
@@ -30,38 +30,49 @@ When installed via npm, these docs live under `node_modules/@k8ordo/i18n/docs/`.
|
|
|
30
30
|
drawn for the current locale, dates in its time zone), the Server
|
|
31
31
|
Component boundary (a message is called before it is passed to a
|
|
32
32
|
`'use client'` component), what reaches the browser, where the locale comes
|
|
33
|
-
from on each side, `/` (
|
|
34
|
-
an effect in static mode) and `<html lang>` / `dir`, static
|
|
35
|
-
|
|
36
|
-
`@k8ordo/form
|
|
33
|
+
from on each side, `/` (`redirect(href('/:locale', …))` from a
|
|
34
|
+
`guard.ts` in server mode, an effect in static mode) and `<html lang>` / `dir`, the static build's
|
|
35
|
+
`[locale]/paths.ts`, and pairing with `@k8ordo/router` / `@k8ordo/ui` /
|
|
36
|
+
`@k8ordo/form` (and zod's default wording in the request's locale).
|
|
37
37
|
|
|
38
38
|
## Entry point
|
|
39
39
|
|
|
40
40
|
- `@k8ordo/i18n` — `defineLocales(definitions, { default? })` takes each
|
|
41
41
|
locale's `{ timeZone, dir }` by tag (both required; an unknown time zone
|
|
42
42
|
throws) and returns `{ all, definitions, default, is, negotiate, localize,
|
|
43
|
-
delocalize,
|
|
43
|
+
delocalize, paramsSchema, getLocale, run, negotiateRequest, dateTimeFormat,
|
|
44
44
|
numberFormat, relativeTimeFormat, pluralRules, listFormat }`; the last five
|
|
45
45
|
return the `Intl` object for the current locale, cached per locale and
|
|
46
46
|
options, and `dateTimeFormat` always in that locale's `timeZone` (its
|
|
47
|
-
options type refuses one); `
|
|
48
|
-
|
|
47
|
+
options type refuses one); `localize(pathname, locale)` replaces a leading
|
|
48
|
+
segment that is one of the set's locales and puts one in front otherwise,
|
|
49
|
+
so a language switcher passes `usePathname()` as it is; a static build
|
|
50
|
+
takes the locales from
|
|
51
|
+
`src/routes/[locale]/paths.ts`, which returns `locales.all`; typed links
|
|
52
|
+
come from
|
|
49
53
|
the router's `bindParams(() => ({ locale: locales.getLocale() }))`,
|
|
50
54
|
written by the application (imported from `@k8ordo/framework` in a
|
|
51
55
|
framework application); the first entry is the default unless told otherwise (and `default` is then typed as the whole union), every tag
|
|
52
56
|
must be BCP 47, `negotiate(list)` tries each requested tag in order (exact,
|
|
53
|
-
then language) before falling back to the default,
|
|
57
|
+
then language) before falling back to the default, skipping a tag that is
|
|
58
|
+
not BCP 47 and a `null` or `undefined` entry (so
|
|
59
|
+
`negotiate([saved?.value, ...navigator.languages])` needs no filter),
|
|
60
|
+
`getLocale()` is the
|
|
54
61
|
locale of the render in progress (not a hook), and `run(locale, fn)`
|
|
55
62
|
(server only) runs `fn` under a locale. `negotiateRequest(request, {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
63
|
+
chosen? })` chooses for anything with a request's `headers` (a `Request`,
|
|
64
|
+
the framework's `request` prop) — `chosen`, the visitor's earlier choice
|
|
65
|
+
as the application read it (`cookies().get('locale')`; `null` or
|
|
66
|
+
`undefined` for none), first, then `Accept-Language` — through the same
|
|
67
|
+
rule as `negotiate`; the package reads no cookie itself. `parseAcceptLanguage(header)` turns an `Accept-Language`
|
|
59
68
|
header into a preference list. `currentLocale()` returns the current locale as the
|
|
60
69
|
defined set resolves it — the one named, else the set's default — or `null`
|
|
61
70
|
where no set is defined in that environment; it is for a library rendering
|
|
62
71
|
inside an application it does not know (`@k8ordo/ui`'s built-in text). `message(variants)`
|
|
63
72
|
declares one message — text in every locale, or a function of the same
|
|
64
73
|
parameters in every locale — and returns `Message<Args>`, a function that
|
|
65
|
-
renders the current locale's text
|
|
74
|
+
renders the current locale's text, and throws where no set is defined
|
|
75
|
+
(`@k8ordo/framework` runs `src/i18n.ts` before any route file; a test or a
|
|
76
|
+
Storybook preview imports it). Types: `Locales`, `LocaleOf`, `LocaleDefinition`, `IntlFormats`, `LocaleDateTimeFormatOptions`, `Message`,
|
|
66
77
|
`Variants`, `Register`, `RegisteredLocale`, `Delocalized`,
|
|
67
78
|
`LocaleParamsSchema`, `LocalesOptions`, `NegotiateRequestOptions`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@k8ordo/i18n",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "The locale axis of an application, owned. One locale set derives the URL segment, negotiation, the params schema, the static paths, and the current locale on either side of the network; each message is a function that reads it, so a bundler keeps only what a page names.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"i18n",
|
|
@@ -40,12 +40,12 @@
|
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@arethetypeswrong/cli": "0.18.5",
|
|
43
|
-
"@vitest/browser-playwright": "5.
|
|
43
|
+
"@vitest/browser-playwright": "5.1.1",
|
|
44
44
|
"publint": "0.3.25",
|
|
45
45
|
"typescript": "7.0.2",
|
|
46
|
-
"vite": "npm:@voidzero-dev/vite-plus-core@
|
|
47
|
-
"vite-plus": "1.
|
|
48
|
-
"vitest": "5.
|
|
46
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@8.3.8000",
|
|
47
|
+
"vite-plus": "1.7.0",
|
|
48
|
+
"vitest": "5.1.1"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"typescript": ">=7.0.0"
|
package/dist/cookie.d.mts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The value of one cookie in a `Cookie` header, or `null` when the header or
|
|
3
|
-
* the cookie is missing. The first cookie of that name wins — a browser sends
|
|
4
|
-
* the one with the most specific path first — and a value in double quotes
|
|
5
|
-
* (RFC 6265 allows them) comes back without them. Values are not
|
|
6
|
-
* percent-decoded: a locale tag needs no encoding.
|
|
7
|
-
*/
|
|
8
|
-
export declare const readCookie: (header: string | null, name: string) => string | null;
|
package/dist/cookie.mjs
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
// src/cookie.ts
|
|
2
|
-
/**
|
|
3
|
-
* The value of one cookie in a `Cookie` header, or `null` when the header or
|
|
4
|
-
* the cookie is missing. The first cookie of that name wins — a browser sends
|
|
5
|
-
* the one with the most specific path first — and a value in double quotes
|
|
6
|
-
* (RFC 6265 allows them) comes back without them. Values are not
|
|
7
|
-
* percent-decoded: a locale tag needs no encoding.
|
|
8
|
-
*/
|
|
9
|
-
const readCookie = (header, name) => {
|
|
10
|
-
if (header === null) return null;
|
|
11
|
-
for (const pair of header.split(";")) {
|
|
12
|
-
const separator = pair.indexOf("=");
|
|
13
|
-
if (separator === -1 || pair.slice(0, separator).trim() !== name) continue;
|
|
14
|
-
const value = pair.slice(separator + 1).trim();
|
|
15
|
-
return value.length >= 2 && value.startsWith("\"") && value.endsWith("\"") ? value.slice(1, -1) : value;
|
|
16
|
-
}
|
|
17
|
-
return null;
|
|
18
|
-
};
|
|
19
|
-
export { readCookie };
|