@hamur/i18n 0.0.0-stage → 0.0.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.
Files changed (155) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +38 -2
  3. package/dist/chunks/bun-index-1ve1hj66.js +744 -0
  4. package/dist/chunks/bun-index-8bw4xpv8.js +93 -0
  5. package/dist/chunks/bun-index-ej6vjnjs.js +297 -0
  6. package/dist/chunks/bun-index-pdepkyy1.js +474 -0
  7. package/dist/src/adapters/angular.js +67 -0
  8. package/dist/src/adapters/html.js +34 -0
  9. package/dist/src/adapters/preact.js +23 -0
  10. package/dist/src/adapters/react.js +23 -0
  11. package/dist/src/adapters/solid.js +48 -0
  12. package/dist/src/adapters/svelte.js +74 -0
  13. package/dist/src/adapters/vue.js +48 -0
  14. package/dist/src/cli.js +1898 -0
  15. package/dist/src/index.js +2755 -0
  16. package/dist/src/load.js +35 -0
  17. package/dist/src/server/index.js +395 -0
  18. package/dist/types/src/adapters/angular.d.ts +17 -0
  19. package/dist/types/src/adapters/bind.d.ts +44 -0
  20. package/dist/types/src/adapters/html.d.ts +5 -0
  21. package/dist/types/src/adapters/preact.d.ts +6 -0
  22. package/dist/types/src/adapters/react.d.ts +6 -0
  23. package/dist/types/src/adapters/solid.d.ts +9 -0
  24. package/dist/types/src/adapters/svelte.d.ts +12 -0
  25. package/dist/types/src/adapters/vue.d.ts +8 -0
  26. package/dist/types/src/build/catalog.d.ts +5 -0
  27. package/dist/types/src/build/drop.d.ts +12 -0
  28. package/dist/types/src/build/extract.d.ts +11 -0
  29. package/dist/types/src/build/head.d.ts +4 -0
  30. package/dist/types/src/build/messages.d.ts +27 -0
  31. package/dist/types/src/build/prerender.d.ts +10 -0
  32. package/dist/types/src/build/routes.d.ts +14 -0
  33. package/dist/types/src/build/split.d.ts +37 -0
  34. package/dist/types/src/build/types.d.ts +15 -0
  35. package/dist/types/src/cli.d.ts +12 -0
  36. package/dist/types/src/compiler/compile.d.ts +17 -0
  37. package/dist/types/src/compiler/mf2-parse.d.ts +62 -0
  38. package/dist/types/src/compiler/mf2.d.ts +3 -0
  39. package/dist/types/src/compiler/parse.d.ts +58 -0
  40. package/dist/types/src/compiler/skeleton.d.ts +7 -0
  41. package/dist/types/src/index.d.ts +2 -0
  42. package/dist/types/src/load.d.ts +7 -0
  43. package/dist/types/src/module.d.ts +15 -0
  44. package/dist/types/src/options.d.ts +7 -0
  45. package/dist/types/src/runtime/catalog-client.d.ts +3 -0
  46. package/dist/types/src/runtime/catalog-loader.d.ts +15 -0
  47. package/dist/types/src/runtime/catalog-server.d.ts +5 -0
  48. package/dist/types/src/runtime/catalog.d.ts +60 -0
  49. package/dist/types/src/runtime/chain.d.ts +3 -0
  50. package/dist/types/src/runtime/format.d.ts +27 -0
  51. package/dist/types/src/runtime/head.d.ts +9 -0
  52. package/dist/types/src/runtime/interpret.d.ts +20 -0
  53. package/dist/types/src/runtime/link.d.ts +8 -0
  54. package/dist/types/src/runtime/locale.d.ts +73 -0
  55. package/dist/types/src/runtime/match.d.ts +54 -0
  56. package/dist/types/src/runtime/messages.d.ts +42 -0
  57. package/dist/types/src/runtime/middleware.d.ts +2 -0
  58. package/dist/types/src/runtime/negotiate.d.ts +9 -0
  59. package/dist/types/src/runtime/plugin.d.ts +4 -0
  60. package/dist/types/src/runtime/redirect.d.ts +32 -0
  61. package/dist/types/src/runtime/rich.d.ts +5 -0
  62. package/dist/types/src/runtime/translate.d.ts +21 -0
  63. package/dist/types/src/server/catalog.d.ts +2 -0
  64. package/dist/types/src/server/context.d.ts +48 -0
  65. package/dist/types/src/server/door-all-catalog.d.ts +3 -0
  66. package/dist/types/src/server/door-all.d.ts +2 -0
  67. package/dist/types/src/server/door-catalog.d.ts +3 -0
  68. package/dist/types/src/server/door-root-catalog.d.ts +3 -0
  69. package/dist/types/src/server/door-root.d.ts +2 -0
  70. package/dist/types/src/server/door.d.ts +2 -0
  71. package/dist/types/src/server/index.d.ts +7 -0
  72. package/dist/types/src/server/preferred.d.ts +6 -0
  73. package/dist/types/src/server/redirect.d.ts +3 -0
  74. package/dist/types/src/server/setup.d.ts +2 -0
  75. package/dist/types/src/types.d.ts +163 -0
  76. package/package.json +122 -4
  77. package/src/adapters/angular.ts +65 -0
  78. package/src/adapters/bind.ts +144 -0
  79. package/src/adapters/html.ts +35 -0
  80. package/src/adapters/preact.ts +18 -0
  81. package/src/adapters/react.ts +18 -0
  82. package/src/adapters/solid.ts +44 -0
  83. package/src/adapters/svelte.ts +75 -0
  84. package/src/adapters/test/SveltePage.svelte +18 -0
  85. package/src/adapters/test/SvelteReporting.svelte +20 -0
  86. package/src/adapters/test/SvelteRich.svelte +11 -0
  87. package/src/adapters/test/SvelteRichPlain.svelte +11 -0
  88. package/src/adapters/test/peer.ts +7 -0
  89. package/src/adapters/test/preact-client.ts +43 -0
  90. package/src/adapters/test/preact-page.ts +34 -0
  91. package/src/adapters/test/preact-rich.ts +9 -0
  92. package/src/adapters/test/react-client.ts +49 -0
  93. package/src/adapters/test/react-page.ts +34 -0
  94. package/src/adapters/test/react-rich.ts +15 -0
  95. package/src/adapters/test/runtime.ts +104 -0
  96. package/src/adapters/test/solid-client.ts +33 -0
  97. package/src/adapters/test/solid-page.ts +30 -0
  98. package/src/adapters/test/solid-rich.ts +5 -0
  99. package/src/adapters/test/svelte-client.ts +51 -0
  100. package/src/adapters/test/svelte-log.ts +2 -0
  101. package/src/adapters/test/svelte-plugin.ts +20 -0
  102. package/src/adapters/test/vue-page.ts +26 -0
  103. package/src/adapters/test/vue-rich.ts +16 -0
  104. package/src/adapters/vue.ts +44 -0
  105. package/src/build/catalog.ts +44 -0
  106. package/src/build/drop.ts +36 -0
  107. package/src/build/extract.ts +207 -0
  108. package/src/build/head.ts +10 -0
  109. package/src/build/messages.ts +347 -0
  110. package/src/build/prerender.ts +48 -0
  111. package/src/build/routes.ts +269 -0
  112. package/src/build/split.ts +240 -0
  113. package/src/build/types.ts +156 -0
  114. package/src/cli.ts +25 -0
  115. package/src/compiler/compile.ts +190 -0
  116. package/src/compiler/mf2-parse.ts +469 -0
  117. package/src/compiler/mf2.ts +393 -0
  118. package/src/compiler/parse.ts +265 -0
  119. package/src/compiler/skeleton.ts +85 -0
  120. package/src/env.d.ts +55 -0
  121. package/src/index.ts +24 -0
  122. package/src/load.ts +41 -0
  123. package/src/module.ts +66 -0
  124. package/src/options.ts +198 -0
  125. package/src/runtime/catalog-client.ts +28 -0
  126. package/src/runtime/catalog-loader.ts +28 -0
  127. package/src/runtime/catalog-server.ts +36 -0
  128. package/src/runtime/catalog.ts +190 -0
  129. package/src/runtime/chain.ts +9 -0
  130. package/src/runtime/format.ts +102 -0
  131. package/src/runtime/head.ts +57 -0
  132. package/src/runtime/interpret.ts +504 -0
  133. package/src/runtime/link.ts +36 -0
  134. package/src/runtime/locale.ts +392 -0
  135. package/src/runtime/match.ts +334 -0
  136. package/src/runtime/messages.ts +129 -0
  137. package/src/runtime/middleware.ts +32 -0
  138. package/src/runtime/negotiate.ts +61 -0
  139. package/src/runtime/plugin.ts +69 -0
  140. package/src/runtime/redirect.ts +191 -0
  141. package/src/runtime/rich.ts +21 -0
  142. package/src/runtime/translate.ts +131 -0
  143. package/src/server/catalog.ts +11 -0
  144. package/src/server/context.ts +191 -0
  145. package/src/server/door-all-catalog.ts +7 -0
  146. package/src/server/door-all.ts +5 -0
  147. package/src/server/door-catalog.ts +6 -0
  148. package/src/server/door-root-catalog.ts +7 -0
  149. package/src/server/door-root.ts +5 -0
  150. package/src/server/door.ts +4 -0
  151. package/src/server/index.ts +11 -0
  152. package/src/server/preferred.ts +12 -0
  153. package/src/server/redirect.ts +26 -0
  154. package/src/server/setup.ts +14 -0
  155. package/src/types.ts +182 -0
