@quilted/preact-localize 0.0.0-preview-20240627021955

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 (84) hide show
  1. package/CHANGELOG.md +222 -0
  2. package/LICENSE.md +21 -0
  3. package/build/esm/Localization.mjs +26 -0
  4. package/build/esm/context.mjs +6 -0
  5. package/build/esm/hooks/formatting.mjs +5 -0
  6. package/build/esm/hooks/locale-from-environment.mjs +15 -0
  7. package/build/esm/hooks/locale.mjs +5 -0
  8. package/build/esm/index.mjs +13 -0
  9. package/build/esm/request-router.mjs +49 -0
  10. package/build/esm/routing/LocalizedLink.mjs +26 -0
  11. package/build/esm/routing/LocalizedNavigation.mjs +48 -0
  12. package/build/esm/routing/LocalizedRouter.mjs +34 -0
  13. package/build/esm/routing/context.mjs +6 -0
  14. package/build/esm/routing/localization/by-locale.mjs +50 -0
  15. package/build/esm/routing/localization/by-path.mjs +23 -0
  16. package/build/esm/routing/localization/by-subdomain.mjs +25 -0
  17. package/build/esnext/Localization.esnext +35 -0
  18. package/build/esnext/context.esnext +6 -0
  19. package/build/esnext/hooks/formatting.esnext +5 -0
  20. package/build/esnext/hooks/locale-from-environment.esnext +13 -0
  21. package/build/esnext/hooks/locale.esnext +5 -0
  22. package/build/esnext/index.esnext +13 -0
  23. package/build/esnext/request-router.esnext +48 -0
  24. package/build/esnext/routing/LocalizedLink.esnext +21 -0
  25. package/build/esnext/routing/LocalizedNavigation.esnext +36 -0
  26. package/build/esnext/routing/LocalizedRouter.esnext +32 -0
  27. package/build/esnext/routing/context.esnext +6 -0
  28. package/build/esnext/routing/localization/by-locale.esnext +54 -0
  29. package/build/esnext/routing/localization/by-path.esnext +20 -0
  30. package/build/esnext/routing/localization/by-subdomain.esnext +22 -0
  31. package/build/tsconfig.tsbuildinfo +1 -0
  32. package/build/typescript/Localization.d.ts +7 -0
  33. package/build/typescript/Localization.d.ts.map +1 -0
  34. package/build/typescript/context.d.ts +4 -0
  35. package/build/typescript/context.d.ts.map +1 -0
  36. package/build/typescript/hooks/formatting.d.ts +2 -0
  37. package/build/typescript/hooks/formatting.d.ts.map +1 -0
  38. package/build/typescript/hooks/locale-from-environment.d.ts +2 -0
  39. package/build/typescript/hooks/locale-from-environment.d.ts.map +1 -0
  40. package/build/typescript/hooks/locale.d.ts +2 -0
  41. package/build/typescript/hooks/locale.d.ts.map +1 -0
  42. package/build/typescript/index.d.ts +9 -0
  43. package/build/typescript/index.d.ts.map +1 -0
  44. package/build/typescript/request-router.d.ts +16 -0
  45. package/build/typescript/request-router.d.ts.map +1 -0
  46. package/build/typescript/routing/LocalizedLink.d.ts +9 -0
  47. package/build/typescript/routing/LocalizedLink.d.ts.map +1 -0
  48. package/build/typescript/routing/LocalizedNavigation.d.ts +13 -0
  49. package/build/typescript/routing/LocalizedNavigation.d.ts.map +1 -0
  50. package/build/typescript/routing/LocalizedRouter.d.ts +9 -0
  51. package/build/typescript/routing/LocalizedRouter.d.ts.map +1 -0
  52. package/build/typescript/routing/LocalizedRouting.d.ts +9 -0
  53. package/build/typescript/routing/LocalizedRouting.d.ts.map +1 -0
  54. package/build/typescript/routing/context.d.ts +4 -0
  55. package/build/typescript/routing/context.d.ts.map +1 -0
  56. package/build/typescript/routing/localization/by-locale.d.ts +6 -0
  57. package/build/typescript/routing/localization/by-locale.d.ts.map +1 -0
  58. package/build/typescript/routing/localization/by-path.d.ts +6 -0
  59. package/build/typescript/routing/localization/by-path.d.ts.map +1 -0
  60. package/build/typescript/routing/localization/by-subdomain.d.ts +7 -0
  61. package/build/typescript/routing/localization/by-subdomain.d.ts.map +1 -0
  62. package/build/typescript/routing/types.d.ts +17 -0
  63. package/build/typescript/routing/types.d.ts.map +1 -0
  64. package/build/typescript/routing.d.ts +9 -0
  65. package/build/typescript/routing.d.ts.map +1 -0
  66. package/configuration/rollup.config.js +3 -0
  67. package/package.json +67 -0
  68. package/source/Localization.tsx +45 -0
  69. package/source/context.ts +7 -0
  70. package/source/hooks/formatting.ts +3 -0
  71. package/source/hooks/locale-from-environment.ts +15 -0
  72. package/source/hooks/locale.ts +3 -0
  73. package/source/index.ts +26 -0
  74. package/source/request-router.ts +98 -0
  75. package/source/routing/LocalizedLink.tsx +27 -0
  76. package/source/routing/LocalizedNavigation.tsx +64 -0
  77. package/source/routing/LocalizedRouter.ts +46 -0
  78. package/source/routing/context.ts +8 -0
  79. package/source/routing/localization/by-locale.ts +71 -0
  80. package/source/routing/localization/by-path.ts +31 -0
  81. package/source/routing/localization/by-subdomain.ts +34 -0
  82. package/source/routing/types.ts +16 -0
  83. package/source/routing.ts +12 -0
  84. package/tsconfig.json +17 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,222 @@
