@hamur/i18n 0.0.0-stage → 0.0.2
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/LICENSE +22 -0
- package/README.md +38 -2
- package/dist/chunks/bun-index-1ve1hj66.js +744 -0
- package/dist/chunks/bun-index-8bw4xpv8.js +93 -0
- package/dist/chunks/bun-index-ej6vjnjs.js +297 -0
- package/dist/chunks/bun-index-pdepkyy1.js +474 -0
- package/dist/src/adapters/angular.js +67 -0
- package/dist/src/adapters/html.js +34 -0
- package/dist/src/adapters/preact.js +23 -0
- package/dist/src/adapters/react.js +23 -0
- package/dist/src/adapters/solid.js +48 -0
- package/dist/src/adapters/svelte.js +74 -0
- package/dist/src/adapters/vue.js +48 -0
- package/dist/src/cli.js +1898 -0
- package/dist/src/index.js +2755 -0
- package/dist/src/load.js +35 -0
- package/dist/src/server/index.js +395 -0
- package/dist/types/src/adapters/angular.d.ts +20 -0
- package/dist/types/src/adapters/bind.d.ts +44 -0
- package/dist/types/src/adapters/html.d.ts +5 -0
- package/dist/types/src/adapters/preact.d.ts +6 -0
- package/dist/types/src/adapters/react.d.ts +6 -0
- package/dist/types/src/adapters/solid.d.ts +9 -0
- package/dist/types/src/adapters/svelte.d.ts +12 -0
- package/dist/types/src/adapters/vue.d.ts +8 -0
- package/dist/types/src/build/catalog.d.ts +5 -0
- package/dist/types/src/build/drop.d.ts +12 -0
- package/dist/types/src/build/extract.d.ts +11 -0
- package/dist/types/src/build/head.d.ts +4 -0
- package/dist/types/src/build/messages.d.ts +27 -0
- package/dist/types/src/build/prerender.d.ts +10 -0
- package/dist/types/src/build/routes.d.ts +14 -0
- package/dist/types/src/build/split.d.ts +37 -0
- package/dist/types/src/build/types.d.ts +15 -0
- package/dist/types/src/cli.d.ts +12 -0
- package/dist/types/src/compiler/compile.d.ts +17 -0
- package/dist/types/src/compiler/mf2-parse.d.ts +62 -0
- package/dist/types/src/compiler/mf2.d.ts +3 -0
- package/dist/types/src/compiler/parse.d.ts +58 -0
- package/dist/types/src/compiler/skeleton.d.ts +7 -0
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/load.d.ts +7 -0
- package/dist/types/src/module.d.ts +15 -0
- package/dist/types/src/options.d.ts +7 -0
- package/dist/types/src/runtime/catalog-client.d.ts +3 -0
- package/dist/types/src/runtime/catalog-loader.d.ts +15 -0
- package/dist/types/src/runtime/catalog-server.d.ts +5 -0
- package/dist/types/src/runtime/catalog.d.ts +60 -0
- package/dist/types/src/runtime/chain.d.ts +3 -0
- package/dist/types/src/runtime/format.d.ts +27 -0
- package/dist/types/src/runtime/head.d.ts +9 -0
- package/dist/types/src/runtime/interpret.d.ts +20 -0
- package/dist/types/src/runtime/link.d.ts +8 -0
- package/dist/types/src/runtime/locale.d.ts +73 -0
- package/dist/types/src/runtime/match.d.ts +54 -0
- package/dist/types/src/runtime/messages.d.ts +42 -0
- package/dist/types/src/runtime/middleware.d.ts +2 -0
- package/dist/types/src/runtime/negotiate.d.ts +9 -0
- package/dist/types/src/runtime/plugin.d.ts +4 -0
- package/dist/types/src/runtime/redirect.d.ts +32 -0
- package/dist/types/src/runtime/rich.d.ts +5 -0
- package/dist/types/src/runtime/translate.d.ts +21 -0
- package/dist/types/src/server/catalog.d.ts +2 -0
- package/dist/types/src/server/context.d.ts +48 -0
- package/dist/types/src/server/door-all-catalog.d.ts +3 -0
- package/dist/types/src/server/door-all.d.ts +2 -0
- package/dist/types/src/server/door-catalog.d.ts +3 -0
- package/dist/types/src/server/door-root-catalog.d.ts +3 -0
- package/dist/types/src/server/door-root.d.ts +2 -0
- package/dist/types/src/server/door.d.ts +2 -0
- package/dist/types/src/server/index.d.ts +7 -0
- package/dist/types/src/server/preferred.d.ts +6 -0
- package/dist/types/src/server/redirect.d.ts +3 -0
- package/dist/types/src/server/setup.d.ts +2 -0
- package/dist/types/src/types.d.ts +163 -0
- package/package.json +155 -4
- package/src/adapters/angular.ts +58 -0
- package/src/adapters/bind.ts +144 -0
- package/src/adapters/html.ts +35 -0
- package/src/adapters/preact.ts +18 -0
- package/src/adapters/react.ts +18 -0
- package/src/adapters/solid.ts +44 -0
- package/src/adapters/svelte.ts +75 -0
- package/src/adapters/test/SveltePage.svelte +18 -0
- package/src/adapters/test/SvelteReporting.svelte +20 -0
- package/src/adapters/test/SvelteRich.svelte +11 -0
- package/src/adapters/test/SvelteRichPlain.svelte +11 -0
- package/src/adapters/test/peer.ts +7 -0
- package/src/adapters/test/preact-client.ts +43 -0
- package/src/adapters/test/preact-page.ts +34 -0
- package/src/adapters/test/preact-rich.ts +9 -0
- package/src/adapters/test/react-client.ts +49 -0
- package/src/adapters/test/react-page.ts +34 -0
- package/src/adapters/test/react-rich.ts +15 -0
- package/src/adapters/test/runtime.ts +104 -0
- package/src/adapters/test/solid-client.ts +33 -0
- package/src/adapters/test/solid-page.ts +30 -0
- package/src/adapters/test/solid-rich.ts +5 -0
- package/src/adapters/test/svelte-client.ts +51 -0
- package/src/adapters/test/svelte-log.ts +2 -0
- package/src/adapters/test/svelte-plugin.ts +20 -0
- package/src/adapters/test/vue-page.ts +26 -0
- package/src/adapters/test/vue-rich.ts +16 -0
- package/src/adapters/vue.ts +44 -0
- package/src/build/catalog.ts +44 -0
- package/src/build/drop.ts +36 -0
- package/src/build/extract.ts +207 -0
- package/src/build/head.ts +10 -0
- package/src/build/messages.ts +347 -0
- package/src/build/prerender.ts +48 -0
- package/src/build/routes.ts +269 -0
- package/src/build/split.ts +240 -0
- package/src/build/types.ts +156 -0
- package/src/cli.ts +25 -0
- package/src/compiler/compile.ts +190 -0
- package/src/compiler/mf2-parse.ts +469 -0
- package/src/compiler/mf2.ts +393 -0
- package/src/compiler/parse.ts +265 -0
- package/src/compiler/skeleton.ts +85 -0
- package/src/env.d.ts +55 -0
- package/src/index.ts +24 -0
- package/src/load.ts +41 -0
- package/src/module.ts +66 -0
- package/src/options.ts +198 -0
- package/src/runtime/catalog-client.ts +28 -0
- package/src/runtime/catalog-loader.ts +28 -0
- package/src/runtime/catalog-server.ts +36 -0
- package/src/runtime/catalog.ts +190 -0
- package/src/runtime/chain.ts +9 -0
- package/src/runtime/format.ts +102 -0
- package/src/runtime/head.ts +57 -0
- package/src/runtime/interpret.ts +504 -0
- package/src/runtime/link.ts +36 -0
- package/src/runtime/locale.ts +392 -0
- package/src/runtime/match.ts +334 -0
- package/src/runtime/messages.ts +129 -0
- package/src/runtime/middleware.ts +32 -0
- package/src/runtime/negotiate.ts +61 -0
- package/src/runtime/plugin.ts +69 -0
- package/src/runtime/redirect.ts +191 -0
- package/src/runtime/rich.ts +21 -0
- package/src/runtime/translate.ts +131 -0
- package/src/server/catalog.ts +11 -0
- package/src/server/context.ts +191 -0
- package/src/server/door-all-catalog.ts +7 -0
- package/src/server/door-all.ts +5 -0
- package/src/server/door-catalog.ts +6 -0
- package/src/server/door-root-catalog.ts +7 -0
- package/src/server/door-root.ts +5 -0
- package/src/server/door.ts +4 -0
- package/src/server/index.ts +11 -0
- package/src/server/preferred.ts +12 -0
- package/src/server/redirect.ts +26 -0
- package/src/server/setup.ts +14 -0
- package/src/types.ts +182 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type HamurEvent } from "hamur/server";
|
|
2
|
+
import type { LocaleInfo, ResolvedI18nOptions } from "../types.ts";
|
|
3
|
+
import { type Matcher } from "./match.ts";
|
|
4
|
+
export interface RootRedirectConfig {
|
|
5
|
+
locales: LocaleInfo[];
|
|
6
|
+
defaultLocale: string;
|
|
7
|
+
strategy: ResolvedI18nOptions["strategy"];
|
|
8
|
+
redirectToDefaultLocale?: boolean;
|
|
9
|
+
cookie: string | false;
|
|
10
|
+
/** `app.baseURL`, with its trailing `/`. */
|
|
11
|
+
baseURL: string;
|
|
12
|
+
}
|
|
13
|
+
/** The locale root page: the 200 answer of `/` under `prefix` when no locale was found. */
|
|
14
|
+
/** `origin` is `site.url`; a caller that already holds it passes it. */
|
|
15
|
+
export declare function localeRootPage(event: HamurEvent, config: RootRedirectConfig, origin?: string | undefined): string;
|
|
16
|
+
/** The answer of `GET /`, or `undefined` to let the page `/` render. */
|
|
17
|
+
export declare function rootRedirect(event: HamurEvent, config: RootRedirectConfig, origin?: string): Response | undefined;
|
|
18
|
+
export interface AllRedirectConfig extends RootRedirectConfig {
|
|
19
|
+
copies: string[];
|
|
20
|
+
matcher: Matcher;
|
|
21
|
+
/** Base route name → the codes whose copy is `false`; `"all"` does not send a visitor to one. */
|
|
22
|
+
missing?: Record<string, string[]>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The answer of `detect.redirect: "all"` for a `GET` or `HEAD` of any path: `/` as `rootRedirect`, an unprefixed
|
|
26
|
+
* page path (under `prefix`, any path a locale's copy answers) as a `302` to the target locale's copy, else `undefined`.
|
|
27
|
+
*/
|
|
28
|
+
export declare function allRedirect(event: HamurEvent, config: AllRedirectConfig, origin?: string): Response | undefined;
|
|
29
|
+
/** The door's answer for a `GET` or `HEAD`: `allRedirect` over a config that holds the route matcher, else `rootRedirect`. */
|
|
30
|
+
export declare function redirectAnswer(event: HamurEvent, config: RootRedirectConfig | AllRedirectConfig, origin?: string): Response | undefined;
|
|
31
|
+
export declare const rootStep: import("hamur/app").FunctionMiddleware<{}, {}>;
|
|
32
|
+
export declare const allStep: import("hamur/app").FunctionMiddleware<{}, {}>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { RichJoin, RichParts } from "../types.ts";
|
|
2
|
+
/** The text of a rich message with its tags removed. */
|
|
3
|
+
export declare function richText(parts: RichParts): string;
|
|
4
|
+
/** Walks the parts of a rich message; a tag with a handler becomes `handler(chunks)`, one without becomes its chunks. */
|
|
5
|
+
export declare function renderRich<N>(parts: RichParts, handlers: object, join: RichJoin<N>): N;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { LocaleInfo, Translator } from "../types.ts";
|
|
2
|
+
import { type Fallback } from "./chain.ts";
|
|
3
|
+
import { type FormatterTexts } from "./format.ts";
|
|
4
|
+
import type { MessageStore } from "./messages.ts";
|
|
5
|
+
export interface TranslatorSettings {
|
|
6
|
+
defaultLocale: LocaleInfo;
|
|
7
|
+
fallback: Fallback;
|
|
8
|
+
/** Every configured locale, to format a message taken from a locale of the chain; the default locale alone when absent. */
|
|
9
|
+
locales?: readonly LocaleInfo[];
|
|
10
|
+
timeZone: string;
|
|
11
|
+
/** Missing keys and arguments are logged once each. */
|
|
12
|
+
dev: boolean;
|
|
13
|
+
store: MessageStore;
|
|
14
|
+
}
|
|
15
|
+
/** The formatted-text table of one app context; `translatorOf(locale, scope)` keeps one translator set per scope. */
|
|
16
|
+
export interface TextScope {
|
|
17
|
+
owner: object;
|
|
18
|
+
texts: FormatterTexts;
|
|
19
|
+
}
|
|
20
|
+
/** `translator(locale)`; throws `i18n: locale "fr" is not loaded` until its catalog is in the store. */
|
|
21
|
+
export declare function createTranslators(settings: TranslatorSettings): (locale: LocaleInfo, scope?: TextScope) => Translator;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type HamurRewrite } from "hamur/app";
|
|
2
|
+
import { type HamurEvent } from "hamur/server";
|
|
3
|
+
import { type I18nConfig } from "../runtime/locale.ts";
|
|
4
|
+
import type { I18nRuntime, LocaleInfo, HamurLocale, Translator } from "../types.ts";
|
|
5
|
+
declare module "hamur/server" {
|
|
6
|
+
interface HamurEventContext {
|
|
7
|
+
locale?: HamurLocale;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export interface ServerConfig extends I18nConfig {
|
|
11
|
+
/** `app.baseURL`, with its trailing `/`. */
|
|
12
|
+
baseURL: string;
|
|
13
|
+
/** The answer at the URL of a copy that is `false`; `"404"` when absent. */
|
|
14
|
+
missingPage?: "404" | "redirect" | "rewrite";
|
|
15
|
+
/** `hamur dev`: a host no domain names falls to the order of `resolve` instead of the default locale. */
|
|
16
|
+
dev?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface ServerI18n {
|
|
19
|
+
runtime: I18nRuntime;
|
|
20
|
+
/** `app.baseURL`, with its trailing `/`. */
|
|
21
|
+
baseURL: string;
|
|
22
|
+
/**
|
|
23
|
+
* Path prefix, `?locale`, cookie, `Accept-Language`, the default; the first that names a locale wins. Under `no_prefix`
|
|
24
|
+
* and with domains a cache fork's `cacheVariant` comes first; with domains the host of `getRequestURL` then decides, and a
|
|
25
|
+
* host no domain names gets the default locale, or the order above in `hamur dev`.
|
|
26
|
+
*/
|
|
27
|
+
resolve(event: HamurEvent): LocaleInfo;
|
|
28
|
+
/**
|
|
29
|
+
* The door's step: `event.context.locale`, and under `no_prefix` and with domains `cacheVariant`, which a cache fork keeps; `Vary:
|
|
30
|
+
* Cookie, Accept-Language` under `no_prefix` and when no host decided.
|
|
31
|
+
*/
|
|
32
|
+
enter(event: HamurEvent): void;
|
|
33
|
+
/** The `302` or the `rewrite()` for the URL of a copy that is `false` under `missingPage` `"redirect"` or `"rewrite"`; `undefined` for any other request. */
|
|
34
|
+
missing(event: HamurEvent): Response | HamurRewrite | undefined;
|
|
35
|
+
}
|
|
36
|
+
export declare function createServerI18n(config: ServerConfig): ServerI18n;
|
|
37
|
+
export interface ServerTranslation {
|
|
38
|
+
locale: LocaleInfo;
|
|
39
|
+
/** `t.dynamic(key, args?)` reads a run-time key. */
|
|
40
|
+
t: Translator["t"] & {
|
|
41
|
+
dynamic: Translator["dynamic"];
|
|
42
|
+
};
|
|
43
|
+
n: Translator["n"];
|
|
44
|
+
d: Translator["d"];
|
|
45
|
+
}
|
|
46
|
+
export declare function translationOf(server: ServerI18n, event: HamurEvent): Promise<ServerTranslation>;
|
|
47
|
+
export declare const serverI18n: () => Promise<ServerI18n>;
|
|
48
|
+
export declare const localeStep: import("hamur/app").FunctionMiddleware<{}, {}>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { HamurEvent } from "hamur/server";
|
|
2
|
+
import { type ServerTranslation } from "./context.ts";
|
|
3
|
+
export { preferredLocale, preferredLocaleList } from "./preferred.ts";
|
|
4
|
+
export { i18nRedirect } from "./redirect.ts";
|
|
5
|
+
export { defineCatalog, type CatalogDefinition } from "../runtime/catalog.ts";
|
|
6
|
+
/** The translator of `event.context.locale`, or of the locale the middleware would have set. */
|
|
7
|
+
export declare function useTranslation(event: HamurEvent): Promise<ServerTranslation>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { HamurEvent } from "hamur/server";
|
|
2
|
+
import type { LocaleInfo } from "../types.ts";
|
|
3
|
+
/** Every configured locale the request's `Accept-Language` matches, best first. */
|
|
4
|
+
export declare const preferredLocaleList: (event: HamurEvent) => LocaleInfo[];
|
|
5
|
+
/** The first of `preferredLocaleList(event)`. */
|
|
6
|
+
export declare const preferredLocale: (event: HamurEvent) => LocaleInfo | undefined;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { HamurEvent } from "hamur/server";
|
|
2
|
+
/** The redirect step of the built-in door as a function, for an app that sets `detect.redirect: false` and lists its own middleware. */
|
|
3
|
+
export declare const i18nRedirect: (event: HamurEvent, mode?: "root" | "all") => Response | undefined;
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type { HamurContext } from "hamur/kit";
|
|
2
|
+
export interface LocaleInput {
|
|
3
|
+
code: string;
|
|
4
|
+
language?: string;
|
|
5
|
+
dir?: "ltr" | "rtl";
|
|
6
|
+
name?: string;
|
|
7
|
+
/** The `http:`/`https:` origin that serves this locale without a prefix; on every locale or none. */
|
|
8
|
+
domain?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface I18nOptions {
|
|
11
|
+
locales: (string | LocaleInput)[];
|
|
12
|
+
defaultLocale: string;
|
|
13
|
+
strategy?: "prefix_except_default" | "prefix" | "prefix_and_default" | "no_prefix";
|
|
14
|
+
redirectToDefaultLocale?: boolean;
|
|
15
|
+
detect?: {
|
|
16
|
+
cookie?: string | false;
|
|
17
|
+
redirect?: "root" | "all" | false;
|
|
18
|
+
};
|
|
19
|
+
/** A path per locale; `false` makes no copy of the page in that locale. */
|
|
20
|
+
pages?: Record<string, Partial<Record<string, string | false>>>;
|
|
21
|
+
/** The answer at the URL of a copy that is `false`; default `"404"`. */
|
|
22
|
+
missingPage?: "404" | "redirect" | "rewrite";
|
|
23
|
+
timeZone?: string;
|
|
24
|
+
/** `"default"`, `false`, or a chain of locale codes per code (the default locale ends every chain). */
|
|
25
|
+
fallback?: "default" | false | Record<string, string[]>;
|
|
26
|
+
/** `Link` paths go through `localePath`; default `true`. */
|
|
27
|
+
localizeLinks?: boolean;
|
|
28
|
+
/** `"route"` ships each page the messages it reads, in one chunk per locale and route; default `false`. */
|
|
29
|
+
split?: false | "route";
|
|
30
|
+
/** Keys no static `t()` call in the client build names are left out of every client chunk; default `false`. */
|
|
31
|
+
dropUnusedKeys?: boolean;
|
|
32
|
+
/** Globs (`*` one segment, `**` any) of keys `dropUnusedKeys` keeps; default `[]`. */
|
|
33
|
+
keepKeys?: string[];
|
|
34
|
+
/** The message syntax of every catalog; default `"mf1"`. */
|
|
35
|
+
syntax?: "mf1" | "mf2";
|
|
36
|
+
/** A server file below the root whose default export is `defineCatalog({ load, ttl })`, a run-time catalog. */
|
|
37
|
+
catalog?: string;
|
|
38
|
+
}
|
|
39
|
+
export interface LocaleInfo {
|
|
40
|
+
code: HamurLocale;
|
|
41
|
+
language: string;
|
|
42
|
+
dir: "ltr" | "rtl";
|
|
43
|
+
name: string;
|
|
44
|
+
}
|
|
45
|
+
/** `useLocale()` of an adapter entry: the locale of the current page and every configured locale. */
|
|
46
|
+
export type CurrentLocale = LocaleInfo & {
|
|
47
|
+
locales: readonly LocaleInfo[];
|
|
48
|
+
};
|
|
49
|
+
export interface ResolvedI18nOptions {
|
|
50
|
+
locales: LocaleInfo[];
|
|
51
|
+
defaultLocale: string;
|
|
52
|
+
strategy: "prefix_except_default" | "prefix" | "prefix_and_default" | "no_prefix";
|
|
53
|
+
redirectToDefaultLocale: boolean;
|
|
54
|
+
detect: {
|
|
55
|
+
cookie: string | false;
|
|
56
|
+
redirect: "root" | "all" | false;
|
|
57
|
+
};
|
|
58
|
+
pages: Record<string, Partial<Record<string, string | false>>>;
|
|
59
|
+
missingPage: "404" | "redirect" | "rewrite";
|
|
60
|
+
timeZone: string;
|
|
61
|
+
fallback: "default" | false | Record<string, string[]>;
|
|
62
|
+
localizeLinks: boolean;
|
|
63
|
+
split: false | "route";
|
|
64
|
+
dropUnusedKeys: boolean;
|
|
65
|
+
keepKeys: string[];
|
|
66
|
+
syntax: "mf1" | "mf2";
|
|
67
|
+
/** The path of the run-time catalog's file, as given. */
|
|
68
|
+
catalog?: string;
|
|
69
|
+
/** Locale code → origin, when the locales set `domain`. */
|
|
70
|
+
domains?: Record<string, string>;
|
|
71
|
+
}
|
|
72
|
+
export type I18nSetup = (options: ResolvedI18nOptions, hamur: HamurContext) => void | Promise<void>;
|
|
73
|
+
/** Message key → argument object, filled by typegen. */
|
|
74
|
+
export interface HamurI18nMessages {
|
|
75
|
+
}
|
|
76
|
+
/** Locale code → `true`, filled by typegen. */
|
|
77
|
+
export interface HamurI18nLocales {
|
|
78
|
+
}
|
|
79
|
+
/** Message key → the union of its tag names, filled by typegen for the keys that have tags. */
|
|
80
|
+
export interface HamurI18nTags {
|
|
81
|
+
}
|
|
82
|
+
/** A tag of a rich message and the parts between its open and close. */
|
|
83
|
+
export interface RichNode {
|
|
84
|
+
tag: string;
|
|
85
|
+
children: RichParts;
|
|
86
|
+
}
|
|
87
|
+
export type RichParts = readonly (string | RichNode)[];
|
|
88
|
+
/**
|
|
89
|
+
* The one function an adapter gives `Translator.rich`: it makes its node type from the parts of one level.
|
|
90
|
+
* A string is message text (the adapter escapes it if its output needs it); `{ node }` is what a handler or a nested level returned.
|
|
91
|
+
*/
|
|
92
|
+
export type RichJoin<N> = (parts: readonly (string | {
|
|
93
|
+
node: N;
|
|
94
|
+
})[]) => N;
|
|
95
|
+
export type HamurLocale = keyof HamurI18nLocales extends never ? string : keyof HamurI18nLocales & string;
|
|
96
|
+
export type I18nKey = keyof HamurI18nMessages extends never ? string : keyof HamurI18nMessages & string;
|
|
97
|
+
export type I18nArgsTuple<K> = keyof HamurI18nMessages extends never ? [args?: Record<string, unknown>] : K extends keyof HamurI18nMessages ? keyof HamurI18nMessages[K] extends never ? [] : [args: HamurI18nMessages[K]] : never;
|
|
98
|
+
export type I18nArgsObject<K> = keyof HamurI18nMessages extends never ? Record<string, unknown> : K extends keyof HamurI18nMessages ? HamurI18nMessages[K] : never;
|
|
99
|
+
/** `{ [tag]: (chunks) => node }` for the tags of key `K`; typegen limits the names, and a tag without a handler renders its chunks. */
|
|
100
|
+
export type RichHandlers<K, N> = keyof HamurI18nMessages extends never ? Record<string, (chunks: N) => N> : K extends keyof HamurI18nTags ? {
|
|
101
|
+
[T in HamurI18nTags[K] & string]?: (chunks: N) => N;
|
|
102
|
+
} : Record<string, never>;
|
|
103
|
+
/** `t.rich` of an adapter entry, with the adapter's node type `N` fixed. */
|
|
104
|
+
export type I18nRich<N> = <K extends I18nKey>(key: K, args: I18nArgsObject<K>, handlers: RichHandlers<K, N>) => N;
|
|
105
|
+
export interface I18nRouteLocation {
|
|
106
|
+
name: string;
|
|
107
|
+
params?: Record<string, string | string[]>;
|
|
108
|
+
query?: Record<string, string | string[]>;
|
|
109
|
+
hash?: string;
|
|
110
|
+
}
|
|
111
|
+
export interface Translator {
|
|
112
|
+
readonly locale: LocaleInfo;
|
|
113
|
+
t<K extends I18nKey>(key: K, ...args: I18nArgsTuple<K>): string;
|
|
114
|
+
/** `t` for a message with tags; `join` makes the adapter's node type of each level. */
|
|
115
|
+
rich<N, K extends I18nKey>(key: K, args: I18nArgsObject<K>, handlers: RichHandlers<K, N>, join: RichJoin<N>): N;
|
|
116
|
+
/** An untyped key: the run-time catalog, then the file catalog, then `fallback`. */
|
|
117
|
+
dynamic(key: string, args?: Record<string, unknown>): string;
|
|
118
|
+
n(value: number | bigint, options?: Intl.NumberFormatOptions): string;
|
|
119
|
+
d(value: Date | number, options?: Intl.DateTimeFormatOptions): string;
|
|
120
|
+
}
|
|
121
|
+
export interface I18nRuntime {
|
|
122
|
+
readonly locales: readonly LocaleInfo[];
|
|
123
|
+
readonly defaultLocale: LocaleInfo;
|
|
124
|
+
/** `i18n.timeZone`, `"UTC"` when unset: the zone of `d()` and of the adapters' `Time`. */
|
|
125
|
+
readonly timeZone: string;
|
|
126
|
+
/** The app's current locale: `localeOf(route.path)` under the prefix strategies, the request's or the chosen one under `no_prefix`. */
|
|
127
|
+
readonly locale: LocaleInfo;
|
|
128
|
+
/** `navigateTo(switchLocalePath(code))` under the prefix strategies; under `no_prefix` the cookie, the locale chunk and the navigator's `refresh()`. */
|
|
129
|
+
setLocale(code: HamurLocale): Promise<void>;
|
|
130
|
+
localeOf(path: string): LocaleInfo;
|
|
131
|
+
load(code: HamurLocale): Promise<void>;
|
|
132
|
+
translator(code: HamurLocale): Translator;
|
|
133
|
+
localePath(to: string | I18nRouteLocation, code: HamurLocale): string;
|
|
134
|
+
switchLocalePath(route: {
|
|
135
|
+
path: string;
|
|
136
|
+
name?: string | null;
|
|
137
|
+
params: Record<string, string | string[]>;
|
|
138
|
+
query: Record<string, string | string[]>;
|
|
139
|
+
hash: string;
|
|
140
|
+
}, code: HamurLocale): string;
|
|
141
|
+
/** `localePath(to, code)` on the locale's `domain`, else `site.url`, with `app.baseURL`; `code` is the current locale's by default. Throws with neither origin. */
|
|
142
|
+
absoluteLocalePath(to: string | I18nRouteLocation, code?: HamurLocale): string;
|
|
143
|
+
/** `absoluteLocalePath` of `to` in every locale that has a copy of it, for hreflang lists and sitemaps. */
|
|
144
|
+
allLocalePaths(to: string | I18nRouteLocation): {
|
|
145
|
+
code: string;
|
|
146
|
+
language: string;
|
|
147
|
+
url: string;
|
|
148
|
+
}[];
|
|
149
|
+
routeBaseName(name: string): string;
|
|
150
|
+
/** The configured locales `Accept-Language` (`navigator.languages` in the browser) matches, best first. */
|
|
151
|
+
preferredLocaleList(): LocaleInfo[];
|
|
152
|
+
}
|
|
153
|
+
declare module "hamur" {
|
|
154
|
+
interface HamurModuleOptions {
|
|
155
|
+
i18n?: I18nOptions;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
declare module "hamur/app" {
|
|
159
|
+
interface HamurLinkProps {
|
|
160
|
+
/** The locale of the link's path, the page's by default; `false` keeps `to` as written. */
|
|
161
|
+
locale?: HamurLocale | false;
|
|
162
|
+
}
|
|
163
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,157 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hamur/i18n",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/productdevbook/hamur.git",
|
|
9
|
+
"directory": "packages/i18n"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/types/src/index.d.ts",
|
|
14
|
+
"bun": "./src/index.ts",
|
|
15
|
+
"hamur-source": "./src/index.ts",
|
|
16
|
+
"node": "./dist/src/index.js",
|
|
17
|
+
"default": "./src/index.ts"
|
|
18
|
+
},
|
|
19
|
+
"./server": {
|
|
20
|
+
"types": "./dist/types/src/server/index.d.ts",
|
|
21
|
+
"bun": "./src/server/index.ts",
|
|
22
|
+
"hamur-source": "./src/server/index.ts",
|
|
23
|
+
"node": "./dist/src/server/index.js",
|
|
24
|
+
"default": "./src/server/index.ts"
|
|
25
|
+
},
|
|
26
|
+
"./load": {
|
|
27
|
+
"types": "./dist/types/src/load.d.ts",
|
|
28
|
+
"bun": "./src/load.ts",
|
|
29
|
+
"hamur-source": "./src/load.ts",
|
|
30
|
+
"node": "./dist/src/load.js",
|
|
31
|
+
"default": "./src/load.ts"
|
|
32
|
+
},
|
|
33
|
+
"./cli": {
|
|
34
|
+
"types": "./dist/types/src/cli.d.ts",
|
|
35
|
+
"bun": "./src/cli.ts",
|
|
36
|
+
"hamur-source": "./src/cli.ts",
|
|
37
|
+
"node": "./dist/src/cli.js",
|
|
38
|
+
"default": "./src/cli.ts"
|
|
39
|
+
},
|
|
40
|
+
"./react": {
|
|
41
|
+
"types": "./dist/types/src/adapters/react.d.ts",
|
|
42
|
+
"bun": "./src/adapters/react.ts",
|
|
43
|
+
"hamur-source": "./src/adapters/react.ts",
|
|
44
|
+
"node": "./dist/src/adapters/react.js",
|
|
45
|
+
"default": "./src/adapters/react.ts"
|
|
46
|
+
},
|
|
47
|
+
"./vue": {
|
|
48
|
+
"types": "./dist/types/src/adapters/vue.d.ts",
|
|
49
|
+
"bun": "./src/adapters/vue.ts",
|
|
50
|
+
"hamur-source": "./src/adapters/vue.ts",
|
|
51
|
+
"node": "./dist/src/adapters/vue.js",
|
|
52
|
+
"default": "./src/adapters/vue.ts"
|
|
53
|
+
},
|
|
54
|
+
"./preact": {
|
|
55
|
+
"types": "./dist/types/src/adapters/preact.d.ts",
|
|
56
|
+
"bun": "./src/adapters/preact.ts",
|
|
57
|
+
"hamur-source": "./src/adapters/preact.ts",
|
|
58
|
+
"node": "./dist/src/adapters/preact.js",
|
|
59
|
+
"default": "./src/adapters/preact.ts"
|
|
60
|
+
},
|
|
61
|
+
"./svelte": {
|
|
62
|
+
"types": "./dist/types/src/adapters/svelte.d.ts",
|
|
63
|
+
"bun": "./src/adapters/svelte.ts",
|
|
64
|
+
"hamur-source": "./src/adapters/svelte.ts",
|
|
65
|
+
"node": "./dist/src/adapters/svelte.js",
|
|
66
|
+
"default": "./src/adapters/svelte.ts"
|
|
67
|
+
},
|
|
68
|
+
"./solid": {
|
|
69
|
+
"types": "./dist/types/src/adapters/solid.d.ts",
|
|
70
|
+
"bun": "./src/adapters/solid.ts",
|
|
71
|
+
"hamur-source": "./src/adapters/solid.ts",
|
|
72
|
+
"node": "./dist/src/adapters/solid.js",
|
|
73
|
+
"default": "./src/adapters/solid.ts"
|
|
74
|
+
},
|
|
75
|
+
"./html": {
|
|
76
|
+
"types": "./dist/types/src/adapters/html.d.ts",
|
|
77
|
+
"bun": "./src/adapters/html.ts",
|
|
78
|
+
"hamur-source": "./src/adapters/html.ts",
|
|
79
|
+
"node": "./dist/src/adapters/html.js",
|
|
80
|
+
"default": "./src/adapters/html.ts"
|
|
81
|
+
},
|
|
82
|
+
"./angular": {
|
|
83
|
+
"types": "./dist/types/src/adapters/angular.d.ts",
|
|
84
|
+
"bun": "./src/adapters/angular.ts",
|
|
85
|
+
"hamur-source": "./src/adapters/angular.ts",
|
|
86
|
+
"node": "./dist/src/adapters/angular.js",
|
|
87
|
+
"default": "./src/adapters/angular.ts"
|
|
88
|
+
},
|
|
89
|
+
"./package.json": "./package.json"
|
|
90
|
+
},
|
|
91
|
+
"files": [
|
|
92
|
+
"dist",
|
|
93
|
+
"src"
|
|
94
|
+
],
|
|
95
|
+
"dependencies": {},
|
|
96
|
+
"peerDependencies": {
|
|
97
|
+
"hamur": "^0.0.2",
|
|
98
|
+
"@hamur/adapter-react": "^0.0.2",
|
|
99
|
+
"@hamur/adapter-vue": "^0.0.2",
|
|
100
|
+
"@hamur/adapter-preact": "^0.0.2",
|
|
101
|
+
"@hamur/adapter-svelte": "^0.0.2",
|
|
102
|
+
"@hamur/adapter-solid": "^0.0.2",
|
|
103
|
+
"@hamur/adapter-html": "^0.0.2",
|
|
104
|
+
"@hamur/adapter-angular": "^0.0.2",
|
|
105
|
+
"react": "^19.3.1",
|
|
106
|
+
"preact": "^11.0.2",
|
|
107
|
+
"vue": "^3.5.44",
|
|
108
|
+
"solid-js": "^2.0.4",
|
|
109
|
+
"@solidjs/web": "^2.0.4",
|
|
110
|
+
"svelte": "^5.57.2",
|
|
111
|
+
"@angular/core": "^22.3.0"
|
|
112
|
+
},
|
|
113
|
+
"peerDependenciesMeta": {
|
|
114
|
+
"@hamur/adapter-react": {
|
|
115
|
+
"optional": true
|
|
116
|
+
},
|
|
117
|
+
"@hamur/adapter-vue": {
|
|
118
|
+
"optional": true
|
|
119
|
+
},
|
|
120
|
+
"@hamur/adapter-preact": {
|
|
121
|
+
"optional": true
|
|
122
|
+
},
|
|
123
|
+
"@hamur/adapter-svelte": {
|
|
124
|
+
"optional": true
|
|
125
|
+
},
|
|
126
|
+
"@hamur/adapter-solid": {
|
|
127
|
+
"optional": true
|
|
128
|
+
},
|
|
129
|
+
"@hamur/adapter-html": {
|
|
130
|
+
"optional": true
|
|
131
|
+
},
|
|
132
|
+
"@hamur/adapter-angular": {
|
|
133
|
+
"optional": true
|
|
134
|
+
},
|
|
135
|
+
"react": {
|
|
136
|
+
"optional": true
|
|
137
|
+
},
|
|
138
|
+
"preact": {
|
|
139
|
+
"optional": true
|
|
140
|
+
},
|
|
141
|
+
"vue": {
|
|
142
|
+
"optional": true
|
|
143
|
+
},
|
|
144
|
+
"solid-js": {
|
|
145
|
+
"optional": true
|
|
146
|
+
},
|
|
147
|
+
"@solidjs/web": {
|
|
148
|
+
"optional": true
|
|
149
|
+
},
|
|
150
|
+
"svelte": {
|
|
151
|
+
"optional": true
|
|
152
|
+
},
|
|
153
|
+
"@angular/core": {
|
|
154
|
+
"optional": true
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Pipe, assertInInjectionContext, computed, signal } from "@angular/core";
|
|
2
|
+
import { injectApp, injectRoute } from "@hamur/adapter-angular";
|
|
3
|
+
import { bindFunctions, currentLocale, i18nOf, onSetLocale } from "./bind.ts";
|
|
4
|
+
import * as i0 from "@angular/core";
|
|
5
|
+
const ESCAPES = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" };
|
|
6
|
+
const escapeHtml = (text) => text.replace(/[&<>"']/g, (char) => ESCAPES[char]);
|
|
7
|
+
/** `t.rich` gives HTML for `[innerHTML]`: the message text escaped, each handler's string as written. */
|
|
8
|
+
const join = (parts) => parts.map((part) => (typeof part === "string" ? escapeHtml(part) : part.node)).join("");
|
|
9
|
+
const holders = new WeakMap();
|
|
10
|
+
function holderOf(i18n) {
|
|
11
|
+
let holder = holders.get(i18n);
|
|
12
|
+
if (!holder) {
|
|
13
|
+
const created = signal(i18n.locale, /* @ts-ignore */
|
|
14
|
+
...(ngDevMode ? [{ debugName: "created" }] : /* istanbul ignore next */ []));
|
|
15
|
+
holders.set(i18n, (holder = created));
|
|
16
|
+
onSetLocale(i18n, () => created.set(i18n.locale));
|
|
17
|
+
}
|
|
18
|
+
return holder;
|
|
19
|
+
}
|
|
20
|
+
export function injectLocale() {
|
|
21
|
+
assertInInjectionContext(injectLocale);
|
|
22
|
+
const i18n = i18nOf(injectApp());
|
|
23
|
+
const route = injectRoute();
|
|
24
|
+
const holder = holderOf(i18n);
|
|
25
|
+
return computed(() => {
|
|
26
|
+
holder();
|
|
27
|
+
return currentLocale(i18n, route().path);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
export function injectI18n() {
|
|
31
|
+
assertInInjectionContext(injectI18n);
|
|
32
|
+
const i18n = i18nOf(injectApp());
|
|
33
|
+
const route = injectRoute();
|
|
34
|
+
const holder = holderOf(i18n);
|
|
35
|
+
return {
|
|
36
|
+
locale: injectLocale(),
|
|
37
|
+
...bindFunctions(() => {
|
|
38
|
+
holder();
|
|
39
|
+
return { i18n, route: route() };
|
|
40
|
+
}, join),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* `{{ "home.title" | t: { name } }}`. Not `pure`: Angular would call a pure pipe again only when the key or the arguments
|
|
45
|
+
* object change, never when the locale does or a field of the arguments changes in place; the pipe translates on every check.
|
|
46
|
+
*/
|
|
47
|
+
export class TPipe {
|
|
48
|
+
translate = injectI18n().t;
|
|
49
|
+
transform(key, ...args) {
|
|
50
|
+
return this.translate(key, ...args);
|
|
51
|
+
}
|
|
52
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: TPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe });
|
|
53
|
+
static ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "22.3.2", ngImport: i0, type: TPipe, isStandalone: true, name: "t", pure: false });
|
|
54
|
+
}
|
|
55
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: TPipe, decorators: [{
|
|
56
|
+
type: Pipe,
|
|
57
|
+
args: [{ name: "t", pure: false }]
|
|
58
|
+
}] });
|