@k8ordo/i18n 1.4.4 → 2.3.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/dist/current.d.mts +7 -4
- package/dist/current.mjs +13 -7
- package/dist/message.mjs +2 -3
- package/docs/GUIDE.md +10 -8
- package/package.json +1 -1
package/dist/current.d.mts
CHANGED
|
@@ -25,14 +25,17 @@ export declare const register: (set: RegisteredSet) => void;
|
|
|
25
25
|
* The browser's pathname in the application's terms: Vite's `base` taken
|
|
26
26
|
* off, so the first segment is the one the route table's `[locale]` names —
|
|
27
27
|
* `/docs/en/ui` is `/en/ui` under `base: '/docs/'`.
|
|
28
|
+
*
|
|
29
|
+
* While `@k8ordo/framework` hydrates HTML rendered for another pathname (a
|
|
30
|
+
* prerendered 404, served at whatever URL), it is that pathname, which the
|
|
31
|
+
* framework gives in the same terms and takes back once hydration is done:
|
|
32
|
+
* a message read while hydrating has to agree with the text written there.
|
|
28
33
|
*/
|
|
29
34
|
export declare const browserPathname: () => string;
|
|
30
|
-
/** Whether a set has registered in this environment yet. */
|
|
31
|
-
export declare const setRegistered: () => boolean;
|
|
32
35
|
/**
|
|
33
36
|
* The locale named where this runs, or `null` when nothing names one: no
|
|
34
|
-
* segment in the URL, no request in progress
|
|
35
|
-
*
|
|
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.
|
|
36
39
|
*/
|
|
37
40
|
export declare const namedLocale: () => string | null;
|
|
38
41
|
/** The registered default, when a set registered in this environment. */
|
package/dist/current.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/current.ts
|
|
2
2
|
const STORAGE_KEY = Symbol.for("@k8ordo/i18n/storage");
|
|
3
3
|
const SET_KEY = Symbol.for("@k8ordo/i18n/locales");
|
|
4
|
+
const HYDRATING_KEY = Symbol.for("@k8ordo/i18n/hydrating-pathname");
|
|
4
5
|
const global = globalThis;
|
|
5
6
|
const inBrowser = typeof document !== "undefined";
|
|
6
7
|
/**
|
|
@@ -29,8 +30,15 @@ const register = (set) => {
|
|
|
29
30
|
* The browser's pathname in the application's terms: Vite's `base` taken
|
|
30
31
|
* off, so the first segment is the one the route table's `[locale]` names —
|
|
31
32
|
* `/docs/en/ui` is `/en/ui` under `base: '/docs/'`.
|
|
33
|
+
*
|
|
34
|
+
* While `@k8ordo/framework` hydrates HTML rendered for another pathname (a
|
|
35
|
+
* prerendered 404, served at whatever URL), it is that pathname, which the
|
|
36
|
+
* framework gives in the same terms and takes back once hydration is done:
|
|
37
|
+
* a message read while hydrating has to agree with the text written there.
|
|
32
38
|
*/
|
|
33
39
|
const browserPathname = () => {
|
|
40
|
+
const hydrating = global[HYDRATING_KEY];
|
|
41
|
+
if (hydrating !== void 0) return hydrating;
|
|
34
42
|
const pathname = global.location?.pathname ?? "/";
|
|
35
43
|
const base = import.meta.env === void 0 ? "/" : import.meta.env.BASE_URL;
|
|
36
44
|
if (!base.startsWith("/") || base === "/") return pathname;
|
|
@@ -40,18 +48,16 @@ const browserPathname = () => {
|
|
|
40
48
|
};
|
|
41
49
|
/** The first segment of the browser's URL below the base, whatever it spells. */
|
|
42
50
|
const browserSegment = () => browserPathname().split("/")[1] ?? "";
|
|
43
|
-
/** Whether a set has registered in this environment yet. */
|
|
44
|
-
const setRegistered = () => global[SET_KEY] !== void 0;
|
|
45
51
|
/**
|
|
46
52
|
* The locale named where this runs, or `null` when nothing names one: no
|
|
47
|
-
* segment in the URL, no request in progress
|
|
48
|
-
*
|
|
53
|
+
* segment in the URL, no request in progress, a segment that is not one of
|
|
54
|
+
* the set's (`/fr/…`), or no set defined in this environment to tell.
|
|
49
55
|
*/
|
|
50
56
|
const namedLocale = () => {
|
|
51
57
|
const set = global[SET_KEY];
|
|
58
|
+
if (set === void 0) return null;
|
|
52
59
|
const named = inBrowser ? browserSegment() : localeStorage()?.getStore();
|
|
53
|
-
if (named === void 0 || named
|
|
54
|
-
if (set !== void 0 && !set.is(named)) return null;
|
|
60
|
+
if (named === void 0 || !set.is(named)) return null;
|
|
55
61
|
return named;
|
|
56
62
|
};
|
|
57
63
|
/** The registered default, when a set registered in this environment. */
|
|
@@ -69,4 +75,4 @@ const currentLocale = () => {
|
|
|
69
75
|
if (set === void 0) return null;
|
|
70
76
|
return namedLocale() ?? set.default;
|
|
71
77
|
};
|
|
72
|
-
export { browserPathname, currentLocale, inBrowser, localeStorage, namedLocale, register, registeredDefault
|
|
78
|
+
export { browserPathname, currentLocale, inBrowser, localeStorage, namedLocale, register, registeredDefault };
|
package/dist/message.mjs
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { namedLocale, registeredDefault
|
|
1
|
+
import { namedLocale, registeredDefault } from "./current.mjs";
|
|
2
2
|
// src/message.ts
|
|
3
3
|
function message(variants) {
|
|
4
4
|
return (...args) => {
|
|
5
|
-
const
|
|
6
|
-
const locale = named !== null && (setRegistered() || Object.hasOwn(variants, named)) ? named : registeredDefault();
|
|
5
|
+
const locale = namedLocale() ?? registeredDefault();
|
|
7
6
|
const variant = locale === null ? variants[Object.keys(variants)[0] ?? ""] : variants[locale];
|
|
8
7
|
if (variant === void 0) throw new TypeError(`message: no text for ${JSON.stringify(locale)} in ${JSON.stringify(Object.keys(variants))}`);
|
|
9
8
|
return typeof variant === "function" ? variant(...args) : variant;
|
package/docs/GUIDE.md
CHANGED
|
@@ -362,9 +362,11 @@ base back from the router's `withBase` (imported from `@k8ordo/framework` in
|
|
|
362
362
|
a framework application).
|
|
363
363
|
|
|
364
364
|
A first segment that is not one of the set's locales is no locale, and the
|
|
365
|
-
default applies
|
|
366
|
-
|
|
367
|
-
|
|
365
|
+
default applies, so a 404 page never throws on `/fr/…`. Until the set is
|
|
366
|
+
defined in the browser, no segment is a locale and every message is in its
|
|
367
|
+
first variant: the module that defines it has to run there too.
|
|
368
|
+
`@k8ordo/framework` loads `src/i18n.ts` in the browser before anything
|
|
369
|
+
renders; anywhere else, import it from a client module that runs first.
|
|
368
370
|
|
|
369
371
|
`locales.getLocale()` reads the same source for code that needs the tag
|
|
370
372
|
itself: `<html lang>`, a language switcher, an `Intl` API the set does not
|
|
@@ -481,11 +483,11 @@ at once, and none lends its locale to another. The `404.html` a static host
|
|
|
481
483
|
serves for everything else is rendered once, by the catch-all under the
|
|
482
484
|
build's sentinel segment, which the schema refuses — so it is in the default
|
|
483
485
|
locale whatever the build rendered before it, and cannot follow the
|
|
484
|
-
visitor's. The browser
|
|
485
|
-
|
|
486
|
-
there.
|
|
487
|
-
components come out in their locale once the
|
|
488
|
-
without JavaScript keeps the default.
|
|
486
|
+
visitor's. The browser hydrates it as written: while it hydrates, messages
|
|
487
|
+
read the pathname it was rendered for, so they agree with the text written
|
|
488
|
+
there. Once it is hydrated, `@k8ordo/framework` renders it again where the
|
|
489
|
+
visitor is, so its client components come out in their locale once the
|
|
490
|
+
script runs, and a visitor without JavaScript keeps the default.
|
|
489
491
|
|
|
490
492
|
## Alongside the rest of k8ordo
|
|
491
493
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@k8ordo/i18n",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.3.1",
|
|
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",
|