@@ -0,0 +1,37 @@
1
+ import type { HamurContext } from "hamur/kit";
2
+ export declare const SOURCE: RegExp;
3
+ /** What one file asks of the catalog: the literal keys of its `t` calls, and whether any call has a key that is not a literal. */
4
+ export interface KeyRead {
5
+ keys: Set<string>;
6
+ dynamic: boolean;
7
+ /** Offset of the first call whose key is not a literal. */
8
+ at?: number;
9
+ /** Key to the offset of its first call. */
10
+ positions: Map<string, number>;
11
+ }
12
+ /**
13
+ * The `t("<literal>")`, `t.rich("<literal>")` and `` t(`<no ${}>`) `` calls of one file, under the names a `useI18n()` result was bound to.
14
+ * With `withDynamic`, `t.dynamic("<literal>")` is a key and `t.dynamic(x)` a dynamic call too.
15
+ */
16
+ export declare function readKeys(source: string, withDynamic?: boolean): KeyRead;
17
+ export interface SplitReader {
18
+ /** The keys of `entries` and of the files they import statically, and the files among them with a key that is not a literal. */
19
+ read(entries: string[]): Promise<{
20
+ keys: Set<string>;
21
+ dynamic: string[];
22
+ }>;
23
+ }
24
+ /** Reads each file once; only files under `roots` (the app and its layers, not `node_modules`) are followed. */
25
+ export declare function createSplitReader(roots: string[]): SplitReader;
26
+ export interface SplitKeys {
27
+ keys: Set<string>;
28
+ /** The files whose route ships the whole catalog. */
29
+ dynamic: string[];
30
+ }
31
+ export interface SplitPlan {
32
+ shared: SplitKeys;
33
+ /** Route base name → keys of its page chain and boundaries, those of `shared` left out. */
34
+ routes: Map<string, SplitKeys>;
35
+ }
36
+ /** What each route, and the layouts, `app/app.*` and `app/error.*`, read from the catalogs. */
37
+ export declare function planSplit(hamur: HamurContext): Promise<SplitPlan>;
@@ -0,0 +1,15 @@
1
+ import { type HamurContext } from "hamur/kit";
2
+ import type { I18nSetup, ResolvedI18nOptions } from "../types.ts";
3
+ export interface LocaleAnalysis {
4
+ /** Key to argument name to its TypeScript type, from the default locale. */
5
+ messages: Record<string, Record<string, string>>;
6
+ /** Key to the tag names of its message, from the default locale; only keys with tags. */
7
+ tags: Record<string, string[]>;
8
+ warnings: string[];
9
+ }
10
+ /** The argument types of the default locale and the cross-locale checks; an argument a translation adds or retypes throws. */
11
+ export declare function analyzeLocales(options: ResolvedI18nOptions, roots: string[], rootDir: string): Promise<LocaleAnalysis>;
12
+ export declare function typesTemplate(options: ResolvedI18nOptions, analysis: LocaleAnalysis): string;
13
+ /** Under `prefix` no page is `/`, but the root redirect answers it, so a `Link` to `/` type-checks. */
14
+ export declare function rootPathTypes(options: ResolvedI18nOptions, hamur: HamurContext): string;
15
+ export declare const setupTypes: I18nSetup;
@@ -0,0 +1,12 @@
1
+ import type { HamurContext } from "hamur/kit";
2
+ export interface I18nCommand {
3
+ command: "extract";
4
+ check?: boolean;
5
+ prune?: boolean;
6
+ out(line: string): void;
7
+ }
8
+ /**
9
+ * The body of `hamur i18n`, on a context `createContext` made with the dev config.
10
+ * Resolves to the exit code; a failure rejects with the message the CLI prints.
11
+ */
12
+ export declare function runI18nCommand(args: I18nCommand, hamur: HamurContext): Promise<number>;
@@ -0,0 +1,17 @@
1
+ export type ArgKind = "text" | "number" | "date" | "select";
2
+ export interface CompileOptions {
3
+ /** The file a key came from, for error and warning text. */
4
+ file?: string | ((key: string) => string);
5
+ /** Gets each warning; the default drops them. */
6
+ warn?: (text: string) => void;
7
+ /** The message syntax; default `"mf1"`. */
8
+ syntax?: "mf1" | "mf2";
9
+ }
10
+ export interface CompiledCatalog {
11
+ code: string;
12
+ args: Record<string, Record<string, ArgKind>>;
13
+ /** The tag names of each message that has tags, in order of first use. */
14
+ tags: Record<string, string[]>;
15
+ }
16
+ /** The module text of one locale (`export default { key: string | (a, f) => string }`, no import) and the argument kinds of each message. */
17
+ export declare function compileCatalog(messages: Record<string, string>, language: string, options?: CompileOptions): CompiledCatalog;
@@ -0,0 +1,62 @@
1
+ export interface Literal {
2
+ kind: "literal";
3
+ value: string;
4
+ }
5
+ export interface Variable {
6
+ kind: "variable";
7
+ name: string;
8
+ at: number;
9
+ }
10
+ export interface Option {
11
+ name: string;
12
+ value: Literal | Variable;
13
+ at: number;
14
+ }
15
+ export interface Call {
16
+ name: string;
17
+ at: number;
18
+ options: Option[];
19
+ }
20
+ export interface Expression {
21
+ operand?: Literal | Variable;
22
+ call?: Call;
23
+ at: number;
24
+ }
25
+ export type Part = {
26
+ type: "text";
27
+ value: string;
28
+ } | {
29
+ type: "expression";
30
+ expression: Expression;
31
+ } | {
32
+ type: "markup";
33
+ kind: "open" | "close" | "standalone";
34
+ name: string;
35
+ };
36
+ export interface Declaration {
37
+ input: boolean;
38
+ name: string;
39
+ expression: Expression;
40
+ at: number;
41
+ }
42
+ export interface Variant {
43
+ keys: (string | null)[];
44
+ parts: Part[];
45
+ at: number;
46
+ }
47
+ export interface Matcher {
48
+ at: number;
49
+ selectors: {
50
+ name: string;
51
+ at: number;
52
+ }[];
53
+ variants: Variant[];
54
+ }
55
+ export interface Message {
56
+ declarations: Declaration[];
57
+ parts: Part[];
58
+ matcher?: Matcher;
59
+ }
60
+ export declare const FUNCTIONS: Record<string, readonly string[]>;
61
+ /** One MessageFormat 2 message, parsed and checked; throws `MessageSyntaxError` with the offset of the fault. */
62
+ export declare function parseMf2(source: string): Message;
@@ -0,0 +1,3 @@
1
+ import type { CompileOptions, CompiledCatalog } from "./compile.ts";
2
+ /** The module text of one locale from MessageFormat 2 messages, in the shape `compileCatalog` gives for MF1. */
3
+ export declare function compileMf2Catalog(messages: Record<string, string>, language: string, options?: CompileOptions): CompiledCatalog;
@@ -0,0 +1,58 @@
1
+ export type MessageNode = TextNode | ArgNode | NumberNode | DateNode | PluralNode | SelectNode | PoundNode | TagNode;
2
+ export interface TextNode {
3
+ type: "text";
4
+ value: string;
5
+ }
6
+ export interface ArgNode {
7
+ type: "arg";
8
+ name: string;
9
+ }
10
+ export interface NumberNode {
11
+ type: "number";
12
+ name: string;
13
+ style: "decimal" | "integer" | "percent" | "currency" | "skeleton";
14
+ currency?: string;
15
+ /** `style: "skeleton"`: the `Intl.NumberFormat` options of `::<skeleton>`. */
16
+ options?: Record<string, unknown>;
17
+ scale?: number;
18
+ }
19
+ export interface DateNode {
20
+ type: "date";
21
+ name: string;
22
+ kind: "date" | "time";
23
+ style: "short" | "medium" | "long" | "full";
24
+ }
25
+ export interface PluralNode {
26
+ type: "plural";
27
+ name: string;
28
+ ordinal: boolean;
29
+ offset: number;
30
+ cases: {
31
+ key: string;
32
+ body: MessageNode[];
33
+ }[];
34
+ }
35
+ export interface SelectNode {
36
+ type: "select";
37
+ name: string;
38
+ cases: {
39
+ key: string;
40
+ body: MessageNode[];
41
+ }[];
42
+ }
43
+ export interface PoundNode {
44
+ type: "pound";
45
+ }
46
+ /** `<b>…</b>`; `<br/>` has no children. */
47
+ export interface TagNode {
48
+ type: "tag";
49
+ name: string;
50
+ children: MessageNode[];
51
+ }
52
+ export declare class MessageSyntaxError extends Error {
53
+ readonly offset: number;
54
+ readonly reason: string;
55
+ constructor(offset: number, reason: string);
56
+ }
57
+ /** The ICU MessageFormat 1 subset as a node list; throws `MessageSyntaxError` with the 0-based offset of the fault. */
58
+ export declare function parseMessage(source: string): MessageNode[];
@@ -0,0 +1,7 @@
1
+ export interface Skeleton {
2
+ options: Record<string, unknown>;
3
+ /** The value is multiplied by this before formatting. */
4
+ scale?: number;
5
+ }
6
+ /** The tokens after `::` as `Intl.NumberFormat` options; `offset` is where `source` starts in the message. */
7
+ export declare function parseSkeleton(source: string, offset: number): Skeleton;
@@ -0,0 +1,2 @@
1
+ export { default } from "./module.ts";
2
+ export type { CurrentLocale, I18nArgsObject, I18nArgsTuple, I18nKey, I18nOptions, I18nRich, I18nRouteLocation, I18nRuntime, I18nSetup, LocaleInfo, LocaleInput, ResolvedI18nOptions, HamurI18nLocales, HamurI18nMessages, HamurI18nTags, HamurLocale, RichHandlers, RichJoin, RichNode, RichParts, Translator, } from "./types.ts";
@@ -0,0 +1,7 @@
1
+ import type { LoadContext } from "hamur/app";
2
+ import type { ServerTranslation } from "./server/context.ts";
3
+ /**
4
+ * The translator of the locale a `load` renders in, on both sides. On the server `ctx.event.context.locale`, as
5
+ * `useTranslation(event)` of `@hamur/i18n/server`; in the browser the app's locale, its messages loaded first.
6
+ */
7
+ export declare function useTranslation(ctx: LoadContext): Promise<ServerTranslation>;
@@ -0,0 +1,15 @@
1
+ import { type HamurContext } from "hamur/kit";
2
+ import type { I18nOptions, ResolvedI18nOptions } from "./types.ts";
3
+ /** The options each context's setup resolved, for `runI18nCommand`. */
4
+ export declare const resolvedOptions: WeakMap<HamurContext, ResolvedI18nOptions>;
5
+ /** One static host serves one domain. */
6
+ export declare const DOMAIN_STATIC = "i18n: locales[].domain needs a server preset (static serves one host)";
7
+ /** A static host has no server to call `load` or answer `_hamur/i18n/<code>`. */
8
+ export declare const CATALOG_STATIC = "i18n.catalog needs a server preset (static has no server to load the catalog)";
9
+ /** `getRequestURL` answers the public origin's host for every request, so no other domain is ever seen. */
10
+ export declare const DOMAIN_ORIGIN = "i18n: locales[].domain cannot be used with app.origin, which gives every request one host";
11
+ /** The presets whose CDN keeps an `isr` answer by path, not host. */
12
+ export declare const PATH_KEYED_ISR: Set<string>;
13
+ export declare const domainIsrError: (preset: string, pattern: string) => string;
14
+ declare const _default: import("hamur/kit").HamurModule<I18nOptions>;
15
+ export default _default;
@@ -0,0 +1,7 @@
1
+ import type { I18nOptions, ResolvedI18nOptions } from "./types.ts";
2
+ export declare const CODE: RegExp;
3
+ /** `i18n.catalog` naming no file. */
4
+ export declare const catalogMissing: (catalog: unknown) => string;
5
+ export declare const BASE_URL_REMOVED = "i18n.baseUrl was removed: set site.url (HAMUR_SITE_URL at run time)";
6
+ /** The options with their defaults, or the build error of the first value that is wrong. */
7
+ export declare function resolveOptions(input: Partial<I18nOptions> | undefined): ResolvedI18nOptions;
@@ -0,0 +1,3 @@
1
+ import { type CatalogFrom } from "./catalog-loader.ts";
2
+ /** `GET <baseURL>_hamur/i18n/<code>` once per document and code (the head preloads it), in `messageStore` before the render. */
3
+ export declare function loadCatalog(code: string, from: CatalogFrom): Promise<undefined>;
@@ -0,0 +1,15 @@
1
+ import type { HamurEvent } from "hamur/server";
2
+ /** What `loadCatalog` of the server or the client module needs: the request on the server, `app.baseURL` in the browser. */
3
+ export interface CatalogFrom {
4
+ event?: HamurEvent | undefined;
5
+ baseURL: string;
6
+ /** In the browser, the version the server rendered with (`app.modulePayload.i18nCatalog`). */
7
+ version?: string | undefined;
8
+ }
9
+ /** Loads the run-time catalog of `code` into `messageStore`; the server's answers the version it holds. */
10
+ export type CatalogLoader = (code: string, from: CatalogFrom) => Promise<string | undefined>;
11
+ /** The preload and fetch URL; `?v=` names the catalog the server rendered with, so the browser never reuses another. */
12
+ export declare const catalogURL: (baseURL: string, code: string, version: string | undefined) => string;
13
+ /** The server's loader, which the module's door installs on its first request when `i18n.catalog` is set. */
14
+ export declare const serverCatalog: () => CatalogLoader | undefined;
15
+ export declare function installServerCatalog(loader: CatalogLoader): void;
@@ -0,0 +1,5 @@
1
+ import { type HamurEvent } from "hamur/server";
2
+ import type { CatalogFrom } from "./catalog-loader.ts";
3
+ /** The run-time catalog of `code`, in `messageStore` for the render of `from.event`. */
4
+ export declare function loadCatalog(code: string, from: CatalogFrom): Promise<string | undefined>;
5
+ export declare const catalogAnswer: (event: HamurEvent) => Promise<Response>;
@@ -0,0 +1,60 @@
1
+ import type { HamurEvent } from "hamur/server";
2
+ import type { HamurLocale } from "../types.ts";
3
+ import type { MessageStore } from "./messages.ts";
4
+ /** The default export of the file `i18n.catalog` names. */
5
+ export interface CatalogDefinition {
6
+ /** The messages of `code`, source strings in the app's `syntax`; called once per code and `ttl`. */
7
+ load(code: HamurLocale, event: HamurEvent): Promise<Record<string, string>> | Record<string, string>;
8
+ /** Seconds the parsed catalog is kept; default 300. */
9
+ ttl?: number;
10
+ }
11
+ export declare const DEFAULT_TTL = 300;
12
+ /** The run-time catalog of `i18n.catalog`; checks `load` and `ttl` and returns the definition. */
13
+ export declare function defineCatalog(definition: CatalogDefinition): CatalogDefinition;
14
+ /** The value kept in `useStorage("cache")` under `i18n:catalog:<code>`: source strings, parsed again by each process, so no build's parse tree outlives it. */
15
+ export interface StoredCatalog {
16
+ syntax: "mf1" | "mf2";
17
+ /** Milliseconds since the epoch. */
18
+ expires: number;
19
+ messages: Record<string, string>;
20
+ }
21
+ export interface CatalogStorage {
22
+ getItem(key: string): Promise<unknown>;
23
+ setItem(key: string, value: StoredCatalog, options: {
24
+ ttl: number;
25
+ }): Promise<void>;
26
+ }
27
+ export interface CatalogEntry {
28
+ /** The messages that parsed; `{}` after a failed load. */
29
+ messages: Record<string, string>;
30
+ expires: number;
31
+ ttl: number;
32
+ failed: boolean;
33
+ /** The `?v=` of the preload and the client's fetch: the catalog this entry holds. */
34
+ version: string;
35
+ }
36
+ /** A `load` that has not answered after this many milliseconds fails. */
37
+ export declare const LOAD_TIMEOUT = 10000;
38
+ export interface CatalogCacheOptions {
39
+ definition: () => Promise<CatalogDefinition>;
40
+ syntax: "mf1" | "mf2";
41
+ codes: readonly HamurLocale[];
42
+ store: MessageStore;
43
+ storage?: () => CatalogStorage;
44
+ warn: (line: string) => void;
45
+ now?: () => number;
46
+ /** Milliseconds; default `LOAD_TIMEOUT`. */
47
+ timeout?: number;
48
+ }
49
+ export interface CatalogCache {
50
+ /**
51
+ * The entry of `code`, loaded at most once per `ttl`; concurrent calls share one load. Once `ttl` passes the stale entry
52
+ * answers while one load refreshes it in the background. `undefined` for a code not configured.
53
+ */
54
+ ensure(code: string, event: HamurEvent | undefined): Promise<CatalogEntry | undefined>;
55
+ }
56
+ export declare const catalogKey: (code: string) => string;
57
+ /** The server's run-time catalogs: parsed once per code and `ttl`, kept in storage, installed in `store`; a failure logs once per `ttl`. */
58
+ export declare function createCatalogCache(options: CatalogCacheOptions): CatalogCache;
59
+ /** `GET <baseURL>_hamur/i18n/<code>`: the run-time messages as JSON, 404 for a code not configured, 405 for another method. */
60
+ export declare function answerCatalog(request: Request, cache: CatalogCache, event: HamurEvent | undefined): Promise<Response>;
@@ -0,0 +1,3 @@
1
+ export type Fallback = "default" | false | Record<string, string[]>;
2
+ /** The locales a missing key of `code` is looked up in, in order; the default locale ends every chain but its own. */
3
+ export declare function fallbackChain(fallback: Fallback | undefined, defaultLocale: string, code: string): string[];
@@ -0,0 +1,27 @@
1
+ import type { RichParts } from "../types.ts";
2
+ type Out = string | RichParts;
3
+ export type PluralCase = Out | ((count: string) => Out);
4
+ /** The formatter set `f` the compiled messages receive, bound to one locale. */
5
+ export interface Formatters {
6
+ n(value: number | bigint | unknown, options?: Intl.NumberFormatOptions): string;
7
+ d(value: Date | number | unknown, options?: Intl.DateTimeFormatOptions): string;
8
+ p(value: unknown, offset: number, cases: Record<string, PluralCase>, ordinal: boolean): Out;
9
+ s(value: unknown, cases: Record<string, Out | (() => Out)>): Out;
10
+ }
11
+ export declare const numberFormat: (language: string, options?: Intl.NumberFormatOptions) => Intl.NumberFormat;
12
+ export declare const dateFormat: (language: string, options: Intl.DateTimeFormatOptions) => Intl.DateTimeFormat;
13
+ export declare const pluralRule: (language: string, type: Intl.PluralRulesOptions["type"]) => Intl.PluralRules;
14
+ /** The server's formatted text by `<kind>|<language>|<options>`, then by the value's key (a `Date` by its time, `-0` apart from `0`). */
15
+ export type FormattedTexts = Record<string, Record<string, string>>;
16
+ export interface FormatterTexts {
17
+ read(): FormattedTexts | undefined;
18
+ write: boolean;
19
+ /** Called on the first write when `read()` has no table yet. */
20
+ create?(): FormattedTexts;
21
+ }
22
+ /**
23
+ * `Intl` formatters are cached per language and options; `timeZone` is the config's unless a call's options set one.
24
+ * With `texts`, `n`, `d` and the plural category read the server's text first.
25
+ */
26
+ export declare function createFormatters(language: string, timeZone: string, texts?: FormatterTexts): Formatters;
27
+ export {};
@@ -0,0 +1,9 @@
1
+ import type { ResolvedRoute } from "hamur/adapter";
2
+ import { type AppContext } from "hamur/app";
3
+ /**
4
+ * The one i18n head entry of `app`, written on the first call and patched after; under `no_prefix` the locale is
5
+ * `i18n.locale` and no alternate is written; with domains the locale is `i18n.locale` and each link's origin is its locale's domain.
6
+ */
7
+ export declare function applyLocaleHead(app: AppContext, route: ResolvedRoute): void;
8
+ /** Browser only: writes the stacked entries to the document once the new page has rendered; the adapters apply only from `useHead`. */
9
+ export declare function commitLocaleHead(app: AppContext): void;
@@ -0,0 +1,20 @@
1
+ import { type Message as Mf2Message } from "../compiler/mf2-parse.ts";
2
+ import { type MessageNode } from "../compiler/parse.ts";
3
+ import type { Catalog } from "./messages.ts";
4
+ /** A run-time message, parsed: MF1 nodes, or an MF2 message under `syntax: "mf2"`. */
5
+ export type ParsedMessage = MessageNode[] | Mf2Message;
6
+ export type ParsedCatalog = Record<string, ParsedMessage>;
7
+ export interface ParsedRun {
8
+ /** The messages that parsed, as given. */
9
+ messages: Record<string, string>;
10
+ parsed: ParsedCatalog;
11
+ /** A message that is not a string or does not parse: its key and the reason, with the column. */
12
+ skipped: {
13
+ key: string;
14
+ reason: string;
15
+ }[];
16
+ }
17
+ /** Each message of `messages` parsed once; one that fails is skipped with its reason. */
18
+ export declare function parseCatalog(messages: unknown, syntax: "mf1" | "mf2"): ParsedRun;
19
+ /** The catalog of `parsed` in the compiled modules' shape, each message rendered by walking its nodes. */
20
+ export declare function interpretCatalog(parsed: ParsedCatalog, syntax: "mf1" | "mf2"): Catalog;
@@ -0,0 +1,8 @@
1
+ import type { LinkResolver } from "hamur/app";
2
+ import type { I18nRuntime } from "../types.ts";
3
+ /**
4
+ * The link resolver of `localizeLinks`: `to` in the `locale` prop's locale, the page's by default; `locale={false}`
5
+ * or `""` keeps it. A file (a last segment with an extension), a server route of `server` (its patterns), `/api` and
6
+ * `/_hamur` are left as written, since a catch-all page would match them.
7
+ */
8
+ export declare function i18nLinkResolver(i18n: I18nRuntime, server?: readonly string[]): LinkResolver;
@@ -0,0 +1,73 @@
1
+ import { type AppContext } from "hamur/app";
2
+ import type { I18nRuntime, LocaleInfo, ResolvedI18nOptions, HamurLocale } from "../types.ts";
3
+ import type { Fallback } from "./chain.ts";
4
+ import { type RouteTable } from "./match.ts";
5
+ /** What the runtime reads from `#hamur/i18n/options` and `#hamur/i18n/routes`. */
6
+ export interface I18nConfig extends RouteTable {
7
+ locales: LocaleInfo[];
8
+ defaultLocale: string;
9
+ strategy: ResolvedI18nOptions["strategy"];
10
+ cookie: string | false;
11
+ /** `app.baseURL` with its trailing `/`, the fallback of `app.config.runtimeApp`. */
12
+ baseURL?: string;
13
+ /** `Intl` time zone of `d()` and date arguments; `"UTC"` when absent. */
14
+ timeZone?: string;
15
+ /** `"default"` when absent. */
16
+ fallback?: Fallback;
17
+ }
18
+ /** The prefix of the `cacheVariant` the server door sets under `no_prefix`, `locale:<code>`. */
19
+ export declare const LOCALE_VARIANT = "locale:";
20
+ export declare function configOf(runtime: I18nRuntime): I18nConfig;
21
+ /** Loads `code` for route `base` through an app's runtime (the route middleware); `undefined` for a runtime that is not bound to an app. */
22
+ export declare const routeLoadOf: (runtime: I18nRuntime) => ((code: string, base: string | undefined) => Promise<void>) | undefined;
23
+ /** The locale a request's context names: `locale`, else the `cacheVariant` a cache fork keeps. */
24
+ export declare function contextLocale(runtime: I18nRuntime, context: {
25
+ locale?: unknown;
26
+ cacheVariant?: unknown;
27
+ } | undefined): LocaleInfo | undefined;
28
+ /** The locale cookie, for a year, on `app.baseURL`. */
29
+ export declare function writeLocaleCookie(app: AppContext, cookie: string, code: string): void;
30
+ /** True when a page's locale comes from the request (`no_prefix`, or the host under `locales[].domain`), not from its path. */
31
+ export declare function readsRequestLocale(runtime: I18nRuntime): boolean;
32
+ /**
33
+ * What a page route answers on a host of `code`: `true` renders it, a string is the app path (query kept) of the
34
+ * host locale's localized copy that an original answers `301` with, and `false` is another locale's copy, a 404.
35
+ */
36
+ export declare function domainRoute(runtime: I18nRuntime, route: {
37
+ name?: string | null;
38
+ params: Record<string, string | string[]>;
39
+ query: Record<string, string | string[]>;
40
+ }, code: HamurLocale): boolean | string;
41
+ /** The page `path` (below `app.baseURL`) names when that locale's copy is `false`: the base route name, the code and the params. */
42
+ export declare function missingCopyOf(runtime: I18nRuntime, path: string): {
43
+ name: string;
44
+ code: string;
45
+ params: Record<string, string | string[]>;
46
+ } | undefined;
47
+ /** Whether the copy of route `name` in `code` is `false`, so it has a path but no page. */
48
+ export declare const copyMissing: (runtime: I18nRuntime, name: string, code: string) => boolean;
49
+ export declare const routeBaseName: (name: string) => string;
50
+ /** The runtime `useTranslation(ctx)` of `@hamur/i18n/load` reads, which the app plugin holds: the process's on the server, the app's in the browser. */
51
+ export declare const loadRuntime: {
52
+ current?: I18nRuntime;
53
+ };
54
+ /** `app.i18n` over the route table and the resolved locales. */
55
+ export declare function createI18nRuntime(config: I18nConfig): I18nRuntime;
56
+ export interface AppLocaleOptions {
57
+ /** On the client, the app path the document was loaded at. */
58
+ path?: string;
59
+ /** On the client, the document's `?locale` value: under `no_prefix` the cookie keeps the locale it chose. */
60
+ queryLocale?: string;
61
+ /** `navigateTo` of `hamur/app`. */
62
+ navigate?: (to: string) => Promise<void>;
63
+ /** The navigator's `refresh()`: `refreshData()` of `hamur/app`. */
64
+ refresh?: () => Promise<void>;
65
+ /** Loads the run-time catalog of a locale beside its chunks, when `i18n.catalog` is set. */
66
+ catalog?: (code: string) => Promise<void>;
67
+ }
68
+ /**
69
+ * `i18n` with translators bound to `app`. On the server they record what `n`, `d` and plurals print in
70
+ * `app.modulePayload.i18n.texts` from the page render's `page:start`, and an error page starts a table of its own at `page:error`;
71
+ * on the client they read it until the first navigation after `app:mounted`. `locale` and `setLocale` of this app.
72
+ */
73
+ export declare function forApp(i18n: I18nRuntime, app: AppContext, server?: boolean, options?: AppLocaleOptions): I18nRuntime;
@@ -0,0 +1,54 @@
1
+ export type MixedPart = string | {
2
+ param: string;
3
+ optional?: true;
4
+ };
5
+ export type Segment = {
6
+ kind: "static";
7
+ text: string;
8
+ } | {
9
+ kind: "mixed";
10
+ parts: MixedPart[];
11
+ } | {
12
+ kind: "param";
13
+ name: string;
14
+ } | {
15
+ kind: "optional";
16
+ name: string;
17
+ } | {
18
+ kind: "catchAll";
19
+ name: string;
20
+ } | {
21
+ kind: "optionalCatchAll";
22
+ name: string;
23
+ };
24
+ /** `#hamur/i18n/routes`: each base route name's pattern per locale code, and the codes that have a URL prefix. */
25
+ export interface RouteTable {
26
+ copies: string[];
27
+ routes: Record<string, Record<string, string>>;
28
+ /** Paths that match like `routes` but are never built: the default locale's prefixed copies under `prefix_and_default`. */
29
+ aliases?: Record<string, Record<string, string>>;
30
+ /** Base route name → the codes whose copy is `false` (`i18n.pages`); the path such a copy would have had is in `routes`, but no page answers it. */
31
+ missing?: Record<string, string[]>;
32
+ /** Locale code → origin; every code then has a path in `routes`, and `copies` is empty. */
33
+ domains?: Record<string, string>;
34
+ }
35
+ export interface PathMatch {
36
+ name: string;
37
+ code: string;
38
+ params: Record<string, string | string[]>;
39
+ }
40
+ /** The run of `-_.~` that leaves with the optional param at `index` of a mixed segment: after it, or before it when it ends the segment. */
41
+ export declare function optionalRun(parts: MixedPart[], index: number): {
42
+ side: "before" | "after";
43
+ run: string;
44
+ };
45
+ /** The segments of a route pattern; throws when it is not one. */
46
+ export declare function parsePattern(pattern: string): Segment[];
47
+ export interface Matcher {
48
+ /** The base route name, locale code and params of an app path; one trailing `/` is ignored and segments are percent-decoded. */
49
+ matchPath(path: string): PathMatch | undefined;
50
+ /** The app path of a base route name in a locale, its params filled in. */
51
+ buildPath(name: string, code: string, params?: Record<string, string | string[]>): string;
52
+ }
53
+ /** The matcher over `#hamur/i18n/routes`, ranking as the core router does. */
54
+ export declare function createMatcher(table: RouteTable): Matcher;
@@ -0,0 +1,42 @@
1
+ import type { AppContext } from "hamur/app";
2
+ import type { RichParts } from "../types.ts";
3
+ import type { Formatters } from "./format.ts";
4
+ /** A message with tags returns its parts; any other returns a string. */
5
+ export type Message = string | ((args: Record<string, unknown>, f: Formatters) => string | RichParts);
6
+ export type Catalog = Record<string, Message>;
7
+ export type MessageLoaders = Record<string, () => Promise<{
8
+ default: Catalog;
9
+ fallbacks?: string[];
10
+ }>>;
11
+ /** Locale code → `_shared` and route base name → its chunk, under `split: "route"` on the client. */
12
+ export type RouteMessageLoaders = Record<string, Record<string, () => Promise<{
13
+ default: Catalog;
14
+ }>>>;
15
+ /** `#hamur/i18n/chunks`: a locale's chunk URL, or under `split: "route"` its `_shared` and route chunk URLs. */
16
+ export type MessageChunks = Record<string, string | Record<string, string>>;
17
+ export interface MessageStore {
18
+ load(code: string): Promise<void>;
19
+ loadAll(): Promise<void>;
20
+ get(code: string): Catalog | undefined;
21
+ /** Keys the build filled from the default locale (dev builds only). */
22
+ filled(code: string): ReadonlySet<string> | undefined;
23
+ /** The `_shared` chunk and the route's chunk of `code` once each; the whole catalog when the build did not split. */
24
+ loadRoute(code: string, base: string | undefined): Promise<void>;
25
+ install(loaders: MessageLoaders, chunks?: MessageChunks, routes?: RouteMessageLoaders): void;
26
+ /** The chunk URLs the page of route `base` needs in `code`. */
27
+ chunksOf(code: string, base: string | undefined): string[];
28
+ /** The run-time catalog of `code`, whose messages win over the file catalog's key by key. */
29
+ remote(code: string): Catalog | undefined;
30
+ /** `undefined` drops it, so the file catalog answers. */
31
+ setRemote(code: string, catalog: Catalog | undefined): void;
32
+ }
33
+ /** The compiled catalogs of the locales loaded so far; a locale loads once. */
34
+ export declare function createMessageStore(): MessageStore;
35
+ export declare const messageStore: MessageStore;
36
+ export declare const loadMessages: (code: string) => Promise<void>;
37
+ export declare const loadAllMessages: () => Promise<void>;
38
+ export declare const loadRouteMessages: (code: string, base: string | undefined) => Promise<void>;
39
+ /** On the server, `modulepreload` links in the head for the client chunks of `code` and, under `split: "route"`, of route `base`. */
40
+ export declare function preloadMessages(app: AppContext, code: string, base?: string): void;
41
+ /** On the server, the `preload` link of the run-time catalog of `code`, which the browser fetches before it hydrates. */
42
+ export declare function preloadCatalog(app: AppContext, baseURL: string, code: string, version: string | undefined): void;
@@ -0,0 +1,2 @@
1
+ declare const _default: import("hamur/app").Middleware;
2
+ export default _default;
@@ -0,0 +1,9 @@
1
+ import type { LocaleInfo } from "../types.ts";
2
+ /** The first locale a range picks: one whose `language` or `code` equals it, else the first whose language subtag does. */
3
+ export declare function pickLocale(ranges: readonly string[], locales: readonly LocaleInfo[]): LocaleInfo | undefined;
4
+ /** The ranges of an `Accept-Language` header in `q` order (ties keep header order), without `*`, `q=0` and malformed entries. */
5
+ export declare function acceptedRanges(header: string | null | undefined): string[];
6
+ /** Every configured locale an `Accept-Language` header matches, best first, each once. */
7
+ export declare function negotiateAll(header: string | null | undefined, locales: readonly LocaleInfo[]): LocaleInfo[];
8
+ /** The locale an `Accept-Language` header asks for, if any. */
9
+ export declare function negotiate(header: string | null | undefined, locales: readonly LocaleInfo[]): LocaleInfo | undefined;
@@ -0,0 +1,4 @@
1
+ declare const _default: import("hamur/app").AppPlugin<{
2
+ i18n: import("../types.ts").I18nRuntime;
3
+ }>;
4
+ export default _default;