@quilted/preact-localize 0.1.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 (77) hide show
  1. package/CHANGELOG.md +202 -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 +12 -0
  9. package/build/esm/request-router.mjs +49 -0
  10. package/build/esm/routing/LocalizedLink.mjs +26 -0
  11. package/build/esm/routing/LocalizedRouting.mjs +65 -0
  12. package/build/esm/routing/context.mjs +6 -0
  13. package/build/esm/routing/localization/by-locale.mjs +50 -0
  14. package/build/esm/routing/localization/by-path.mjs +23 -0
  15. package/build/esm/routing/localization/by-subdomain.mjs +25 -0
  16. package/build/esnext/Localization.esnext +35 -0
  17. package/build/esnext/context.esnext +6 -0
  18. package/build/esnext/hooks/formatting.esnext +5 -0
  19. package/build/esnext/hooks/locale-from-environment.esnext +13 -0
  20. package/build/esnext/hooks/locale.esnext +5 -0
  21. package/build/esnext/index.esnext +12 -0
  22. package/build/esnext/request-router.esnext +49 -0
  23. package/build/esnext/routing/LocalizedLink.esnext +21 -0
  24. package/build/esnext/routing/LocalizedRouting.esnext +64 -0
  25. package/build/esnext/routing/context.esnext +6 -0
  26. package/build/esnext/routing/localization/by-locale.esnext +58 -0
  27. package/build/esnext/routing/localization/by-path.esnext +20 -0
  28. package/build/esnext/routing/localization/by-subdomain.esnext +22 -0
  29. package/build/tsconfig.tsbuildinfo +1 -0
  30. package/build/typescript/Localization.d.ts +7 -0
  31. package/build/typescript/Localization.d.ts.map +1 -0
  32. package/build/typescript/context.d.ts +4 -0
  33. package/build/typescript/context.d.ts.map +1 -0
  34. package/build/typescript/hooks/formatting.d.ts +2 -0
  35. package/build/typescript/hooks/formatting.d.ts.map +1 -0
  36. package/build/typescript/hooks/locale-from-environment.d.ts +2 -0
  37. package/build/typescript/hooks/locale-from-environment.d.ts.map +1 -0
  38. package/build/typescript/hooks/locale.d.ts +2 -0
  39. package/build/typescript/hooks/locale.d.ts.map +1 -0
  40. package/build/typescript/index.d.ts +9 -0
  41. package/build/typescript/index.d.ts.map +1 -0
  42. package/build/typescript/request-router.d.ts +16 -0
  43. package/build/typescript/request-router.d.ts.map +1 -0
  44. package/build/typescript/routing/LocalizedLink.d.ts +9 -0
  45. package/build/typescript/routing/LocalizedLink.d.ts.map +1 -0
  46. package/build/typescript/routing/LocalizedRouting.d.ts +9 -0
  47. package/build/typescript/routing/LocalizedRouting.d.ts.map +1 -0
  48. package/build/typescript/routing/context.d.ts +4 -0
  49. package/build/typescript/routing/context.d.ts.map +1 -0
  50. package/build/typescript/routing/localization/by-locale.d.ts +6 -0
  51. package/build/typescript/routing/localization/by-locale.d.ts.map +1 -0
  52. package/build/typescript/routing/localization/by-path.d.ts +6 -0
  53. package/build/typescript/routing/localization/by-path.d.ts.map +1 -0
  54. package/build/typescript/routing/localization/by-subdomain.d.ts +7 -0
  55. package/build/typescript/routing/localization/by-subdomain.d.ts.map +1 -0
  56. package/build/typescript/routing/types.d.ts +17 -0
  57. package/build/typescript/routing/types.d.ts.map +1 -0
  58. package/build/typescript/routing.d.ts +8 -0
  59. package/build/typescript/routing.d.ts.map +1 -0
  60. package/configuration/rollup.config.js +3 -0
  61. package/package.json +67 -0
  62. package/source/Localization.tsx +45 -0
  63. package/source/context.ts +7 -0
  64. package/source/hooks/formatting.ts +3 -0
  65. package/source/hooks/locale-from-environment.ts +15 -0
  66. package/source/hooks/locale.ts +3 -0
  67. package/source/index.ts +26 -0
  68. package/source/request-router.ts +98 -0
  69. package/source/routing/LocalizedLink.tsx +27 -0
  70. package/source/routing/LocalizedRouting.tsx +94 -0
  71. package/source/routing/context.ts +8 -0
  72. package/source/routing/localization/by-locale.ts +71 -0
  73. package/source/routing/localization/by-path.ts +31 -0
  74. package/source/routing/localization/by-subdomain.ts +34 -0
  75. package/source/routing/types.ts +16 -0
  76. package/source/routing.ts +11 -0
  77. package/tsconfig.json +17 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,202 @@