1
+ # @quilted/react-localize
2
+
3
+ ## 0.0.0-preview-20240627021955
4
+
5
+ ### Patch Changes
6
+
7
+ - Simplify routing library
8
+
9
+ - Updated dependencies []:
10
+ - @quilted/request-router@0.0.0-preview-20240627021955
11
+ - @quilted/preact-router@0.0.0-preview-20240627021955
12
+
13
+ ## 1.0.0
14
+
15
+ ### Patch Changes
16
+
17
+ - [#757](https://github.com/lemonmade/quilt/pull/757) [`00cac4b`](https://github.com/lemonmade/quilt/commit/00cac4b4d01831ba654e94152d7a67a0ef75043b) Thanks [@lemonmade](https://github.com/lemonmade)! - Simplify routing library
18
+
19
+ - Updated dependencies [[`00cac4b`](https://github.com/lemonmade/quilt/commit/00cac4b4d01831ba654e94152d7a67a0ef75043b)]:
20
+ - @quilted/request-router@0.3.0
21
+ - @quilted/preact-router@0.2.0
22
+
23
+ ## 0.2.2
24
+
25
+ ### Patch Changes
26
+
27
+ - [#716](https://github.com/lemonmade/quilt/pull/716) [`7daafca`](https://github.com/lemonmade/quilt/commit/7daafca900b3d9ea66be179394eadf7998cc94be) Thanks [@lemonmade](https://github.com/lemonmade)! - Refactor browser APIs
28
+
29
+ - Updated dependencies [[`7daafca`](https://github.com/lemonmade/quilt/commit/7daafca900b3d9ea66be179394eadf7998cc94be)]:
30
+ - @quilted/react-browser@0.0.1
31
+ - @quilted/react-router@0.4.1
32
+
33
+ ## 0.2.1
34
+
35
+ ### Patch Changes
36
+
37
+ - [`905e92ef`](https://github.com/lemonmade/quilt/commit/905e92ef32adad8658042f437b9cfbd248cce3c3) Thanks [@lemonmade](https://github.com/lemonmade)! - Simplify React exports
38
+
39
+ ## 0.2.0
40
+
41
+ ### Minor Changes
42
+
43
+ - [#645](https://github.com/lemonmade/quilt/pull/645) [`302ed847`](https://github.com/lemonmade/quilt/commit/302ed8479f9c035ef39d48137de958dba50690ca) Thanks [@lemonmade](https://github.com/lemonmade)! - Removed CommonJS support
44
+
45
+ The `require` export condition is no longer provided by any package. Quilt only supports ESModules, so if you need to use the CommonJS version, you will need to pre-process Quilt’s output code on your own.
46
+
47
+ ### Patch Changes
48
+
49
+ - Updated dependencies [[`302ed847`](https://github.com/lemonmade/quilt/commit/302ed8479f9c035ef39d48137de958dba50690ca)]:
50
+ - @quilted/useful-react-types@2.0.0
51
+ - @quilted/localize@0.2.0
52
+ - @quilted/react-html@0.4.0
53
+ - @quilted/react-http@0.4.0
54
+ - @quilted/react-router@0.4.0
55
+ - @quilted/react-utilities@0.2.0
56
+ - @quilted/request-router@0.2.0
57
+
58
+ ## 0.1.19
59
+
60
+ ### Patch Changes
61
+
62
+ - [#612](https://github.com/lemonmade/quilt/pull/612) [`bc849bc7`](https://github.com/lemonmade/quilt/commit/bc849bc740318936656162fde851b784ed6ef78f) Thanks [@lemonmade](https://github.com/lemonmade)! - Simplify app template APIs
63
+
64
+ - Updated dependencies [[`bc849bc7`](https://github.com/lemonmade/quilt/commit/bc849bc740318936656162fde851b784ed6ef78f)]:
65
+ - @quilted/react-http@0.3.31
66
+ - @quilted/request-router@0.1.8
67
+
68
+ ## 0.1.18
69
+
70
+ ### Patch Changes
71
+
72
+ - Updated dependencies [[`9122cbbc`](https://github.com/lemonmade/quilt/commit/9122cbbce965bf5b432027e0707b2d619857fa67)]:
73
+ - @quilted/useful-react-types@1.0.0
74
+
75
+ ## 0.1.17
76
+
77
+ ### Patch Changes
78
+
79
+ - [`6e999a5f`](https://github.com/lemonmade/quilt/commit/6e999a5f2b3001923fdb83d9954318742e3a84e3) Thanks [@lemonmade](https://github.com/lemonmade)! - Add basic translation function
80
+
81
+ ## 0.1.16
82
+
83
+ ### Patch Changes
84
+
85
+ - [`7a431339`](https://github.com/lemonmade/quilt/commit/7a4313390827aefdbbf9b28f2a5aa6213d6b33f5) Thanks [@lemonmade](https://github.com/lemonmade)! - Add automatic `dir` property in Automatically add `dir` HTML attribute
86
+
87
+ ## 0.1.15
88
+
89
+ ### Patch Changes
90
+
91
+ - [`97812120`](https://github.com/lemonmade/quilt/commit/978121207c65a4450a8ca9e43d017c6425a315c3) Thanks [@lemonmade](https://github.com/lemonmade)! - Update Preact dependencies and fix some missing peer dependencies
92
+
93
+ - Updated dependencies [[`1a22a1c5`](https://github.com/lemonmade/quilt/commit/1a22a1c572c3d37d2564eb9eb908904a6d254201), [`97812120`](https://github.com/lemonmade/quilt/commit/978121207c65a4450a8ca9e43d017c6425a315c3)]:
94
+ - @quilted/request-router@0.1.5
95
+ - @quilted/react-http@0.3.29
96
+
97
+ ## 0.1.14
98
+
99
+ ### Patch Changes
100
+
101
+ - [#536](https://github.com/lemonmade/quilt/pull/536) [`cf6e2de1`](https://github.com/lemonmade/quilt/commit/cf6e2de186d8644fad9afcedda85c05002e909e1) Thanks [@lemonmade](https://github.com/lemonmade)! - Update to TypeScript 5.0
102
+
103
+ - Updated dependencies [[`cf6e2de1`](https://github.com/lemonmade/quilt/commit/cf6e2de186d8644fad9afcedda85c05002e909e1)]:
104
+ - @quilted/react-http@0.3.28
105
+ - @quilted/request-router@0.1.4
106
+
107
+ ## 0.1.13
108
+
109
+ ### Patch Changes
110
+
111
+ - [`8f1d275b`](https://github.com/lemonmade/quilt/commit/8f1d275b6de0abbc6f61bcd5401555f6480eb474) Thanks [@lemonmade](https://github.com/lemonmade)! - Remove need for @babel/runtime peer dependency
112
+
113
+ - Updated dependencies [[`8f1d275b`](https://github.com/lemonmade/quilt/commit/8f1d275b6de0abbc6f61bcd5401555f6480eb474), [`50215b7c`](https://github.com/lemonmade/quilt/commit/50215b7c005c21440bca04935fda87d98d9d9d01)]:
114
+ - @quilted/react-http@0.3.27
115
+ - @quilted/request-router@0.1.3
116
+
117
+ ## 0.1.12
118
+
119
+ ### Patch Changes
120
+
121
+ - [#474](https://github.com/lemonmade/quilt/pull/474) [`8890fad8`](https://github.com/lemonmade/quilt/commit/8890fad8d04efa95b362f4beaefcdbd51e65ba04) Thanks [@lemonmade](https://github.com/lemonmade)! - Looser React version restrictions
122
+
123
+ - Updated dependencies [[`e647289c`](https://github.com/lemonmade/quilt/commit/e647289c14a2bf8d0d9d322cd3fe1be3f675c535), [`8890fad8`](https://github.com/lemonmade/quilt/commit/8890fad8d04efa95b362f4beaefcdbd51e65ba04)]:
124
+ - @quilted/react-http@0.3.25
125
+
126
+ ## 0.1.11
127
+
128
+ ### Patch Changes
129
+
130
+ - [`a011fa25`](https://github.com/lemonmade/quilt/commit/a011fa25808818ed4e5651a25f267becc327ef2d) Thanks [@lemonmade](https://github.com/lemonmade)! - Fix Localization `lang` attribute on client
131
+
132
+ ## 0.1.10
133
+
134
+ ### Patch Changes
135
+
136
+ - [#447](https://github.com/lemonmade/quilt/pull/447) [`6ad741b2`](https://github.com/lemonmade/quilt/commit/6ad741b241027c8d7612e206497935ad938ea6c9) Thanks [@lemonmade](https://github.com/lemonmade)! - Simplify app templates
137
+
138
+ ## 0.1.9
139
+
140
+ ### Patch Changes
141
+
142
+ - [#436](https://github.com/lemonmade/quilt/pull/436) [`3171fcee`](https://github.com/lemonmade/quilt/commit/3171fceeddfb14c253ac45e34e1e2f9ab6e3f6c0) Thanks [@lemonmade](https://github.com/lemonmade)! - Rename http-handlers to request-router
143
+
144
+ - Updated dependencies [[`3171fcee`](https://github.com/lemonmade/quilt/commit/3171fceeddfb14c253ac45e34e1e2f9ab6e3f6c0)]:
145
+ - @quilted/request-router@0.1.1
146
+
147
+ ## 0.1.8
148
+
149
+ ### Patch Changes
150
+
151
+ - [`51482122`](https://github.com/lemonmade/quilt/commit/514821223b4d8eb9c5289265c7cd2b4ef0b2e8b3) Thanks [@lemonmade](https://github.com/lemonmade)! - Add a utility for creating optional context
152
+
153
+ ## 0.1.7
154
+
155
+ ### Patch Changes
156
+
157
+ - [`a12c3576`](https://github.com/lemonmade/quilt/commit/a12c357693b173461f51a35fb7efdd0a9267e471) Thanks [@lemonmade](https://github.com/lemonmade)! - Fix more build issues
158
+
159
+ - Updated dependencies [[`a12c3576`](https://github.com/lemonmade/quilt/commit/a12c357693b173461f51a35fb7efdd0a9267e471)]:
160
+ - @quilted/http-handlers@0.2.43
161
+ - @quilted/react-http@0.3.23
162
+
163
+ ## 0.1.6
164
+
165
+ ### Patch Changes
166
+
167
+ - [`0629288e`](https://github.com/lemonmade/quilt/commit/0629288ee4ba2e2ccfd73fbb216c3559e1a5c77e) Thanks [@lemonmade](https://github.com/lemonmade)! - Fix missing package builds
168
+
169
+ - Updated dependencies [[`0629288e`](https://github.com/lemonmade/quilt/commit/0629288ee4ba2e2ccfd73fbb216c3559e1a5c77e)]:
170
+ - @quilted/http-handlers@0.2.42
171
+ - @quilted/react-http@0.3.22
172
+
173
+ ## 0.1.5
174
+
175
+ ### Patch Changes
176
+
177
+ - [#364](https://github.com/lemonmade/quilt/pull/364) [`4dc1808a`](https://github.com/lemonmade/quilt/commit/4dc1808a86d15e821b218b528617430cbd8b5b48) Thanks [@lemonmade](https://github.com/lemonmade)! - Update to simplified Quilt config
178
+
179
+ - Updated dependencies [[`4dc1808a`](https://github.com/lemonmade/quilt/commit/4dc1808a86d15e821b218b528617430cbd8b5b48)]:
180
+ - @quilted/http-handlers@0.2.41
181
+ - @quilted/react-http@0.3.21
182
+
183
+ ## 0.1.4
184
+
185
+ ### Patch Changes
186
+
187
+ - [#359](https://github.com/lemonmade/quilt/pull/359) [`2eceac54`](https://github.com/lemonmade/quilt/commit/2eceac546fa3ee3e2c4d2887ab4a6a021acb52cd) Thanks [@lemonmade](https://github.com/lemonmade)! - Update TypeScript and ESLint to latest versions
188
+
189
+ * [#361](https://github.com/lemonmade/quilt/pull/361) [`47065859`](https://github.com/lemonmade/quilt/commit/47065859c330e2da23d8758fb165ae84a4f1af4f) Thanks [@lemonmade](https://github.com/lemonmade)! - Move to native Request and Response objects
190
+
191
+ * Updated dependencies [[`2eceac54`](https://github.com/lemonmade/quilt/commit/2eceac546fa3ee3e2c4d2887ab4a6a021acb52cd), [`47065859`](https://github.com/lemonmade/quilt/commit/47065859c330e2da23d8758fb165ae84a4f1af4f)]:
192
+ - @quilted/http-handlers@0.2.38
193
+ - @quilted/react-http@0.3.20
194
+
195
+ ## 0.1.3
196
+
197
+ ### Patch Changes
198
+
199
+ - [`ddaa8197`](https://github.com/lemonmade/quilt/commit/ddaa8197c5576d5c29b90bfc7752596df56c97b1) Thanks [@lemonmade](https://github.com/lemonmade)! - Improve localization fallback behavior
200
+
201
+ ## 0.1.2
202
+
203
+ ### Patch Changes
204
+
205
+ - [#269](https://github.com/lemonmade/quilt/pull/269) [`c78a1e13`](https://github.com/lemonmade/quilt/commit/c78a1e1365807e072cda4fece55d53712210cad4) Thanks [@lemonmade](https://github.com/lemonmade)! - Add localized routing
206
+
207
+ ## 0.1.1
208
+
209
+ ### Patch Changes
210
+
211
+ - [#263](https://github.com/lemonmade/quilt/pull/263) [`6df853eb`](https://github.com/lemonmade/quilt/commit/6df853eb1e83abfa00e88b43e91b350da28d2704) Thanks [@lemonmade](https://github.com/lemonmade)! - Rename locale from environment hook
212
+
213
+ ## 0.1.0
214
+
215
+ ### Minor Changes
216
+
217
+ - [`b0040c72`](https://github.com/lemonmade/quilt/commit/b0040c7279931f426df5d7fac91a3a35ea49eae1) Thanks [@lemonmade](https://github.com/lemonmade)! - Initial localization packages
218
+
219
+ ### Patch Changes
220
+
221
+ - Updated dependencies [[`b0040c72`](https://github.com/lemonmade/quilt/commit/b0040c7279931f426df5d7fac91a3a35ea49eae1)]:
222
+ - @quilted/localize@0.1.0
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019-present, Chris Sauvé and contributors
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,26 @@
1
+ import { useMemo } from 'preact/hooks';
2
+ import { createLocalizedFormatting } from '@quilted/localize';
3
+ import { HTMLAttributes } from '@quilted/preact-browser';
4
+ import { LocaleContext, LocalizedFormattingContext } from './context.mjs';
5
+ import { jsx, jsxs } from 'preact/jsx-runtime';
6
+
7
+ const RTL_LOCALES = new Set(['ar', 'arc', 'ckb', 'dv', 'fa', 'ha', 'he', 'khw', 'ks', 'ps', 'sd', 'ur', 'uz-AF', 'yi']);
8
+ function Localization({
9
+ locale,
10
+ direction = RTL_LOCALES.has(locale) ? 'rtl' : 'ltr',
11
+ children
12
+ }) {
13
+ const formatting = useMemo(() => createLocalizedFormatting(locale), [locale]);
14
+ return jsx(LocaleContext.Provider, {
15
+ value: locale,
16
+ children: jsxs(LocalizedFormattingContext.Provider, {
17
+ value: formatting,
18
+ children: [jsx(HTMLAttributes, {
19
+ lang: locale,
20
+ dir: direction
21
+ }), children]
22
+ })
23
+ });
24
+ }
25
+
26
+ export { Localization };
@@ -0,0 +1,6 @@
1
+ import { createOptionalContext } from '@quilted/preact-context';
2
+
3
+ const LocalizedFormattingContext = createOptionalContext();
4
+ const LocaleContext = createOptionalContext();
5
+
6
+ export { LocaleContext, LocalizedFormattingContext };
@@ -0,0 +1,5 @@
1
+ import { LocalizedFormattingContext } from '../context.mjs';
2
+
3
+ const useLocalizedFormatting = LocalizedFormattingContext.use;
4
+
5
+ export { useLocalizedFormatting };
@@ -0,0 +1,15 @@
1
+ import { parseAcceptLanguageHeader } from '@quilted/localize';
2
+ import { useBrowserDetails } from '@quilted/preact-browser';
3
+
4
+ function useLocaleFromEnvironment() {
5
+ if (typeof navigator === 'object' && navigator.language) {
6
+ return navigator.language;
7
+ }
8
+ const browserDetails = useBrowserDetails({
9
+ optional: true
10
+ });
11
+ const acceptLanguage = browserDetails?.request.headers?.get('Accept-Language');
12
+ return acceptLanguage && parseAcceptLanguageHeader(acceptLanguage);
13
+ }
14
+
15
+ export { useLocaleFromEnvironment };
@@ -0,0 +1,5 @@
1
+ import { LocaleContext } from '../context.mjs';
2
+
3
+ const useLocale = LocaleContext.use;
4
+
5
+ export { useLocale };
@@ -0,0 +1,13 @@
1
+ export { MissingTranslationError, MissingTranslationPlaceholderError, createLocalizedFormatting, createTranslate, parseAcceptLanguageHeader } from '@quilted/localize';
2
+ export { Localization } from './Localization.mjs';
3
+ export { useLocalizedFormatting } from './hooks/formatting.mjs';
4
+ export { useLocale } from './hooks/locale.mjs';
5
+ export { useLocaleFromEnvironment } from './hooks/locale-from-environment.mjs';
6
+ export { LocalizedFormattingContext } from './context.mjs';
7
+ export { LocalizedLink } from './routing/LocalizedLink.mjs';
8
+ export { LocalizedNavigation } from './routing/LocalizedNavigation.mjs';
9
+ export { LocalizedRouter } from './routing/LocalizedRouter.mjs';
10
+ export { useRouteLocalization } from './routing/context.mjs';
11
+ export { createRouteLocalization } from './routing/localization/by-locale.mjs';
12
+ export { createRoutePathLocalization } from './routing/localization/by-path.mjs';
13
+ export { createRouteSubdomainLocalization } from './routing/localization/by-subdomain.mjs';
@@ -0,0 +1,49 @@
1
+ import { RedirectResponse } from '@quilted/request-router';
2
+ import { parseAcceptLanguageHeader } from '@quilted/localize';
3
+
4
+ function createRequestRouterLocalization({
5
+ localization,
6
+ requestLocale: customRequestLocale
7
+ }) {
8
+ const {
9
+ matchLocale,
10
+ redirectURL,
11
+ defaultLocale,
12
+ localeFromURL
13
+ } = localization;
14
+ const getDefaultLocaleFromRequest = request => {
15
+ const acceptLanguage = request.headers.get('Accept-Language');
16
+ return acceptLanguage && parseAcceptLanguageHeader(acceptLanguage) || undefined;
17
+ };
18
+ const getLocaleForRequest = customRequestLocale ?? getDefaultLocaleFromRequest;
19
+ function localeRedirect(request, {
20
+ to,
21
+ ...options
22
+ }) {
23
+ return new RedirectResponse(redirectURL(new URL(request.url), {
24
+ to
25
+ }), options);
26
+ }
27
+ return {
28
+ redirect: localeRedirect,
29
+ localizedRequestHandler(handler, {
30
+ include = () => true
31
+ } = {}) {
32
+ return async (request, ...args) => {
33
+ if (!include(request)) return handler(request, ...args);
34
+ const url = new URL(request.url);
35
+ const urlLocale = localeFromURL(url);
36
+ const requestLocale = getLocaleForRequest(request, () => getDefaultLocaleFromRequest(request));
37
+ const matchedLocale = (requestLocale == null ? undefined : matchLocale(requestLocale)) ?? defaultLocale;
38
+ if (urlLocale !== matchedLocale) {
39
+ return new RedirectResponse(redirectURL(url, {
40
+ to: matchedLocale
41
+ }));
42
+ }
43
+ return handler(request, ...args);
44
+ };
45
+ }
46
+ };
47
+ }
48
+
49
+ export { createRequestRouterLocalization };
@@ -0,0 +1,26 @@
1
+ import { useMemo } from 'preact/hooks';
2
+ import { useRouter, useCurrentURL, Link } from '@quilted/preact-router';
3
+ import { useRouteLocalization } from './context.mjs';
4
+ import { jsx } from 'preact/jsx-runtime';
5
+
6
+ function LocalizedLink({
7
+ to,
8
+ locale,
9
+ ...props
10
+ }) {
11
+ const router = useRouter();
12
+ const url = useCurrentURL();
13
+ const {
14
+ redirectURL
15
+ } = useRouteLocalization();
16
+ const resolvedURL = useMemo(() => redirectURL(to ? router.resolve(to).url : url, {
17
+ to: locale
18
+ }), [to, url, locale, redirectURL, router]);
19
+ return jsx(Link, {
20
+ hrefLang: locale,
21
+ to: resolvedURL,
22
+ ...props
23
+ });
24
+ }
25
+
26
+ export { LocalizedLink };
@@ -0,0 +1,48 @@
1
+ import { useMemo } from 'preact/hooks';
2
+ import { useBrowserDetails } from '@quilted/preact-browser';
3
+ import { Navigation } from '@quilted/preact-router';
4
+ import { Localization } from '../Localization.mjs';
5
+ import { useLocaleFromEnvironment } from '../hooks/locale-from-environment.mjs';
6
+ import { LocalizedRouter } from './LocalizedRouter.mjs';
7
+ import { RouteLocalizationContext } from './context.mjs';
8
+ import { jsx } from 'preact/jsx-runtime';
9
+
10
+ function LocalizedNavigation({
11
+ locale: explicitLocale,
12
+ router,
13
+ routes,
14
+ context,
15
+ localization,
16
+ children
17
+ }) {
18
+ const browser = useBrowserDetails({
19
+ optional: true
20
+ });
21
+ const resolvedRouter = useMemo(() => router ?? new LocalizedRouter(browser?.request.url, {
22
+ localization: localization
23
+ }), [router]);
24
+ const resolvedLocalization = resolvedRouter.localization;
25
+ const localeFromEnvironment = useLocaleFromEnvironment();
26
+ let resolvedLocale;
27
+ if (explicitLocale) {
28
+ resolvedLocale = explicitLocale;
29
+ } else if (localeFromEnvironment != null && localeFromEnvironment.toLowerCase().startsWith(resolvedLocalization.locale.toLowerCase())) {
30
+ resolvedLocale = localeFromEnvironment;
31
+ } else {
32
+ resolvedLocale = resolvedLocalization.locale;
33
+ }
34
+ return jsx(Localization, {
35
+ locale: resolvedLocale,
36
+ children: jsx(RouteLocalizationContext.Provider, {
37
+ value: resolvedLocalization,
38
+ children: jsx(Navigation, {
39
+ router: resolvedRouter,
40
+ routes: routes,
41
+ context: context,
42
+ children: children
43
+ })
44
+ })
45
+ });
46
+ }
47
+
48
+ export { LocalizedNavigation };
@@ -0,0 +1,34 @@
1
+ import { Router } from '@quilted/preact-router';
2
+
3
+ class LocalizedRouter extends Router {
4
+ constructor(initial, {
5
+ localization,
6
+ isExternal: explicitIsExternal
7
+ }) {
8
+ const {
9
+ localeFromURL
10
+ } = localization;
11
+ super(initial, {
12
+ isExternal(url, currentURL) {
13
+ return matchedLocale !== localeFromURL(url) || (explicitIsExternal?.(url, currentURL) ?? false);
14
+ }
15
+ });
16
+ const currentURL = this.currentRequest.url;
17
+ const matchedLocale = localeFromURL(currentURL);
18
+ const resolvedLocalization = {
19
+ locale: matchedLocale ?? localization.defaultLocale,
20
+ ...localization
21
+ };
22
+ this.localization = resolvedLocalization;
23
+ const {
24
+ pathname: rootPath
25
+ } = localization.redirectURL(new URL('/', currentURL), {
26
+ to: resolvedLocalization.locale
27
+ });
28
+ if (rootPath.length > 1) Object.assign(this, {
29
+ base: rootPath
30
+ });
31
+ }
32
+ }
33
+
34
+ export { LocalizedRouter };
@@ -0,0 +1,6 @@
1
+ import { createOptionalContext } from '@quilted/preact-context';
2
+
3
+ const RouteLocalizationContext = createOptionalContext();
4
+ const useRouteLocalization = RouteLocalizationContext.use;
5
+
6
+ export { RouteLocalizationContext, useRouteLocalization };
@@ -0,0 +1,50 @@
1
+ function createRouteLocalization({
2
+ locales: localeMap,
3
+ default: defaultLocale
4
+ }) {
5
+ const sortedLocaleMap = [...localeMap].sort(([, a], [, b]) => b.length - a.length);
6
+ const locales = [...localeMap.keys()].sort((a, b) => b.length - a.length);
7
+ return {
8
+ locales,
9
+ redirectURL,
10
+ matchLocale,
11
+ localeFromURL,
12
+ defaultLocale
13
+ };
14
+ function matchLocale(requestLocale) {
15
+ const language = requestLocale.split('-')[0];
16
+ return locales.find(locale => locale === requestLocale || locale === language);
17
+ }
18
+ function localeFromURL(url) {
19
+ const hostname = url.hostname.toLowerCase();
20
+ const pathname = normalizePath(url.pathname.toLowerCase());
21
+ for (const [locale, target] of sortedLocaleMap) {
22
+ if (target.startsWith('/')) {
23
+ if (pathname.startsWith(target)) return locale;
24
+ } else {
25
+ if (hostname === target) return locale;
26
+ }
27
+ }
28
+ }
29
+ function redirectURL(from, {
30
+ to: toLocale
31
+ }) {
32
+ const fromLocale = localeFromURL(from);
33
+ const toUrl = new URL(from);
34
+ if (fromLocale === toLocale) return toUrl;
35
+ const target = localeMap.get(toLocale) ?? localeMap.get(defaultLocale);
36
+ if (target.startsWith('/')) {
37
+ const fromTarget = fromLocale == null ? '/' : localeMap.get(fromLocale) ?? '/';
38
+ toUrl.pathname = normalizePath(toUrl.pathname.replace(fromTarget, target));
39
+ } else {
40
+ toUrl.hostname = target;
41
+ }
42
+ return toUrl;
43
+ }
44
+ }
45
+ function normalizePath(path) {
46
+ if (path.length === 0) return '/';
47
+ return path.endsWith('/') && path.length > 1 ? path.slice(0, -1) : path;
48
+ }
49
+
50
+ export { createRouteLocalization };
@@ -0,0 +1,23 @@
1
+ import { createRouteLocalization } from './by-locale.mjs';
2
+
3
+ function createRoutePathLocalization({
4
+ locales,
5
+ default: defaultLocaleDefinition
6
+ }) {
7
+ const localeMap = new Map();
8
+ const defaultLocale = typeof defaultLocaleDefinition === 'string' ? defaultLocaleDefinition : defaultLocaleDefinition.locale;
9
+ const defaultLocaleNested = typeof defaultLocaleDefinition === 'string' || (defaultLocaleDefinition.nested ?? true);
10
+ for (const locale of locales) {
11
+ if (!defaultLocaleNested && locale === defaultLocale) {
12
+ localeMap.set(locale, '/');
13
+ } else {
14
+ localeMap.set(locale, `/${locale.toLowerCase()}`);
15
+ }
16
+ }
17
+ return createRouteLocalization({
18
+ locales: localeMap,
19
+ default: defaultLocale
20
+ });
21
+ }
22
+
23
+ export { createRoutePathLocalization };
@@ -0,0 +1,25 @@
1
+ import { createRouteLocalization } from './by-locale.mjs';
2
+
3
+ function createRouteSubdomainLocalization({
4
+ base,
5
+ locales,
6
+ default: defaultLocaleDefinition
7
+ }) {
8
+ const localeMap = new Map();
9
+ const normalizedBase = base.replace(/^https?:\/\//, '');
10
+ const defaultLocale = typeof defaultLocaleDefinition === 'string' ? defaultLocaleDefinition : defaultLocaleDefinition.locale;
11
+ const defaultLocaleNested = typeof defaultLocaleDefinition === 'string' || (defaultLocaleDefinition.nested ?? true);
12
+ for (const locale of locales) {
13
+ if (!defaultLocaleNested && locale === defaultLocale) {
14
+ localeMap.set(locale, normalizedBase);
15
+ } else {
16
+ localeMap.set(locale, `${locale.toLowerCase()}.${normalizedBase}`);
17
+ }
18
+ }
19
+ return createRouteLocalization({
20
+ locales: localeMap,
21
+ default: defaultLocale
22
+ });
23
+ }
24
+
25
+ export { createRouteSubdomainLocalization };
@@ -0,0 +1,35 @@
1
+ import { jsx, jsxs } from 'preact/jsx-runtime';
2
+ import { useMemo } from 'preact/hooks';
3
+ import { createLocalizedFormatting } from '@quilted/localize';
4
+ import { HTMLAttributes } from '@quilted/preact-browser';
5
+ import { LocaleContext, LocalizedFormattingContext } from './context.esnext';
6
+
7
+ const RTL_LOCALES = /* @__PURE__ */ new Set([
8
+ "ar",
9
+ "arc",
10
+ "ckb",
11
+ "dv",
12
+ "fa",
13
+ "ha",
14
+ "he",
15
+ "khw",
16
+ "ks",
17
+ "ps",
18
+ "sd",
19
+ "ur",
20
+ "uz-AF",
21
+ "yi"
22
+ ]);
23
+ function Localization({
24
+ locale,
25
+ direction = RTL_LOCALES.has(locale) ? "rtl" : "ltr",
26
+ children
27
+ }) {
28
+ const formatting = useMemo(() => createLocalizedFormatting(locale), [locale]);
29
+ return /* @__PURE__ */ jsx(LocaleContext.Provider, { value: locale, children: /* @__PURE__ */ jsxs(LocalizedFormattingContext.Provider, { value: formatting, children: [
30
+ /* @__PURE__ */ jsx(HTMLAttributes, { lang: locale, dir: direction }),
31
+ children
32
+ ] }) });
33
+ }
34
+
35
+ export { Localization };
@@ -0,0 +1,6 @@
1
+ import { createOptionalContext } from '@quilted/preact-context';
2
+
3
+ const LocalizedFormattingContext = createOptionalContext();
4
+ const LocaleContext = createOptionalContext();
5
+
6
+ export { LocaleContext, LocalizedFormattingContext };
@@ -0,0 +1,5 @@
1
+ import { LocalizedFormattingContext } from '../context.esnext';
2
+
3
+ const useLocalizedFormatting = LocalizedFormattingContext.use;
4
+
5
+ export { useLocalizedFormatting };
@@ -0,0 +1,13 @@
1
+ import { parseAcceptLanguageHeader } from '@quilted/localize';
2
+ import { useBrowserDetails } from '@quilted/preact-browser';
3
+
4
+ function useLocaleFromEnvironment() {
5
+ if (typeof navigator === "object" && navigator.language) {
6
+ return navigator.language;
7
+ }
8
+ const browserDetails = useBrowserDetails({ optional: true });
9
+ const acceptLanguage = browserDetails?.request.headers?.get("Accept-Language");
10
+ return acceptLanguage && parseAcceptLanguageHeader(acceptLanguage);
11
+ }
12
+
13
+ export { useLocaleFromEnvironment };
@@ -0,0 +1,5 @@
1
+ import { LocaleContext } from '../context.esnext';
2
+
3
+ const useLocale = LocaleContext.use;
4
+
5
+ export { useLocale };