remix-intlayer 9.5.4

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 (128) hide show
  1. package/README.md +331 -0
  2. package/dist/cjs/IntlayerProvider.cjs +36 -0
  3. package/dist/cjs/IntlayerProvider.cjs.map +1 -0
  4. package/dist/cjs/context.cjs +23 -0
  5. package/dist/cjs/context.cjs.map +1 -0
  6. package/dist/cjs/format/index.cjs +20 -0
  7. package/dist/cjs/format/useCompact.cjs +18 -0
  8. package/dist/cjs/format/useCompact.cjs.map +1 -0
  9. package/dist/cjs/format/useCurrency.cjs +18 -0
  10. package/dist/cjs/format/useCurrency.cjs.map +1 -0
  11. package/dist/cjs/format/useDate.cjs +27 -0
  12. package/dist/cjs/format/useDate.cjs.map +1 -0
  13. package/dist/cjs/format/useIntl.cjs +24 -0
  14. package/dist/cjs/format/useIntl.cjs.map +1 -0
  15. package/dist/cjs/format/useList.cjs +18 -0
  16. package/dist/cjs/format/useList.cjs.map +1 -0
  17. package/dist/cjs/format/useNumber.cjs +21 -0
  18. package/dist/cjs/format/useNumber.cjs.map +1 -0
  19. package/dist/cjs/format/usePercentage.cjs +18 -0
  20. package/dist/cjs/format/usePercentage.cjs.map +1 -0
  21. package/dist/cjs/format/useRelativeTime.cjs +18 -0
  22. package/dist/cjs/format/useRelativeTime.cjs.map +1 -0
  23. package/dist/cjs/format/useUnit.cjs +18 -0
  24. package/dist/cjs/format/useUnit.cjs.map +1 -0
  25. package/dist/cjs/html/index.cjs +33 -0
  26. package/dist/cjs/index.cjs +17 -0
  27. package/dist/cjs/localeRouting.cjs +211 -0
  28. package/dist/cjs/localeRouting.cjs.map +1 -0
  29. package/dist/cjs/markdown/index.cjs +45 -0
  30. package/dist/cjs/middleware.cjs +92 -0
  31. package/dist/cjs/middleware.cjs.map +1 -0
  32. package/dist/cjs/persistLocale.cjs +51 -0
  33. package/dist/cjs/persistLocale.cjs.map +1 -0
  34. package/dist/cjs/requestStorage.cjs +25 -0
  35. package/dist/cjs/requestStorage.cjs.map +1 -0
  36. package/dist/cjs/resolveLocale.cjs +21 -0
  37. package/dist/cjs/resolveLocale.cjs.map +1 -0
  38. package/dist/cjs/useDictionary.cjs +33 -0
  39. package/dist/cjs/useDictionary.cjs.map +1 -0
  40. package/dist/cjs/useIntlayer.cjs +31 -0
  41. package/dist/cjs/useIntlayer.cjs.map +1 -0
  42. package/dist/cjs/useLocale.cjs +33 -0
  43. package/dist/cjs/useLocale.cjs.map +1 -0
  44. package/dist/esm/IntlayerProvider.mjs +35 -0
  45. package/dist/esm/IntlayerProvider.mjs.map +1 -0
  46. package/dist/esm/context.mjs +21 -0
  47. package/dist/esm/context.mjs.map +1 -0
  48. package/dist/esm/format/index.mjs +11 -0
  49. package/dist/esm/format/useCompact.mjs +17 -0
  50. package/dist/esm/format/useCompact.mjs.map +1 -0
  51. package/dist/esm/format/useCurrency.mjs +17 -0
  52. package/dist/esm/format/useCurrency.mjs.map +1 -0
  53. package/dist/esm/format/useDate.mjs +26 -0
  54. package/dist/esm/format/useDate.mjs.map +1 -0
  55. package/dist/esm/format/useIntl.mjs +23 -0
  56. package/dist/esm/format/useIntl.mjs.map +1 -0
  57. package/dist/esm/format/useList.mjs +17 -0
  58. package/dist/esm/format/useList.mjs.map +1 -0
  59. package/dist/esm/format/useNumber.mjs +20 -0
  60. package/dist/esm/format/useNumber.mjs.map +1 -0
  61. package/dist/esm/format/usePercentage.mjs +17 -0
  62. package/dist/esm/format/usePercentage.mjs.map +1 -0
  63. package/dist/esm/format/useRelativeTime.mjs +17 -0
  64. package/dist/esm/format/useRelativeTime.mjs.map +1 -0
  65. package/dist/esm/format/useUnit.mjs +17 -0
  66. package/dist/esm/format/useUnit.mjs.map +1 -0
  67. package/dist/esm/html/index.mjs +3 -0
  68. package/dist/esm/index.mjs +9 -0
  69. package/dist/esm/localeRouting.mjs +210 -0
  70. package/dist/esm/localeRouting.mjs.map +1 -0
  71. package/dist/esm/markdown/index.mjs +3 -0
  72. package/dist/esm/middleware.mjs +91 -0
  73. package/dist/esm/middleware.mjs.map +1 -0
  74. package/dist/esm/persistLocale.mjs +49 -0
  75. package/dist/esm/persistLocale.mjs.map +1 -0
  76. package/dist/esm/requestStorage.mjs +22 -0
  77. package/dist/esm/requestStorage.mjs.map +1 -0
  78. package/dist/esm/resolveLocale.mjs +20 -0
  79. package/dist/esm/resolveLocale.mjs.map +1 -0
  80. package/dist/esm/useDictionary.mjs +32 -0
  81. package/dist/esm/useDictionary.mjs.map +1 -0
  82. package/dist/esm/useIntlayer.mjs +30 -0
  83. package/dist/esm/useIntlayer.mjs.map +1 -0
  84. package/dist/esm/useLocale.mjs +32 -0
  85. package/dist/esm/useLocale.mjs.map +1 -0
  86. package/dist/types/IntlayerProvider.d.ts +39 -0
  87. package/dist/types/IntlayerProvider.d.ts.map +1 -0
  88. package/dist/types/context.d.ts +31 -0
  89. package/dist/types/context.d.ts.map +1 -0
  90. package/dist/types/format/index.d.ts +10 -0
  91. package/dist/types/format/useCompact.d.ts +5 -0
  92. package/dist/types/format/useCompact.d.ts.map +1 -0
  93. package/dist/types/format/useCurrency.d.ts +5 -0
  94. package/dist/types/format/useCurrency.d.ts.map +1 -0
  95. package/dist/types/format/useDate.d.ts +8 -0
  96. package/dist/types/format/useDate.d.ts.map +1 -0
  97. package/dist/types/format/useIntl.d.ts +15 -0
  98. package/dist/types/format/useIntl.d.ts.map +1 -0
  99. package/dist/types/format/useList.d.ts +5 -0
  100. package/dist/types/format/useList.d.ts.map +1 -0
  101. package/dist/types/format/useNumber.d.ts +8 -0
  102. package/dist/types/format/useNumber.d.ts.map +1 -0
  103. package/dist/types/format/usePercentage.d.ts +5 -0
  104. package/dist/types/format/usePercentage.d.ts.map +1 -0
  105. package/dist/types/format/useRelativeTime.d.ts +5 -0
  106. package/dist/types/format/useRelativeTime.d.ts.map +1 -0
  107. package/dist/types/format/useUnit.d.ts +5 -0
  108. package/dist/types/format/useUnit.d.ts.map +1 -0
  109. package/dist/types/html/index.d.ts +2 -0
  110. package/dist/types/index.d.ts +8 -0
  111. package/dist/types/localeRouting.d.ts +75 -0
  112. package/dist/types/localeRouting.d.ts.map +1 -0
  113. package/dist/types/markdown/index.d.ts +2 -0
  114. package/dist/types/middleware.d.ts +49 -0
  115. package/dist/types/middleware.d.ts.map +1 -0
  116. package/dist/types/persistLocale.d.ts +16 -0
  117. package/dist/types/persistLocale.d.ts.map +1 -0
  118. package/dist/types/requestStorage.d.ts +21 -0
  119. package/dist/types/requestStorage.d.ts.map +1 -0
  120. package/dist/types/resolveLocale.d.ts +16 -0
  121. package/dist/types/resolveLocale.d.ts.map +1 -0
  122. package/dist/types/useDictionary.d.ts +24 -0
  123. package/dist/types/useDictionary.d.ts.map +1 -0
  124. package/dist/types/useIntlayer.d.ts +21 -0
  125. package/dist/types/useIntlayer.d.ts.map +1 -0
  126. package/dist/types/useLocale.d.ts +25 -0
  127. package/dist/types/useLocale.d.ts.map +1 -0
  128. package/package.json +129 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useNumber.cjs","names":["internationalization","getRequestLocale","number"],"sources":["../../../src/format/useNumber.ts"],"sourcesContent":["import { internationalization } from '@intlayer/config/built';\nimport { number } from '@intlayer/core/formatters';\nimport type { DeclaredLocales } from '@intlayer/types/module_augmentation';\nimport { getRequestLocale } from '../requestStorage';\n\nconst { defaultLocale } = internationalization;\n\n/**\n * Remix hook that provides a localized number formatter bound to the request locale.\n */\nexport const useNumber = () => {\n const locale = getRequestLocale() ?? (defaultLocale as DeclaredLocales);\n\n return (...args: Parameters<typeof number>) =>\n number(args[0], {\n ...args[1],\n locale: args[1]?.locale ?? locale,\n });\n};\n"],"mappings":";;;;;;AAKA,MAAM,EAAE,kBAAkBA;;;;AAK1B,MAAa,kBAAkB;CAC7B,MAAM,SAASC,wCAAiB,KAAM;CAEtC,QAAQ,GAAG,aACTC,kCAAO,KAAK,IAAI;EACd,GAAG,KAAK;EACR,QAAQ,KAAK,EAAE,EAAE,UAAU;CAC7B,CAAC;AACL"}