1
+ # @quilted/react-localize
2
+
3
+ ## 0.2.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#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
8
+
9
+ - Updated dependencies [[`7daafca`](https://github.com/lemonmade/quilt/commit/7daafca900b3d9ea66be179394eadf7998cc94be)]:
10
+ - @quilted/react-browser@0.0.1
11
+ - @quilted/react-router@0.4.1
12
+
13
+ ## 0.2.1
14
+
15
+ ### Patch Changes
16
+
17
+ - [`905e92ef`](https://github.com/lemonmade/quilt/commit/905e92ef32adad8658042f437b9cfbd248cce3c3) Thanks [@lemonmade](https://github.com/lemonmade)! - Simplify React exports
18
+
19
+ ## 0.2.0
20
+
21
+ ### Minor Changes
22
+
23
+ - [#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
24
+
25
+ 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.
26
+
27
+ ### Patch Changes
28
+
29
+ - Updated dependencies [[`302ed847`](https://github.com/lemonmade/quilt/commit/302ed8479f9c035ef39d48137de958dba50690ca)]:
30
+ - @quilted/useful-react-types@2.0.0
31
+ - @quilted/localize@0.2.0
32
+ - @quilted/react-html@0.4.0
33
+ - @quilted/react-http@0.4.0
34
+ - @quilted/react-router@0.4.0
35
+ - @quilted/react-utilities@0.2.0
36
+ - @quilted/request-router@0.2.0
37
+
38
+ ## 0.1.19
39
+
40
+ ### Patch Changes
41
+
42
+ - [#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
43
+
44
+ - Updated dependencies [[`bc849bc7`](https://github.com/lemonmade/quilt/commit/bc849bc740318936656162fde851b784ed6ef78f)]:
45
+ - @quilted/react-http@0.3.31
46
+ - @quilted/request-router@0.1.8
47
+
48
+ ## 0.1.18
49
+
50
+ ### Patch Changes
51
+
52
+ - Updated dependencies [[`9122cbbc`](https://github.com/lemonmade/quilt/commit/9122cbbce965bf5b432027e0707b2d619857fa67)]:
53
+ - @quilted/useful-react-types@1.0.0
54
+
55
+ ## 0.1.17
56
+
57
+ ### Patch Changes
58
+
59
+ - [`6e999a5f`](https://github.com/lemonmade/quilt/commit/6e999a5f2b3001923fdb83d9954318742e3a84e3) Thanks [@lemonmade](https://github.com/lemonmade)! - Add basic translation function
60
+
61
+ ## 0.1.16
62
+
63
+ ### Patch Changes
64
+
65
+ - [`7a431339`](https://github.com/lemonmade/quilt/commit/7a4313390827aefdbbf9b28f2a5aa6213d6b33f5) Thanks [@lemonmade](https://github.com/lemonmade)! - Add automatic `dir` property in Automatically add `dir` HTML attribute
66
+
67
+ ## 0.1.15
68
+
69
+ ### Patch Changes
70
+
71
+ - [`97812120`](https://github.com/lemonmade/quilt/commit/978121207c65a4450a8ca9e43d017c6425a315c3) Thanks [@lemonmade](https://github.com/lemonmade)! - Update Preact dependencies and fix some missing peer dependencies
72
+
73
+ - Updated dependencies [[`1a22a1c5`](https://github.com/lemonmade/quilt/commit/1a22a1c572c3d37d2564eb9eb908904a6d254201), [`97812120`](https://github.com/lemonmade/quilt/commit/978121207c65a4450a8ca9e43d017c6425a315c3)]:
74
+ - @quilted/request-router@0.1.5
75
+ - @quilted/react-http@0.3.29
76
+
77
+ ## 0.1.14
78
+
79
+ ### Patch Changes
80
+
81
+ - [#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
82
+
83
+ - Updated dependencies [[`cf6e2de1`](https://github.com/lemonmade/quilt/commit/cf6e2de186d8644fad9afcedda85c05002e909e1)]:
84
+ - @quilted/react-http@0.3.28
85
+ - @quilted/request-router@0.1.4
86
+
87
+ ## 0.1.13
88
+
89
+ ### Patch Changes
90
+
91
+ - [`8f1d275b`](https://github.com/lemonmade/quilt/commit/8f1d275b6de0abbc6f61bcd5401555f6480eb474) Thanks [@lemonmade](https://github.com/lemonmade)! - Remove need for @babel/runtime peer dependency
92
+
93
+ - Updated dependencies [[`8f1d275b`](https://github.com/lemonmade/quilt/commit/8f1d275b6de0abbc6f61bcd5401555f6480eb474), [`50215b7c`](https://github.com/lemonmade/quilt/commit/50215b7c005c21440bca04935fda87d98d9d9d01)]:
94
+ - @quilted/react-http@0.3.27
95
+ - @quilted/request-router@0.1.3
96
+
97
+ ## 0.1.12
98
+
99
+ ### Patch Changes
100
+
101
+ - [#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
102
+
103
+ - Updated dependencies [[`e647289c`](https://github.com/lemonmade/quilt/commit/e647289c14a2bf8d0d9d322cd3fe1be3f675c535), [`8890fad8`](https://github.com/lemonmade/quilt/commit/8890fad8d04efa95b362f4beaefcdbd51e65ba04)]:
104
+ - @quilted/react-http@0.3.25
105
+
106
+ ## 0.1.11
107
+
108
+ ### Patch Changes
109
+
110
+ - [`a011fa25`](https://github.com/lemonmade/quilt/commit/a011fa25808818ed4e5651a25f267becc327ef2d) Thanks [@lemonmade](https://github.com/lemonmade)! - Fix Localization `lang` attribute on client
111
+
112
+ ## 0.1.10
113
+
114
+ ### Patch Changes
115
+
116
+ - [#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
117
+
118
+ ## 0.1.9
119
+
120
+ ### Patch Changes
121
+
122
+ - [#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
123
+
124
+ - Updated dependencies [[`3171fcee`](https://github.com/lemonmade/quilt/commit/3171fceeddfb14c253ac45e34e1e2f9ab6e3f6c0)]:
125
+ - @quilted/request-router@0.1.1
126
+
127
+ ## 0.1.8
128
+
129
+ ### Patch Changes
130
+
131
+ - [`51482122`](https://github.com/lemonmade/quilt/commit/514821223b4d8eb9c5289265c7cd2b4ef0b2e8b3) Thanks [@lemonmade](https://github.com/lemonmade)! - Add a utility for creating optional context
132
+
133
+ ## 0.1.7
134
+
135
+ ### Patch Changes
136
+
137
+ - [`a12c3576`](https://github.com/lemonmade/quilt/commit/a12c357693b173461f51a35fb7efdd0a9267e471) Thanks [@lemonmade](https://github.com/lemonmade)! - Fix more build issues
138
+
139
+ - Updated dependencies [[`a12c3576`](https://github.com/lemonmade/quilt/commit/a12c357693b173461f51a35fb7efdd0a9267e471)]:
140
+ - @quilted/http-handlers@0.2.43
141
+ - @quilted/react-http@0.3.23
142
+
143
+ ## 0.1.6
144
+
145
+ ### Patch Changes
146
+
147
+ - [`0629288e`](https://github.com/lemonmade/quilt/commit/0629288ee4ba2e2ccfd73fbb216c3559e1a5c77e) Thanks [@lemonmade](https://github.com/lemonmade)! - Fix missing package builds
148
+
149
+ - Updated dependencies [[`0629288e`](https://github.com/lemonmade/quilt/commit/0629288ee4ba2e2ccfd73fbb216c3559e1a5c77e)]:
150
+ - @quilted/http-handlers@0.2.42
151
+ - @quilted/react-http@0.3.22
152
+
153
+ ## 0.1.5
154
+
155
+ ### Patch Changes
156
+
157
+ - [#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
158
+
159
+ - Updated dependencies [[`4dc1808a`](https://github.com/lemonmade/quilt/commit/4dc1808a86d15e821b218b528617430cbd8b5b48)]:
160
+ - @quilted/http-handlers@0.2.41
161
+ - @quilted/react-http@0.3.21
162
+
163
+ ## 0.1.4
164
+
165
+ ### Patch Changes
166
+
167
+ - [#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
168
+
169
+ * [#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
170
+
171
+ * Updated dependencies [[`2eceac54`](https://github.com/lemonmade/quilt/commit/2eceac546fa3ee3e2c4d2887ab4a6a021acb52cd), [`47065859`](https://github.com/lemonmade/quilt/commit/47065859c330e2da23d8758fb165ae84a4f1af4f)]:
172
+ - @quilted/http-handlers@0.2.38
173
+ - @quilted/react-http@0.3.20
174
+
175
+ ## 0.1.3
176
+
177
+ ### Patch Changes
178
+
179
+ - [`ddaa8197`](https://github.com/lemonmade/quilt/commit/ddaa8197c5576d5c29b90bfc7752596df56c97b1) Thanks [@lemonmade](https://github.com/lemonmade)! - Improve localization fallback behavior
180
+
181
+ ## 0.1.2
182
+
183
+ ### Patch Changes
184
+
185
+ - [#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
186
+
187
+ ## 0.1.1
188
+
189
+ ### Patch Changes
190
+
191
+ - [#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
192
+
193
+ ## 0.1.0
194
+
195
+ ### Minor Changes
196
+
197
+ - [`b0040c72`](https://github.com/lemonmade/quilt/commit/b0040c7279931f426df5d7fac91a3a35ea49eae1) Thanks [@lemonmade](https://github.com/lemonmade)! - Initial localization packages
198
+
199
+ ### Patch Changes
200
+
201
+ - Updated dependencies [[`b0040c72`](https://github.com/lemonmade/quilt/commit/b0040c7279931f426df5d7fac91a3a35ea49eae1)]:
202
+ - @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,12 @@
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 { LocalizedRouting } from './routing/LocalizedRouting.mjs';
9
+ export { useRouteLocalization } from './routing/context.mjs';
10
+ export { createRouteLocalization } from './routing/localization/by-locale.mjs';
11
+ export { createRoutePathLocalization } from './routing/localization/by-path.mjs';
12
+ 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 currentUrl = useCurrentUrl();
13
+ const {
14
+ redirectUrl
15
+ } = useRouteLocalization();
16
+ const resolvedUrl = useMemo(() => redirectUrl(to ? router.resolve(to).url : currentUrl, {
17
+ to: locale
18
+ }), [to, currentUrl, locale, redirectUrl, router]);
19
+ return jsx(Link, {
20
+ hrefLang: locale,
21
+ to: resolvedUrl,
22
+ ...props
23
+ });
24
+ }
25
+
26
+ export { LocalizedLink };
@@ -0,0 +1,65 @@
1
+ import { useMemo, useCallback } from 'preact/hooks';
2
+ import { useInitialURL, Routing } from '@quilted/preact-router';
3
+ import { Localization } from '../Localization.mjs';
4
+ import { useLocaleFromEnvironment } from '../hooks/locale-from-environment.mjs';
5
+ import { RouteLocalizationContext } from './context.mjs';
6
+ import { jsx } from 'preact/jsx-runtime';
7
+
8
+ function LocalizedRouting({
9
+ locale: explicitLocale,
10
+ localization,
11
+ children,
12
+ isExternal: isExplicitlyExternal,
13
+ ...props
14
+ }) {
15
+ const initialUrl = useInitialURL();
16
+ const localeFromEnvironment = useLocaleFromEnvironment();
17
+ const {
18
+ prefix,
19
+ isExternal: isExternalByRouteLocale,
20
+ resolvedLocalization
21
+ } = useMemo(() => {
22
+ const {
23
+ localeFromUrl
24
+ } = localization;
25
+ const matchedLocale = initialUrl && localeFromUrl(initialUrl);
26
+ const resolvedLocalization = {
27
+ locale: matchedLocale ?? localization.defaultLocale,
28
+ ...localization
29
+ };
30
+ const rootUrl = localization.redirectUrl(new URL('/', initialUrl ?? 'https://example.com'), {
31
+ to: resolvedLocalization.locale
32
+ });
33
+ return {
34
+ prefix: rootUrl.pathname.length > 1 ? rootUrl.pathname : undefined,
35
+ isExternal: url => matchedLocale !== localeFromUrl(url),
36
+ resolvedLocalization
37
+ };
38
+ }, [initialUrl, localization]);
39
+ const isExternal = useCallback((url, currentUrl) => {
40
+ return isExternalByRouteLocale(url) || (isExplicitlyExternal?.(url, currentUrl) ?? false);
41
+ }, [isExplicitlyExternal, isExternalByRouteLocale]);
42
+ let resolvedLocale;
43
+ if (explicitLocale) {
44
+ resolvedLocale = explicitLocale;
45
+ } else if (localeFromEnvironment != null && localeFromEnvironment.toLowerCase().startsWith(resolvedLocalization.locale.toLowerCase())) {
46
+ resolvedLocale = localeFromEnvironment;
47
+ } else {
48
+ resolvedLocale = resolvedLocalization.locale;
49
+ }
50
+ return jsx(Routing, {
51
+ ...props,
52
+ prefix: prefix,
53
+ url: initialUrl,
54
+ isExternal: isExternal,
55
+ children: jsx(Localization, {
56
+ locale: resolvedLocale,
57
+ children: jsx(RouteLocalizationContext.Provider, {
58
+ value: resolvedLocalization,
59
+ children: children
60
+ })
61
+ })
62
+ });
63
+ }
64
+
65
+ export { LocalizedRouting };
@@ -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 };
@@ -0,0 +1,12 @@
1
+ export { MissingTranslationError, MissingTranslationPlaceholderError, createLocalizedFormatting, createTranslate, parseAcceptLanguageHeader } from '@quilted/localize';
2
+ export { Localization } from './Localization.esnext';
3
+ export { useLocalizedFormatting } from './hooks/formatting.esnext';
4
+ export { useLocale } from './hooks/locale.esnext';
5
+ export { useLocaleFromEnvironment } from './hooks/locale-from-environment.esnext';
6
+ export { LocalizedFormattingContext } from './context.esnext';
7
+ export { LocalizedLink } from './routing/LocalizedLink.esnext';
8
+ export { LocalizedRouting } from './routing/LocalizedRouting.esnext';
9
+ export { useRouteLocalization } from './routing/context.esnext';
10
+ export { createRouteLocalization } from './routing/localization/by-locale.esnext';
11
+ export { createRoutePathLocalization } from './routing/localization/by-path.esnext';
12
+ export { createRouteSubdomainLocalization } from './routing/localization/by-subdomain.esnext';