@puzzle-ui/i18n 0.0.0-stage → 0.14.0

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 (152) hide show
  1. package/README.md +91 -2
  2. package/ai/instructions.md +141 -0
  3. package/ai/manifest.json +10 -0
  4. package/dist/LICENSE +21 -0
  5. package/dist/_virtual/_rolldown/runtime.cjs +22 -0
  6. package/dist/adapters/next.cjs +11 -0
  7. package/dist/adapters/next.cjs.map +1 -0
  8. package/dist/adapters/next.d.cts +7 -0
  9. package/dist/adapters/next.d.ts +7 -0
  10. package/dist/adapters/next.js +10 -0
  11. package/dist/adapters/next.js.map +1 -0
  12. package/dist/adapters/react-router.cjs +13 -0
  13. package/dist/adapters/react-router.cjs.map +1 -0
  14. package/dist/adapters/react-router.d.cts +10 -0
  15. package/dist/adapters/react-router.d.ts +10 -0
  16. package/dist/adapters/react-router.js +12 -0
  17. package/dist/adapters/react-router.js.map +1 -0
  18. package/dist/adapters/request.cjs +67 -0
  19. package/dist/adapters/request.cjs.map +1 -0
  20. package/dist/adapters/request.d.cts +16 -0
  21. package/dist/adapters/request.d.ts +16 -0
  22. package/dist/adapters/request.js +65 -0
  23. package/dist/adapters/request.js.map +1 -0
  24. package/dist/adapters/tanstack.cjs +13 -0
  25. package/dist/adapters/tanstack.cjs.map +1 -0
  26. package/dist/adapters/tanstack.d.cts +10 -0
  27. package/dist/adapters/tanstack.d.ts +10 -0
  28. package/dist/adapters/tanstack.js +12 -0
  29. package/dist/adapters/tanstack.js.map +1 -0
  30. package/dist/ai/index.cjs +205 -0
  31. package/dist/ai/index.cjs.map +1 -0
  32. package/dist/ai/index.d.cts +75 -0
  33. package/dist/ai/index.d.ts +75 -0
  34. package/dist/ai/index.js +195 -0
  35. package/dist/ai/index.js.map +1 -0
  36. package/dist/ai/review.cjs +319 -0
  37. package/dist/ai/review.cjs.map +1 -0
  38. package/dist/ai/review.d.cts +91 -0
  39. package/dist/ai/review.d.ts +91 -0
  40. package/dist/ai/review.js +315 -0
  41. package/dist/ai/review.js.map +1 -0
  42. package/dist/cli/bin.cjs +10 -0
  43. package/dist/cli/bin.cjs.map +1 -0
  44. package/dist/cli/bin.d.cts +1 -0
  45. package/dist/cli/bin.d.ts +1 -0
  46. package/dist/cli/bin.js +12 -0
  47. package/dist/cli/bin.js.map +1 -0
  48. package/dist/cli/index.cjs +310 -0
  49. package/dist/cli/index.cjs.map +1 -0
  50. package/dist/cli/index.d.cts +7 -0
  51. package/dist/cli/index.d.ts +7 -0
  52. package/dist/cli/index.js +309 -0
  53. package/dist/cli/index.js.map +1 -0
  54. package/dist/compiler/index.cjs +218 -0
  55. package/dist/compiler/index.cjs.map +1 -0
  56. package/dist/compiler/index.d.cts +18 -0
  57. package/dist/compiler/index.d.ts +18 -0
  58. package/dist/compiler/index.js +210 -0
  59. package/dist/compiler/index.js.map +1 -0
  60. package/dist/compiler/typescript.cjs +251 -0
  61. package/dist/compiler/typescript.cjs.map +1 -0
  62. package/dist/compiler/typescript.d.cts +27 -0
  63. package/dist/compiler/typescript.d.ts +27 -0
  64. package/dist/compiler/typescript.js +247 -0
  65. package/dist/compiler/typescript.js.map +1 -0
  66. package/dist/compiler/vite.cjs +94 -0
  67. package/dist/compiler/vite.cjs.map +1 -0
  68. package/dist/compiler/vite.d.cts +13 -0
  69. package/dist/compiler/vite.d.ts +13 -0
  70. package/dist/compiler/vite.js +92 -0
  71. package/dist/compiler/vite.js.map +1 -0
  72. package/dist/content/index.cjs +156 -0
  73. package/dist/content/index.cjs.map +1 -0
  74. package/dist/content/index.d.cts +75 -0
  75. package/dist/content/index.d.ts +75 -0
  76. package/dist/content/index.js +151 -0
  77. package/dist/content/index.js.map +1 -0
  78. package/dist/content/sanity.cjs +74 -0
  79. package/dist/content/sanity.cjs.map +1 -0
  80. package/dist/content/sanity.d.cts +19 -0
  81. package/dist/content/sanity.d.ts +19 -0
  82. package/dist/content/sanity.js +73 -0
  83. package/dist/content/sanity.js.map +1 -0
  84. package/dist/devtools/index.cjs +77 -0
  85. package/dist/devtools/index.cjs.map +1 -0
  86. package/dist/devtools/index.d.cts +29 -0
  87. package/dist/devtools/index.d.ts +29 -0
  88. package/dist/devtools/index.js +75 -0
  89. package/dist/devtools/index.js.map +1 -0
  90. package/dist/format.cjs +17 -0
  91. package/dist/format.cjs.map +1 -0
  92. package/dist/format.d.cts +9 -0
  93. package/dist/format.d.ts +9 -0
  94. package/dist/format.js +13 -0
  95. package/dist/format.js.map +1 -0
  96. package/dist/index.cjs +20 -0
  97. package/dist/index.d.cts +5 -0
  98. package/dist/index.d.ts +5 -0
  99. package/dist/index.js +5 -0
  100. package/dist/locale.cjs +158 -0
  101. package/dist/locale.cjs.map +1 -0
  102. package/dist/locale.d.cts +42 -0
  103. package/dist/locale.d.ts +42 -0
  104. package/dist/locale.js +155 -0
  105. package/dist/locale.js.map +1 -0
  106. package/dist/react/index.cjs +239 -0
  107. package/dist/react/index.cjs.map +1 -0
  108. package/dist/react/index.d.cts +105 -0
  109. package/dist/react/index.d.ts +105 -0
  110. package/dist/react/index.js +238 -0
  111. package/dist/react/index.js.map +1 -0
  112. package/dist/routing/index.cjs +381 -0
  113. package/dist/routing/index.cjs.map +1 -0
  114. package/dist/routing/index.d.cts +44 -0
  115. package/dist/routing/index.d.ts +44 -0
  116. package/dist/routing/index.js +380 -0
  117. package/dist/routing/index.js.map +1 -0
  118. package/dist/runtime.cjs +261 -0
  119. package/dist/runtime.cjs.map +1 -0
  120. package/dist/runtime.d.cts +74 -0
  121. package/dist/runtime.d.ts +74 -0
  122. package/dist/runtime.js +257 -0
  123. package/dist/runtime.js.map +1 -0
  124. package/dist/seo/index.cjs +212 -0
  125. package/dist/seo/index.cjs.map +1 -0
  126. package/dist/seo/index.d.cts +52 -0
  127. package/dist/seo/index.d.ts +52 -0
  128. package/dist/seo/index.js +209 -0
  129. package/dist/seo/index.js.map +1 -0
  130. package/dist/seo/origin.cjs +27 -0
  131. package/dist/seo/origin.cjs.map +1 -0
  132. package/dist/seo/origin.js +26 -0
  133. package/dist/seo/origin.js.map +1 -0
  134. package/dist/seo/sitemap.cjs +74 -0
  135. package/dist/seo/sitemap.cjs.map +1 -0
  136. package/dist/seo/sitemap.d.cts +16 -0
  137. package/dist/seo/sitemap.d.ts +16 -0
  138. package/dist/seo/sitemap.js +72 -0
  139. package/dist/seo/sitemap.js.map +1 -0
  140. package/dist/sources/index.cjs +227 -0
  141. package/dist/sources/index.cjs.map +1 -0
  142. package/dist/sources/index.d.cts +41 -0
  143. package/dist/sources/index.d.ts +41 -0
  144. package/dist/sources/index.js +222 -0
  145. package/dist/sources/index.js.map +1 -0
  146. package/dist/sources/node.cjs +50 -0
  147. package/dist/sources/node.cjs.map +1 -0
  148. package/dist/sources/node.d.cts +5 -0
  149. package/dist/sources/node.d.ts +5 -0
  150. package/dist/sources/node.js +49 -0
  151. package/dist/sources/node.js.map +1 -0
  152. package/package.json +256 -4