@@ -0,0 +1,18 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_requestStorage = require('../requestStorage.cjs');
3
+ let _intlayer_config_built = require("@intlayer/config/built");
4
+ let _intlayer_core_formatters = require("@intlayer/core/formatters");
5
+
6
+ //#region src/format/usePercentage.ts
7
+ const { defaultLocale } = _intlayer_config_built.internationalization;
8
+ const usePercentage = () => {
9
+ const locale = require_requestStorage.getRequestLocale() ?? defaultLocale;
10
+ return (...args) => (0, _intlayer_core_formatters.percentage)(args[0], {
11
+ ...args[1],
12
+ locale: args[1]?.locale ?? locale
13
+ });
14
+ };
15
+
16
+ //#endregion
17
+ exports.usePercentage = usePercentage;
18
+ //# sourceMappingURL=usePercentage.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usePercentage.cjs","names":["internationalization","getRequestLocale","percentage"],"sources":["../../../src/format/usePercentage.ts"],"sourcesContent":["import { internationalization } from '@intlayer/config/built';\nimport { percentage } from '@intlayer/core/formatters';\nimport type { DeclaredLocales } from '@intlayer/types/module_augmentation';\nimport { getRequestLocale } from '../requestStorage';\n\nconst { defaultLocale } = internationalization;\n\nexport const usePercentage = () => {\n const locale = getRequestLocale() ?? (defaultLocale as DeclaredLocales);\n\n return (...args: Parameters<typeof percentage>) =>\n percentage(args[0], {\n ...args[1],\n locale: args[1]?.locale ?? locale,\n });\n};\n"],"mappings":";;;;;;AAKA,MAAM,EAAE,kBAAkBA;AAE1B,MAAa,sBAAsB;CACjC,MAAM,SAASC,wCAAiB,KAAM;CAEtC,QAAQ,GAAG,aACTC,sCAAW,KAAK,IAAI;EAClB,GAAG,KAAK;EACR,QAAQ,KAAK,EAAE,EAAE,UAAU;CAC7B,CAAC;AACL"}
@@ -0,0 +1,18 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_requestStorage = require('../requestStorage.cjs');
3
+ let _intlayer_config_built = require("@intlayer/config/built");
4
+ let _intlayer_core_formatters = require("@intlayer/core/formatters");
5
+
6
+ //#region src/format/useRelativeTime.ts
7
+ const { defaultLocale } = _intlayer_config_built.internationalization;
8
+ const useRelativeTime = () => {
9
+ const locale = require_requestStorage.getRequestLocale() ?? defaultLocale;
10
+ return (...args) => (0, _intlayer_core_formatters.relativeTime)(args[0], args[1], {
11
+ ...args[2],
12
+ locale: args[2]?.locale ?? locale
13
+ });
14
+ };
15
+
16
+ //#endregion
17
+ exports.useRelativeTime = useRelativeTime;
18
+ //# sourceMappingURL=useRelativeTime.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useRelativeTime.cjs","names":["internationalization","getRequestLocale","relativeTime"],"sources":["../../../src/format/useRelativeTime.ts"],"sourcesContent":["import { internationalization } from '@intlayer/config/built';\nimport { relativeTime } from '@intlayer/core/formatters';\nimport type { DeclaredLocales } from '@intlayer/types/module_augmentation';\nimport { getRequestLocale } from '../requestStorage';\n\nconst { defaultLocale } = internationalization;\n\nexport const useRelativeTime = () => {\n const locale = getRequestLocale() ?? (defaultLocale as DeclaredLocales);\n\n return (...args: Parameters<typeof relativeTime>) =>\n relativeTime(args[0], args[1], {\n ...args[2],\n locale: args[2]?.locale ?? locale,\n });\n};\n"],"mappings":";;;;;;AAKA,MAAM,EAAE,kBAAkBA;AAE1B,MAAa,wBAAwB;CACnC,MAAM,SAASC,wCAAiB,KAAM;CAEtC,QAAQ,GAAG,aACTC,wCAAa,KAAK,IAAI,KAAK,IAAI;EAC7B,GAAG,KAAK;EACR,QAAQ,KAAK,EAAE,EAAE,UAAU;CAC7B,CAAC;AACL"}
@@ -0,0 +1,18 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_requestStorage = require('../requestStorage.cjs');
3
+ let _intlayer_config_built = require("@intlayer/config/built");
4
+ let _intlayer_core_formatters = require("@intlayer/core/formatters");
5
+
6
+ //#region src/format/useUnit.ts
7
+ const { defaultLocale } = _intlayer_config_built.internationalization;
8
+ const useUnit = () => {
9
+ const locale = require_requestStorage.getRequestLocale() ?? defaultLocale;
10
+ return (...args) => (0, _intlayer_core_formatters.units)(args[0], {
11
+ ...args[1],
12
+ locale: args[1]?.locale ?? locale
13
+ });
14
+ };
15
+
16
+ //#endregion
17
+ exports.useUnit = useUnit;
18
+ //# sourceMappingURL=useUnit.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useUnit.cjs","names":["internationalization","getRequestLocale","units"],"sources":["../../../src/format/useUnit.ts"],"sourcesContent":["import { internationalization } from '@intlayer/config/built';\nimport { units } from '@intlayer/core/formatters';\nimport type { DeclaredLocales } from '@intlayer/types/module_augmentation';\nimport { getRequestLocale } from '../requestStorage';\n\nconst { defaultLocale } = internationalization;\n\nexport const useUnit = () => {\n const locale = getRequestLocale() ?? (defaultLocale as DeclaredLocales);\n\n return (...args: Parameters<typeof units>) =>\n units(args[0], {\n ...args[1],\n locale: args[1]?.locale ?? locale,\n });\n};\n"],"mappings":";;;;;;AAKA,MAAM,EAAE,kBAAkBA;AAE1B,MAAa,gBAAgB;CAC3B,MAAM,SAASC,wCAAiB,KAAM;CAEtC,QAAQ,GAAG,aACTC,iCAAM,KAAK,IAAI;EACb,GAAG,KAAK;EACR,QAAQ,KAAK,EAAE,EAAE,UAAU;CAC7B,CAAC;AACL"}
@@ -0,0 +1,33 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ let vanilla_intlayer_html = require("vanilla-intlayer/html");
3
+
4
+ Object.defineProperty(exports, 'installIntlayerHTML', {
5
+ enumerable: true,
6
+ get: function () {
7
+ return vanilla_intlayer_html.installIntlayerHTML;
8
+ }
9
+ });
10
+ Object.defineProperty(exports, 'installIntlayerHTMLDynamic', {
11
+ enumerable: true,
12
+ get: function () {
13
+ return vanilla_intlayer_html.installIntlayerHTMLDynamic;
14
+ }
15
+ });
16
+ Object.defineProperty(exports, 'renderHTML', {
17
+ enumerable: true,
18
+ get: function () {
19
+ return vanilla_intlayer_html.renderHTML;
20
+ }
21
+ });
22
+ Object.defineProperty(exports, 'useHTML', {
23
+ enumerable: true,
24
+ get: function () {
25
+ return vanilla_intlayer_html.useHTML;
26
+ }
27
+ });
28
+ Object.defineProperty(exports, 'useHTMLRenderer', {
29
+ enumerable: true,
30
+ get: function () {
31
+ return vanilla_intlayer_html.useHTMLRenderer;
32
+ }
33
+ });
@@ -0,0 +1,17 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_context = require('./context.cjs');
3
+ const require_localeRouting = require('./localeRouting.cjs');
4
+ const require_requestStorage = require('./requestStorage.cjs');
5
+ const require_middleware = require('./middleware.cjs');
6
+ const require_useIntlayer = require('./useIntlayer.cjs');
7
+ const require_useLocale = require('./useLocale.cjs');
8
+ const require_useDictionary = require('./useDictionary.cjs');
9
+
10
+ exports.INTLAYER_CONTEXT_PROPERTY = require_context.INTLAYER_CONTEXT_PROPERTY;
11
+ exports.Intlayer = require_context.Intlayer;
12
+ exports.createLocaleRouting = require_localeRouting.createLocaleRouting;
13
+ exports.getIntlayerState = require_requestStorage.getIntlayerState;
14
+ exports.intlayer = require_middleware.intlayer;
15
+ exports.useDictionary = require_useDictionary.useDictionary;
16
+ exports.useIntlayer = require_useIntlayer.useIntlayer;
17
+ exports.useLocale = require_useLocale.useLocale;
@@ -0,0 +1,211 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ let _intlayer_core_localization = require("@intlayer/core/localization");
3
+ let _intlayer_config_defaultValues = require("@intlayer/config/defaultValues");
4
+ let _intlayer_core_utils = require("@intlayer/core/utils");
5
+
6
+ //#region src/localeRouting.ts
7
+ /** Matches a pathname ending with a file extension (`/logo.svg`). */
8
+ const STATIC_ASSET_PATTERN = /\.[a-zA-Z0-9]+$/;
9
+ const PASS = { kind: "pass" };
10
+ /**
11
+ * Decodes a pathname so it can be matched against the rewrite rules, which are
12
+ * written with the characters the locale actually uses (`/doc/релизы`) while
13
+ * the browser sends them percent-encoded. A malformed escape is kept as is.
14
+ */
15
+ const decodePathname = (pathname) => {
16
+ try {
17
+ return decodeURI(pathname);
18
+ } catch {
19
+ return pathname;
20
+ }
21
+ };
22
+ /**
23
+ * Removes a trailing slash, except on the root path. A redirect to `/fr/`
24
+ * would otherwise ping-pong with any trailing-slash normalisation downstream.
25
+ */
26
+ const trimTrailingSlash = (path) => path.length > 1 && path.endsWith("/") ? path.slice(0, -1) : path;
27
+ /**
28
+ * Removes `/{locale}` from the start of a pathname that carries that prefix.
29
+ */
30
+ const stripLocalePrefix = (pathname, locale) => pathname.slice(`/${locale}`.length) || "/";
31
+ /**
32
+ * Builds the locale-routing resolver of a Remix application.
33
+ *
34
+ * Decides, for each request, whether to serve it as is, serve it from another
35
+ * internal path, or redirect it — mirroring the Intlayer proxies of
36
+ * `next-intlayer` and `vite-intlayer`. Unlike them, an internal rewrite hands
37
+ * the router the path *without* its locale prefix: the Remix router has no
38
+ * trailing-slash normalisation to work around, and this lets an application
39
+ * declare each route once (`/about`) whatever the routing mode, the locale
40
+ * being read from the request context instead of a route param.
41
+ *
42
+ * @param configuration - The `internationalization` and `routing` configuration.
43
+ * @param options - Runtime options.
44
+ * @returns A pure function deciding the routing of a request.
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * const resolveLocaleRouting = createLocaleRouting(configuration);
49
+ *
50
+ * resolveLocaleRouting({ url: new URL('http://host/fr/about'), headers });
51
+ * // { kind: 'rewrite', path: '/about', locale: 'fr' }
52
+ * ```
53
+ */
54
+ const createLocaleRouting = (configuration, options = {}) => {
55
+ const { isDevServer = process.env.NODE_ENV === "development", ignore } = options;
56
+ const { locales, defaultLocale } = configuration.internationalization;
57
+ const { basePath = "", mode = _intlayer_config_defaultValues.ROUTING_MODE, rewrite, domains, enableProxy } = configuration.routing ?? {};
58
+ const proxyMode = (0, _intlayer_core_localization.resolveProxyMode)(enableProxy);
59
+ const canUseStorageLocale = (0, _intlayer_core_localization.isProxyStorageLocaleEnabled)(proxyMode, isDevServer);
60
+ const isSearchParamsMode = mode === "search-params";
61
+ const noPrefix = mode === "no-prefix" || isSearchParamsMode;
62
+ const prefixDefault = mode === "prefix-all";
63
+ const rewriteRules = (0, _intlayer_core_localization.getRewriteRules)(rewrite, "url");
64
+ const normalizedBasePath = trimTrailingSlash(basePath && !basePath.startsWith("/") ? `/${basePath}` : basePath);
65
+ const isSupportedLocale = (value) => value !== void 0 && locales.includes(value);
66
+ /**
67
+ * Locale carried by the first path segment (`/fr/about` → `fr`).
68
+ */
69
+ const getPathLocale = (pathname) => {
70
+ const firstSegment = pathname.split("/")[1];
71
+ return isSupportedLocale(firstSegment) ? firstSegment : void 0;
72
+ };
73
+ /**
74
+ * Locale a localized ("pretty") path belongs to. `/a-propos` only exists
75
+ * because a rewrite rule maps it to French, so it declares its locale as
76
+ * explicitly as an `/fr` prefix would; rules whose localized path equals the
77
+ * canonical one carry no signal and are skipped.
78
+ */
79
+ const getRewriteLocale = (pathname) => rewriteRules ? locales.find((candidate) => (0, _intlayer_core_localization.getCanonicalPath)(pathname, candidate, rewriteRules) !== pathname) : void 0;
80
+ /**
81
+ * Locale persisted by the client (cookie / header), when it may be used as
82
+ * a routing source.
83
+ */
84
+ const getStorageLocale = (headers) => canUseStorageLocale ? (0, _intlayer_core_utils.getLocaleFromStorageServer)({
85
+ getCookie: (name) => (0, _intlayer_core_utils.getCookie)(name, headers.get("cookie") ?? void 0),
86
+ getHeader: (name) => headers.get(name)
87
+ }) : void 0;
88
+ /**
89
+ * Best locale for a request that names none: storage, then
90
+ * `Accept-Language`, then the default locale.
91
+ */
92
+ const detectLocale = (headers) => {
93
+ const locale = getStorageLocale(headers) ?? (0, _intlayer_core_localization.localeDetector)({ "accept-language": headers.get("accept-language") ?? void 0 }, locales, defaultLocale);
94
+ return isSupportedLocale(locale) ? locale : defaultLocale;
95
+ };
96
+ /**
97
+ * Query string of a URL, with the `locale` param set in `search-params` mode.
98
+ */
99
+ const withLocaleSearch = (search, locale) => {
100
+ if (!isSearchParamsMode) return search;
101
+ const params = new URLSearchParams(search);
102
+ params.set("locale", locale);
103
+ return `?${params.toString()}`;
104
+ };
105
+ /**
106
+ * Public path of an application path for a locale: base path, then the
107
+ * locale prefix when the routing mode shows one for that locale.
108
+ */
109
+ const toPublicPath = (locale, path, search) => {
110
+ const prefixedPath = !noPrefix && (prefixDefault || locale !== defaultLocale) ? `/${locale}${path === "/" ? "" : path}` : path;
111
+ return `${normalizedBasePath}${trimTrailingSlash(prefixedPath)}${withLocaleSearch(search, locale)}`;
112
+ };
113
+ /**
114
+ * Internal path handed to the router: base path plus the canonical path.
115
+ */
116
+ const toInternalPath = (path, search) => `${normalizedBasePath}${path}${search}`;
117
+ const rewriteTo = (path, search, locale) => ({
118
+ kind: "rewrite",
119
+ path: encodeURI(toInternalPath(path, search)),
120
+ locale
121
+ });
122
+ /**
123
+ * Same-origin redirect. A target equal to the request itself is served in
124
+ * place instead, so a configuration mismatch can never loop.
125
+ */
126
+ const redirectTo = (location, current, persistLocale) => {
127
+ const encodedLocation = encodeURI(location);
128
+ if (encodedLocation === current) return PASS;
129
+ return persistLocale ? {
130
+ kind: "redirect",
131
+ location: encodedLocation,
132
+ status: 302,
133
+ persistLocale
134
+ } : {
135
+ kind: "redirect",
136
+ location: encodedLocation,
137
+ status: 302
138
+ };
139
+ };
140
+ /**
141
+ * `no-prefix` and `search-params` modes: the URL never shows a prefix.
142
+ */
143
+ const handleNoPrefix = ({ url, headers }, pathname, current) => {
144
+ const pathLocale = getPathLocale(pathname);
145
+ if (pathLocale) {
146
+ const canonicalPath = (0, _intlayer_core_localization.getCanonicalPath)(stripLocalePrefix(pathname, pathLocale), pathLocale, rewriteRules);
147
+ return redirectTo(toPublicPath(pathLocale, canonicalPath, url.search), current, pathLocale);
148
+ }
149
+ if (isSearchParamsMode) {
150
+ const searchLocale = url.searchParams.get("locale") ?? void 0;
151
+ const locale = getStorageLocale(headers) ?? (isSupportedLocale(searchLocale) ? searchLocale : void 0) ?? detectLocale(headers);
152
+ const canonicalPath = (0, _intlayer_core_localization.getCanonicalPath)(pathname, locale, rewriteRules);
153
+ return searchLocale === locale ? rewriteTo(canonicalPath, url.search, locale) : redirectTo(`${normalizedBasePath}${pathname}${withLocaleSearch(url.search, locale)}`, current);
154
+ }
155
+ const locale = getRewriteLocale(pathname) ?? detectLocale(headers);
156
+ return rewriteTo((0, _intlayer_core_localization.getCanonicalPath)(pathname, locale, rewriteRules), url.search, locale);
157
+ };
158
+ /**
159
+ * Prefix modes, URL without a locale prefix: redirect to the localized URL
160
+ * when the mode shows a prefix for the resolved locale, serve it otherwise.
161
+ */
162
+ const handleMissingPathLocale = ({ url, headers }, pathname, current) => {
163
+ const locale = getRewriteLocale(pathname) ?? detectLocale(headers);
164
+ const canonicalPath = (0, _intlayer_core_localization.getCanonicalPath)(pathname, locale, rewriteRules);
165
+ if (prefixDefault || locale !== defaultLocale) {
166
+ const { path: localizedPath } = (0, _intlayer_core_localization.resolveLocalizedPath)(canonicalPath, locale, rewriteRules);
167
+ return redirectTo(toPublicPath(locale, localizedPath, url.search), current);
168
+ }
169
+ return rewriteTo(canonicalPath, url.search, locale);
170
+ };
171
+ /**
172
+ * Prefix modes, URL with a locale prefix: normalise it to its localized
173
+ * form, strip the prefix of the default locale when the mode hides it, and
174
+ * serve the canonical path.
175
+ */
176
+ const handleExistingPathLocale = ({ url }, pathname, pathLocale, current) => {
177
+ const rawPath = stripLocalePrefix(pathname, pathLocale);
178
+ const canonicalPath = (0, _intlayer_core_localization.getCanonicalPath)(rawPath, pathLocale, rewriteRules);
179
+ const { path: localizedPath, isRewritten } = (0, _intlayer_core_localization.resolveLocalizedPath)(canonicalPath, pathLocale, rewriteRules);
180
+ if (isRewritten && localizedPath !== rawPath) return redirectTo(toPublicPath(pathLocale, localizedPath, url.search), current);
181
+ if (!prefixDefault && pathLocale === defaultLocale) return redirectTo(toPublicPath(pathLocale, localizedPath, url.search), current, pathLocale);
182
+ return rewriteTo(canonicalPath, url.search, pathLocale);
183
+ };
184
+ return (request) => {
185
+ if (proxyMode === "disabled" || ignore?.(request)) return PASS;
186
+ const { url } = request;
187
+ const fullPathname = decodePathname(url.pathname);
188
+ if (normalizedBasePath && fullPathname !== normalizedBasePath && !fullPathname.startsWith(`${normalizedBasePath}/`)) return PASS;
189
+ const pathname = fullPathname.slice(normalizedBasePath.length) || "/";
190
+ const current = `${url.pathname}${url.search}`;
191
+ const pathLocale = getPathLocale(pathname);
192
+ if (STATIC_ASSET_PATTERN.test(pathname)) return pathLocale ? rewriteTo(stripLocalePrefix(pathname, pathLocale), url.search) : PASS;
193
+ if (noPrefix) return handleNoPrefix(request, pathname, current);
194
+ const localeDomain = pathLocale && domains?.[pathLocale];
195
+ if (localeDomain && (0, _intlayer_core_localization.getDomainHostname)(localeDomain) !== url.hostname) return {
196
+ kind: "redirect",
197
+ location: `${(0, _intlayer_core_localization.getDomainOrigin)(localeDomain)}${encodeURI(stripLocalePrefix(pathname, pathLocale))}${url.search}`,
198
+ status: 301
199
+ };
200
+ if (!pathLocale) {
201
+ const domainLocale = (0, _intlayer_core_localization.getLocaleFromDomain)(url.hostname, domains);
202
+ if (domainLocale) return rewriteTo((0, _intlayer_core_localization.getCanonicalPath)(pathname, domainLocale, rewriteRules), url.search, domainLocale);
203
+ return handleMissingPathLocale(request, pathname, current);
204
+ }
205
+ return handleExistingPathLocale(request, pathname, pathLocale, current);
206
+ };
207
+ };
208
+
209
+ //#endregion
210
+ exports.createLocaleRouting = createLocaleRouting;
211
+ //# sourceMappingURL=localeRouting.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"localeRouting.cjs","names":["ROUTING_MODE","resolveProxyMode","isProxyStorageLocaleEnabled","getRewriteRules","getCanonicalPath","getLocaleFromStorageServer","getCookie","localeDetector","resolveLocalizedPath","getDomainHostname","getDomainOrigin","getLocaleFromDomain"],"sources":["../../src/localeRouting.ts"],"sourcesContent":["import { ROUTING_MODE } from '@intlayer/config/defaultValues';\nimport {\n getCanonicalPath,\n getDomainHostname,\n getDomainOrigin,\n getLocaleFromDomain,\n getRewriteRules,\n isProxyStorageLocaleEnabled,\n localeDetector,\n resolveLocalizedPath,\n resolveProxyMode,\n} from '@intlayer/core/localization';\nimport { getCookie, getLocaleFromStorageServer } from '@intlayer/core/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { IntlayerConfig } from '@intlayer/types/config';\n\n/**\n * The part of an incoming request the locale routing is decided from.\n */\nexport type LocaleRoutingRequest = {\n url: URL;\n headers: Headers;\n};\n\n/**\n * What the locale routing decided for a request.\n *\n * - `pass` — leave the request untouched (proxy disabled, ignored path).\n * - `rewrite` — keep serving the request, but from `path` (locale prefix\n * stripped, rewrite rule resolved to its canonical path). `locale` is the\n * locale the request resolved to, absent for a static asset.\n * - `redirect` — answer with a redirect to `location`. `persistLocale` asks\n * for the locale to be stored on the response so the follow-up request\n * resolves the same locale.\n */\nexport type LocaleRoutingAction =\n | { kind: 'pass' }\n | { kind: 'rewrite'; path: string; locale?: Locale }\n | {\n kind: 'redirect';\n location: string;\n status: 301 | 302;\n persistLocale?: Locale;\n };\n\nexport type LocaleRoutingOptions = {\n /**\n * Whether a development server is serving the app. In `enableProxy: auto`\n * mode, a development server ignores the stored locale (cookie / header)\n * when deciding where a request goes, so a stale cookie cannot keep pulling\n * every navigation to another locale while developing.\n *\n * @default process.env.NODE_ENV === 'development'\n */\n isDevServer?: boolean;\n /**\n * Requests to leave untouched, in addition to static assets.\n */\n ignore?: (request: LocaleRoutingRequest) => boolean;\n};\n\nexport type LocaleRoutingConfiguration = Pick<\n IntlayerConfig,\n 'internationalization' | 'routing'\n>;\n\n/** Matches a pathname ending with a file extension (`/logo.svg`). */\nconst STATIC_ASSET_PATTERN = /\\.[a-zA-Z0-9]+$/;\n\nconst PASS: LocaleRoutingAction = { kind: 'pass' };\n\n/**\n * Decodes a pathname so it can be matched against the rewrite rules, which are\n * written with the characters the locale actually uses (`/doc/релизы`) while\n * the browser sends them percent-encoded. A malformed escape is kept as is.\n */\nconst decodePathname = (pathname: string): string => {\n try {\n return decodeURI(pathname);\n } catch {\n return pathname;\n }\n};\n\n/**\n * Removes a trailing slash, except on the root path. A redirect to `/fr/`\n * would otherwise ping-pong with any trailing-slash normalisation downstream.\n */\nconst trimTrailingSlash = (path: string): string =>\n path.length > 1 && path.endsWith('/') ? path.slice(0, -1) : path;\n\n/**\n * Removes `/{locale}` from the start of a pathname that carries that prefix.\n */\nconst stripLocalePrefix = (pathname: string, locale: Locale): string =>\n pathname.slice(`/${locale}`.length) || '/';\n\n/**\n * Builds the locale-routing resolver of a Remix application.\n *\n * Decides, for each request, whether to serve it as is, serve it from another\n * internal path, or redirect it — mirroring the Intlayer proxies of\n * `next-intlayer` and `vite-intlayer`. Unlike them, an internal rewrite hands\n * the router the path *without* its locale prefix: the Remix router has no\n * trailing-slash normalisation to work around, and this lets an application\n * declare each route once (`/about`) whatever the routing mode, the locale\n * being read from the request context instead of a route param.\n *\n * @param configuration - The `internationalization` and `routing` configuration.\n * @param options - Runtime options.\n * @returns A pure function deciding the routing of a request.\n *\n * @example\n * ```ts\n * const resolveLocaleRouting = createLocaleRouting(configuration);\n *\n * resolveLocaleRouting({ url: new URL('http://host/fr/about'), headers });\n * // { kind: 'rewrite', path: '/about', locale: 'fr' }\n * ```\n */\nexport const createLocaleRouting = (\n configuration: LocaleRoutingConfiguration,\n options: LocaleRoutingOptions = {}\n): ((request: LocaleRoutingRequest) => LocaleRoutingAction) => {\n const { isDevServer = process.env.NODE_ENV === 'development', ignore } =\n options;\n const { locales, defaultLocale } = configuration.internationalization;\n const {\n basePath = '',\n mode = ROUTING_MODE,\n rewrite,\n domains,\n enableProxy,\n } = configuration.routing ?? {};\n\n const proxyMode = resolveProxyMode(enableProxy);\n const canUseStorageLocale = isProxyStorageLocaleEnabled(\n proxyMode,\n isDevServer\n );\n\n const isSearchParamsMode = mode === 'search-params';\n const noPrefix = mode === 'no-prefix' || isSearchParamsMode;\n const prefixDefault = mode === 'prefix-all';\n const rewriteRules = getRewriteRules(rewrite, 'url');\n const normalizedBasePath = trimTrailingSlash(\n basePath && !basePath.startsWith('/') ? `/${basePath}` : basePath\n );\n\n const isSupportedLocale = (value: string | undefined): value is Locale =>\n value !== undefined && locales.includes(value as Locale);\n\n /**\n * Locale carried by the first path segment (`/fr/about` → `fr`).\n */\n const getPathLocale = (pathname: string): Locale | undefined => {\n const firstSegment = pathname.split('/')[1];\n\n return isSupportedLocale(firstSegment) ? firstSegment : undefined;\n };\n\n /**\n * Locale a localized (\"pretty\") path belongs to. `/a-propos` only exists\n * because a rewrite rule maps it to French, so it declares its locale as\n * explicitly as an `/fr` prefix would; rules whose localized path equals the\n * canonical one carry no signal and are skipped.\n */\n const getRewriteLocale = (pathname: string): Locale | undefined =>\n rewriteRules\n ? locales.find(\n (candidate) =>\n getCanonicalPath(pathname, candidate, rewriteRules) !== pathname\n )\n : undefined;\n\n /**\n * Locale persisted by the client (cookie / header), when it may be used as\n * a routing source.\n */\n const getStorageLocale = (headers: Headers): Locale | undefined =>\n canUseStorageLocale\n ? getLocaleFromStorageServer({\n getCookie: (name) =>\n getCookie(name, headers.get('cookie') ?? undefined),\n getHeader: (name) => headers.get(name),\n })\n : undefined;\n\n /**\n * Best locale for a request that names none: storage, then\n * `Accept-Language`, then the default locale.\n */\n const detectLocale = (headers: Headers): Locale => {\n const locale =\n getStorageLocale(headers) ??\n localeDetector(\n { 'accept-language': headers.get('accept-language') ?? undefined },\n locales,\n defaultLocale\n );\n\n return isSupportedLocale(locale) ? locale : defaultLocale;\n };\n\n /**\n * Query string of a URL, with the `locale` param set in `search-params` mode.\n */\n const withLocaleSearch = (search: string, locale: Locale): string => {\n if (!isSearchParamsMode) return search;\n\n const params = new URLSearchParams(search);\n params.set('locale', locale);\n\n return `?${params.toString()}`;\n };\n\n /**\n * Public path of an application path for a locale: base path, then the\n * locale prefix when the routing mode shows one for that locale.\n */\n const toPublicPath = (\n locale: Locale,\n path: string,\n search: string\n ): string => {\n const showPrefix = !noPrefix && (prefixDefault || locale !== defaultLocale);\n const prefixedPath = showPrefix\n ? `/${locale}${path === '/' ? '' : path}`\n : path;\n\n return `${normalizedBasePath}${trimTrailingSlash(prefixedPath)}${withLocaleSearch(search, locale)}`;\n };\n\n /**\n * Internal path handed to the router: base path plus the canonical path.\n */\n const toInternalPath = (path: string, search: string): string =>\n `${normalizedBasePath}${path}${search}`;\n\n const rewriteTo = (\n path: string,\n search: string,\n locale?: Locale\n ): LocaleRoutingAction => ({\n kind: 'rewrite',\n path: encodeURI(toInternalPath(path, search)),\n locale,\n });\n\n /**\n * Same-origin redirect. A target equal to the request itself is served in\n * place instead, so a configuration mismatch can never loop.\n */\n const redirectTo = (\n location: string,\n current: string,\n persistLocale?: Locale\n ): LocaleRoutingAction => {\n const encodedLocation = encodeURI(location);\n\n if (encodedLocation === current) return PASS;\n\n return persistLocale\n ? {\n kind: 'redirect',\n location: encodedLocation,\n status: 302,\n persistLocale,\n }\n : { kind: 'redirect', location: encodedLocation, status: 302 };\n };\n\n /**\n * `no-prefix` and `search-params` modes: the URL never shows a prefix.\n */\n const handleNoPrefix = (\n { url, headers }: LocaleRoutingRequest,\n pathname: string,\n current: string\n ): LocaleRoutingAction => {\n const pathLocale = getPathLocale(pathname);\n\n if (pathLocale) {\n const canonicalPath = getCanonicalPath(\n stripLocalePrefix(pathname, pathLocale),\n pathLocale,\n rewriteRules\n );\n\n // Stripping the prefix drops the only locale signal from the URL, so the\n // locale is persisted for the follow-up request.\n return redirectTo(\n toPublicPath(pathLocale, canonicalPath, url.search),\n current,\n pathLocale\n );\n }\n\n if (isSearchParamsMode) {\n const searchLocale = url.searchParams.get('locale') ?? undefined;\n const locale =\n getStorageLocale(headers) ??\n (isSupportedLocale(searchLocale) ? searchLocale : undefined) ??\n detectLocale(headers);\n const canonicalPath = getCanonicalPath(pathname, locale, rewriteRules);\n\n return searchLocale === locale\n ? rewriteTo(canonicalPath, url.search, locale)\n : redirectTo(\n `${normalizedBasePath}${pathname}${withLocaleSearch(url.search, locale)}`,\n current\n );\n }\n\n const locale = getRewriteLocale(pathname) ?? detectLocale(headers);\n\n return rewriteTo(\n getCanonicalPath(pathname, locale, rewriteRules),\n url.search,\n locale\n );\n };\n\n /**\n * Prefix modes, URL without a locale prefix: redirect to the localized URL\n * when the mode shows a prefix for the resolved locale, serve it otherwise.\n */\n const handleMissingPathLocale = (\n { url, headers }: LocaleRoutingRequest,\n pathname: string,\n current: string\n ): LocaleRoutingAction => {\n // A localized path names its own locale and outranks storage and\n // `Accept-Language`, otherwise `/a-propos` read as `en` matches no rule.\n const locale = getRewriteLocale(pathname) ?? detectLocale(headers);\n const canonicalPath = getCanonicalPath(pathname, locale, rewriteRules);\n\n if (prefixDefault || locale !== defaultLocale) {\n const { path: localizedPath } = resolveLocalizedPath(\n canonicalPath,\n locale,\n rewriteRules\n );\n\n return redirectTo(\n toPublicPath(locale, localizedPath, url.search),\n current\n );\n }\n\n return rewriteTo(canonicalPath, url.search, locale);\n };\n\n /**\n * Prefix modes, URL with a locale prefix: normalise it to its localized\n * form, strip the prefix of the default locale when the mode hides it, and\n * serve the canonical path.\n */\n const handleExistingPathLocale = (\n { url }: LocaleRoutingRequest,\n pathname: string,\n pathLocale: Locale,\n current: string\n ): LocaleRoutingAction => {\n const rawPath = stripLocalePrefix(pathname, pathLocale);\n const canonicalPath = getCanonicalPath(rawPath, pathLocale, rewriteRules);\n const { path: localizedPath, isRewritten } = resolveLocalizedPath(\n canonicalPath,\n pathLocale,\n rewriteRules\n );\n\n // `/fr/about` → `/fr/a-propos`\n if (isRewritten && localizedPath !== rawPath) {\n return redirectTo(\n toPublicPath(pathLocale, localizedPath, url.search),\n current\n );\n }\n\n // `/en/about` → `/about`. The locale is persisted: the target no longer\n // names it, and detection could otherwise resolve another one.\n if (!prefixDefault && pathLocale === defaultLocale) {\n return redirectTo(\n toPublicPath(pathLocale, localizedPath, url.search),\n current,\n pathLocale\n );\n }\n\n return rewriteTo(canonicalPath, url.search, pathLocale);\n };\n\n return (request): LocaleRoutingAction => {\n if (proxyMode === 'disabled' || ignore?.(request)) return PASS;\n\n const { url } = request;\n const fullPathname = decodePathname(url.pathname);\n\n if (\n normalizedBasePath &&\n fullPathname !== normalizedBasePath &&\n !fullPathname.startsWith(`${normalizedBasePath}/`)\n ) {\n return PASS;\n }\n\n const pathname = fullPathname.slice(normalizedBasePath.length) || '/';\n const current = `${url.pathname}${url.search}`;\n const pathLocale = getPathLocale(pathname);\n\n // Static assets are served from their unprefixed path, untouched otherwise.\n if (STATIC_ASSET_PATTERN.test(pathname)) {\n return pathLocale\n ? rewriteTo(stripLocalePrefix(pathname, pathLocale), url.search)\n : PASS;\n }\n\n if (noPrefix) return handleNoPrefix(request, pathname, current);\n\n // `host/zh/about` → `https://host.zh/about` when `zh` has its own domain.\n // Domain mapping is stable configuration, hence a cacheable 301.\n const localeDomain = pathLocale && domains?.[pathLocale];\n\n if (localeDomain && getDomainHostname(localeDomain) !== url.hostname) {\n return {\n kind: 'redirect',\n location: `${getDomainOrigin(localeDomain)}${encodeURI(stripLocalePrefix(pathname, pathLocale))}${url.search}`,\n status: 301,\n };\n }\n\n // `host.zh/about` → `/about` as `zh`: the hostname alone names the locale.\n if (!pathLocale) {\n const domainLocale = getLocaleFromDomain(url.hostname, domains);\n\n if (domainLocale) {\n return rewriteTo(\n getCanonicalPath(pathname, domainLocale, rewriteRules),\n url.search,\n domainLocale\n );\n }\n\n return handleMissingPathLocale(request, pathname, current);\n }\n\n return handleExistingPathLocale(request, pathname, pathLocale, current);\n };\n};\n"],"mappings":"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"}
@@ -0,0 +1,45 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ let vanilla_intlayer_markdown = require("vanilla-intlayer/markdown");
3
+
4
+ Object.defineProperty(exports, 'compileMarkdown', {
5
+ enumerable: true,
6
+ get: function () {
7
+ return vanilla_intlayer_markdown.compileMarkdown;
8
+ }
9
+ });
10
+ Object.defineProperty(exports, 'installIntlayerMarkdown', {
11
+ enumerable: true,
12
+ get: function () {
13
+ return vanilla_intlayer_markdown.installIntlayerMarkdown;
14
+ }
15
+ });
16
+ Object.defineProperty(exports, 'installIntlayerMarkdownDynamic', {
17
+ enumerable: true,
18
+ get: function () {
19
+ return vanilla_intlayer_markdown.installIntlayerMarkdownDynamic;
20
+ }
21
+ });
22
+ Object.defineProperty(exports, 'parseMarkdown', {
23
+ enumerable: true,
24
+ get: function () {
25
+ return vanilla_intlayer_markdown.parseMarkdown;
26
+ }
27
+ });
28
+ Object.defineProperty(exports, 'renderMarkdown', {
29
+ enumerable: true,
30
+ get: function () {
31
+ return vanilla_intlayer_markdown.renderMarkdown;
32
+ }
33
+ });
34
+ Object.defineProperty(exports, 'useMarkdown', {
35
+ enumerable: true,
36
+ get: function () {
37
+ return vanilla_intlayer_markdown.useMarkdown;
38
+ }
39
+ });
40
+ Object.defineProperty(exports, 'useMarkdownRenderer', {
41
+ enumerable: true,
42
+ get: function () {
43
+ return vanilla_intlayer_markdown.useMarkdownRenderer;
44
+ }
45
+ });
@@ -0,0 +1,92 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_context = require('./context.cjs');
3
+ const require_localeRouting = require('./localeRouting.cjs');
4
+ const require_persistLocale = require('./persistLocale.cjs');
5
+ const require_requestStorage = require('./requestStorage.cjs');
6
+ let _intlayer_config_built = require("@intlayer/config/built");
7
+ let _intlayer_config_node = require("@intlayer/config/node");
8
+ let _intlayer_core_localization = require("@intlayer/core/localization");
9
+ let _intlayer_engine_build = require("@intlayer/engine/build");
10
+
11
+ //#region src/middleware.ts
12
+ /**
13
+ * Builds the redirect response of a locale-routing decision.
14
+ *
15
+ * Built by hand rather than with `Response.redirect`, whose headers are
16
+ * immutable and could not carry the locale cookie.
17
+ */
18
+ const createRedirectResponse = (action) => {
19
+ const headers = new Headers({ location: action.location });
20
+ if (action.persistLocale) require_persistLocale.writeLocaleHeaders(headers, action.persistLocale, { includeCookie: true });
21
+ return new Response(null, {
22
+ status: action.status,
23
+ headers
24
+ });
25
+ };
26
+ /**
27
+ * Remix 3 router middleware handling the internationalization layer:
28
+ * locale routing (redirects and internal rewrites, as `next-intlayer` and
29
+ * `vite-intlayer` do), locale resolution and locale persistence.
30
+ *
31
+ * Locale routing follows `routing.mode`. In the prefix modes, `/fr/about` is
32
+ * served from the `/about` route as French, `/about` is redirected to the
33
+ * localized URL of the detected locale (cookie / header, then
34
+ * `Accept-Language`) unless that locale needs no prefix, and `routing.rewrite`
35
+ * rules are applied both ways (`/fr/about` → `/fr/a-propos`). The router
36
+ * therefore matches every locale against the same unprefixed routes, and
37
+ * `routing.enableProxy: false` turns the routing off while keeping the
38
+ * locale resolution.
39
+ *
40
+ * The resolved locale is exposed as `context.get(Intlayer)` /
41
+ * `context.intlayer`, and the rest of the request runs inside an
42
+ * `AsyncLocalStorage` scope bound to the request context, so the hooks of this
43
+ * package (`useLocale`, `useIntlayer`, `useDictionary`) read that state from
44
+ * anywhere downstream — route handlers, views, Remix UI components — with the
45
+ * same signature as `react-intlayer`.
46
+ *
47
+ * @param options - Locale-routing options.
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * import { createRouter } from 'remix/router';
52
+ * import { intlayer, useIntlayer } from 'remix-intlayer';
53
+ *
54
+ * const router = createRouter({ middleware: [intlayer()] });
55
+ *
56
+ * // Serves `/`, `/fr`, `/es`… — the locale comes from the request context.
57
+ * router.get('/', () => {
58
+ * const { title } = useIntlayer('home');
59
+ * return new Response(title);
60
+ * });
61
+ * ```
62
+ */
63
+ const intlayer = (options = {}) => {
64
+ const configuration = (0, _intlayer_config_node.getConfiguration)();
65
+ (0, _intlayer_engine_build.prepareIntlayer)(configuration);
66
+ const resolveLocaleRouting = require_localeRouting.createLocaleRouting({
67
+ internationalization: configuration.internationalization ?? _intlayer_config_built.internationalization,
68
+ routing: configuration.routing ?? _intlayer_config_built.routing
69
+ }, options);
70
+ return async (context, next) => {
71
+ const action = resolveLocaleRouting(context);
72
+ if (action.kind === "redirect") return createRedirectResponse(action);
73
+ let routedLocale;
74
+ if (action.kind === "rewrite") {
75
+ context.url = new URL(action.path, context.url);
76
+ routedLocale = action.locale;
77
+ }
78
+ const locale = routedLocale ?? await (0, _intlayer_core_localization.getLocaleFromRequest)(context);
79
+ if (routedLocale) require_persistLocale.writeLocaleHeaders(context.headers, routedLocale);
80
+ context.set(require_context.Intlayer, {
81
+ locale,
82
+ defaultLocale: configuration.internationalization?.defaultLocale ?? _intlayer_config_built.internationalization.defaultLocale,
83
+ availableLocales: configuration.internationalization?.locales ?? _intlayer_config_built.internationalization.locales
84
+ }, { property: require_context.INTLAYER_CONTEXT_PROPERTY });
85
+ const response = await require_requestStorage.requestStorage.run(context, next);
86
+ return routedLocale ? require_persistLocale.withLocaleHeaders(response, routedLocale) : response;
87
+ };
88
+ };
89
+
90
+ //#endregion
91
+ exports.intlayer = intlayer;
92
+ //# sourceMappingURL=middleware.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middleware.cjs","names":["getConfiguration","createLocaleRouting","internationalization","routing","getLocaleFromRequest","Intlayer","INTLAYER_CONTEXT_PROPERTY","requestStorage","withLocaleHeaders"],"sources":["../../src/middleware.ts"],"sourcesContent":["import { internationalization, routing } from '@intlayer/config/built';\nimport { getConfiguration } from '@intlayer/config/node';\nimport { getLocaleFromRequest } from '@intlayer/core/localization';\nimport { prepareIntlayer } from '@intlayer/engine/build';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport type { DeclaredLocales } from '@intlayer/types/module_augmentation';\nimport type { Middleware } from 'remix/router';\nimport {\n INTLAYER_CONTEXT_PROPERTY,\n Intlayer,\n type IntlayerState,\n} from './context';\nimport {\n createLocaleRouting,\n type LocaleRoutingAction,\n type LocaleRoutingOptions,\n} from './localeRouting';\nimport { withLocaleHeaders, writeLocaleHeaders } from './persistLocale';\nimport { requestStorage } from './requestStorage';\n\nexport type IntlayerMiddlewareOptions = LocaleRoutingOptions;\n\n/**\n * Builds the redirect response of a locale-routing decision.\n *\n * Built by hand rather than with `Response.redirect`, whose headers are\n * immutable and could not carry the locale cookie.\n */\nconst createRedirectResponse = (\n action: Extract<LocaleRoutingAction, { kind: 'redirect' }>\n): Response => {\n const headers = new Headers({ location: action.location });\n\n if (action.persistLocale) {\n writeLocaleHeaders(headers, action.persistLocale, { includeCookie: true });\n }\n\n return new Response(null, { status: action.status, headers });\n};\n\n/**\n * Remix 3 router middleware handling the internationalization layer:\n * locale routing (redirects and internal rewrites, as `next-intlayer` and\n * `vite-intlayer` do), locale resolution and locale persistence.\n *\n * Locale routing follows `routing.mode`. In the prefix modes, `/fr/about` is\n * served from the `/about` route as French, `/about` is redirected to the\n * localized URL of the detected locale (cookie / header, then\n * `Accept-Language`) unless that locale needs no prefix, and `routing.rewrite`\n * rules are applied both ways (`/fr/about` → `/fr/a-propos`). The router\n * therefore matches every locale against the same unprefixed routes, and\n * `routing.enableProxy: false` turns the routing off while keeping the\n * locale resolution.\n *\n * The resolved locale is exposed as `context.get(Intlayer)` /\n * `context.intlayer`, and the rest of the request runs inside an\n * `AsyncLocalStorage` scope bound to the request context, so the hooks of this\n * package (`useLocale`, `useIntlayer`, `useDictionary`) read that state from\n * anywhere downstream — route handlers, views, Remix UI components — with the\n * same signature as `react-intlayer`.\n *\n * @param options - Locale-routing options.\n *\n * @example\n * ```ts\n * import { createRouter } from 'remix/router';\n * import { intlayer, useIntlayer } from 'remix-intlayer';\n *\n * const router = createRouter({ middleware: [intlayer()] });\n *\n * // Serves `/`, `/fr`, `/es`… — the locale comes from the request context.\n * router.get('/', () => {\n * const { title } = useIntlayer('home');\n * return new Response(title);\n * });\n * ```\n */\nexport const intlayer = (\n options: IntlayerMiddlewareOptions = {}\n): Middleware<{\n key: typeof Intlayer;\n value: IntlayerState;\n property: typeof INTLAYER_CONTEXT_PROPERTY;\n}> => {\n // Ensures the generated dictionaries exist when the server starts without a\n // prior `intlayer build`. Fire-and-forget, like the other server integrations.\n const configuration = getConfiguration();\n prepareIntlayer(configuration);\n\n const resolveLocaleRouting = createLocaleRouting(\n {\n internationalization:\n configuration.internationalization ?? internationalization,\n routing: configuration.routing ?? routing,\n },\n options\n );\n\n return async (context, next) => {\n const action = resolveLocaleRouting(context);\n\n if (action.kind === 'redirect') return createRedirectResponse(action);\n\n let routedLocale: Locale | undefined;\n\n if (action.kind === 'rewrite') {\n // The router matches against `context.url`, so reassigning it is the\n // internal rewrite. The original request stays untouched.\n context.url = new URL(action.path, context.url);\n routedLocale = action.locale;\n }\n\n const locale =\n routedLocale ?? ((await getLocaleFromRequest(context)) as Locale);\n\n if (routedLocale) {\n // Propagates the locale downstream through the configured storage\n // header, like the Next.js and Vite proxies do.\n writeLocaleHeaders(context.headers, routedLocale);\n }\n\n context.set(\n Intlayer,\n {\n locale: locale as DeclaredLocales,\n defaultLocale: (configuration.internationalization?.defaultLocale ??\n internationalization.defaultLocale) as DeclaredLocales,\n availableLocales: (configuration.internationalization?.locales ??\n internationalization.locales) as DeclaredLocales[],\n },\n { property: INTLAYER_CONTEXT_PROPERTY }\n );\n\n const response = await requestStorage.run(context, next);\n\n return routedLocale ? withLocaleHeaders(response, routedLocale) : response;\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;AA4BA,MAAM,0BACJ,WACa;CACb,MAAM,UAAU,IAAI,QAAQ,EAAE,UAAU,OAAO,SAAS,CAAC;CAEzD,IAAI,OAAO,eACT,yCAAmB,SAAS,OAAO,eAAe,EAAE,eAAe,KAAK,CAAC;CAG3E,OAAO,IAAI,SAAS,MAAM;EAAE,QAAQ,OAAO;EAAQ;CAAQ,CAAC;AAC9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,MAAa,YACX,UAAqC,CAAC,MAKlC;CAGJ,MAAM,oBAAgBA,wCAAiB;CACvC,4CAAgB,aAAa;CAE7B,MAAM,uBAAuBC,0CAC3B;EACE,sBACE,cAAc,wBAAwBC;EACxC,SAAS,cAAc,WAAWC;CACpC,GACA,OACF;CAEA,OAAO,OAAO,SAAS,SAAS;EAC9B,MAAM,SAAS,qBAAqB,OAAO;EAE3C,IAAI,OAAO,SAAS,YAAY,OAAO,uBAAuB,MAAM;EAEpE,IAAI;EAEJ,IAAI,OAAO,SAAS,WAAW;GAG7B,QAAQ,MAAM,IAAI,IAAI,OAAO,MAAM,QAAQ,GAAG;GAC9C,eAAe,OAAO;EACxB;EAEA,MAAM,SACJ,gBAAkB,UAAMC,kDAAqB,OAAO;EAEtD,IAAI,cAGF,yCAAmB,QAAQ,SAAS,YAAY;EAGlD,QAAQ,IACNC,0BACA;GACU;GACR,eAAgB,cAAc,sBAAsB,iBAClDH,4CAAqB;GACvB,kBAAmB,cAAc,sBAAsB,WACrDA,4CAAqB;EACzB,GACA,EAAE,UAAUI,0CAA0B,CACxC;EAEA,MAAM,WAAW,MAAMC,sCAAe,IAAI,SAAS,IAAI;EAEvD,OAAO,eAAeC,wCAAkB,UAAU,YAAY,IAAI;CACpE;AACF"}
@@ -0,0 +1,51 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ let _intlayer_core_utils = require("@intlayer/core/utils");
3
+ let remix_headers = require("remix/headers");
4
+
5
+ //#region src/persistLocale.ts
6
+ /**
7
+ * Serializes a locale cookie as a `Set-Cookie` header value.
8
+ */
9
+ const serializeCookie = (name, value, attributes) => new remix_headers.SetCookie({
10
+ name,
11
+ value,
12
+ path: attributes.path,
13
+ domain: attributes.domain,
14
+ expires: attributes.expires === void 0 ? void 0 : new Date(attributes.expires),
15
+ secure: attributes.secure,
16
+ httpOnly: attributes.httpOnly,
17
+ sameSite: attributes.sameSite ? attributes.sameSite.charAt(0).toUpperCase() + attributes.sameSite.slice(1) : void 0
18
+ }).toString();
19
+ /**
20
+ * Writes the locale into the storage headers configured in `routing.storage`
21
+ * (custom header, and the cookie when `includeCookie` is set).
22
+ */
23
+ const writeLocaleHeaders = (headers, locale, { includeCookie = false } = {}) => (0, _intlayer_core_utils.setLocaleInStorageServer)(locale, {
24
+ setHeader: (name, value) => headers.set(name, value),
25
+ setCookieStore: includeCookie ? (name, value, attributes) => headers.append("set-cookie", serializeCookie(name, value, attributes)) : void 0
26
+ });
27
+ /**
28
+ * Returns the response with the locale headers set. A response whose headers
29
+ * are immutable (`Response.redirect`, `Response.error`) is re-created.
30
+ */
31
+ const withLocaleHeaders = (response, locale) => {
32
+ const localeHeaders = new Headers();
33
+ writeLocaleHeaders(localeHeaders, locale);
34
+ try {
35
+ for (const [name, value] of localeHeaders) response.headers.set(name, value);
36
+ return response;
37
+ } catch {
38
+ const headers = new Headers(response.headers);
39
+ for (const [name, value] of localeHeaders) headers.set(name, value);
40
+ return new Response(response.body, {
41
+ status: response.status,
42
+ statusText: response.statusText,
43
+ headers
44
+ });
45
+ }
46
+ };
47
+
48
+ //#endregion
49
+ exports.withLocaleHeaders = withLocaleHeaders;
50
+ exports.writeLocaleHeaders = writeLocaleHeaders;
51
+ //# sourceMappingURL=persistLocale.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"persistLocale.cjs","names":["SetCookie","setLocaleInStorageServer"],"sources":["../../src/persistLocale.ts"],"sourcesContent":["import {\n type CookieBuildAttributes,\n setLocaleInStorageServer,\n} from '@intlayer/core/utils';\nimport type { Locale } from '@intlayer/types/allLocales';\nimport { SetCookie } from 'remix/headers';\n\n/**\n * Serializes a locale cookie as a `Set-Cookie` header value.\n */\nconst serializeCookie = (\n name: string,\n value: string,\n attributes: CookieBuildAttributes\n): string =>\n new SetCookie({\n name,\n value,\n path: attributes.path,\n domain: attributes.domain,\n expires:\n attributes.expires === undefined\n ? undefined\n : new Date(attributes.expires),\n secure: attributes.secure,\n httpOnly: attributes.httpOnly,\n sameSite: attributes.sameSite\n ? ((attributes.sameSite.charAt(0).toUpperCase() +\n attributes.sameSite.slice(1)) as 'Strict' | 'Lax' | 'None')\n : undefined,\n }).toString();\n\n/**\n * Writes the locale into the storage headers configured in `routing.storage`\n * (custom header, and the cookie when `includeCookie` is set).\n */\nexport const writeLocaleHeaders = (\n headers: Headers,\n locale: Locale,\n { includeCookie = false }: { includeCookie?: boolean } = {}\n): void =>\n setLocaleInStorageServer(locale, {\n setHeader: (name, value) => headers.set(name, value),\n setCookieStore: includeCookie\n ? (name, value, attributes) =>\n headers.append('set-cookie', serializeCookie(name, value, attributes))\n : undefined,\n });\n\n/**\n * Returns the response with the locale headers set. A response whose headers\n * are immutable (`Response.redirect`, `Response.error`) is re-created.\n */\nexport const withLocaleHeaders = (\n response: Response,\n locale: Locale\n): Response => {\n // Written apart first: `setLocaleInStorageServer` swallows setter errors,\n // so an immutable target has to be detected on the copy.\n const localeHeaders = new Headers();\n writeLocaleHeaders(localeHeaders, locale);\n\n try {\n for (const [name, value] of localeHeaders) {\n response.headers.set(name, value);\n }\n\n return response;\n } catch {\n const headers = new Headers(response.headers);\n\n for (const [name, value] of localeHeaders) {\n headers.set(name, value);\n }\n\n return new Response(response.body, {\n status: response.status,\n statusText: response.statusText,\n headers,\n });\n }\n};\n"],"mappings":";;;;;;;;AAUA,MAAM,mBACJ,MACA,OACA,eAEA,IAAIA,wBAAU;CACZ;CACA;CACA,MAAM,WAAW;CACjB,QAAQ,WAAW;CACnB,SACE,WAAW,YAAY,SACnB,SACA,IAAI,KAAK,WAAW,OAAO;CACjC,QAAQ,WAAW;CACnB,UAAU,WAAW;CACrB,UAAU,WAAW,WACf,WAAW,SAAS,OAAO,CAAC,CAAC,CAAC,YAAY,IAC1C,WAAW,SAAS,MAAM,CAAC,IAC7B;AACN,CAAC,CAAC,CAAC,SAAS;;;;;AAMd,MAAa,sBACX,SACA,QACA,EAAE,gBAAgB,UAAuC,CAAC,UAE1DC,+CAAyB,QAAQ;CAC/B,YAAY,MAAM,UAAU,QAAQ,IAAI,MAAM,KAAK;CACnD,gBAAgB,iBACX,MAAM,OAAO,eACZ,QAAQ,OAAO,cAAc,gBAAgB,MAAM,OAAO,UAAU,CAAC,IACvE;AACN,CAAC;;;;;AAMH,MAAa,qBACX,UACA,WACa;CAGb,MAAM,gBAAgB,IAAI,QAAQ;CAClC,mBAAmB,eAAe,MAAM;CAExC,IAAI;EACF,KAAK,MAAM,CAAC,MAAM,UAAU,eAC1B,SAAS,QAAQ,IAAI,MAAM,KAAK;EAGlC,OAAO;CACT,QAAQ;EACN,MAAM,UAAU,IAAI,QAAQ,SAAS,OAAO;EAE5C,KAAK,MAAM,CAAC,MAAM,UAAU,eAC1B,QAAQ,IAAI,MAAM,KAAK;EAGzB,OAAO,IAAI,SAAS,SAAS,MAAM;GACjC,QAAQ,SAAS;GACjB,YAAY,SAAS;GACrB;EACF,CAAC;CACH;AACF"}
@@ -0,0 +1,25 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_context = require('./context.cjs');
3
+ let node_async_hooks = require("node:async_hooks");
4
+
5
+ //#region src/requestStorage.ts
6
+ /**
7
+ * Request context of the request being handled, made available to every
8
+ * function running inside the `intlayer()` middleware scope — route handlers,
9
+ * views, Remix UI components rendered from them — without passing it around.
10
+ */
11
+ const requestStorage = new node_async_hooks.AsyncLocalStorage();
12
+ /**
13
+ * Returns the Intlayer state of the request being handled, if any.
14
+ */
15
+ const getIntlayerState = () => requestStorage.getStore()?.get(require_context.Intlayer);
16
+ /**
17
+ * Returns the locale of the request being handled, if any.
18
+ */
19
+ const getRequestLocale = () => getIntlayerState()?.locale;
20
+
21
+ //#endregion
22
+ exports.getIntlayerState = getIntlayerState;
23
+ exports.getRequestLocale = getRequestLocale;
24
+ exports.requestStorage = requestStorage;
25
+ //# sourceMappingURL=requestStorage.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"requestStorage.cjs","names":["AsyncLocalStorage","Intlayer"],"sources":["../../src/requestStorage.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\nimport type { DeclaredLocales } from '@intlayer/types/module_augmentation';\nimport type { RequestContext } from 'remix/router';\nimport { Intlayer, type IntlayerState } from './context';\n\n/**\n * Request context of the request being handled, made available to every\n * function running inside the `intlayer()` middleware scope — route handlers,\n * views, Remix UI components rendered from them — without passing it around.\n */\nexport const requestStorage = new AsyncLocalStorage<RequestContext<any, any>>();\n\n/**\n * Returns the Intlayer state of the request being handled, if any.\n */\nexport const getIntlayerState = (): IntlayerState | undefined =>\n requestStorage.getStore()?.get(Intlayer) as IntlayerState | undefined;\n\n/**\n * Returns the locale of the request being handled, if any.\n */\nexport const getRequestLocale = (): DeclaredLocales | undefined =>\n getIntlayerState()?.locale;\n"],"mappings":";;;;;;;;;;AAUA,MAAa,iBAAiB,IAAIA,mCAA4C;;;;AAK9E,MAAa,yBACX,eAAe,SAAS,CAAC,EAAE,IAAIC,wBAAQ;;;;AAKzC,MAAa,yBACX,iBAAiB,CAAC,EAAE"}