@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.
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 +20 -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 +155 -4
  77. package/src/adapters/angular.ts +58 -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
package/src/types.ts ADDED
@@ -0,0 +1,182 @@
1
+ // The augmentation alone does not load "hamur" under TS 7.
2
+ import type {} from "hamur"
3
+ import type {} from "hamur/app"
4
+ import type { HamurContext } from "hamur/kit"
5
+
6
+ export interface LocaleInput {
7
+ code: string
8
+ language?: string
9
+ dir?: "ltr" | "rtl"
10
+ name?: string
11
+ /** The `http:`/`https:` origin that serves this locale without a prefix; on every locale or none. */
12
+ domain?: string
13
+ }
14
+
15
+ export interface I18nOptions {
16
+ locales: (string | LocaleInput)[]
17
+ defaultLocale: string
18
+ strategy?: "prefix_except_default" | "prefix" | "prefix_and_default" | "no_prefix"
19
+ redirectToDefaultLocale?: boolean
20
+ detect?: { cookie?: string | false; redirect?: "root" | "all" | false }
21
+ /** A path per locale; `false` makes no copy of the page in that locale. */
22
+ pages?: Record<string, Partial<Record<string, string | false>>>
23
+ /** The answer at the URL of a copy that is `false`; default `"404"`. */
24
+ missingPage?: "404" | "redirect" | "rewrite"
25
+ timeZone?: string
26
+ /** `"default"`, `false`, or a chain of locale codes per code (the default locale ends every chain). */
27
+ fallback?: "default" | false | Record<string, string[]>
28
+ /** `Link` paths go through `localePath`; default `true`. */
29
+ localizeLinks?: boolean
30
+ /** `"route"` ships each page the messages it reads, in one chunk per locale and route; default `false`. */
31
+ split?: false | "route"
32
+ /** Keys no static `t()` call in the client build names are left out of every client chunk; default `false`. */
33
+ dropUnusedKeys?: boolean
34
+ /** Globs (`*` one segment, `**` any) of keys `dropUnusedKeys` keeps; default `[]`. */
35
+ keepKeys?: string[]
36
+ /** The message syntax of every catalog; default `"mf1"`. */
37
+ syntax?: "mf1" | "mf2"
38
+ /** A server file below the root whose default export is `defineCatalog({ load, ttl })`, a run-time catalog. */
39
+ catalog?: string
40
+ }
41
+
42
+ export interface LocaleInfo {
43
+ code: HamurLocale
44
+ language: string
45
+ dir: "ltr" | "rtl"
46
+ name: string
47
+ }
48
+
49
+ /** `useLocale()` of an adapter entry: the locale of the current page and every configured locale. */
50
+ export type CurrentLocale = LocaleInfo & { locales: readonly LocaleInfo[] }
51
+
52
+ export interface ResolvedI18nOptions {
53
+ locales: LocaleInfo[]
54
+ defaultLocale: string
55
+ strategy: "prefix_except_default" | "prefix" | "prefix_and_default" | "no_prefix"
56
+ redirectToDefaultLocale: boolean
57
+ detect: { cookie: string | false; redirect: "root" | "all" | false }
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
+
73
+ export type I18nSetup = (options: ResolvedI18nOptions, hamur: HamurContext) => void | Promise<void>
74
+
75
+ /** Message key → argument object, filled by typegen. */
76
+ export interface HamurI18nMessages {}
77
+ /** Locale code → `true`, filled by typegen. */
78
+ export interface HamurI18nLocales {}
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
+ /**
90
+ * The one function an adapter gives `Translator.rich`: it makes its node type from the parts of one level.
91
+ * A string is message text (the adapter escapes it if its output needs it); `{ node }` is what a handler or a nested level returned.
92
+ */
93
+ export type RichJoin<N> = (parts: readonly (string | { node: N })[]) => N
94
+
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
98
+ ? [args?: Record<string, unknown>]
99
+ : K extends keyof HamurI18nMessages
100
+ ? keyof HamurI18nMessages[K] extends never
101
+ ? []
102
+ : [args: HamurI18nMessages[K]]
103
+ : never
104
+
105
+ export type I18nArgsObject<K> = keyof HamurI18nMessages extends never
106
+ ? Record<string, unknown>
107
+ : K extends keyof HamurI18nMessages
108
+ ? HamurI18nMessages[K]
109
+ : never
110
+
111
+ /** `{ [tag]: (chunks) => node }` for the tags of key `K`; typegen limits the names, and a tag without a handler renders its chunks. */
112
+ export type RichHandlers<K, N> = keyof HamurI18nMessages extends never
113
+ ? Record<string, (chunks: N) => N>
114
+ : K extends keyof HamurI18nTags
115
+ ? { [T in HamurI18nTags[K] & string]?: (chunks: N) => N }
116
+ : Record<string, never>
117
+
118
+ /** `t.rich` of an adapter entry, with the adapter's node type `N` fixed. */
119
+ export type I18nRich<N> = <K extends I18nKey>(key: K, args: I18nArgsObject<K>, handlers: RichHandlers<K, N>) => N
120
+
121
+ export interface I18nRouteLocation {
122
+ name: string
123
+ params?: Record<string, string | string[]>
124
+ query?: Record<string, string | string[]>
125
+ hash?: string
126
+ }
127
+
128
+ export interface Translator {
129
+ readonly locale: LocaleInfo
130
+ t<K extends I18nKey>(key: K, ...args: I18nArgsTuple<K>): string
131
+ /** `t` for a message with tags; `join` makes the adapter's node type of each level. */
132
+ rich<N, K extends I18nKey>(key: K, args: I18nArgsObject<K>, handlers: RichHandlers<K, N>, join: RichJoin<N>): N
133
+ /** An untyped key: the run-time catalog, then the file catalog, then `fallback`. */
134
+ dynamic(key: string, args?: Record<string, unknown>): string
135
+ n(value: number | bigint, options?: Intl.NumberFormatOptions): string
136
+ d(value: Date | number, options?: Intl.DateTimeFormatOptions): string
137
+ }
138
+
139
+ export interface I18nRuntime {
140
+ readonly locales: readonly LocaleInfo[]
141
+ readonly defaultLocale: LocaleInfo
142
+ /** `i18n.timeZone`, `"UTC"` when unset: the zone of `d()` and of the adapters' `Time`. */
143
+ readonly timeZone: string
144
+ /** The app's current locale: `localeOf(route.path)` under the prefix strategies, the request's or the chosen one under `no_prefix`. */
145
+ readonly locale: LocaleInfo
146
+ /** `navigateTo(switchLocalePath(code))` under the prefix strategies; under `no_prefix` the cookie, the locale chunk and the navigator's `refresh()`. */
147
+ setLocale(code: HamurLocale): Promise<void>
148
+ localeOf(path: string): LocaleInfo
149
+ load(code: HamurLocale): Promise<void>
150
+ translator(code: HamurLocale): Translator
151
+ localePath(to: string | I18nRouteLocation, code: HamurLocale): string
152
+ switchLocalePath(
153
+ route: {
154
+ path: string
155
+ name?: string | null
156
+ params: Record<string, string | string[]>
157
+ query: Record<string, string | string[]>
158
+ hash: string
159
+ },
160
+ code: HamurLocale,
161
+ ): string
162
+ /** `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. */
163
+ absoluteLocalePath(to: string | I18nRouteLocation, code?: HamurLocale): string
164
+ /** `absoluteLocalePath` of `to` in every locale that has a copy of it, for hreflang lists and sitemaps. */
165
+ allLocalePaths(to: string | I18nRouteLocation): { code: string; language: string; url: string }[]
166
+ routeBaseName(name: string): string
167
+ /** The configured locales `Accept-Language` (`navigator.languages` in the browser) matches, best first. */
168
+ preferredLocaleList(): LocaleInfo[]
169
+ }
170
+
171
+ declare module "hamur" {
172
+ interface HamurModuleOptions {
173
+ i18n?: I18nOptions
174
+ }
175
+ }
176
+
177
+ declare module "hamur/app" {
178
+ interface HamurLinkProps {
179
+ /** The locale of the link's path, the page's by default; `false` keeps `to` as written. */
180
+ locale?: HamurLocale | false
181
+ }
182
+ }