@koine/i18n 2.0.0-beta.124 → 2.0.0-beta.126

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 (194) hide show
  1. package/adapter-js/generators/config.cjs.js +70 -0
  2. package/adapter-js/generators/config.esm.js +68 -0
  3. package/adapter-js/generators/createT.cjs.js +201 -0
  4. package/adapter-js/generators/createT.esm.js +199 -0
  5. package/adapter-js/generators/defaultI18nMetadata.cjs.js +24 -0
  6. package/adapter-js/generators/defaultI18nMetadata.esm.js +22 -0
  7. package/adapter-js/generators/formatTo.cjs.js +76 -0
  8. package/adapter-js/generators/formatTo.esm.js +74 -0
  9. package/adapter-js/generators/formatUrl.cjs.js +27 -0
  10. package/adapter-js/generators/formatUrl.esm.js +25 -0
  11. package/adapter-js/generators/getI18nDictionaries.cjs.js +21 -0
  12. package/adapter-js/generators/getI18nDictionaries.esm.js +19 -0
  13. package/adapter-js/generators/getI18nDictionaries_inline.cjs.js +37 -0
  14. package/adapter-js/generators/getI18nDictionaries_inline.esm.js +35 -0
  15. package/adapter-js/generators/getI18nMetadata.cjs.js +67 -0
  16. package/adapter-js/generators/getI18nMetadata.esm.js +65 -0
  17. package/adapter-js/generators/getT.cjs.js +38 -0
  18. package/adapter-js/generators/getT.esm.js +36 -0
  19. package/adapter-js/generators/isLocale.cjs.js +22 -0
  20. package/adapter-js/generators/isLocale.esm.js +20 -0
  21. package/adapter-js/generators/loadTranslations_inline.cjs.js +15 -0
  22. package/adapter-js/generators/loadTranslations_inline.esm.js +13 -0
  23. package/adapter-js/generators/pathnameToRouteId.cjs.js +33 -0
  24. package/adapter-js/generators/pathnameToRouteId.esm.js +31 -0
  25. package/adapter-js/generators/routes.cjs.js +87 -0
  26. package/adapter-js/generators/routes.esm.js +85 -0
  27. package/adapter-js/generators/t.cjs.js +103 -0
  28. package/adapter-js/generators/t.esm.js +101 -0
  29. package/adapter-js/generators/tInterpolateParams.cjs.js +32 -0
  30. package/adapter-js/generators/tInterpolateParams.esm.js +30 -0
  31. package/adapter-js/generators/tPluralise.cjs.js +23 -0
  32. package/adapter-js/generators/tPluralise.esm.js +21 -0
  33. package/adapter-js/generators/to.cjs.js +176 -0
  34. package/adapter-js/generators/to.esm.js +174 -0
  35. package/adapter-js/generators/types.cjs.js +432 -0
  36. package/adapter-js/generators/types.esm.js +430 -0
  37. package/adapter-js/index.cjs.js +80 -0
  38. package/adapter-js/index.d.ts +24 -139
  39. package/adapter-js/index.esm.js +75 -0
  40. package/adapter-next/generators/I18nApp.cjs.js +89 -0
  41. package/adapter-next/generators/I18nApp.esm.js +87 -0
  42. package/adapter-next/generators/I18nDocument.cjs.js +90 -0
  43. package/adapter-next/generators/I18nDocument.esm.js +88 -0
  44. package/adapter-next/generators/I18nHead.cjs.js +59 -0
  45. package/adapter-next/generators/I18nHead.esm.js +57 -0
  46. package/adapter-next/generators/I18nLayout.cjs.js +136 -0
  47. package/adapter-next/generators/I18nLayout.esm.js +134 -0
  48. package/adapter-next/generators/I18nLayoutRoot.cjs.js +43 -0
  49. package/adapter-next/generators/I18nLayoutRoot.esm.js +41 -0
  50. package/adapter-next/generators/I18nPage.cjs.js +231 -0
  51. package/adapter-next/generators/I18nPage.esm.js +229 -0
  52. package/adapter-next/generators/I18nSetter.cjs.js +57 -0
  53. package/adapter-next/generators/I18nSetter.esm.js +55 -0
  54. package/adapter-next/generators/i18nGet.cjs.js +151 -0
  55. package/adapter-next/generators/i18nGet.esm.js +149 -0
  56. package/adapter-next/generators/i18nServer.cjs.js +73 -0
  57. package/adapter-next/generators/i18nServer.esm.js +71 -0
  58. package/adapter-next/generators/next-redirects.cjs.js +22 -0
  59. package/adapter-next/generators/next-redirects.esm.js +20 -0
  60. package/adapter-next/generators/next-rewrites.cjs.js +22 -0
  61. package/adapter-next/generators/next-rewrites.esm.js +20 -0
  62. package/adapter-next/generators/useRouteId.cjs.js +74 -0
  63. package/adapter-next/generators/useRouteId.esm.js +72 -0
  64. package/adapter-next/generators/useTo.cjs.js +49 -0
  65. package/adapter-next/generators/useTo.esm.js +47 -0
  66. package/adapter-next/generators/useToSpa.cjs.js +47 -0
  67. package/adapter-next/generators/useToSpa.esm.js +45 -0
  68. package/adapter-next/index.cjs.js +60 -0
  69. package/adapter-next/index.d.ts +54 -319
  70. package/adapter-next/index.esm.js +55 -0
  71. package/adapter-next/plugin-async.cjs.js +39 -0
  72. package/adapter-next/plugin-async.esm.js +37 -0
  73. package/adapter-next/plugin-legacy.cjs.js +211 -0
  74. package/adapter-next/plugin-legacy.esm.js +209 -0
  75. package/adapter-next/plugin-shared.cjs.js +59 -0
  76. package/adapter-next/plugin-shared.esm.js +55 -0
  77. package/adapter-next/plugin.cjs.js +38 -0
  78. package/adapter-next/plugin.esm.js +35 -0
  79. package/adapter-next/redirects.cjs.js +75 -0
  80. package/adapter-next/redirects.esm.js +72 -0
  81. package/adapter-next/rewrites.cjs.js +88 -0
  82. package/adapter-next/rewrites.esm.js +85 -0
  83. package/adapter-next/transformPathname.cjs.js +14 -0
  84. package/adapter-next/transformPathname.esm.js +12 -0
  85. package/adapter-next/webpackPluginI18n.cjs.js +38 -0
  86. package/adapter-next/webpackPluginI18n.esm.js +36 -0
  87. package/adapter-next-translate/generators/DynamicNamespaces.cjs.js +22 -0
  88. package/adapter-next-translate/generators/DynamicNamespaces.esm.js +20 -0
  89. package/adapter-next-translate/generators/I18nProvider.cjs.js +22 -0
  90. package/adapter-next-translate/generators/I18nProvider.esm.js +20 -0
  91. package/adapter-next-translate/generators/Trans.cjs.js +44 -0
  92. package/adapter-next-translate/generators/Trans.esm.js +42 -0
  93. package/adapter-next-translate/generators/TransText.cjs.js +22 -0
  94. package/adapter-next-translate/generators/TransText.esm.js +20 -0
  95. package/adapter-next-translate/generators/getT.cjs.js +28 -0
  96. package/adapter-next-translate/generators/getT.esm.js +26 -0
  97. package/adapter-next-translate/generators/nextTranslateI18n.cjs.js +34 -0
  98. package/adapter-next-translate/generators/nextTranslateI18n.esm.js +32 -0
  99. package/adapter-next-translate/generators/useLocale.cjs.js +22 -0
  100. package/adapter-next-translate/generators/useLocale.esm.js +20 -0
  101. package/adapter-next-translate/generators/useT.cjs.js +56 -0
  102. package/adapter-next-translate/generators/useT.esm.js +54 -0
  103. package/adapter-next-translate/index.cjs.js +46 -0
  104. package/adapter-next-translate/index.d.ts +57 -337
  105. package/adapter-next-translate/index.esm.js +41 -0
  106. package/adapter-react/generators/I18nEffects.cjs.js +37 -0
  107. package/adapter-react/generators/I18nEffects.esm.js +35 -0
  108. package/adapter-react/generators/I18nHeadTags.cjs.js +54 -0
  109. package/adapter-react/generators/I18nHeadTags.esm.js +52 -0
  110. package/adapter-react/generators/I18nLocaleContext.cjs.js +68 -0
  111. package/adapter-react/generators/I18nLocaleContext.esm.js +66 -0
  112. package/adapter-react/generators/I18nMetadata.cjs.js +145 -0
  113. package/adapter-react/generators/I18nMetadata.esm.js +143 -0
  114. package/adapter-react/generators/I18nRoute.cjs.js +117 -0
  115. package/adapter-react/generators/I18nRoute.esm.js +115 -0
  116. package/adapter-react/generators/I18nTranslate.cjs.js +116 -0
  117. package/adapter-react/generators/I18nTranslate.esm.js +114 -0
  118. package/adapter-react/generators/Trans.cjs.js +62 -0
  119. package/adapter-react/generators/Trans.esm.js +60 -0
  120. package/adapter-react/generators/TransText.cjs.js +36 -0
  121. package/adapter-react/generators/TransText.esm.js +34 -0
  122. package/adapter-react/generators/formatElements.cjs.js +72 -0
  123. package/adapter-react/generators/formatElements.esm.js +70 -0
  124. package/adapter-react/generators/getLocale.cjs.js +35 -0
  125. package/adapter-react/generators/getLocale.esm.js +33 -0
  126. package/adapter-react/generators/getT.cjs.js +60 -0
  127. package/adapter-react/generators/getT.esm.js +58 -0
  128. package/adapter-react/generators/getTo.cjs.js +46 -0
  129. package/adapter-react/generators/getTo.esm.js +44 -0
  130. package/adapter-react/index.cjs.js +46 -0
  131. package/adapter-react/index.d.ts +44 -259
  132. package/adapter-react/index.esm.js +41 -0
  133. package/{formatRoutePathname.cjs.js → client/formatRoutePathname.cjs.js} +3 -0
  134. package/{formatRoutePathname.esm.js → client/formatRoutePathname.esm.js} +1 -1
  135. package/client/interpolateTo.cjs.js +46 -0
  136. package/client/interpolateTo.esm.js +41 -0
  137. package/client/routeHasDynamicPortion.cjs.js +8 -0
  138. package/client/routeHasDynamicPortion.esm.js +3 -0
  139. package/compiler/api.cjs.js +87 -0
  140. package/compiler/api.d.ts +3 -3
  141. package/compiler/api.esm.js +85 -0
  142. package/compiler/code/adapters.cjs.js +68 -0
  143. package/compiler/code/adapters.d.ts +3 -18
  144. package/compiler/code/adapters.esm.js +63 -0
  145. package/compiler/code/data-routes.cjs.js +218 -0
  146. package/compiler/code/data-routes.esm.js +215 -0
  147. package/compiler/code/data-translations.cjs.js +194 -0
  148. package/compiler/code/data-translations.esm.js +191 -0
  149. package/compiler/code/data.cjs.js +38 -0
  150. package/compiler/code/data.d.ts +2 -12
  151. package/compiler/code/data.esm.js +35 -0
  152. package/compiler/code/generate.cjs.js +70 -0
  153. package/compiler/code/generate.d.ts +2 -12
  154. package/compiler/code/generate.esm.js +68 -0
  155. package/compiler/code/tsCompile.cjs.js +78 -0
  156. package/compiler/code/tsCompile.esm.js +57 -0
  157. package/compiler/code/write.cjs.js +253 -0
  158. package/compiler/code/write.d.ts +2 -12
  159. package/compiler/code/write.esm.js +230 -0
  160. package/compiler/config.cjs.js +50 -0
  161. package/compiler/config.esm.js +47 -0
  162. package/compiler/createAdapter.cjs.js +46 -0
  163. package/compiler/createAdapter.d.ts +2 -2
  164. package/compiler/createAdapter.esm.js +43 -0
  165. package/compiler/helpers.cjs.js +42 -0
  166. package/compiler/helpers.esm.js +36 -0
  167. package/compiler/input/data-local.cjs.js +46 -0
  168. package/compiler/input/data-local.esm.js +44 -0
  169. package/compiler/input/data-remote.cjs.js +36 -0
  170. package/compiler/input/data-remote.esm.js +34 -0
  171. package/compiler/input/data.cjs.js +12 -0
  172. package/compiler/input/data.esm.js +10 -0
  173. package/compiler/input/write.cjs.js +21 -0
  174. package/compiler/input/write.esm.js +19 -0
  175. package/compiler/pluralisation.cjs.js +93 -0
  176. package/compiler/pluralisation.d.ts +2 -2
  177. package/compiler/pluralisation.esm.js +82 -0
  178. package/compiler/summary/data.cjs.js +39 -0
  179. package/compiler/summary/data.esm.js +36 -0
  180. package/compiler/summary/generate.cjs.js +32 -0
  181. package/compiler/summary/generate.esm.js +30 -0
  182. package/compiler/summary/write.cjs.js +27 -0
  183. package/compiler/summary/write.esm.js +25 -0
  184. package/compiler-worker.cjs.js +1 -13
  185. package/compiler-worker.esm.js +1 -13
  186. package/compiler.cjs.js +1 -13
  187. package/compiler.esm.js +1 -13
  188. package/index.cjs.js +5 -41
  189. package/index.esm.js +3 -43
  190. package/next.cjs.js +6 -367
  191. package/next.esm.js +3 -367
  192. package/package.json +9 -3
  193. package/api.cjs.js +0 -13842
  194. package/api.esm.js +0 -13817