package/README.md CHANGED
@@ -1,3 +1,92 @@
1
- # Temporary Holding Version
1
+ # @puzzle-ui/i18n
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Framework-agnostic locale, translation, formatting, routing, catalog validation,
4
+ and multilingual SEO primitives, with an optional React integration.
5
+
6
+ This workspace package is pre-release and has not been published to npm.
7
+
8
+ ## Install
9
+
10
+ After publication, install with `bun add @puzzle-ui/i18n`. In this repository,
11
+ the package is available through the Bun workspace.
12
+
13
+ Install React 19 when using the `@puzzle-ui/i18n/react` entry point.
14
+
15
+ ## Quick start
16
+
17
+ ```ts
18
+ import { createI18n } from "@puzzle-ui/i18n";
19
+
20
+ const i18n = createI18n({
21
+ locales: [{ locale: "en" }, { locale: "nl" }],
22
+ defaultLocale: "en",
23
+ catalogs: {
24
+ en: { greeting: "Hello {name}", files: "{count, plural, one {# file} other {# files}}" },
25
+ nl: { greeting: "Hallo {name}", files: "{count, plural, one {# bestand} other {# bestanden}}" },
26
+ },
27
+ });
28
+
29
+ const t = i18n.createTranslator("nl").t;
30
+ t("greeting", { name: "Ada" });
31
+ t("files", { count: 2 });
32
+ ```
33
+
34
+ Catalogs and locale bindings are immutable per runtime instance. Create a
35
+ translator for each request or render tree; do not keep a mutable global
36
+ current locale. The runtime uses ICU message syntax through
37
+ [`intl-messageformat`](https://formatjs.io/docs/intl-messageformat/).
38
+
39
+ ## Public entry points
40
+
41
+ - `@puzzle-ui/i18n`: locale registry, resolution, typed translation runtime,
42
+ Intl formatting, and catalog utilities.
43
+ - `@puzzle-ui/i18n/routing`: safe locale-prefixed path matching and generation.
44
+ - `@puzzle-ui/i18n/react`: a provider factory, translation/locale hooks, an
45
+ accessible locale select control, rich-text and lazy namespace hooks, locale
46
+ document attributes, and locale-bound Intl formatter hooks.
47
+ - `@puzzle-ui/i18n/devtools`: an opt-in diagnostics store and React panel for
48
+ runtime and route events; request adapters can report configured route-map
49
+ outcomes using parameter-free route templates. Dynamically import it only in
50
+ development builds.
51
+ - `@puzzle-ui/i18n/next`, `/tanstack`, and `/react-router`: request-locale
52
+ resolution helpers compatible with their native route/server-loader APIs.
53
+ - `@puzzle-ui/i18n/compiler`: catalog consistency checks and type declaration
54
+ generation, locale-aware declarations, and optional TypeScript AST
55
+ extraction/transforms at `/compiler/typescript`.
56
+ - `@puzzle-ui/i18n/vite`: a Vite plugin for catalog and source diagnostics.
57
+ - `Translator.rich`: framework-neutral ICU rich-tag formatting; the React
58
+ integration provides `useRichTranslation` for React nodes and
59
+ `useNamespace` for lazy namespace loading.
60
+ - `@puzzle-ui/i18n/sources`: immutable in-memory/HTTP JSON catalog sources;
61
+ `/sources/node` adds filesystem loading.
62
+ - `@puzzle-ui/i18n/content`: versioned content resolution with locale fallback,
63
+ draft preview gating, an adapter-owned validation hook, and an injected-client
64
+ Sanity content source.
65
+ - `@puzzle-ui/i18n/cli`: command implementation behind the `puzzle-i18n`
66
+ binary for initialization, catalog validation, source scanning, and
67
+ declaration generation, with project-config support.
68
+ - `@puzzle-ui/i18n/seo`: canonical and hreflang data compatible with
69
+ `@puzzle-ui/seo`'s `SeoPageMetadata`, route-map validation, and Schema.org
70
+ language helpers; `/seo/sitemap` delegates localized sitemap generation to
71
+ `@puzzle-ui/seo`.
72
+ - `@puzzle-ui/i18n/ai`: an injected suggestion-provider contract that validates
73
+ ICU syntax and placeholders, supports authorization and quality checks,
74
+ rate-limits requests, and provides an optimistic human-review workflow with
75
+ in-memory and transactional persistence adapters.
76
+
77
+ See the [multilingual documentation](https://puzzle-ui.com/docs/multilingual)
78
+ for API details and current limitations.
79
+
80
+ ## Current scope
81
+
82
+ This pre-release provides the framework-independent runtime, React bindings,
83
+ localized routing utilities, JSON/HTTP/filesystem/TypeScript catalog sources,
84
+ catalog validation, AST transforms and type generation, a Vite validation
85
+ plugin, localized SEO metadata and sitemaps, versioned content resolution with
86
+ an injected-client Sanity adapter, and an authorized, quality-scored human
87
+ translation-review workflow. Next.js, TanStack Start/Router, and React Router
88
+ fixture applications exercise request resolution and framework-native
89
+ rendering APIs; the helpers do not register application routes or middleware.
90
+ Provider credentials, database implementations, CMS clients, identity, and
91
+ reviewer authorization remain owned by the consuming application. TypeScript 5
92
+ and Vite are optional peers for compiler scanning and the Vite plugin.
@@ -0,0 +1,141 @@
1
+ # Using Puzzle Multilingual
2
+
3
+ These instructions are for developers and AI coding agents working in an
4
+ application that installs `@puzzle-ui/i18n`.
5
+
6
+ ## Install and supported scope
7
+
8
+ This workspace package is pre-release and has not been published to npm.
9
+ Confirm its availability before installing it from a registry.
10
+
11
+ Install `@puzzle-ui/i18n`. React 19 is an optional peer and is needed only for
12
+ `@puzzle-ui/i18n/react`. The current release exposes:
13
+
14
+ - The framework-independent runtime and formatting APIs from the package root.
15
+ - Locale-prefixed path utilities from `@puzzle-ui/i18n/routing`.
16
+ - React provider bindings from `@puzzle-ui/i18n/react`.
17
+ - Catalog validation and type generation from `@puzzle-ui/i18n/compiler`.
18
+ - TypeScript source transforms and source-map generation from
19
+ `@puzzle-ui/i18n/compiler/typescript`.
20
+ - Canonical/hreflang metadata values from `@puzzle-ui/i18n/seo`.
21
+ - Request-locale helpers from `@puzzle-ui/i18n/next`, `/tanstack`, and
22
+ `/react-router`.
23
+ - The optional diagnostics collector and panel from `@puzzle-ui/i18n/devtools`;
24
+ dynamically import this entry only in development builds.
25
+ - An injected suggestion and review-workflow API from `@puzzle-ui/i18n/ai`.
26
+ - In-memory/HTTP JSON sources from `@puzzle-ui/i18n/sources` and Node
27
+ filesystem sources from `@puzzle-ui/i18n/sources/node`.
28
+ - Versioned locale-fallback content resolution and an injected-client Sanity
29
+ source from `@puzzle-ui/i18n/content`.
30
+ - The `puzzle-i18n` binary for non-destructive initialization, validation,
31
+ declaration generation, and source scanning.
32
+ - TypeScript AST scanning from `@puzzle-ui/i18n/compiler/typescript` and
33
+ build-time validation from `@puzzle-ui/i18n/vite`; both require optional
34
+ TypeScript 5 and Vite peers as applicable.
35
+
36
+ The framework adapters resolve locale from native request/header/route-loader
37
+ values; they do not register routes or implement middleware. The Sanity source
38
+ accepts an application-owned client and does not add a vendor SDK or credentials.
39
+ The CLI loads project configuration for validation, type generation, and
40
+ scanning. No built-in AI provider or application identity system is included.
41
+ Do not invent imports or claim support for other CMS integrations.
42
+
43
+ ## Configure locales and catalogs
44
+
45
+ Create one runtime from BCP 47 locale definitions and catalogs, then create a
46
+ translator for each request or render tree:
47
+
48
+ ```ts
49
+ import { createI18n } from "@puzzle-ui/i18n";
50
+
51
+ const i18n = createI18n({
52
+ locales: [{ locale: "en" }, { locale: "nl" }],
53
+ defaultLocale: "en",
54
+ catalogs: {
55
+ en: { greeting: "Hello {name}" },
56
+ nl: { greeting: "Hallo {name}" },
57
+ },
58
+ });
59
+
60
+ const t = i18n.createTranslator("nl").t;
61
+ t("greeting", { name: "Ada" });
62
+ ```
63
+
64
+ Translation keys and required interpolation values are inferred from the
65
+ catalogs.
66
+
67
+ ## Messages and formatting
68
+
69
+ The root runtime uses `IntlMessageFormat` for ICU messages and platform `Intl`
70
+ for number, currency, date, relative-time, and list formatting. `t()` returns
71
+ plain text. In React, use `useRichTranslation` for ICU tag syntax and supply
72
+ explicit React components; never pass translation strings to
73
+ `dangerouslySetInnerHTML`. Use `useLocaleAttributes` for document `lang` and
74
+ `dir`, and the corresponding formatter hooks when formatting within a render
75
+ tree.
76
+
77
+ Never store a current locale in a module global. Resolve a locale from request
78
+ data with `resolveLocale`, then create a translator for that request. A
79
+ translator closes over one locale and does not mutate the runtime. In React,
80
+ create bindings once from the configured runtime and provide the request/render
81
+ locale through the provider.
82
+
83
+ ## Routing and SEO
84
+
85
+ Use `@puzzle-ui/i18n/routing` only for safe same-origin locale-prefixed paths.
86
+ It does not implement framework routes, localized slugs, domains, or redirects.
87
+ Use `@puzzle-ui/i18n/seo` to create canonical/hreflang values for the existing
88
+ `@puzzle-ui/seo` metadata APIs rather than creating a competing SEO provider.
89
+ Use `validateLocalizedSeo` to check route-map coverage and canonical collisions,
90
+ `withStructuredDataLocale` for Schema.org `inLanguage`, and
91
+ `@puzzle-ui/i18n/seo/sitemap` to generate reciprocal localized alternates via
92
+ the existing SEO sitemap engine.
93
+
94
+ ## Catalog sources and CLI
95
+
96
+ Use `@puzzle-ui/i18n/sources` for memory, TypeScript, or HTTP JSON catalogs and
97
+ `@puzzle-ui/i18n/sources/node` for filesystem JSON catalogs. The CLI supports
98
+ `validate`, `generate-types`, and `scan` from a validated project config or
99
+ with explicit paths; `init` creates non-overwriting config and catalog files.
100
+ Review the CLI help for config overrides and command-specific options.
101
+
102
+ Filesystem sources use `<root>/<locale>.json` for a complete catalog and
103
+ `<root>/<locale>/<namespace path>.json` for a namespace. HTTP sources support
104
+ per-locale/namespace caching, explicit invalidation, retries, timeouts, and
105
+ abort signals. Keep credentials server-side and never cache across locales.
106
+ Pass a source as `namespaceLoader` to `createI18n`; `loadNamespace` and
107
+ `useNamespace` load it for the requested locale and its fallback chain. Handle
108
+ the hook's explicit `loading`, `ready`, and `error` states.
109
+
110
+ ## Catalog checks and AI suggestions
111
+
112
+ Use `@puzzle-ui/i18n/compiler` for ICU syntax, locale key, and placeholder
113
+ validation and declaration generation, including locale-difference output.
114
+ Use `@puzzle-ui/i18n/compiler/typescript` for static key extraction and safe
115
+ literal-key transforms with source maps. Use `@puzzle-ui/i18n/vite` for
116
+ build-time catalog and source validation.
117
+
118
+ Use `@puzzle-ui/i18n/ai` only from a trusted server environment with an injected
119
+ provider; never expose provider credentials or source content to clients. The
120
+ returned suggestion always has `approved: false`; provider calls are blocked in
121
+ browser runtimes. Use
122
+ `createAuthorizedSuggestionProvider` to gate provider requests, and configure
123
+ terminology/context quality checks as advisory signals. Use
124
+ `createTranslationReviewWorkflow` and
125
+ `createTransactionalTranslationReviewStore` with an application persistence
126
+ implementation for durable suggestions, atomic revision checks, and audit
127
+ history. The in-memory store is only for tests and demos. The host application
128
+ must still authenticate and authorize reviewers; no API key or provider package
129
+ is included.
130
+
131
+ ## Agent workflow
132
+
133
+ 1. Read the actual public exports from `@puzzle-ui/i18n` and the entry point
134
+ matching the application framework.
135
+ 2. Reuse the app's existing locale and route configuration.
136
+ 3. Do not import package internals or invent hooks, middleware, loaders, or
137
+ configuration fields.
138
+ 4. Keep server secrets and request-specific state on the server/request.
139
+ 5. Add tests for configuration, fallback, SSR isolation, route behavior, and
140
+ locale switching.
141
+ 6. Run the application's typecheck, tests, and production build after changes.
@@ -0,0 +1,10 @@
1
+ {
2
+ "schemaVersion": 1,
3
+ "packageName": "@puzzle-ui/i18n",
4
+ "instructionVersion": 1,
5
+ "contentFile": "instructions.md",
6
+ "instructionDependencies": [
7
+ "@puzzle-ui/seo"
8
+ ],
9
+ "documentationUrl": "https://puzzle-ui.com/docs/multilingual"
10
+ }
package/dist/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sjoerd van Den Berg
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,22 @@
1
+ // \0rolldown/runtime.js
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+ exports.__toESM = __toESM;
@@ -0,0 +1,11 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_request = require("./request.cjs");
3
+ // src/adapters/next.ts
4
+ const resolveNextRequestLocale = (options) => {
5
+ const resolution = require_request.resolveHeadersLocale(options.headers, options);
6
+ if (options.pathname) require_request.reportLocalizedRoute(options.pathname, options);
7
+ return resolution;
8
+ };
9
+ exports.resolveNextRequestLocale = resolveNextRequestLocale;
10
+
11
+ //# sourceMappingURL=next.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"next.cjs","names":["resolveHeadersLocale"],"sources":["../../src/adapters/next.ts"],"sourcesContent":["import { reportLocalizedRoute, resolveHeadersLocale } from \"./request\";\n\nimport type {\n HeaderReader,\n RequestLocaleOptions,\n} from \"./request\";\nimport type { LocaleResolution } from \"../locale\";\n\nexport interface NextRequestLocaleOptions extends RequestLocaleOptions {\n headers: HeaderReader;\n pathname?: string;\n}\n\nexport const resolveNextRequestLocale = (\n options: NextRequestLocaleOptions,\n): LocaleResolution => {\n const resolution = resolveHeadersLocale(options.headers, options);\n\n if (options.pathname) {\n reportLocalizedRoute(options.pathname, options);\n }\n\n return resolution;\n};\n"],"mappings":";;;AAaA,MAAa,4BACX,YACqB;CACrB,MAAM,aAAaA,gBAAAA,qBAAqB,QAAQ,SAAS,OAAO;CAEhE,IAAI,QAAQ,UACV,gBAAA,qBAAqB,QAAQ,UAAU,OAAO;CAGhD,OAAO;AACT"}
@@ -0,0 +1,7 @@
1
+ import { HeaderReader, RequestLocaleOptions } from "./request.cjs";
2
+ import { LocaleResolution } from "../locale.cjs";
3
+ export interface NextRequestLocaleOptions extends RequestLocaleOptions {
4
+ headers: HeaderReader;
5
+ pathname?: string;
6
+ }
7
+ export declare const resolveNextRequestLocale: (options: NextRequestLocaleOptions) => LocaleResolution;
@@ -0,0 +1,7 @@
1
+ import { HeaderReader, RequestLocaleOptions } from "./request.js";
2
+ import { LocaleResolution } from "../locale.js";
3
+ export interface NextRequestLocaleOptions extends RequestLocaleOptions {
4
+ headers: HeaderReader;
5
+ pathname?: string;
6
+ }
7
+ export declare const resolveNextRequestLocale: (options: NextRequestLocaleOptions) => LocaleResolution;
@@ -0,0 +1,10 @@
1
+ import { reportLocalizedRoute, resolveHeadersLocale } from "./request.js";
2
+ // src/adapters/next.ts
3
+ const resolveNextRequestLocale = (options) => {
4
+ const resolution = resolveHeadersLocale(options.headers, options);
5
+ if (options.pathname) reportLocalizedRoute(options.pathname, options);
6
+ return resolution;
7
+ };
8
+ export { resolveNextRequestLocale };
9
+
10
+ //# sourceMappingURL=next.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"next.js","names":[],"sources":["../../src/adapters/next.ts"],"sourcesContent":["import { reportLocalizedRoute, resolveHeadersLocale } from \"./request\";\n\nimport type {\n HeaderReader,\n RequestLocaleOptions,\n} from \"./request\";\nimport type { LocaleResolution } from \"../locale\";\n\nexport interface NextRequestLocaleOptions extends RequestLocaleOptions {\n headers: HeaderReader;\n pathname?: string;\n}\n\nexport const resolveNextRequestLocale = (\n options: NextRequestLocaleOptions,\n): LocaleResolution => {\n const resolution = resolveHeadersLocale(options.headers, options);\n\n if (options.pathname) {\n reportLocalizedRoute(options.pathname, options);\n }\n\n return resolution;\n};\n"],"mappings":";;AAaA,MAAa,4BACX,YACqB;CACrB,MAAM,aAAa,qBAAqB,QAAQ,SAAS,OAAO;CAEhE,IAAI,QAAQ,UACV,qBAAqB,QAAQ,UAAU,OAAO;CAGhD,OAAO;AACT"}
@@ -0,0 +1,13 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_request = require("./request.cjs");
3
+ // src/adapters/react-router.ts
4
+ const createReactRouterLocaleLoader = (options) => {
5
+ const localeParam = options.localeParam ?? "locale";
6
+ return ({ request, params }) => require_request.resolveRequestLocale(request, {
7
+ ...options,
8
+ urlLocale: params[localeParam] ?? options.urlLocale
9
+ });
10
+ };
11
+ exports.createReactRouterLocaleLoader = createReactRouterLocaleLoader;
12
+
13
+ //# sourceMappingURL=react-router.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react-router.cjs","names":["resolveRequestLocale"],"sources":["../../src/adapters/react-router.ts"],"sourcesContent":["import { resolveRequestLocale } from \"./request\";\n\nimport type { RequestLocaleOptions } from \"./request\";\nimport type { LocaleResolution } from \"../locale\";\n\nexport interface ReactRouterLocaleLoaderArgs {\n request: Request;\n params: Readonly<Record<string, string | undefined>>;\n}\n\nexport interface ReactRouterLocaleLoaderOptions extends RequestLocaleOptions {\n localeParam?: string;\n}\n\nexport const createReactRouterLocaleLoader = (\n options: ReactRouterLocaleLoaderOptions,\n): ((args: ReactRouterLocaleLoaderArgs) => LocaleResolution) => {\n const localeParam = options.localeParam ?? \"locale\";\n\n return ({ request, params }) =>\n resolveRequestLocale(request, {\n ...options,\n urlLocale: params[localeParam] ?? options.urlLocale,\n });\n};\n"],"mappings":";;;AAcA,MAAa,iCACX,YAC8D;CAC9D,MAAM,cAAc,QAAQ,eAAe;CAE3C,QAAQ,EAAE,SAAS,aACjBA,gBAAAA,qBAAqB,SAAS;EAC5B,GAAG;EACH,WAAW,OAAO,gBAAgB,QAAQ;CAC5C,CAAC;AACL"}
@@ -0,0 +1,10 @@
1
+ import { RequestLocaleOptions } from "./request.cjs";
2
+ import { LocaleResolution } from "../locale.cjs";
3
+ export interface ReactRouterLocaleLoaderArgs {
4
+ request: Request;
5
+ params: Readonly<Record<string, string | undefined>>;
6
+ }
7
+ export interface ReactRouterLocaleLoaderOptions extends RequestLocaleOptions {
8
+ localeParam?: string;
9
+ }
10
+ export declare const createReactRouterLocaleLoader: (options: ReactRouterLocaleLoaderOptions) => ((args: ReactRouterLocaleLoaderArgs) => LocaleResolution);
@@ -0,0 +1,10 @@
1
+ import { RequestLocaleOptions } from "./request.js";
2
+ import { LocaleResolution } from "../locale.js";
3
+ export interface ReactRouterLocaleLoaderArgs {
4
+ request: Request;
5
+ params: Readonly<Record<string, string | undefined>>;
6
+ }
7
+ export interface ReactRouterLocaleLoaderOptions extends RequestLocaleOptions {
8
+ localeParam?: string;
9
+ }
10
+ export declare const createReactRouterLocaleLoader: (options: ReactRouterLocaleLoaderOptions) => ((args: ReactRouterLocaleLoaderArgs) => LocaleResolution);
@@ -0,0 +1,12 @@
1
+ import { resolveRequestLocale } from "./request.js";
2
+ // src/adapters/react-router.ts
3
+ const createReactRouterLocaleLoader = (options) => {
4
+ const localeParam = options.localeParam ?? "locale";
5
+ return ({ request, params }) => resolveRequestLocale(request, {
6
+ ...options,
7
+ urlLocale: params[localeParam] ?? options.urlLocale
8
+ });
9
+ };
10
+ export { createReactRouterLocaleLoader };
11
+
12
+ //# sourceMappingURL=react-router.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react-router.js","names":[],"sources":["../../src/adapters/react-router.ts"],"sourcesContent":["import { resolveRequestLocale } from \"./request\";\n\nimport type { RequestLocaleOptions } from \"./request\";\nimport type { LocaleResolution } from \"../locale\";\n\nexport interface ReactRouterLocaleLoaderArgs {\n request: Request;\n params: Readonly<Record<string, string | undefined>>;\n}\n\nexport interface ReactRouterLocaleLoaderOptions extends RequestLocaleOptions {\n localeParam?: string;\n}\n\nexport const createReactRouterLocaleLoader = (\n options: ReactRouterLocaleLoaderOptions,\n): ((args: ReactRouterLocaleLoaderArgs) => LocaleResolution) => {\n const localeParam = options.localeParam ?? \"locale\";\n\n return ({ request, params }) =>\n resolveRequestLocale(request, {\n ...options,\n urlLocale: params[localeParam] ?? options.urlLocale,\n });\n};\n"],"mappings":";;AAcA,MAAa,iCACX,YAC8D;CAC9D,MAAM,cAAc,QAAQ,eAAe;CAE3C,QAAQ,EAAE,SAAS,aACjB,qBAAqB,SAAS;EAC5B,GAAG;EACH,WAAW,OAAO,gBAAgB,QAAQ;CAC5C,CAAC;AACL"}
@@ -0,0 +1,67 @@
1
+ const require_routing_index = require("../routing/index.cjs");
2
+ const require_locale = require("../locale.cjs");
3
+ // src/adapters/request.ts
4
+ const validateCookieName = (cookieName) => {
5
+ if (!cookieName || /[;=\s]/.test(cookieName)) throw new Error(`Invalid locale cookie name "${cookieName}".`);
6
+ };
7
+ const readCookie = (cookieHeader, cookieName) => {
8
+ if (!cookieHeader) return;
9
+ for (const part of cookieHeader.split(";")) {
10
+ const separator = part.indexOf("=");
11
+ if (separator < 0 || part.slice(0, separator).trim() !== cookieName) continue;
12
+ try {
13
+ return decodeURIComponent(part.slice(separator + 1).trim());
14
+ } catch {
15
+ return;
16
+ }
17
+ }
18
+ };
19
+ const resolveHeadersLocale = (headers, options) => {
20
+ const cookieName = options.cookieName ?? "puzzle-locale";
21
+ validateCookieName(cookieName);
22
+ return require_locale.resolveLocale({
23
+ registry: options.registry,
24
+ urlLocale: options.urlLocale,
25
+ cookie: readCookie(headers.get("cookie"), cookieName),
26
+ acceptLanguage: headers.get("accept-language") ?? void 0
27
+ });
28
+ };
29
+ const reportLocalizedRoute = (pathname, options) => {
30
+ if (!options.onRoute || !options.routes) return;
31
+ const routing = require_routing_index.createLocalizedRouting({
32
+ registry: options.registry,
33
+ basePath: options.basePath,
34
+ routes: options.routes
35
+ });
36
+ const match = routing.match(pathname);
37
+ const route = routing.matchRoute(pathname);
38
+ const routePath = route ? options.routes[route.routeId]?.[route.locale] : void 0;
39
+ const pathnameTemplate = typeof routePath === "string" ? routePath : routePath?.path ?? "<unmatched>";
40
+ options.onRoute(Object.freeze({
41
+ type: "route",
42
+ pathname: pathnameTemplate,
43
+ status: route ? route.redirectTo ? "redirected" : "matched" : "unmatched",
44
+ locale: route?.locale ?? match.locale,
45
+ ...route ? { routeId: route.routeId } : {}
46
+ }));
47
+ };
48
+ const resolveRequestLocale = (request, options) => {
49
+ const routing = require_routing_index.createLocalizedRouting({
50
+ registry: options.registry,
51
+ basePath: options.basePath,
52
+ routes: options.routes
53
+ });
54
+ const pathname = new URL(request.url).pathname;
55
+ const match = routing.match(pathname);
56
+ const resolution = resolveHeadersLocale(request.headers, {
57
+ ...options,
58
+ urlLocale: options.urlLocale ?? (match.matchedPrefix ? match.locale : void 0)
59
+ });
60
+ reportLocalizedRoute(pathname, options);
61
+ return resolution;
62
+ };
63
+ exports.reportLocalizedRoute = reportLocalizedRoute;
64
+ exports.resolveHeadersLocale = resolveHeadersLocale;
65
+ exports.resolveRequestLocale = resolveRequestLocale;
66
+
67
+ //# sourceMappingURL=request.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request.cjs","names":["resolveLocale","createLocalizedRouting"],"sources":["../../src/adapters/request.ts"],"sourcesContent":["import { createLocalizedRouting } from \"../routing\";\nimport { resolveLocale } from \"../locale\";\n\nimport type { LocalizedRouteMap } from \"../routing\";\nimport type { I18nRuntimeEvent } from \"../runtime\";\nimport type { LocaleRegistry, LocaleResolution } from \"../locale\";\n\nexport interface HeaderReader {\n get(name: string): string | null;\n}\n\nexport interface RequestLocaleOptions {\n registry: LocaleRegistry;\n cookieName?: string;\n urlLocale?: string;\n basePath?: string;\n routes?: LocalizedRouteMap;\n onRoute?: (event: Extract<I18nRuntimeEvent, { type: \"route\" }>) => void;\n}\n\nconst validateCookieName = (cookieName: string): void => {\n if (!cookieName || /[;=\\s]/.test(cookieName)) {\n throw new Error(`Invalid locale cookie name \"${cookieName}\".`);\n }\n};\n\nconst readCookie = (\n cookieHeader: string | null,\n cookieName: string,\n): string | undefined => {\n if (!cookieHeader) {\n return undefined;\n }\n\n for (const part of cookieHeader.split(\";\")) {\n const separator = part.indexOf(\"=\");\n\n if (separator < 0 || part.slice(0, separator).trim() !== cookieName) {\n continue;\n }\n\n try {\n return decodeURIComponent(part.slice(separator + 1).trim());\n } catch {\n return undefined;\n }\n }\n\n return undefined;\n};\n\nexport const resolveHeadersLocale = (\n headers: HeaderReader,\n options: RequestLocaleOptions,\n): LocaleResolution => {\n const cookieName = options.cookieName ?? \"puzzle-locale\";\n validateCookieName(cookieName);\n\n return resolveLocale({\n registry: options.registry,\n urlLocale: options.urlLocale,\n cookie: readCookie(headers.get(\"cookie\"), cookieName),\n acceptLanguage: headers.get(\"accept-language\") ?? undefined,\n });\n};\n\nexport const reportLocalizedRoute = (\n pathname: string,\n options: RequestLocaleOptions,\n): void => {\n if (!options.onRoute || !options.routes) {\n return;\n }\n\n const routing = createLocalizedRouting({\n registry: options.registry,\n basePath: options.basePath,\n routes: options.routes,\n });\n const match = routing.match(pathname);\n const route = routing.matchRoute(pathname);\n const routePath = route\n ? options.routes[route.routeId]?.[route.locale]\n : undefined;\n const pathnameTemplate = typeof routePath === \"string\"\n ? routePath\n : routePath?.path ?? \"<unmatched>\";\n\n options.onRoute(Object.freeze({\n type: \"route\",\n pathname: pathnameTemplate,\n status: route\n ? route.redirectTo\n ? \"redirected\"\n : \"matched\"\n : \"unmatched\",\n locale: route?.locale ?? match.locale,\n ...(route ? { routeId: route.routeId } : {}),\n }));\n};\n\nexport const resolveRequestLocale = (\n request: Request,\n options: RequestLocaleOptions,\n): LocaleResolution => {\n const routing = createLocalizedRouting({\n registry: options.registry,\n basePath: options.basePath,\n routes: options.routes,\n });\n const pathname = new URL(request.url).pathname;\n const match = routing.match(pathname);\n const resolution = resolveHeadersLocale(request.headers, {\n ...options,\n urlLocale: options.urlLocale ??\n (match.matchedPrefix ? match.locale : undefined),\n });\n\n reportLocalizedRoute(pathname, options);\n\n return resolution;\n};\n"],"mappings":";;;AAoBA,MAAM,sBAAsB,eAA6B;CACvD,IAAI,CAAC,cAAc,SAAS,KAAK,UAAU,GACzC,MAAM,IAAI,MAAM,+BAA+B,WAAW,GAAG;AAEjE;AAEA,MAAM,cACJ,cACA,eACuB;CACvB,IAAI,CAAC,cACH;CAGF,KAAK,MAAM,QAAQ,aAAa,MAAM,GAAG,GAAG;EAC1C,MAAM,YAAY,KAAK,QAAQ,GAAG;EAElC,IAAI,YAAY,KAAK,KAAK,MAAM,GAAG,SAAS,CAAC,CAAC,KAAK,MAAM,YACvD;EAGF,IAAI;GACF,OAAO,mBAAmB,KAAK,MAAM,YAAY,CAAC,CAAC,CAAC,KAAK,CAAC;EAC5D,QAAQ;GACN;EACF;CACF;AAGF;AAEA,MAAa,wBACX,SACA,YACqB;CACrB,MAAM,aAAa,QAAQ,cAAc;CACzC,mBAAmB,UAAU;CAE7B,OAAOA,eAAAA,cAAc;EACnB,UAAU,QAAQ;EAClB,WAAW,QAAQ;EACnB,QAAQ,WAAW,QAAQ,IAAI,QAAQ,GAAG,UAAU;EACpD,gBAAgB,QAAQ,IAAI,iBAAiB,KAAK,KAAA;CACpD,CAAC;AACH;AAEA,MAAa,wBACX,UACA,YACS;CACT,IAAI,CAAC,QAAQ,WAAW,CAAC,QAAQ,QAC/B;CAGF,MAAM,UAAUC,sBAAAA,uBAAuB;EACrC,UAAU,QAAQ;EAClB,UAAU,QAAQ;EAClB,QAAQ,QAAQ;CAClB,CAAC;CACD,MAAM,QAAQ,QAAQ,MAAM,QAAQ;CACpC,MAAM,QAAQ,QAAQ,WAAW,QAAQ;CACzC,MAAM,YAAY,QACd,QAAQ,OAAO,MAAM,QAAQ,GAAG,MAAM,UACtC,KAAA;CACJ,MAAM,mBAAmB,OAAO,cAAc,WAC1C,YACA,WAAW,QAAQ;CAEvB,QAAQ,QAAQ,OAAO,OAAO;EAC5B,MAAM;EACN,UAAU;EACV,QAAQ,QACJ,MAAM,aACJ,eACA,YACF;EACJ,QAAQ,OAAO,UAAU,MAAM;EAC/B,GAAI,QAAQ,EAAE,SAAS,MAAM,QAAQ,IAAI,CAAC;CAC5C,CAAC,CAAC;AACJ;AAEA,MAAa,wBACX,SACA,YACqB;CACrB,MAAM,UAAUA,sBAAAA,uBAAuB;EACrC,UAAU,QAAQ;EAClB,UAAU,QAAQ;EAClB,QAAQ,QAAQ;CAClB,CAAC;CACD,MAAM,WAAW,IAAI,IAAI,QAAQ,GAAG,CAAC,CAAC;CACtC,MAAM,QAAQ,QAAQ,MAAM,QAAQ;CACpC,MAAM,aAAa,qBAAqB,QAAQ,SAAS;EACvD,GAAG;EACH,WAAW,QAAQ,cAChB,MAAM,gBAAgB,MAAM,SAAS,KAAA;CAC1C,CAAC;CAED,qBAAqB,UAAU,OAAO;CAEtC,OAAO;AACT"}
@@ -0,0 +1,16 @@
1
+ import { LocalizedRouteMap } from "../routing/index.cjs";
2
+ import { I18nRuntimeEvent } from "../runtime.cjs";
3
+ import { LocaleRegistry } from "../locale.cjs";
4
+ export interface HeaderReader {
5
+ get(name: string): string | null;
6
+ }
7
+ export interface RequestLocaleOptions {
8
+ registry: LocaleRegistry;
9
+ cookieName?: string;
10
+ urlLocale?: string;
11
+ basePath?: string;
12
+ routes?: LocalizedRouteMap;
13
+ onRoute?: (event: Extract<I18nRuntimeEvent, {
14
+ type: "route";
15
+ }>) => void;
16
+ }
@@ -0,0 +1,16 @@
1
+ import { LocalizedRouteMap } from "../routing/index.js";
2
+ import { I18nRuntimeEvent } from "../runtime.js";
3
+ import { LocaleRegistry } from "../locale.js";
4
+ export interface HeaderReader {
5
+ get(name: string): string | null;
6
+ }
7
+ export interface RequestLocaleOptions {
8
+ registry: LocaleRegistry;
9
+ cookieName?: string;
10
+ urlLocale?: string;
11
+ basePath?: string;
12
+ routes?: LocalizedRouteMap;
13
+ onRoute?: (event: Extract<I18nRuntimeEvent, {
14
+ type: "route";
15
+ }>) => void;
16
+ }
@@ -0,0 +1,65 @@
1
+ import { createLocalizedRouting } from "../routing/index.js";
2
+ import { resolveLocale } from "../locale.js";
3
+ // src/adapters/request.ts
4
+ const validateCookieName = (cookieName) => {
5
+ if (!cookieName || /[;=\s]/.test(cookieName)) throw new Error(`Invalid locale cookie name "${cookieName}".`);
6
+ };
7
+ const readCookie = (cookieHeader, cookieName) => {
8
+ if (!cookieHeader) return;
9
+ for (const part of cookieHeader.split(";")) {
10
+ const separator = part.indexOf("=");
11
+ if (separator < 0 || part.slice(0, separator).trim() !== cookieName) continue;
12
+ try {
13
+ return decodeURIComponent(part.slice(separator + 1).trim());
14
+ } catch {
15
+ return;
16
+ }
17
+ }
18
+ };
19
+ const resolveHeadersLocale = (headers, options) => {
20
+ const cookieName = options.cookieName ?? "puzzle-locale";
21
+ validateCookieName(cookieName);
22
+ return resolveLocale({
23
+ registry: options.registry,
24
+ urlLocale: options.urlLocale,
25
+ cookie: readCookie(headers.get("cookie"), cookieName),
26
+ acceptLanguage: headers.get("accept-language") ?? void 0
27
+ });
28
+ };
29
+ const reportLocalizedRoute = (pathname, options) => {
30
+ if (!options.onRoute || !options.routes) return;
31
+ const routing = createLocalizedRouting({
32
+ registry: options.registry,
33
+ basePath: options.basePath,
34
+ routes: options.routes
35
+ });
36
+ const match = routing.match(pathname);
37
+ const route = routing.matchRoute(pathname);
38
+ const routePath = route ? options.routes[route.routeId]?.[route.locale] : void 0;
39
+ const pathnameTemplate = typeof routePath === "string" ? routePath : routePath?.path ?? "<unmatched>";
40
+ options.onRoute(Object.freeze({
41
+ type: "route",
42
+ pathname: pathnameTemplate,
43
+ status: route ? route.redirectTo ? "redirected" : "matched" : "unmatched",
44
+ locale: route?.locale ?? match.locale,
45
+ ...route ? { routeId: route.routeId } : {}
46
+ }));
47
+ };
48
+ const resolveRequestLocale = (request, options) => {
49
+ const routing = createLocalizedRouting({
50
+ registry: options.registry,
51
+ basePath: options.basePath,
52
+ routes: options.routes
53
+ });
54
+ const pathname = new URL(request.url).pathname;
55
+ const match = routing.match(pathname);
56
+ const resolution = resolveHeadersLocale(request.headers, {
57
+ ...options,
58
+ urlLocale: options.urlLocale ?? (match.matchedPrefix ? match.locale : void 0)
59
+ });
60
+ reportLocalizedRoute(pathname, options);
61
+ return resolution;
62
+ };
63
+ export { reportLocalizedRoute, resolveHeadersLocale, resolveRequestLocale };
64
+
65
+ //# sourceMappingURL=request.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request.js","names":[],"sources":["../../src/adapters/request.ts"],"sourcesContent":["import { createLocalizedRouting } from \"../routing\";\nimport { resolveLocale } from \"../locale\";\n\nimport type { LocalizedRouteMap } from \"../routing\";\nimport type { I18nRuntimeEvent } from \"../runtime\";\nimport type { LocaleRegistry, LocaleResolution } from \"../locale\";\n\nexport interface HeaderReader {\n get(name: string): string | null;\n}\n\nexport interface RequestLocaleOptions {\n registry: LocaleRegistry;\n cookieName?: string;\n urlLocale?: string;\n basePath?: string;\n routes?: LocalizedRouteMap;\n onRoute?: (event: Extract<I18nRuntimeEvent, { type: \"route\" }>) => void;\n}\n\nconst validateCookieName = (cookieName: string): void => {\n if (!cookieName || /[;=\\s]/.test(cookieName)) {\n throw new Error(`Invalid locale cookie name \"${cookieName}\".`);\n }\n};\n\nconst readCookie = (\n cookieHeader: string | null,\n cookieName: string,\n): string | undefined => {\n if (!cookieHeader) {\n return undefined;\n }\n\n for (const part of cookieHeader.split(\";\")) {\n const separator = part.indexOf(\"=\");\n\n if (separator < 0 || part.slice(0, separator).trim() !== cookieName) {\n continue;\n }\n\n try {\n return decodeURIComponent(part.slice(separator + 1).trim());\n } catch {\n return undefined;\n }\n }\n\n return undefined;\n};\n\nexport const resolveHeadersLocale = (\n headers: HeaderReader,\n options: RequestLocaleOptions,\n): LocaleResolution => {\n const cookieName = options.cookieName ?? \"puzzle-locale\";\n validateCookieName(cookieName);\n\n return resolveLocale({\n registry: options.registry,\n urlLocale: options.urlLocale,\n cookie: readCookie(headers.get(\"cookie\"), cookieName),\n acceptLanguage: headers.get(\"accept-language\") ?? undefined,\n });\n};\n\nexport const reportLocalizedRoute = (\n pathname: string,\n options: RequestLocaleOptions,\n): void => {\n if (!options.onRoute || !options.routes) {\n return;\n }\n\n const routing = createLocalizedRouting({\n registry: options.registry,\n basePath: options.basePath,\n routes: options.routes,\n });\n const match = routing.match(pathname);\n const route = routing.matchRoute(pathname);\n const routePath = route\n ? options.routes[route.routeId]?.[route.locale]\n : undefined;\n const pathnameTemplate = typeof routePath === \"string\"\n ? routePath\n : routePath?.path ?? \"<unmatched>\";\n\n options.onRoute(Object.freeze({\n type: \"route\",\n pathname: pathnameTemplate,\n status: route\n ? route.redirectTo\n ? \"redirected\"\n : \"matched\"\n : \"unmatched\",\n locale: route?.locale ?? match.locale,\n ...(route ? { routeId: route.routeId } : {}),\n }));\n};\n\nexport const resolveRequestLocale = (\n request: Request,\n options: RequestLocaleOptions,\n): LocaleResolution => {\n const routing = createLocalizedRouting({\n registry: options.registry,\n basePath: options.basePath,\n routes: options.routes,\n });\n const pathname = new URL(request.url).pathname;\n const match = routing.match(pathname);\n const resolution = resolveHeadersLocale(request.headers, {\n ...options,\n urlLocale: options.urlLocale ??\n (match.matchedPrefix ? match.locale : undefined),\n });\n\n reportLocalizedRoute(pathname, options);\n\n return resolution;\n};\n"],"mappings":";;;AAoBA,MAAM,sBAAsB,eAA6B;CACvD,IAAI,CAAC,cAAc,SAAS,KAAK,UAAU,GACzC,MAAM,IAAI,MAAM,+BAA+B,WAAW,GAAG;AAEjE;AAEA,MAAM,cACJ,cACA,eACuB;CACvB,IAAI,CAAC,cACH;CAGF,KAAK,MAAM,QAAQ,aAAa,MAAM,GAAG,GAAG;EAC1C,MAAM,YAAY,KAAK,QAAQ,GAAG;EAElC,IAAI,YAAY,KAAK,KAAK,MAAM,GAAG,SAAS,CAAC,CAAC,KAAK,MAAM,YACvD;EAGF,IAAI;GACF,OAAO,mBAAmB,KAAK,MAAM,YAAY,CAAC,CAAC,CAAC,KAAK,CAAC;EAC5D,QAAQ;GACN;EACF;CACF;AAGF;AAEA,MAAa,wBACX,SACA,YACqB;CACrB,MAAM,aAAa,QAAQ,cAAc;CACzC,mBAAmB,UAAU;CAE7B,OAAO,cAAc;EACnB,UAAU,QAAQ;EAClB,WAAW,QAAQ;EACnB,QAAQ,WAAW,QAAQ,IAAI,QAAQ,GAAG,UAAU;EACpD,gBAAgB,QAAQ,IAAI,iBAAiB,KAAK,KAAA;CACpD,CAAC;AACH;AAEA,MAAa,wBACX,UACA,YACS;CACT,IAAI,CAAC,QAAQ,WAAW,CAAC,QAAQ,QAC/B;CAGF,MAAM,UAAU,uBAAuB;EACrC,UAAU,QAAQ;EAClB,UAAU,QAAQ;EAClB,QAAQ,QAAQ;CAClB,CAAC;CACD,MAAM,QAAQ,QAAQ,MAAM,QAAQ;CACpC,MAAM,QAAQ,QAAQ,WAAW,QAAQ;CACzC,MAAM,YAAY,QACd,QAAQ,OAAO,MAAM,QAAQ,GAAG,MAAM,UACtC,KAAA;CACJ,MAAM,mBAAmB,OAAO,cAAc,WAC1C,YACA,WAAW,QAAQ;CAEvB,QAAQ,QAAQ,OAAO,OAAO;EAC5B,MAAM;EACN,UAAU;EACV,QAAQ,QACJ,MAAM,aACJ,eACA,YACF;EACJ,QAAQ,OAAO,UAAU,MAAM;EAC/B,GAAI,QAAQ,EAAE,SAAS,MAAM,QAAQ,IAAI,CAAC;CAC5C,CAAC,CAAC;AACJ;AAEA,MAAa,wBACX,SACA,YACqB;CACrB,MAAM,UAAU,uBAAuB;EACrC,UAAU,QAAQ;EAClB,UAAU,QAAQ;EAClB,QAAQ,QAAQ;CAClB,CAAC;CACD,MAAM,WAAW,IAAI,IAAI,QAAQ,GAAG,CAAC,CAAC;CACtC,MAAM,QAAQ,QAAQ,MAAM,QAAQ;CACpC,MAAM,aAAa,qBAAqB,QAAQ,SAAS;EACvD,GAAG;EACH,WAAW,QAAQ,cAChB,MAAM,gBAAgB,MAAM,SAAS,KAAA;CAC1C,CAAC;CAED,qBAAqB,UAAU,OAAO;CAEtC,OAAO;AACT"}
@@ -0,0 +1,13 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_request = require("./request.cjs");
3
+ // src/adapters/tanstack.ts
4
+ const createTanStackLocaleLoader = (options) => {
5
+ const localeParam = options.localeParam ?? "locale";
6
+ return ({ request, params }) => require_request.resolveRequestLocale(request, {
7
+ ...options,
8
+ urlLocale: params?.[localeParam] ?? options.urlLocale
9
+ });
10
+ };
11
+ exports.createTanStackLocaleLoader = createTanStackLocaleLoader;
12
+
13
+ //# sourceMappingURL=tanstack.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tanstack.cjs","names":["resolveRequestLocale"],"sources":["../../src/adapters/tanstack.ts"],"sourcesContent":["import { resolveRequestLocale } from \"./request\";\n\nimport type { RequestLocaleOptions } from \"./request\";\nimport type { LocaleResolution } from \"../locale\";\n\nexport interface TanStackLocaleLoaderArgs {\n request: Request;\n params?: Readonly<Record<string, string | undefined>>;\n}\n\nexport interface TanStackLocaleLoaderOptions extends RequestLocaleOptions {\n localeParam?: string;\n}\n\nexport const createTanStackLocaleLoader = (\n options: TanStackLocaleLoaderOptions,\n): ((args: TanStackLocaleLoaderArgs) => LocaleResolution) => {\n const localeParam = options.localeParam ?? \"locale\";\n\n return ({ request, params }) =>\n resolveRequestLocale(request, {\n ...options,\n urlLocale: params?.[localeParam] ?? options.urlLocale,\n });\n};\n"],"mappings":";;;AAcA,MAAa,8BACX,YAC2D;CAC3D,MAAM,cAAc,QAAQ,eAAe;CAE3C,QAAQ,EAAE,SAAS,aACjBA,gBAAAA,qBAAqB,SAAS;EAC5B,GAAG;EACH,WAAW,SAAS,gBAAgB,QAAQ;CAC9C,CAAC;AACL"}
@@ -0,0 +1,10 @@
1
+ import { RequestLocaleOptions } from "./request.cjs";
2
+ import { LocaleResolution } from "../locale.cjs";
3
+ export interface TanStackLocaleLoaderArgs {
4
+ request: Request;
5
+ params?: Readonly<Record<string, string | undefined>>;
6
+ }
7
+ export interface TanStackLocaleLoaderOptions extends RequestLocaleOptions {
8
+ localeParam?: string;
9
+ }
10
+ export declare const createTanStackLocaleLoader: (options: TanStackLocaleLoaderOptions) => ((args: TanStackLocaleLoaderArgs) => LocaleResolution);
@@ -0,0 +1,10 @@
1
+ import { RequestLocaleOptions } from "./request.js";
2
+ import { LocaleResolution } from "../locale.js";
3
+ export interface TanStackLocaleLoaderArgs {
4
+ request: Request;
5
+ params?: Readonly<Record<string, string | undefined>>;
6
+ }
7
+ export interface TanStackLocaleLoaderOptions extends RequestLocaleOptions {
8
+ localeParam?: string;
9
+ }
10
+ export declare const createTanStackLocaleLoader: (options: TanStackLocaleLoaderOptions) => ((args: TanStackLocaleLoaderArgs) => LocaleResolution);