@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,144 @@
1
+ import type { AppContext } from "hamur/app"
2
+ import { readsRequestLocale } from "../runtime/locale.ts"
3
+ import type {
4
+ CurrentLocale,
5
+ I18nRich,
6
+ I18nRouteLocation,
7
+ I18nRuntime,
8
+ HamurLocale,
9
+ LocaleInfo,
10
+ RichJoin,
11
+ Translator,
12
+ } from "../types.ts"
13
+
14
+ export interface I18nRoute {
15
+ path: string
16
+ name?: string | null
17
+ params: Record<string, string | string[]>
18
+ query: Record<string, string | string[]>
19
+ hash: string
20
+ }
21
+
22
+ export interface I18nFunctions<N> {
23
+ /** `t.dynamic(key, args?)`: a run-time key, untyped. */
24
+ t: Translator["t"] & { rich: I18nRich<N>; dynamic: Translator["dynamic"] }
25
+ n: Translator["n"]
26
+ d: Translator["d"]
27
+ localePath(to: string | I18nRouteLocation): string
28
+ switchLocalePath(code: HamurLocale): string
29
+ absoluteLocalePath(to: string | I18nRouteLocation, code?: HamurLocale): string
30
+ allLocalePaths(to: string | I18nRouteLocation): { code: string; language: string; url: string }[]
31
+ routeBaseName(name: string): string
32
+ }
33
+
34
+ const known = new WeakMap<I18nRuntime, Map<string, CurrentLocale>>()
35
+
36
+ /** The locale of the page at `path`; under `no_prefix` and with domains the path holds none, so it is `app.i18n.locale`. */
37
+ export function localeAt(i18n: I18nRuntime, path: string): LocaleInfo {
38
+ return readsRequestLocale(i18n) ? i18n.locale : i18n.localeOf(path)
39
+ }
40
+
41
+ const setListeners = new WeakMap<I18nRuntime, Set<() => void>>()
42
+
43
+ /** For libraries that hold the locale in a reactive holder: runs `listener` after every `setLocale` of `i18n` settles. */
44
+ export function onSetLocale(i18n: I18nRuntime, listener: () => void): void {
45
+ let listeners = setListeners.get(i18n)
46
+ if (!listeners) {
47
+ const created = new Set<() => void>()
48
+ setListeners.set(i18n, (listeners = created))
49
+ const original = i18n.setLocale
50
+ i18n.setLocale = async (code) => {
51
+ await original.call(i18n, code)
52
+ for (const run of [...created]) run()
53
+ }
54
+ }
55
+ listeners.add(listener)
56
+ }
57
+
58
+ export function currentLocale(i18n: I18nRuntime, path: string): CurrentLocale {
59
+ const info = localeAt(i18n, path)
60
+ let byCode = known.get(i18n)
61
+ if (!byCode) known.set(i18n, (byCode = new Map()))
62
+ let current = byCode.get(info.code)
63
+ if (!current) byCode.set(info.code, (current = { ...info, locales: i18n.locales }))
64
+ return current
65
+ }
66
+
67
+ export function i18nOf(app: AppContext): I18nRuntime {
68
+ const i18n = (app as { i18n?: I18nRuntime }).i18n
69
+ if (!i18n) throw new Error('useI18n() needs the "@hamur/i18n" module: app.i18n is missing')
70
+ return i18n
71
+ }
72
+
73
+ export function bindFunctions<N>(
74
+ read: () => { i18n: I18nRuntime; route: I18nRoute },
75
+ join: RichJoin<N>,
76
+ ): I18nFunctions<N> {
77
+ const translator = () => {
78
+ const { i18n, route } = read()
79
+ return i18n.translator(localeAt(i18n, route.path).code)
80
+ }
81
+ return {
82
+ t: Object.assign(
83
+ ((key: string, ...args: unknown[]) =>
84
+ (translator().t as (key: string, ...args: unknown[]) => string)(key, ...args)) as Translator["t"],
85
+ {
86
+ rich: bindRich(read, join),
87
+ dynamic: (key: string, args?: Record<string, unknown>) => translator().dynamic(key, args),
88
+ },
89
+ ),
90
+ n: (value, options) => translator().n(value, options),
91
+ d: (value, options) => translator().d(value, options),
92
+ localePath(to) {
93
+ const { i18n, route } = read()
94
+ return i18n.localePath(to, localeAt(i18n, route.path).code)
95
+ },
96
+ switchLocalePath(code) {
97
+ const { i18n, route } = read()
98
+ const { path, name, params, query, hash } = route
99
+ return i18n.switchLocalePath({ path, name, params, query, hash }, code)
100
+ },
101
+ absoluteLocalePath(to, code) {
102
+ const { i18n, route } = read()
103
+ return i18n.absoluteLocalePath(to, code ?? localeAt(i18n, route.path).code)
104
+ },
105
+ allLocalePaths: (to) => read().i18n.allLocalePaths(to),
106
+ routeBaseName: (name) => read().i18n.routeBaseName(name),
107
+ }
108
+ }
109
+
110
+ /** `t.rich` of an adapter, reading the page locale like `t` does; `join` makes the adapter's node type. */
111
+ export function bindRich<N>(read: () => { i18n: I18nRuntime; route: I18nRoute }, join: RichJoin<N>): I18nRich<N> {
112
+ return (key, args, handlers) => {
113
+ const { i18n, route } = read()
114
+ return i18n.translator(localeAt(i18n, route.path).code).rich(key, args, handlers, join)
115
+ }
116
+ }
117
+
118
+ interface Bound<N> {
119
+ functions: I18nFunctions<N>
120
+ route: I18nRoute
121
+ }
122
+
123
+ // One adapter's join fixes N for every entry of an app.
124
+ const bound = new WeakMap<object, Map<string, Bound<any>>>()
125
+
126
+ /** For libraries whose entry may not import a hook: one object per app and locale code, reading the route of the latest render. */
127
+ export function sharedFunctions<N>(
128
+ app: AppContext,
129
+ i18n: I18nRuntime,
130
+ route: I18nRoute,
131
+ join: RichJoin<N>,
132
+ ): I18nFunctions<N> {
133
+ let byCode = bound.get(app)
134
+ if (!byCode) bound.set(app, (byCode = new Map()))
135
+ const code = localeAt(i18n, route.path).code
136
+ let entry = byCode.get(code)
137
+ if (!entry) {
138
+ const created: Bound<N> = { route, functions: undefined as never }
139
+ created.functions = bindFunctions(() => ({ i18n, route: created.route }), join)
140
+ byCode.set(code, (entry = created))
141
+ }
142
+ entry.route = route
143
+ return entry.functions
144
+ }
@@ -0,0 +1,35 @@
1
+ import { useApp, useRoute } from "@hamur/adapter-html"
2
+ import type { CurrentLocale, RichJoin, Translator } from "../types.ts"
3
+ import { bindFunctions, currentLocale, i18nOf, type I18nFunctions } from "./bind.ts"
4
+
5
+ export type { CurrentLocale }
6
+
7
+ const ESCAPES: Record<string, string> = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }
8
+
9
+ const escapeHtml = (text: string): string => text.replace(/[&<>"']/g, (char) => ESCAPES[char]!)
10
+
11
+ const join: RichJoin<string> = (parts) =>
12
+ parts.map((part) => (typeof part === "string" ? escapeHtml(part) : part.node)).join("")
13
+
14
+ export function useLocale(): CurrentLocale {
15
+ return currentLocale(i18nOf(useApp()), useRoute().path)
16
+ }
17
+
18
+ export function useI18n(): I18nFunctions<string> {
19
+ const i18n = i18nOf(useApp())
20
+ const route = useRoute()
21
+ const bound = bindFunctions(() => ({ i18n, route }), join)
22
+ return {
23
+ ...bound,
24
+ t: Object.assign(
25
+ ((key: string, ...args: unknown[]) =>
26
+ escapeHtml((bound.t as (key: string, ...args: unknown[]) => string)(key, ...args))) as Translator["t"],
27
+ {
28
+ rich: bound.t.rich,
29
+ dynamic: (key: string, args?: Record<string, unknown>) => escapeHtml(bound.t.dynamic(key, args)),
30
+ },
31
+ ),
32
+ n: (value, options) => escapeHtml(bound.n(value, options)),
33
+ d: (value, options) => escapeHtml(bound.d(value, options)),
34
+ }
35
+ }
@@ -0,0 +1,18 @@
1
+ import { useApp, useRoute } from "@hamur/adapter-preact"
2
+ import { createElement, Fragment, type ComponentChildren } from "preact"
3
+ import type { CurrentLocale, RichJoin } from "../types.ts"
4
+ import { currentLocale, i18nOf, sharedFunctions, type I18nFunctions } from "./bind.ts"
5
+
6
+ export type { CurrentLocale }
7
+
8
+ const join: RichJoin<ComponentChildren> = (parts) =>
9
+ parts.map((part, index) => (typeof part === "string" ? part : createElement(Fragment, { key: index }, part.node)))
10
+
11
+ export function useLocale(): CurrentLocale {
12
+ return currentLocale(i18nOf(useApp()), useRoute().path)
13
+ }
14
+
15
+ export function useI18n(): I18nFunctions<ComponentChildren> {
16
+ const app = useApp()
17
+ return sharedFunctions(app, i18nOf(app), useRoute(), join)
18
+ }
@@ -0,0 +1,18 @@
1
+ import { useApp, useRoute } from "@hamur/adapter-react"
2
+ import { createElement, Fragment, type ReactNode } from "react"
3
+ import type { CurrentLocale, RichJoin } from "../types.ts"
4
+ import { currentLocale, i18nOf, sharedFunctions, type I18nFunctions } from "./bind.ts"
5
+
6
+ export type { CurrentLocale }
7
+
8
+ const join: RichJoin<ReactNode> = (parts) =>
9
+ parts.map((part, index) => (typeof part === "string" ? part : createElement(Fragment, { key: index }, part.node)))
10
+
11
+ export function useLocale(): CurrentLocale {
12
+ return currentLocale(i18nOf(useApp()), useRoute().path)
13
+ }
14
+
15
+ export function useI18n(): I18nFunctions<ReactNode> {
16
+ const app = useApp()
17
+ return sharedFunctions(app, i18nOf(app), useRoute(), join)
18
+ }
@@ -0,0 +1,44 @@
1
+ import { createMemo, createSignal, type Accessor } from "solid-js"
2
+ import type { JSX } from "@solidjs/web"
3
+ import { useApp, useRoute } from "@hamur/adapter-solid"
4
+ import type { CurrentLocale, I18nRuntime, LocaleInfo, RichJoin } from "../types.ts"
5
+ import { bindFunctions, currentLocale, i18nOf, onSetLocale, type I18nFunctions } from "./bind.ts"
6
+
7
+ export type { CurrentLocale }
8
+
9
+ const join: RichJoin<JSX.Element> = (parts) => parts.map((part) => (typeof part === "string" ? part : part.node))
10
+
11
+ const holders = new WeakMap<I18nRuntime, Accessor<LocaleInfo>>()
12
+
13
+ function holderOf(i18n: I18nRuntime): Accessor<LocaleInfo> {
14
+ let holder = holders.get(i18n)
15
+ if (!holder) {
16
+ const [read, write] = createSignal(i18n.locale)
17
+ holders.set(i18n, (holder = read))
18
+ onSetLocale(i18n, () => void write(i18n.locale))
19
+ }
20
+ return holder
21
+ }
22
+
23
+ export function useLocale(): Accessor<CurrentLocale> {
24
+ const i18n = i18nOf(useApp())
25
+ const route = useRoute()
26
+ const holder = holderOf(i18n)
27
+ return createMemo(() => {
28
+ holder()
29
+ return currentLocale(i18n, route().path)
30
+ })
31
+ }
32
+
33
+ export function useI18n(): I18nFunctions<JSX.Element> & { locale: Accessor<CurrentLocale> } {
34
+ const i18n = i18nOf(useApp())
35
+ const route = useRoute()
36
+ const holder = holderOf(i18n)
37
+ return {
38
+ locale: useLocale(),
39
+ ...bindFunctions(() => {
40
+ holder()
41
+ return { i18n, route: route() }
42
+ }, join),
43
+ }
44
+ }
@@ -0,0 +1,75 @@
1
+ import { createSubscriber } from "svelte/reactivity"
2
+ import { useApp, useRoute } from "@hamur/adapter-svelte"
3
+ import type { CurrentLocale, I18nRuntime, RichJoin } from "../types.ts"
4
+ import { bindFunctions, currentLocale, i18nOf, onSetLocale, type I18nFunctions } from "./bind.ts"
5
+
6
+ export type { CurrentLocale }
7
+
8
+ /** What `t.rich` returns in svelte: text and elements for a snippet to render with `{#each}`; a handler returns `[{ tag: "b", children: chunks }]`. */
9
+ export type SvelteRich = readonly (string | { readonly tag: string; readonly children: SvelteRich })[]
10
+
11
+ const join: RichJoin<SvelteRich> = (parts) => parts.flatMap((part) => (typeof part === "string" ? [part] : part.node))
12
+
13
+ const holders = new WeakMap<I18nRuntime, () => void>()
14
+
15
+ /** The holder of the locale: reading it inside an effect or a `$derived` re-runs it after every `setLocale`. */
16
+ function holderOf(i18n: I18nRuntime): () => void {
17
+ let holder = holders.get(i18n)
18
+ if (!holder) {
19
+ const updates = new Set<() => void>()
20
+ holders.set(
21
+ i18n,
22
+ (holder = createSubscriber((update) => {
23
+ updates.add(update)
24
+ return () => void updates.delete(update)
25
+ })),
26
+ )
27
+ onSetLocale(i18n, () => {
28
+ for (const update of [...updates]) update()
29
+ })
30
+ }
31
+ return holder
32
+ }
33
+
34
+ export function useLocale(): CurrentLocale {
35
+ const i18n = i18nOf(useApp())
36
+ const route = useRoute()
37
+ const holder = holderOf(i18n)
38
+ const current = () => {
39
+ holder()
40
+ return currentLocale(i18n, route.path)
41
+ }
42
+ return {
43
+ get code() {
44
+ return current().code
45
+ },
46
+ get language() {
47
+ return current().language
48
+ },
49
+ get dir() {
50
+ return current().dir
51
+ },
52
+ get name() {
53
+ return current().name
54
+ },
55
+ get locales() {
56
+ return current().locales
57
+ },
58
+ }
59
+ }
60
+
61
+ export function useI18n(): I18nFunctions<SvelteRich> & { readonly locale: CurrentLocale } {
62
+ const i18n = i18nOf(useApp())
63
+ const route = useRoute()
64
+ const holder = holderOf(i18n)
65
+ const locale = useLocale()
66
+ return {
67
+ get locale() {
68
+ return locale
69
+ },
70
+ ...bindFunctions(() => {
71
+ holder()
72
+ return { i18n, route }
73
+ }, join),
74
+ }
75
+ }
@@ -0,0 +1,18 @@
1
+ <script lang="ts">
2
+ import { useI18n } from "../svelte.ts"
3
+
4
+ const { t, locale, localePath, switchLocalePath } = useI18n()
5
+ const text = $derived(
6
+ [
7
+ t("home.hello", { name: "Ada" }),
8
+ locale.code,
9
+ locale.language,
10
+ locale.dir,
11
+ locale.locales.length,
12
+ localePath("/about"),
13
+ switchLocalePath("ar"),
14
+ ].join("|"),
15
+ )
16
+ </script>
17
+
18
+ <p>{text}</p>
@@ -0,0 +1,20 @@
1
+ <script lang="ts">
2
+ import { onMount } from "svelte"
3
+ import { useI18n } from "../svelte.ts"
4
+ import { mounts, texts } from "./svelte-log.ts"
5
+
6
+ const { t, locale, localePath, switchLocalePath } = useI18n()
7
+ const text = $derived(
8
+ [
9
+ t("home.hello", { name: "Ada" }),
10
+ locale.code,
11
+ locale.language,
12
+ locale.dir,
13
+ locale.locales.length,
14
+ localePath("/about"),
15
+ switchLocalePath("ar"),
16
+ ].join("|"),
17
+ )
18
+ onMount(() => void mounts.push(1))
19
+ $effect(() => void texts.push(text))
20
+ </script>
@@ -0,0 +1,11 @@
1
+ <script lang="ts">
2
+ import { useI18n, type SvelteRich } from "../svelte.ts"
3
+
4
+ const { t } = useI18n()
5
+ </script>
6
+
7
+ {#snippet rich(parts: SvelteRich)}{#each parts as part}{#if typeof part === "string"}{part}{:else}<b
8
+ >{@render rich(part.children)}</b
9
+ >{/if}{/each}{/snippet}
10
+
11
+ <p>{@render rich(t.rich("home.bold", { name: "Ada" }, { b: (chunks) => [{ tag: "b", children: chunks }] }))}</p>
@@ -0,0 +1,11 @@
1
+ <script lang="ts">
2
+ import { useI18n, type SvelteRich } from "../svelte.ts"
3
+
4
+ const { t } = useI18n()
5
+ </script>
6
+
7
+ {#snippet rich(parts: SvelteRich)}{#each parts as part}{#if typeof part === "string"}{part}{:else}<b
8
+ >{@render rich(part.children)}</b
9
+ >{/if}{/each}{/snippet}
10
+
11
+ <p>{@render rich(t.rich("home.bold", { name: "<script>" }, {}))}</p>
@@ -0,0 +1,7 @@
1
+ import { join } from "node:path"
2
+
3
+ const packages = join(import.meta.dir, "../../../..")
4
+
5
+ /** The test pages import a UI library the way its adapter does: from the adapter's own `node_modules`. */
6
+ export const peer = (library: string, adapter: string): Promise<unknown> =>
7
+ import(Bun.resolveSync(library, join(packages, `adapter-${adapter}`)))
@@ -0,0 +1,43 @@
1
+ export {}
2
+ Object.assign(globalThis, { window: {} })
3
+ const { mount, update } = await import("../../../../adapter-preact/src/runtime/client.ts")
4
+ const { i18n, noPrefixI18n, routes } = await import("./runtime.ts")
5
+ const { ReportingPage, mounts, texts } = await import("./preact-page.ts")
6
+ type Context = Parameters<typeof update>[0]
7
+
8
+ const container = {
9
+ nodeType: 1,
10
+ localName: "div",
11
+ nodeName: "DIV",
12
+ namespaceURI: "http://www.w3.org/1999/xhtml",
13
+ firstChild: null,
14
+ childNodes: [],
15
+ }
16
+ const settle = () => new Promise((resolve) => setTimeout(resolve, 30))
17
+ const context = (route: typeof routes.fr, runtime: unknown = i18n) =>
18
+ ({
19
+ url: new URL(`http://localhost${route.path}`),
20
+ route,
21
+ tree: { components: [{ id: "page", component: ReportingPage }] },
22
+ payload: { data: {} },
23
+ head: {},
24
+ app: { i18n: runtime },
25
+ baseURL: "/",
26
+ root: container,
27
+ }) as unknown as Context
28
+
29
+ if (process.argv[2] === "set-locale") {
30
+ const runtime: ReturnType<typeof noPrefixI18n> = noPrefixI18n(
31
+ async () => void (await update(context(routes.about, runtime))),
32
+ )
33
+ await mount(context(routes.about, runtime))
34
+ await settle()
35
+ await runtime.setLocale("fr")
36
+ await settle()
37
+ } else {
38
+ await mount(context(routes.fr))
39
+ await settle()
40
+ await update(context(routes.home))
41
+ await settle()
42
+ }
43
+ console.log(JSON.stringify({ mounts: mounts.length, texts }))
@@ -0,0 +1,34 @@
1
+ import { peer } from "./peer.ts"
2
+ import { useI18n, useLocale } from "../preact.ts"
3
+
4
+ const { h } = (await peer("preact", "preact")) as { h(type: string, props: null, child: unknown): never }
5
+ const { useEffect } = (await peer("preact/hooks", "preact")) as {
6
+ useEffect(effect: () => void, deps?: unknown[]): void
7
+ }
8
+
9
+ export const mounts: number[] = []
10
+ export const texts: string[] = []
11
+
12
+ function useText() {
13
+ const { t, localePath, switchLocalePath } = useI18n()
14
+ const locale = useLocale()
15
+ return [
16
+ t("home.hello", { name: "Ada" }),
17
+ locale.code,
18
+ locale.language,
19
+ locale.dir,
20
+ locale.locales.length,
21
+ localePath("/about"),
22
+ switchLocalePath("ar"),
23
+ ].join("|")
24
+ }
25
+
26
+ export const Page = () => h("p", null, useText())
27
+
28
+ /** The client harness renders into a stub container that cannot hold elements, so this page reports instead. */
29
+ export function ReportingPage() {
30
+ const text = useText()
31
+ useEffect(() => void mounts.push(1), [])
32
+ useEffect(() => void texts.push(text))
33
+ return null
34
+ }
@@ -0,0 +1,9 @@
1
+ import { peer } from "./peer.ts"
2
+ import { useI18n } from "../preact.ts"
3
+
4
+ const { h } = (await peer("preact", "preact")) as { h(type: string, props: null, child: unknown): never }
5
+
6
+ export const BoldPage = () =>
7
+ h("p", null, useI18n().t.rich("home.bold", { name: "Ada" }, { b: (chunks) => h("b", null, chunks) }))
8
+
9
+ export const PlainPage = () => h("p", null, useI18n().t.rich("home.bold", { name: "<script>" }, {}))
@@ -0,0 +1,49 @@
1
+ export {}
2
+ const noop = () => {}
3
+ Object.assign(globalThis, {
4
+ window: { event: undefined, HTMLIFrameElement: class {}, document: { activeElement: null, body: null } },
5
+ })
6
+ const { mount, update } = await import("../../../../adapter-react/src/runtime/client.ts")
7
+ const { i18n, noPrefixI18n, routes } = await import("./runtime.ts")
8
+ const { ReportingPage, mounts, texts } = await import("./react-page.ts")
9
+ type Context = Parameters<typeof update>[0]
10
+
11
+ const container = {
12
+ nodeType: 1,
13
+ tagName: "DIV",
14
+ nodeName: "DIV",
15
+ namespaceURI: "http://www.w3.org/1999/xhtml",
16
+ ownerDocument: { nodeType: 9, addEventListener: noop, removeEventListener: noop },
17
+ addEventListener: noop,
18
+ removeEventListener: noop,
19
+ firstChild: null,
20
+ childNodes: [],
21
+ }
22
+ const settle = () => new Promise((resolve) => setTimeout(resolve, 30))
23
+ const context = (route: typeof routes.fr, runtime: unknown = i18n) =>
24
+ ({
25
+ url: new URL(`http://localhost${route.path}`),
26
+ route,
27
+ tree: { components: [{ id: "page", component: ReportingPage }] },
28
+ payload: { data: {} },
29
+ head: {},
30
+ app: { i18n: runtime },
31
+ baseURL: "/",
32
+ root: container,
33
+ }) as unknown as Context
34
+
35
+ if (process.argv[2] === "set-locale") {
36
+ const runtime: ReturnType<typeof noPrefixI18n> = noPrefixI18n(
37
+ async () => void (await update(context(routes.about, runtime))),
38
+ )
39
+ await mount(context(routes.about, runtime))
40
+ await settle()
41
+ await runtime.setLocale("fr")
42
+ await settle()
43
+ } else {
44
+ await mount(context(routes.fr))
45
+ await settle()
46
+ await update(context(routes.home))
47
+ await settle()
48
+ }
49
+ console.log(JSON.stringify({ mounts: mounts.length, texts }))
@@ -0,0 +1,34 @@
1
+ import { peer } from "./peer.ts"
2
+ import { useI18n, useLocale } from "../react.ts"
3
+
4
+ const { createElement, useEffect } = (await peer("react", "react")) as {
5
+ createElement(type: string, props: null, child: unknown): never
6
+ useEffect(effect: () => void, deps?: unknown[]): void
7
+ }
8
+
9
+ export const mounts: number[] = []
10
+ export const texts: string[] = []
11
+
12
+ function useText() {
13
+ const { t, localePath, switchLocalePath } = useI18n()
14
+ const locale = useLocale()
15
+ return [
16
+ t("home.hello", { name: "Ada" }),
17
+ locale.code,
18
+ locale.language,
19
+ locale.dir,
20
+ locale.locales.length,
21
+ localePath("/about"),
22
+ switchLocalePath("ar"),
23
+ ].join("|")
24
+ }
25
+
26
+ export const Page = () => createElement("p", null, useText())
27
+
28
+ /** The client harness renders into a stub container that cannot hold elements, so this page reports instead. */
29
+ export function ReportingPage() {
30
+ const text = useText()
31
+ useEffect(() => void mounts.push(1), [])
32
+ useEffect(() => void texts.push(text))
33
+ return null
34
+ }
@@ -0,0 +1,15 @@
1
+ import { peer } from "./peer.ts"
2
+ import { useI18n } from "../react.ts"
3
+
4
+ const { createElement } = (await peer("react", "react")) as {
5
+ createElement(type: string, props: null, child: unknown): never
6
+ }
7
+
8
+ export const BoldPage = () =>
9
+ createElement(
10
+ "p",
11
+ null,
12
+ useI18n().t.rich("home.bold", { name: "Ada" }, { b: (chunks) => createElement("b", null, chunks) }),
13
+ )
14
+
15
+ export const PlainPage = () => createElement("p", null, useI18n().t.rich("home.bold", { name: "<script>" }, {}))