@@ -0,0 +1,70 @@
1
+ 'use strict';
2
+
3
+ var createAdapter = require('../../compiler/createAdapter.cjs.js');
4
+
5
+ var o = createAdapter.createGenerator("js", (e)=>{
6
+ let { config: o } = e, l = `[${o.locales.map((e)=>`"${e}"`).join(", ")}]`;
7
+ return {
8
+ locales: {
9
+ name: "locales",
10
+ ext: "ts",
11
+ index: !0,
12
+ content: ()=>/* js */ `
13
+ export const locales = ${l} as const;
14
+
15
+ export default locales;
16
+ `
17
+ },
18
+ defaultLocale: {
19
+ name: "defaultLocale",
20
+ ext: "ts",
21
+ index: !0,
22
+ content: ()=>/* js */ `
23
+ import type { I18n } from "./types";
24
+
25
+ export const defaultLocale: I18n.Locale = "${o.defaultLocale}";
26
+
27
+ export default defaultLocale;
28
+ `
29
+ },
30
+ config: {
31
+ name: "config",
32
+ ext: "ts",
33
+ index: !0,
34
+ content: ()=>/* js */ `
35
+ import { locales } from "./locales";
36
+ import { defaultLocale } from "./defaultLocale";
37
+
38
+ /**
39
+ */
40
+ export const config = {
41
+ locales,
42
+ defaultLocale,
43
+ hideDefaultLocaleInUrl: ${o.hideDefaultLocaleInUrl},
44
+ }
45
+
46
+ export default config;
47
+ `
48
+ },
49
+ configCjs: {
50
+ name: "config.cjs",
51
+ ext: "js",
52
+ content: ()=>/* js */ `
53
+ const { locales } = require("./locales");
54
+ const { defaultLocale } = require("./defaultLocale");
55
+
56
+ const config = {
57
+ locales,
58
+ defaultLocale,
59
+ hideDefaultLocaleInUrl: ${o.hideDefaultLocaleInUrl},
60
+ };
61
+
62
+ exports.config = config;
63
+
64
+ module.exports = config;
65
+ `
66
+ }
67
+ };
68
+ });
69
+
70
+ module.exports = o;
@@ -0,0 +1,68 @@
1
+ import { createGenerator } from '../../compiler/createAdapter.esm.js';
2
+
3
+ var o = createGenerator("js", (e)=>{
4
+ let { config: o } = e, l = `[${o.locales.map((e)=>`"${e}"`).join(", ")}]`;
5
+ return {
6
+ locales: {
7
+ name: "locales",
8
+ ext: "ts",
9
+ index: !0,
10
+ content: ()=>/* js */ `
11
+ export const locales = ${l} as const;
12
+
13
+ export default locales;
14
+ `
15
+ },
16
+ defaultLocale: {
17
+ name: "defaultLocale",
18
+ ext: "ts",
19
+ index: !0,
20
+ content: ()=>/* js */ `
21
+ import type { I18n } from "./types";
22
+
23
+ export const defaultLocale: I18n.Locale = "${o.defaultLocale}";
24
+
25
+ export default defaultLocale;
26
+ `
27
+ },
28
+ config: {
29
+ name: "config",
30
+ ext: "ts",
31
+ index: !0,
32
+ content: ()=>/* js */ `
33
+ import { locales } from "./locales";
34
+ import { defaultLocale } from "./defaultLocale";
35
+
36
+ /**
37
+ */
38
+ export const config = {
39
+ locales,
40
+ defaultLocale,
41
+ hideDefaultLocaleInUrl: ${o.hideDefaultLocaleInUrl},
42
+ }
43
+
44
+ export default config;
45
+ `
46
+ },
47
+ configCjs: {
48
+ name: "config.cjs",
49
+ ext: "js",
50
+ content: ()=>/* js */ `
51
+ const { locales } = require("./locales");
52
+ const { defaultLocale } = require("./defaultLocale");
53
+
54
+ const config = {
55
+ locales,
56
+ defaultLocale,
57
+ hideDefaultLocaleInUrl: ${o.hideDefaultLocaleInUrl},
58
+ };
59
+
60
+ exports.config = config;
61
+
62
+ module.exports = config;
63
+ `
64
+ }
65
+ };
66
+ });
67
+
68
+ export { o as default };
@@ -0,0 +1,201 @@
1
+ 'use strict';
2
+
3
+ var createAdapter = require('../../compiler/createAdapter.cjs.js');
4
+ var helpers = require('../../compiler/helpers.cjs.js');
5
+
6
+ var t = createAdapter.createGenerator("js", (e)=>{
7
+ let { options: n } = e, { start: r, end: a } = n.translations.tokens.dynamicDelimiters;
8
+ return {
9
+ createT: {
10
+ name: "createT",
11
+ ext: "ts",
12
+ index: !0,
13
+ content: ()=>/* js */ `
14
+ import type { I18n } from "./types";
15
+ import { defaultLocale } from "./defaultLocale";
16
+ import { tInterpolateParams } from "./tInterpolateParams";
17
+
18
+ // An optional parameter allowEmptyStrings - true as default.
19
+ // If allowEmptyStrings parameter is marked as false,
20
+ // it should log an error when an empty string is attempted to be translated
21
+ // and return the namespace and key as result of the translation.
22
+ const allowEmptyStrings = true;
23
+
24
+ /**
25
+ * @see https://github.com/aralroca/next-translate/blob/master/src/transCore.tsx
26
+ */
27
+ export function createT<TNamespace extends I18n.TranslateNamespace>(
28
+ dictionaries: I18n.Dictionaries,
29
+ pluralRules: Intl.PluralRules,
30
+ locale: string = defaultLocale,
31
+ ) {
32
+ const interpolateUnknown = (
33
+ value: unknown,
34
+ query?: I18n.TranslationQuery | null,
35
+ ): typeof value => {
36
+ if (Array.isArray(value)) {
37
+ return value.map((val) => interpolateUnknown(val, query));
38
+ }
39
+ if (value instanceof Object) {
40
+ return objectInterpolation(
41
+ value as Record<string, unknown>,
42
+ query,
43
+ locale,
44
+ );
45
+ }
46
+ return interpolation(value as string, query, locale);
47
+ };
48
+
49
+ return <
50
+ TPath extends I18n.TranslationsAllPaths,
51
+ TReturn = I18n.TranslationAtPath<TPath>,
52
+ >(
53
+ path: TPath,
54
+ query?: I18n.TranslationQuery,
55
+ options?: I18n.TranslationOptions,
56
+ ): TReturn => {
57
+ const [namespace, key] = path.split("${n.translations.tokens.namespaceDelimiter}");
58
+ const dic = (namespace && dictionaries[namespace]) || {};
59
+ const pluralisedKey = getPluralisedKey(pluralRules, dic, key, query, options);
60
+ const dicValue = getDicValue(dic, pluralisedKey, query, options);
61
+ const value =
62
+ typeof dicValue === "object"
63
+ ? JSON.parse(JSON.stringify(dicValue))
64
+ : dicValue;
65
+
66
+ const empty =
67
+ typeof value === "undefined" ||
68
+ (typeof value === "object" && !Object.keys(value).length) ||
69
+ (value === "" && !allowEmptyStrings);
70
+
71
+ // no need to try interpolation
72
+ if (empty) {
73
+ return (query === "" ? "" : path) as unknown as TReturn;
74
+ }
75
+
76
+ // this can return an empty string if either value was already empty
77
+ // or it contained only an interpolation (e.g. "{{name}}") and the query param was empty
78
+ return interpolateUnknown(value, query) as TReturn;
79
+ };
80
+ }
81
+
82
+ // const Empty = new Symbol("Empty tranlsation message")
83
+
84
+ /**
85
+ * Get value from key (allow nested keys as parent.children)
86
+ */
87
+ function getDicValue(
88
+ dic: I18n.TranslationsDictionaryLoose,
89
+ key: string = "",
90
+ query?: I18n.TranslationQuery,
91
+ options?: I18n.TranslationOptions,
92
+ ): unknown | undefined {
93
+ const keySeparator = ".";
94
+ const keyParts = keySeparator ? key.split(keySeparator) : [key];
95
+ const returnObjects =
96
+ query === "obj" ||
97
+ options === "obj" ||
98
+ (options instanceof Object && options.returnObjects);
99
+
100
+ if (key === keySeparator && returnObjects) return dic;
101
+
102
+ const value: string | object = keyParts.reduce(
103
+ (val: I18n.TranslationsDictionaryLoose | string, key: string) => {
104
+ if (typeof val === "string") {
105
+ return {};
106
+ }
107
+
108
+ const res = val[key as keyof typeof val];
109
+
110
+ // pass all truthy values or (empty) strings
111
+ return res || (typeof res === "string" ? res : {});
112
+ },
113
+ dic,
114
+ );
115
+
116
+ if (
117
+ typeof value === "string" ||
118
+ (returnObjects && Object.keys(value).length > 0)
119
+ ) {
120
+ return value;
121
+ }
122
+
123
+ if (Array.isArray(value) && returnObjects) return value;
124
+ return undefined;
125
+ }
126
+
127
+ /**
128
+ * Control plural keys depending the {{count}} variable
129
+ */
130
+ function getPluralisedKey(
131
+ pluralRules: Intl.PluralRules,
132
+ dic: I18n.TranslationsDictionaryLoose,
133
+ key: string,
134
+ query?: I18n.TranslationQuery | null,
135
+ options?: I18n.TranslationOptions,
136
+ ): string {
137
+ const count = query instanceof Object ? query["count"] : null;
138
+
139
+ if (!query || typeof count !== "number") return key;
140
+
141
+ const numKey = \`\${key}_\${count}\`;
142
+ if (getDicValue(dic, numKey, query, options) !== undefined) return numKey;
143
+
144
+ const pluralKey = \`\${key}_\${pluralRules.select(count)}\`;
145
+ if (getDicValue(dic, pluralKey, query, options) !== undefined) {
146
+ return pluralKey;
147
+ }
148
+
149
+ const nestedNumKey = \`\${key}.\${count}\`;
150
+ if (getDicValue(dic, nestedNumKey, query, options) !== undefined)
151
+ return nestedNumKey;
152
+
153
+ const nestedKey = \`\${key}.\${pluralRules.select(count)}\`;
154
+ if (getDicValue(dic, nestedKey, query, options) !== undefined)
155
+ return nestedKey;
156
+
157
+ return key;
158
+ }
159
+
160
+ /**
161
+ * Replace {{variables}} to query values
162
+ */
163
+ function interpolation(
164
+ text?: string,
165
+ query?: I18n.TranslationQuery | null,
166
+ _locale?: string | undefined,
167
+ ): string {
168
+ if (!text || !query || query === "obj") return text || "";
169
+
170
+ return tInterpolateParams(text, query);
171
+ // return Object.keys(query).reduce((all, key) => {
172
+ // const regex = new RegExp(\`${helpers.escapeEachChar(r)}\\s*\${key}(?:[\\s,]+([\\w-]*))?\\s*\$${helpers.escapeEachChar(a)}\`, "gm");
173
+ // return all.replace(regex, (_match) => query[key as keyof typeof query] as string);
174
+ // }, text);
175
+ }
176
+
177
+ function objectInterpolation(
178
+ obj: Record<string, string | unknown>,
179
+ query?: I18n.TranslationQuery | null,
180
+ locale?: string,
181
+ ) {
182
+ if (!query || Object.keys(query).length === 0) return obj;
183
+ Object.keys(obj).forEach((key) => {
184
+ if (obj[key] instanceof Object)
185
+ objectInterpolation(
186
+ obj[key] as Record<string, string | unknown>,
187
+ query,
188
+ locale,
189
+ );
190
+ if (typeof obj[key] === "string")
191
+ obj[key] = interpolation(obj[key] as string, query, locale);
192
+ });
193
+
194
+ return obj;
195
+ }
196
+ `
197
+ }
198
+ };
199
+ });
200
+
201
+ module.exports = t;
@@ -0,0 +1,199 @@
1
+ import { createGenerator } from '../../compiler/createAdapter.esm.js';
2
+ import { escapeEachChar } from '../../compiler/helpers.esm.js';
3
+
4
+ var t = createGenerator("js", (e)=>{
5
+ let { options: n } = e, { start: r, end: a } = n.translations.tokens.dynamicDelimiters;
6
+ return {
7
+ createT: {
8
+ name: "createT",
9
+ ext: "ts",
10
+ index: !0,
11
+ content: ()=>/* js */ `
12
+ import type { I18n } from "./types";
13
+ import { defaultLocale } from "./defaultLocale";
14
+ import { tInterpolateParams } from "./tInterpolateParams";
15
+
16
+ // An optional parameter allowEmptyStrings - true as default.
17
+ // If allowEmptyStrings parameter is marked as false,
18
+ // it should log an error when an empty string is attempted to be translated
19
+ // and return the namespace and key as result of the translation.
20
+ const allowEmptyStrings = true;
21
+
22
+ /**
23
+ * @see https://github.com/aralroca/next-translate/blob/master/src/transCore.tsx
24
+ */
25
+ export function createT<TNamespace extends I18n.TranslateNamespace>(
26
+ dictionaries: I18n.Dictionaries,
27
+ pluralRules: Intl.PluralRules,
28
+ locale: string = defaultLocale,
29
+ ) {
30
+ const interpolateUnknown = (
31
+ value: unknown,
32
+ query?: I18n.TranslationQuery | null,
33
+ ): typeof value => {
34
+ if (Array.isArray(value)) {
35
+ return value.map((val) => interpolateUnknown(val, query));
36
+ }
37
+ if (value instanceof Object) {
38
+ return objectInterpolation(
39
+ value as Record<string, unknown>,
40
+ query,
41
+ locale,
42
+ );
43
+ }
44
+ return interpolation(value as string, query, locale);
45
+ };
46
+
47
+ return <
48
+ TPath extends I18n.TranslationsAllPaths,
49
+ TReturn = I18n.TranslationAtPath<TPath>,
50
+ >(
51
+ path: TPath,
52
+ query?: I18n.TranslationQuery,
53
+ options?: I18n.TranslationOptions,
54
+ ): TReturn => {
55
+ const [namespace, key] = path.split("${n.translations.tokens.namespaceDelimiter}");
56
+ const dic = (namespace && dictionaries[namespace]) || {};
57
+ const pluralisedKey = getPluralisedKey(pluralRules, dic, key, query, options);
58
+ const dicValue = getDicValue(dic, pluralisedKey, query, options);
59
+ const value =
60
+ typeof dicValue === "object"
61
+ ? JSON.parse(JSON.stringify(dicValue))
62
+ : dicValue;
63
+
64
+ const empty =
65
+ typeof value === "undefined" ||
66
+ (typeof value === "object" && !Object.keys(value).length) ||
67
+ (value === "" && !allowEmptyStrings);
68
+
69
+ // no need to try interpolation
70
+ if (empty) {
71
+ return (query === "" ? "" : path) as unknown as TReturn;
72
+ }
73
+
74
+ // this can return an empty string if either value was already empty
75
+ // or it contained only an interpolation (e.g. "{{name}}") and the query param was empty
76
+ return interpolateUnknown(value, query) as TReturn;
77
+ };
78
+ }
79
+
80
+ // const Empty = new Symbol("Empty tranlsation message")
81
+
82
+ /**
83
+ * Get value from key (allow nested keys as parent.children)
84
+ */
85
+ function getDicValue(
86
+ dic: I18n.TranslationsDictionaryLoose,
87
+ key: string = "",
88
+ query?: I18n.TranslationQuery,
89
+ options?: I18n.TranslationOptions,
90
+ ): unknown | undefined {
91
+ const keySeparator = ".";
92
+ const keyParts = keySeparator ? key.split(keySeparator) : [key];
93
+ const returnObjects =
94
+ query === "obj" ||
95
+ options === "obj" ||
96
+ (options instanceof Object && options.returnObjects);
97
+
98
+ if (key === keySeparator && returnObjects) return dic;
99
+
100
+ const value: string | object = keyParts.reduce(
101
+ (val: I18n.TranslationsDictionaryLoose | string, key: string) => {
102
+ if (typeof val === "string") {
103
+ return {};
104
+ }
105
+
106
+ const res = val[key as keyof typeof val];
107
+
108
+ // pass all truthy values or (empty) strings
109
+ return res || (typeof res === "string" ? res : {});
110
+ },
111
+ dic,
112
+ );
113
+
114
+ if (
115
+ typeof value === "string" ||
116
+ (returnObjects && Object.keys(value).length > 0)
117
+ ) {
118
+ return value;
119
+ }
120
+
121
+ if (Array.isArray(value) && returnObjects) return value;
122
+ return undefined;
123
+ }
124
+
125
+ /**
126
+ * Control plural keys depending the {{count}} variable
127
+ */
128
+ function getPluralisedKey(
129
+ pluralRules: Intl.PluralRules,
130
+ dic: I18n.TranslationsDictionaryLoose,
131
+ key: string,
132
+ query?: I18n.TranslationQuery | null,
133
+ options?: I18n.TranslationOptions,
134
+ ): string {
135
+ const count = query instanceof Object ? query["count"] : null;
136
+
137
+ if (!query || typeof count !== "number") return key;
138
+
139
+ const numKey = \`\${key}_\${count}\`;
140
+ if (getDicValue(dic, numKey, query, options) !== undefined) return numKey;
141
+
142
+ const pluralKey = \`\${key}_\${pluralRules.select(count)}\`;
143
+ if (getDicValue(dic, pluralKey, query, options) !== undefined) {
144
+ return pluralKey;
145
+ }
146
+
147
+ const nestedNumKey = \`\${key}.\${count}\`;
148
+ if (getDicValue(dic, nestedNumKey, query, options) !== undefined)
149
+ return nestedNumKey;
150
+
151
+ const nestedKey = \`\${key}.\${pluralRules.select(count)}\`;
152
+ if (getDicValue(dic, nestedKey, query, options) !== undefined)
153
+ return nestedKey;
154
+
155
+ return key;
156
+ }
157
+
158
+ /**
159
+ * Replace {{variables}} to query values
160
+ */
161
+ function interpolation(
162
+ text?: string,
163
+ query?: I18n.TranslationQuery | null,
164
+ _locale?: string | undefined,
165
+ ): string {
166
+ if (!text || !query || query === "obj") return text || "";
167
+
168
+ return tInterpolateParams(text, query);
169
+ // return Object.keys(query).reduce((all, key) => {
170
+ // const regex = new RegExp(\`${escapeEachChar(r)}\\s*\${key}(?:[\\s,]+([\\w-]*))?\\s*\$${escapeEachChar(a)}\`, "gm");
171
+ // return all.replace(regex, (_match) => query[key as keyof typeof query] as string);
172
+ // }, text);
173
+ }
174
+
175
+ function objectInterpolation(
176
+ obj: Record<string, string | unknown>,
177
+ query?: I18n.TranslationQuery | null,
178
+ locale?: string,
179
+ ) {
180
+ if (!query || Object.keys(query).length === 0) return obj;
181
+ Object.keys(obj).forEach((key) => {
182
+ if (obj[key] instanceof Object)
183
+ objectInterpolation(
184
+ obj[key] as Record<string, string | unknown>,
185
+ query,
186
+ locale,
187
+ );
188
+ if (typeof obj[key] === "string")
189
+ obj[key] = interpolation(obj[key] as string, query, locale);
190
+ });
191
+
192
+ return obj;
193
+ }
194
+ `
195
+ }
196
+ };
197
+ });
198
+
199
+ export { t as default };
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ var createAdapter = require('../../compiler/createAdapter.cjs.js');
4
+
5
+ var e = createAdapter.createGenerator("js", (t)=>({
6
+ defaultI18nMetadata: {
7
+ // dir: createGenerator.dirs.internal,
8
+ name: "defaultI18nMetadata",
9
+ ext: "ts",
10
+ content: ()=>/* js */ `
11
+ import type { I18n } from "./types";
12
+
13
+ /**
14
+ * @internal
15
+ */
16
+ export const defaultI18nMetadata: I18n.Metadata = {
17
+ canonical: "",
18
+ alternates: {}
19
+ }
20
+ `
21
+ }
22
+ }));
23
+
24
+ module.exports = e;
@@ -0,0 +1,22 @@
1
+ import { createGenerator } from '../../compiler/createAdapter.esm.js';
2
+
3
+ var e = createGenerator("js", (t)=>({
4
+ defaultI18nMetadata: {
5
+ // dir: createGenerator.dirs.internal,
6
+ name: "defaultI18nMetadata",
7
+ ext: "ts",
8
+ content: ()=>/* js */ `
9
+ import type { I18n } from "./types";
10
+
11
+ /**
12
+ * @internal
13
+ */
14
+ export const defaultI18nMetadata: I18n.Metadata = {
15
+ canonical: "",
16
+ alternates: {}
17
+ }
18
+ `
19
+ }
20
+ }));
21
+
22
+ export { e as default };
@@ -0,0 +1,76 @@
1
+ 'use strict';
2
+
3
+ var createAdapter = require('../../compiler/createAdapter.cjs.js');
4
+
5
+ var a = createAdapter.createGenerator("js", (e)=>{
6
+ let { config: a } = e;
7
+ return {
8
+ formatTo: {
9
+ name: "formatTo",
10
+ ext: "ts",
11
+ content: ()=>/* js */ `
12
+ import { defaultLocale } from "./defaultLocale";
13
+
14
+ /**
15
+ * @internal
16
+ */
17
+ export function formatTo(
18
+ locale: string | undefined,
19
+ pathname: string,
20
+ params?: object,
21
+ ) {
22
+ locale = locale || defaultLocale;
23
+ if (process.env["NODE_ENV"] === "development") {
24
+ if (params) {
25
+ pathname.replace(/\\[(.*?)\\]/g, (_, dynamicKey) => {
26
+ const key = dynamicKey as Extract<keyof typeof params, string>;
27
+
28
+ if (!(key in params)) {
29
+ console.warn(
30
+ "[@koine/i18n]::formatTo, using '" +
31
+ pathname +
32
+ "' without param '" +
33
+ key +
34
+ "'",
35
+ { params }
36
+ );
37
+ }
38
+
39
+ if (!["string", "number"].includes(typeof params[key])) {
40
+ console.warn(
41
+ "[@koine/i18n]::formatTo, using '" +
42
+ pathname +
43
+ "' with unserializable param '" +
44
+ key +
45
+ "' (type '" +
46
+ Object.prototype.toString.call((params[key])).slice(8, -1) +
47
+ "')",
48
+ );
49
+ }
50
+ return "";
51
+ });
52
+ }
53
+ }
54
+
55
+ if (params) {
56
+ pathname = pathname.replace(
57
+ /\\[(.*?)\\]/g,
58
+ (_, key) =>
59
+ params[key as keyof typeof params] + "",
60
+ )
61
+ }
62
+ ${a.hideDefaultLocaleInUrl ? `
63
+ if (locale !== defaultLocale) {
64
+ return "/" + locale + (pathname === "/" ? "" : pathname);
65
+ }
66
+ ` : ""}
67
+ return pathname;
68
+ }
69
+
70
+ // export default formatTo;
71
+ `
72
+ }
73
+ };
74
+ });
75
+
76
+ module.exports = a;