generaltranslation 8.2.12 → 8.2.14

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 (222) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/{ApiError-IYfaOR30.mjs → ApiError-0DxxIHLp.mjs} +1 -1
  3. package/dist/{ApiError-CZ45tkW6.cjs.map → ApiError-0DxxIHLp.mjs.map} +1 -1
  4. package/dist/{ApiError-CZ45tkW6.cjs → ApiError-D-IBuHj6.cjs} +1 -1
  5. package/dist/{ApiError-IYfaOR30.mjs.map → ApiError-D-IBuHj6.cjs.map} +1 -1
  6. package/dist/LocaleConfig.d.ts +1 -0
  7. package/dist/LocaleConfig.js +1 -0
  8. package/dist/backwards-compatability/dataConversion.d.ts +23 -0
  9. package/dist/backwards-compatability/dataConversion.js +207 -0
  10. package/dist/backwards-compatability/oldHashJsxChildren.d.ts +23 -0
  11. package/dist/backwards-compatability/oldHashJsxChildren.js +79 -0
  12. package/dist/backwards-compatability/oldTypes.d.ts +21 -0
  13. package/dist/backwards-compatability/oldTypes.js +1 -0
  14. package/dist/backwards-compatability/typeChecking.d.ts +21 -0
  15. package/dist/backwards-compatability/typeChecking.js +61 -0
  16. package/dist/{base64-2fu94Klt.cjs → base64-C6BHySOc.cjs} +17 -1
  17. package/dist/base64-C6BHySOc.cjs.map +1 -0
  18. package/dist/{base64-DH0STixb.mjs → base64-CWITCfhU.mjs} +6 -2
  19. package/dist/base64-CWITCfhU.mjs.map +1 -0
  20. package/dist/core.cjs +9 -8
  21. package/dist/core.d.cts +1 -2
  22. package/dist/core.d.mts +1 -2
  23. package/dist/core.d.ts +1 -0
  24. package/dist/core.js +1 -0
  25. package/dist/core.mjs +2 -2
  26. package/dist/derive/condenseVars.d.ts +7 -0
  27. package/dist/derive/condenseVars.js +30 -0
  28. package/dist/derive/declareVar.d.ts +26 -0
  29. package/dist/derive/declareVar.js +38 -0
  30. package/dist/derive/decodeVars.d.ts +9 -0
  31. package/dist/derive/decodeVars.js +52 -0
  32. package/dist/derive/derive.d.ts +53 -0
  33. package/dist/derive/derive.js +55 -0
  34. package/dist/derive/extractVars.d.ts +9 -0
  35. package/dist/derive/extractVars.js +34 -0
  36. package/dist/derive/index.d.ts +7 -0
  37. package/dist/derive/index.js +7 -0
  38. package/dist/derive/indexVars.d.ts +6 -0
  39. package/dist/derive/indexVars.js +52 -0
  40. package/dist/derive/utils/constants.d.ts +2 -0
  41. package/dist/derive/utils/constants.js +2 -0
  42. package/dist/derive/utils/regex.d.ts +2 -0
  43. package/dist/derive/utils/regex.js +4 -0
  44. package/dist/derive/utils/sanitizeVar.d.ts +12 -0
  45. package/dist/derive/utils/sanitizeVar.js +38 -0
  46. package/dist/derive/utils/traverseHelpers.d.ts +4 -0
  47. package/dist/derive/utils/traverseHelpers.js +22 -0
  48. package/dist/derive/utils/traverseIcu.d.ts +20 -0
  49. package/dist/derive/utils/traverseIcu.js +49 -0
  50. package/dist/derive/utils/types.d.ts +23 -0
  51. package/dist/derive/utils/types.js +1 -0
  52. package/dist/errors/ApiError.d.ts +7 -0
  53. package/dist/errors/ApiError.js +33 -0
  54. package/dist/errors.cjs +1 -1
  55. package/dist/errors.d.ts +1 -0
  56. package/dist/errors.js +1 -0
  57. package/dist/errors.mjs +1 -1
  58. package/dist/id/hashSource.d.ts +25 -0
  59. package/dist/id/hashSource.js +98 -0
  60. package/dist/id/hashTemplate.d.ts +4 -0
  61. package/dist/id/hashTemplate.js +6 -0
  62. package/dist/id/types.d.ts +7 -0
  63. package/dist/id/types.js +1 -0
  64. package/dist/{id-CyiXsQrY.cjs → id-C2orn1MA.cjs} +2 -2
  65. package/dist/{id-CyiXsQrY.cjs.map → id-C2orn1MA.cjs.map} +1 -1
  66. package/dist/{id-DbD7K-HL.mjs → id-DEaFhGqX.mjs} +2 -2
  67. package/dist/{id-DbD7K-HL.mjs.map → id-DEaFhGqX.mjs.map} +1 -1
  68. package/dist/id.cjs +1 -1
  69. package/dist/id.d.cts +1 -1
  70. package/dist/id.d.mts +1 -1
  71. package/dist/id.d.ts +3 -0
  72. package/dist/id.js +3 -0
  73. package/dist/id.mjs +1 -1
  74. package/dist/index.cjs +368 -385
  75. package/dist/index.cjs.map +1 -1
  76. package/dist/index.d.cts +4 -242
  77. package/dist/index.d.mts +4 -242
  78. package/dist/index.d.ts +786 -0
  79. package/dist/index.js +1345 -0
  80. package/dist/index.mjs +215 -352
  81. package/dist/index.mjs.map +1 -1
  82. package/dist/internal.cjs +7 -7
  83. package/dist/internal.cjs.map +1 -1
  84. package/dist/internal.d.cts +7 -6
  85. package/dist/internal.d.mts +7 -6
  86. package/dist/internal.d.ts +19 -0
  87. package/dist/internal.js +17 -0
  88. package/dist/internal.mjs +5 -5
  89. package/dist/internal.mjs.map +1 -1
  90. package/dist/{isVariable-B08mggBy.cjs → isVariable-Ba1gLXdB.cjs} +1 -1
  91. package/dist/{isVariable-B08mggBy.cjs.map → isVariable-Ba1gLXdB.cjs.map} +1 -1
  92. package/dist/{isVariable-CYsKFHvR.mjs → isVariable-fAKEB7gF.mjs} +1 -1
  93. package/dist/{isVariable-CYsKFHvR.mjs.map → isVariable-fAKEB7gF.mjs.map} +1 -1
  94. package/dist/locales/getPluralForm.d.ts +9 -0
  95. package/dist/locales/getPluralForm.js +63 -0
  96. package/dist/logging/errors.d.ts +10 -0
  97. package/dist/logging/errors.js +29 -0
  98. package/dist/logging/logger.d.ts +118 -0
  99. package/dist/logging/logger.js +256 -0
  100. package/dist/projects/getProjectData.d.ts +1 -0
  101. package/dist/projects/getProjectData.js +87 -0
  102. package/dist/settings/plurals.d.ts +3 -0
  103. package/dist/settings/plurals.js +14 -0
  104. package/dist/settings/settings.d.ts +2 -0
  105. package/dist/settings/settings.js +2 -0
  106. package/dist/settings/settingsUrls.d.ts +3 -0
  107. package/dist/settings/settingsUrls.js +3 -0
  108. package/dist/translate/api.d.ts +1 -0
  109. package/dist/translate/api.js +1 -0
  110. package/dist/translate/awaitJobs.d.ts +19 -0
  111. package/dist/translate/awaitJobs.js +112 -0
  112. package/dist/translate/checkJobStatus.d.ts +8 -0
  113. package/dist/translate/checkJobStatus.js +55 -0
  114. package/dist/translate/createBranch.d.ts +10 -0
  115. package/dist/translate/createBranch.js +53 -0
  116. package/dist/translate/createTag.d.ts +19 -0
  117. package/dist/translate/createTag.js +67 -0
  118. package/dist/translate/downloadFileBatch.d.ts +1 -0
  119. package/dist/translate/downloadFileBatch.js +77 -0
  120. package/dist/translate/enqueueFiles.d.ts +8 -0
  121. package/dist/translate/enqueueFiles.js +96 -0
  122. package/dist/translate/getOrphanedFiles.d.ts +8 -0
  123. package/dist/translate/getOrphanedFiles.js +97 -0
  124. package/dist/translate/processFileMoves.d.ts +25 -0
  125. package/dist/translate/processFileMoves.js +90 -0
  126. package/dist/translate/publishFiles.d.ts +17 -0
  127. package/dist/translate/publishFiles.js +56 -0
  128. package/dist/translate/queryBranchData.d.ts +3 -0
  129. package/dist/translate/queryBranchData.js +53 -0
  130. package/dist/translate/queryFileData.d.ts +42 -0
  131. package/dist/translate/queryFileData.js +71 -0
  132. package/dist/translate/querySourceFile.d.ts +1 -0
  133. package/dist/translate/querySourceFile.js +67 -0
  134. package/dist/translate/setupProject.d.ts +11 -0
  135. package/dist/translate/setupProject.js +63 -0
  136. package/dist/translate/submitUserEditDiffs.d.ts +12 -0
  137. package/dist/translate/submitUserEditDiffs.js +68 -0
  138. package/dist/translate/translateMany.d.ts +1 -0
  139. package/dist/translate/translateMany.js +103 -0
  140. package/dist/translate/uploadSourceFiles.d.ts +1 -0
  141. package/dist/translate/uploadSourceFiles.js +87 -0
  142. package/dist/translate/uploadTranslations.d.ts +1 -0
  143. package/dist/translate/uploadTranslations.js +101 -0
  144. package/dist/translate/utils/apiRequest.d.ts +1 -0
  145. package/dist/translate/utils/apiRequest.js +130 -0
  146. package/dist/translate/utils/batch.d.ts +52 -0
  147. package/dist/translate/utils/batch.js +126 -0
  148. package/dist/translate/utils/fetchWithTimeout.d.ts +1 -0
  149. package/dist/translate/utils/fetchWithTimeout.js +93 -0
  150. package/dist/translate/utils/generateRequestHeaders.d.ts +2 -0
  151. package/dist/translate/utils/generateRequestHeaders.js +26 -0
  152. package/dist/translate/utils/handleFetchError.d.ts +1 -0
  153. package/dist/translate/utils/handleFetchError.js +12 -0
  154. package/dist/translate/utils/validateFileFormatTransform.d.ts +16 -0
  155. package/dist/translate/utils/validateFileFormatTransform.js +29 -0
  156. package/dist/translate/utils/validateResponse.d.ts +1 -0
  157. package/dist/translate/utils/validateResponse.js +72 -0
  158. package/dist/{types-V5rqd1WR.d.cts → types-73XFwmhH.d.mts} +39 -115
  159. package/dist/{types-BsGsgJWF.d.mts → types-YrrGRHBP.d.cts} +39 -115
  160. package/dist/types-dir/api/branch.d.ts +10 -0
  161. package/dist/types-dir/api/branch.js +1 -0
  162. package/dist/types-dir/api/checkFileTranslations.d.ts +37 -0
  163. package/dist/types-dir/api/checkFileTranslations.js +1 -0
  164. package/dist/types-dir/api/downloadFile.d.ts +3 -0
  165. package/dist/types-dir/api/downloadFile.js +1 -0
  166. package/dist/types-dir/api/downloadFileBatch.d.ts +35 -0
  167. package/dist/types-dir/api/downloadFileBatch.js +1 -0
  168. package/dist/types-dir/api/enqueueEntries.d.ts +21 -0
  169. package/dist/types-dir/api/enqueueEntries.js +1 -0
  170. package/dist/types-dir/api/enqueueFiles.d.ts +70 -0
  171. package/dist/types-dir/api/enqueueFiles.js +1 -0
  172. package/dist/types-dir/api/entry.d.ts +39 -0
  173. package/dist/types-dir/api/entry.js +1 -0
  174. package/dist/types-dir/api/fetchTranslations.d.ts +12 -0
  175. package/dist/types-dir/api/fetchTranslations.js +1 -0
  176. package/dist/types-dir/api/file.d.ts +57 -0
  177. package/dist/types-dir/api/file.js +1 -0
  178. package/dist/types-dir/api/json.d.ts +5 -0
  179. package/dist/types-dir/api/json.js +1 -0
  180. package/dist/types-dir/api/project.d.ts +7 -0
  181. package/dist/types-dir/api/project.js +1 -0
  182. package/dist/types-dir/api/translate.d.ts +34 -0
  183. package/dist/types-dir/api/translate.js +1 -0
  184. package/dist/types-dir/api/translateMany.d.ts +5 -0
  185. package/dist/types-dir/api/translateMany.js +1 -0
  186. package/dist/types-dir/api/translationStatus.d.ts +9 -0
  187. package/dist/types-dir/api/translationStatus.js +1 -0
  188. package/dist/types-dir/api/uploadFiles.d.ts +44 -0
  189. package/dist/types-dir/api/uploadFiles.js +1 -0
  190. package/dist/types-dir/transformations.d.ts +8 -0
  191. package/dist/types-dir/transformations.js +1 -0
  192. package/dist/types.cjs +7 -16
  193. package/dist/types.d.cts +2 -2
  194. package/dist/types.d.mts +2 -2
  195. package/dist/types.d.ts +127 -0
  196. package/dist/types.js +1 -0
  197. package/dist/types.mjs +1 -15
  198. package/dist/utils/base64.d.ts +2 -0
  199. package/dist/utils/base64.js +28 -0
  200. package/dist/utils/isSupportedFileFormatTransform.d.ts +8 -0
  201. package/dist/utils/isSupportedFileFormatTransform.js +26 -0
  202. package/dist/utils/isVariable.d.ts +2 -0
  203. package/dist/utils/isVariable.js +22 -0
  204. package/dist/utils/minify.d.ts +2 -0
  205. package/dist/utils/minify.js +10 -0
  206. package/dist/utils/stableStringify.d.ts +1 -0
  207. package/dist/utils/stableStringify.js +40 -0
  208. package/package.json +5 -4
  209. package/dist/IntlCache-CAW8tKhd.cjs +0 -212
  210. package/dist/IntlCache-CAW8tKhd.cjs.map +0 -1
  211. package/dist/IntlCache-WZk0rKvj.mjs +0 -195
  212. package/dist/IntlCache-WZk0rKvj.mjs.map +0 -1
  213. package/dist/base64-2fu94Klt.cjs.map +0 -1
  214. package/dist/base64-DH0STixb.mjs.map +0 -1
  215. package/dist/core-7RJsqw6c.mjs +0 -1498
  216. package/dist/core-7RJsqw6c.mjs.map +0 -1
  217. package/dist/core-CGhlXrrK.d.mts +0 -209
  218. package/dist/core-CST4J4WU.cjs +0 -1677
  219. package/dist/core-CST4J4WU.cjs.map +0 -1
  220. package/dist/core-D-E8qhLn.d.cts +0 -209
  221. package/dist/types.cjs.map +0 -1
  222. package/dist/types.mjs.map +0 -1
@@ -0,0 +1,30 @@
1
+ import { TYPE, } from '@formatjs/icu-messageformat-parser/types.js';
2
+ import { printAST } from '@formatjs/icu-messageformat-parser/printer.js';
3
+ import { traverseIcu } from './utils/traverseIcu';
4
+ import { VAR_IDENTIFIER } from './utils/constants';
5
+ import { isGTIndexedSelectElement } from './utils/traverseHelpers';
6
+ /**
7
+ * Given an indexed ICU string, condenses any select to an argument
8
+ * indexVars('Hello {_gt_1, select, other {World}}') => 'Hello {_gt_1}'
9
+ * @param {string} icuString - The ICU string to condense.
10
+ * @returns {string} The condensed ICU string.
11
+ */
12
+ export function condenseVars(icuString) {
13
+ // Check if the string contains _gt_
14
+ if (!icuString.includes(VAR_IDENTIFIER)) {
15
+ return icuString;
16
+ }
17
+ // Replace with argument
18
+ function visitor(child) {
19
+ child.type = TYPE.argument;
20
+ Reflect.deleteProperty(child, 'options');
21
+ }
22
+ var ast = traverseIcu({
23
+ icuString: icuString,
24
+ shouldVisit: isGTIndexedSelectElement,
25
+ visitor: visitor,
26
+ options: { recurseIntoVisited: false },
27
+ });
28
+ // Serialize
29
+ return printAST(ast);
30
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Mark as a non-translatable string. Use within a derive() call to mark content as not derivable (e.g., not possible to statically analyze).
3
+ *
4
+ * @example
5
+ * function nonDerivableFunction() {
6
+ * return Math.random();
7
+ * }
8
+ *
9
+ * function derivableFunction() {
10
+ * if (condition) {
11
+ * return declareVar(nonDerivableFunction())
12
+ * }
13
+ * return 'John Doe';
14
+ * }
15
+ *
16
+ * const gt = useGT();
17
+ * gt(`My name is ${derive(derivableFunction())}`);
18
+ *
19
+ * @param {string | number | boolean | null | undefined} variable - The variable to sanitize.
20
+ * @param {Object} [options] - The options for the sanitization.
21
+ * @param {string} [options.$name] - The name of the variable.
22
+ * @returns {string} The sanitized value.
23
+ */
24
+ export declare function declareVar(variable: string | number | boolean | null | undefined, options?: {
25
+ $name?: string;
26
+ }): string;
@@ -0,0 +1,38 @@
1
+ import { VAR_IDENTIFIER, VAR_NAME_IDENTIFIER } from './utils/constants';
2
+ import { sanitizeVar } from './utils/sanitizeVar';
3
+ /**
4
+ * Mark as a non-translatable string. Use within a derive() call to mark content as not derivable (e.g., not possible to statically analyze).
5
+ *
6
+ * @example
7
+ * function nonDerivableFunction() {
8
+ * return Math.random();
9
+ * }
10
+ *
11
+ * function derivableFunction() {
12
+ * if (condition) {
13
+ * return declareVar(nonDerivableFunction())
14
+ * }
15
+ * return 'John Doe';
16
+ * }
17
+ *
18
+ * const gt = useGT();
19
+ * gt(`My name is ${derive(derivableFunction())}`);
20
+ *
21
+ * @param {string | number | boolean | null | undefined} variable - The variable to sanitize.
22
+ * @param {Object} [options] - The options for the sanitization.
23
+ * @param {string} [options.$name] - The name of the variable.
24
+ * @returns {string} The sanitized value.
25
+ */
26
+ export function declareVar(variable, options) {
27
+ // Variable section.
28
+ var sanitizedVariable = sanitizeVar(String(variable !== null && variable !== void 0 ? variable : ''));
29
+ var variableSection = " other {".concat(sanitizedVariable, "}");
30
+ // Name section.
31
+ var nameSection = '';
32
+ if (options === null || options === void 0 ? void 0 : options.$name) {
33
+ var sanitizedName = sanitizeVar(options.$name);
34
+ nameSection = " ".concat(VAR_NAME_IDENTIFIER, " {").concat(sanitizedName, "}");
35
+ }
36
+ // interpolate
37
+ return "{".concat(VAR_IDENTIFIER, ", select,").concat(variableSection).concat(nameSection, "}");
38
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Given an encoded ICU string, interpolate only _gt_ variables that have been marked with declareVar()
3
+ * @example
4
+ * const encodedIcu = "Hi" + declareVar("Brian") + ", my name is {name}"
5
+ * // 'Hi {_gt_, select, other {Brian}}, my name is {name}'
6
+ * decodeVars(encodedIcu)
7
+ * // 'Hi Brian, my name is {name}'
8
+ */
9
+ export declare function decodeVars(icuString: string): string;
@@ -0,0 +1,52 @@
1
+ import { traverseIcu } from './utils/traverseIcu';
2
+ import { VAR_IDENTIFIER } from './utils/constants';
3
+ import { isGTUnindexedSelectElement } from './utils/traverseHelpers';
4
+ /**
5
+ * Given an encoded ICU string, interpolate only _gt_ variables that have been marked with declareVar()
6
+ * @example
7
+ * const encodedIcu = "Hi" + declareVar("Brian") + ", my name is {name}"
8
+ * // 'Hi {_gt_, select, other {Brian}}, my name is {name}'
9
+ * decodeVars(encodedIcu)
10
+ * // 'Hi Brian, my name is {name}'
11
+ */
12
+ export function decodeVars(icuString) {
13
+ // Check if the string contains _gt_
14
+ if (!icuString.includes(VAR_IDENTIFIER)) {
15
+ return icuString;
16
+ }
17
+ // Record the location of the variable
18
+ var variableLocations = [];
19
+ function visitor(child) {
20
+ var _a, _b, _c, _d;
21
+ variableLocations.push({
22
+ start: (_b = (_a = child.location) === null || _a === void 0 ? void 0 : _a.start.offset) !== null && _b !== void 0 ? _b : 0,
23
+ end: (_d = (_c = child.location) === null || _c === void 0 ? void 0 : _c.end.offset) !== null && _d !== void 0 ? _d : 0,
24
+ value: child.options.other.value.length > 0
25
+ ? child.options.other.value[0].value
26
+ : '',
27
+ });
28
+ }
29
+ // Find all variable identifiers.
30
+ traverseIcu({
31
+ icuString: icuString,
32
+ shouldVisit: isGTUnindexedSelectElement,
33
+ visitor: visitor,
34
+ options: {
35
+ recurseIntoVisited: false,
36
+ captureLocation: true,
37
+ },
38
+ });
39
+ // Construct output string
40
+ var previousIndex = 0;
41
+ var outputList = [];
42
+ for (var i = 0; i < variableLocations.length; i++) {
43
+ outputList.push(icuString.slice(previousIndex, variableLocations[i].start));
44
+ outputList.push(variableLocations[i].value);
45
+ previousIndex = variableLocations[i].end;
46
+ }
47
+ if (previousIndex < icuString.length) {
48
+ outputList.push(icuString.slice(previousIndex));
49
+ }
50
+ var outputString = outputList.join('');
51
+ return outputString;
52
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Marks content as derivable by the GT compiler and CLI.
3
+ *
4
+ * Use `derive()` when a translation string or context needs content that is
5
+ * computed from source code, but should still be discovered during extraction
6
+ * instead of treated as a runtime interpolation variable. The CLI attempts to
7
+ * resolve the derivable expression into every possible static value and
8
+ * includes those values in the source content that gets translated.
9
+ *
10
+ * `derive()` returns its argument unchanged at runtime.
11
+ *
12
+ * Run `gt validate` after adding or changing `derive()` calls to verify that
13
+ * each derivable expression can be resolved by the CLI before translating or
14
+ * building.
15
+ *
16
+ * @example
17
+ * ```jsx
18
+ * function getSubject() {
19
+ * return (Math.random() > 0.5) ? "Alice" : "Brian";
20
+ * }
21
+ * ...
22
+ * gt(`My name is ${derive(getSubject())}`);
23
+ * ```
24
+ *
25
+ * @param {T extends string | boolean | number | null | undefined} content - Content to derive for translation extraction.
26
+ * @returns {T} The same content, unchanged at runtime.
27
+ */
28
+ export declare function derive<T extends string | boolean | number | null | undefined>(content: T): T;
29
+ /**
30
+ * @deprecated Use derive() instead.
31
+ *
32
+ * Marks content as derivable by the GT compiler and CLI.
33
+ *
34
+ * Use `derive()` instead of `declareStatic()` for new code. This alias is kept
35
+ * for backwards compatibility and returns its argument unchanged at runtime.
36
+ *
37
+ * Run `gt validate` after adding or changing derived content to verify that
38
+ * each derivable expression can be resolved by the CLI before translating or
39
+ * building.
40
+ *
41
+ * @example
42
+ * ```jsx
43
+ * function getSubject() {
44
+ * return (Math.random() > 0.5) ? "Alice" : "Brian";
45
+ * }
46
+ * ...
47
+ * gt(`My name is ${declareStatic(getSubject())}`);
48
+ * ```
49
+ *
50
+ * @param {T extends string | boolean | number | null | undefined} content - Content to derive for translation extraction.
51
+ * @returns {T} The same content, unchanged at runtime.
52
+ */
53
+ export declare const declareStatic: typeof derive;
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Marks content as derivable by the GT compiler and CLI.
3
+ *
4
+ * Use `derive()` when a translation string or context needs content that is
5
+ * computed from source code, but should still be discovered during extraction
6
+ * instead of treated as a runtime interpolation variable. The CLI attempts to
7
+ * resolve the derivable expression into every possible static value and
8
+ * includes those values in the source content that gets translated.
9
+ *
10
+ * `derive()` returns its argument unchanged at runtime.
11
+ *
12
+ * Run `gt validate` after adding or changing `derive()` calls to verify that
13
+ * each derivable expression can be resolved by the CLI before translating or
14
+ * building.
15
+ *
16
+ * @example
17
+ * ```jsx
18
+ * function getSubject() {
19
+ * return (Math.random() > 0.5) ? "Alice" : "Brian";
20
+ * }
21
+ * ...
22
+ * gt(`My name is ${derive(getSubject())}`);
23
+ * ```
24
+ *
25
+ * @param {T extends string | boolean | number | null | undefined} content - Content to derive for translation extraction.
26
+ * @returns {T} The same content, unchanged at runtime.
27
+ */
28
+ export function derive(content) {
29
+ return content;
30
+ }
31
+ /**
32
+ * @deprecated Use derive() instead.
33
+ *
34
+ * Marks content as derivable by the GT compiler and CLI.
35
+ *
36
+ * Use `derive()` instead of `declareStatic()` for new code. This alias is kept
37
+ * for backwards compatibility and returns its argument unchanged at runtime.
38
+ *
39
+ * Run `gt validate` after adding or changing derived content to verify that
40
+ * each derivable expression can be resolved by the CLI before translating or
41
+ * building.
42
+ *
43
+ * @example
44
+ * ```jsx
45
+ * function getSubject() {
46
+ * return (Math.random() > 0.5) ? "Alice" : "Brian";
47
+ * }
48
+ * ...
49
+ * gt(`My name is ${declareStatic(getSubject())}`);
50
+ * ```
51
+ *
52
+ * @param {T extends string | boolean | number | null | undefined} content - Content to derive for translation extraction.
53
+ * @returns {T} The same content, unchanged at runtime.
54
+ */
55
+ export var declareStatic = derive;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Given an unindexed ICU string, extracts all the _gt_ variables and an indexed mapping of the variable to the values
3
+ *
4
+ * extractVars('Hello {_gt_, select, other {World}}') => { _gt_1: 'World' }
5
+ *
6
+ * @param {string} icuString - The ICU string to extract variables from.
7
+ * @returns {Record<string, string>} A mapping of the variable to the value.
8
+ */
9
+ export declare function extractVars(icuString: string): Record<string, string>;
@@ -0,0 +1,34 @@
1
+ import { VAR_IDENTIFIER } from './utils/constants';
2
+ import { isGTUnindexedSelectElement } from './utils/traverseHelpers';
3
+ import { traverseIcu } from './utils/traverseIcu';
4
+ /**
5
+ * Given an unindexed ICU string, extracts all the _gt_ variables and an indexed mapping of the variable to the values
6
+ *
7
+ * extractVars('Hello {_gt_, select, other {World}}') => { _gt_1: 'World' }
8
+ *
9
+ * @param {string} icuString - The ICU string to extract variables from.
10
+ * @returns {Record<string, string>} A mapping of the variable to the value.
11
+ */
12
+ export function extractVars(icuString) {
13
+ // Check if the string contains _gt_
14
+ if (!icuString.includes(VAR_IDENTIFIER)) {
15
+ return {};
16
+ }
17
+ // Extract all the _gt_# variables
18
+ var index = 1;
19
+ var variables = {};
20
+ function visitor(child) {
21
+ var _a;
22
+ variables[child.value + index] = child.options.other.value.length
23
+ ? (_a = child.options.other.value[0]) === null || _a === void 0 ? void 0 : _a.value
24
+ : '';
25
+ index += 1;
26
+ }
27
+ traverseIcu({
28
+ icuString: icuString,
29
+ shouldVisit: isGTUnindexedSelectElement,
30
+ visitor: visitor,
31
+ options: { recurseIntoVisited: false },
32
+ });
33
+ return variables;
34
+ }
@@ -0,0 +1,7 @@
1
+ export * from './decodeVars';
2
+ export * from './declareVar';
3
+ export * from './derive';
4
+ export * from './indexVars';
5
+ export * from './extractVars';
6
+ export * from './condenseVars';
7
+ export * from './utils/constants';
@@ -0,0 +1,7 @@
1
+ export * from './decodeVars';
2
+ export * from './declareVar';
3
+ export * from './derive';
4
+ export * from './indexVars';
5
+ export * from './extractVars';
6
+ export * from './condenseVars';
7
+ export * from './utils/constants';
@@ -0,0 +1,6 @@
1
+ import type { IcuMessage } from '@generaltranslation/format/types';
2
+ /**
3
+ * Given an ICU string adds identifiers to each _gt_ placeholder
4
+ * indexVars('Hello {_gt_} {_gt_} World') => 'Hello {_gt_1_} {_gt_2_} World'
5
+ */
6
+ export declare function indexVars(icuString: IcuMessage): string;
@@ -0,0 +1,52 @@
1
+ import { VAR_IDENTIFIER } from './utils/constants';
2
+ import { traverseIcu } from './utils/traverseIcu';
3
+ import { isGTUnindexedSelectElement } from './utils/traverseHelpers';
4
+ /**
5
+ * Given an ICU string adds identifiers to each _gt_ placeholder
6
+ * indexVars('Hello {_gt_} {_gt_} World') => 'Hello {_gt_1_} {_gt_2_} World'
7
+ */
8
+ export function indexVars(icuString) {
9
+ // Check if the string contains _gt_
10
+ if (!icuString.includes(VAR_IDENTIFIER)) {
11
+ return icuString;
12
+ }
13
+ // Record the location of the variable
14
+ var variableLocations = [];
15
+ function visitor(child) {
16
+ var _a, _b, _c, _d, _e, _f, _g, _h;
17
+ variableLocations.push({
18
+ start: (_b = (_a = child.location) === null || _a === void 0 ? void 0 : _a.start.offset) !== null && _b !== void 0 ? _b : 0,
19
+ end: (_d = (_c = child.location) === null || _c === void 0 ? void 0 : _c.end.offset) !== null && _d !== void 0 ? _d : 0,
20
+ otherStart: (_f = (_e = child.options.other.location) === null || _e === void 0 ? void 0 : _e.start.offset) !== null && _f !== void 0 ? _f : 0,
21
+ otherEnd: (_h = (_g = child.options.other.location) === null || _g === void 0 ? void 0 : _g.end.offset) !== null && _h !== void 0 ? _h : 0,
22
+ });
23
+ }
24
+ // Find all variable identifiers.
25
+ traverseIcu({
26
+ icuString: icuString,
27
+ shouldVisit: isGTUnindexedSelectElement,
28
+ visitor: visitor,
29
+ options: { recurseIntoVisited: false, captureLocation: true },
30
+ });
31
+ // Index each variable and collapse the other option.
32
+ var result = [];
33
+ var current = 0;
34
+ for (var i = 0; i < variableLocations.length; i++) {
35
+ var _a = variableLocations[i], start = _a.start, end = _a.end, otherStart = _a.otherStart, otherEnd = _a.otherEnd;
36
+ // Before the variable
37
+ result.push(icuString.slice(current, start));
38
+ // Replace the variable with the new identifier (+1 is for the curly brace)
39
+ result.push(icuString.slice(start, start + VAR_IDENTIFIER.length + 1));
40
+ // Add the new identifier.
41
+ result.push(String(i + 1));
42
+ // After the variable
43
+ result.push(icuString.slice(start + VAR_IDENTIFIER.length + 1, otherStart));
44
+ // Before the other option
45
+ result.push('{}');
46
+ // The other option
47
+ result.push(icuString.slice(otherEnd, end));
48
+ current = end;
49
+ }
50
+ result.push(icuString.slice(current, icuString.length));
51
+ return result.join('');
52
+ }
@@ -0,0 +1,2 @@
1
+ export declare const VAR_IDENTIFIER = "_gt_";
2
+ export declare const VAR_NAME_IDENTIFIER = "_gt_var_name";
@@ -0,0 +1,2 @@
1
+ export var VAR_IDENTIFIER = '_gt_';
2
+ export var VAR_NAME_IDENTIFIER = '_gt_var_name';
@@ -0,0 +1,2 @@
1
+ export declare const GT_INDEXED_IDENTIFIER_REGEX: RegExp;
2
+ export declare const GT_UNINDEXED_IDENTIFIER_REGEX: RegExp;
@@ -0,0 +1,4 @@
1
+ import { VAR_IDENTIFIER } from './constants';
2
+ // Regex for _gt_# select
3
+ export var GT_INDEXED_IDENTIFIER_REGEX = new RegExp("^".concat(VAR_IDENTIFIER, "\\d+$"));
4
+ export var GT_UNINDEXED_IDENTIFIER_REGEX = new RegExp("^".concat(VAR_IDENTIFIER, "$"));
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Sanitizes string by escaping ICU syntax
3
+ *
4
+ * Sanitize arbitrary string so it does not break the following ICU message syntax:
5
+ * {_gt_, select, other {string_here}}
6
+ *
7
+ * Escapes ICU special characters by:
8
+ * 1. Doubling all single quotes (U+0027 ')
9
+ * 2. Adding a single quote before the first special character ({}<>)
10
+ * 3. Adding a single quote after the last special character ({}<>)
11
+ */
12
+ export declare function sanitizeVar(string: string): string;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Sanitizes string by escaping ICU syntax
3
+ *
4
+ * Sanitize arbitrary string so it does not break the following ICU message syntax:
5
+ * {_gt_, select, other {string_here}}
6
+ *
7
+ * Escapes ICU special characters by:
8
+ * 1. Doubling all single quotes (U+0027 ')
9
+ * 2. Adding a single quote before the first special character ({}<>)
10
+ * 3. Adding a single quote after the last special character ({}<>)
11
+ */
12
+ export function sanitizeVar(string) {
13
+ // First, double all single quotes (both ASCII and Unicode).
14
+ var result = string.replace(/'/g, "''");
15
+ // Find first and last positions of special characters
16
+ var specialChars = /[{}<>]/;
17
+ var firstSpecialIndex = result.search(specialChars);
18
+ if (firstSpecialIndex === -1) {
19
+ // No special characters; return with just doubled quotes.
20
+ return result;
21
+ }
22
+ // Find last special character position
23
+ var lastSpecialIndex = -1;
24
+ for (var i = result.length - 1; i >= 0; i--) {
25
+ if (specialChars.test(result[i])) {
26
+ lastSpecialIndex = i;
27
+ break;
28
+ }
29
+ }
30
+ // Insert quotes around the special character region.
31
+ result =
32
+ result.slice(0, firstSpecialIndex) +
33
+ "'" +
34
+ result.slice(firstSpecialIndex, lastSpecialIndex + 1) +
35
+ "'" +
36
+ result.slice(lastSpecialIndex + 1);
37
+ return result;
38
+ }
@@ -0,0 +1,4 @@
1
+ import { GTIndexedSelectElement, GTUnindexedSelectElement } from './types';
2
+ import { type MessageFormatElement } from '@formatjs/icu-messageformat-parser/types.js';
3
+ export declare function isGTIndexedSelectElement(child: MessageFormatElement): child is GTIndexedSelectElement;
4
+ export declare function isGTUnindexedSelectElement(child: MessageFormatElement): child is GTUnindexedSelectElement;
@@ -0,0 +1,22 @@
1
+ import { GT_INDEXED_IDENTIFIER_REGEX, GT_UNINDEXED_IDENTIFIER_REGEX, } from './regex';
2
+ import { TYPE, } from '@formatjs/icu-messageformat-parser/types.js';
3
+ // Visit any _gt_# select
4
+ export function isGTIndexedSelectElement(child) {
5
+ var _a;
6
+ return (child.type === TYPE.select &&
7
+ GT_INDEXED_IDENTIFIER_REGEX.test(child.value) &&
8
+ !!child.options.other &&
9
+ (child.options.other.value.length === 0 ||
10
+ (child.options.other.value.length > 0 &&
11
+ ((_a = child.options.other.value[0]) === null || _a === void 0 ? void 0 : _a.type) === TYPE.literal)));
12
+ }
13
+ // Visit any _gt_ select
14
+ export function isGTUnindexedSelectElement(child) {
15
+ var _a;
16
+ return (child.type === TYPE.select &&
17
+ GT_UNINDEXED_IDENTIFIER_REGEX.test(child.value) &&
18
+ !!child.options.other &&
19
+ (child.options.other.value.length === 0 ||
20
+ (child.options.other.value.length > 0 &&
21
+ ((_a = child.options.other.value[0]) === null || _a === void 0 ? void 0 : _a.type) === TYPE.literal)));
22
+ }
@@ -0,0 +1,20 @@
1
+ import { MessageFormatElement, ParserOptions } from '@formatjs/icu-messageformat-parser';
2
+ type TraverseIcuOptions = ParserOptions & {
3
+ recurseIntoVisited?: boolean;
4
+ };
5
+ /**
6
+ * Given an ICU string, traverse the AST and call the visitor function for each element that matches the type T
7
+ * @param icu - The ICU string to traverse
8
+ * @param shouldVisit - A function that returns true if the element should be visited
9
+ * @param visitor - A function that is called for each element that matches the type T
10
+ * @returns The modified AST of the ICU string.
11
+ *
12
+ * @note This function is a heavy operation, use sparingly
13
+ */
14
+ export declare function traverseIcu<T extends MessageFormatElement>({ icuString, shouldVisit, visitor, options: { recurseIntoVisited, ...otherOptions }, }: {
15
+ icuString: string;
16
+ shouldVisit: (element: MessageFormatElement) => element is T;
17
+ visitor: (element: T) => void;
18
+ options: TraverseIcuOptions;
19
+ }): MessageFormatElement[];
20
+ export {};
@@ -0,0 +1,49 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import { parse, TYPE, } from '@formatjs/icu-messageformat-parser';
13
+ /**
14
+ * Given an ICU string, traverse the AST and call the visitor function for each element that matches the type T
15
+ * @param icu - The ICU string to traverse
16
+ * @param shouldVisit - A function that returns true if the element should be visited
17
+ * @param visitor - A function that is called for each element that matches the type T
18
+ * @returns The modified AST of the ICU string.
19
+ *
20
+ * @note This function is a heavy operation, use sparingly
21
+ */
22
+ export function traverseIcu(_a) {
23
+ var icuString = _a.icuString, shouldVisit = _a.shouldVisit, visitor = _a.visitor, _b = _a.options, _c = _b.recurseIntoVisited, recurseIntoVisited = _c === void 0 ? true : _c, otherOptions = __rest(_b, ["recurseIntoVisited"]);
24
+ var ast = parse(icuString, otherOptions);
25
+ handleChildren(ast);
26
+ return ast;
27
+ function handleChildren(children) {
28
+ children.map(handleChild);
29
+ }
30
+ function handleChild(child) {
31
+ // handle select var
32
+ var visited = false;
33
+ if (shouldVisit(child)) {
34
+ visitor(child);
35
+ visited = true;
36
+ }
37
+ // recurse on children
38
+ if (!visited || recurseIntoVisited) {
39
+ if (child.type === TYPE.select || child.type === TYPE.plural) {
40
+ Object.values(child.options)
41
+ .map(function (option) { return option.value; })
42
+ .map(handleChildren);
43
+ }
44
+ else if (child.type === TYPE.tag) {
45
+ handleChildren(child.children);
46
+ }
47
+ }
48
+ }
49
+ }
@@ -0,0 +1,23 @@
1
+ import { VAR_IDENTIFIER } from './constants';
2
+ import type { PluralOrSelectOption, LiteralElement, SelectElement } from '@formatjs/icu-messageformat-parser/types.js';
3
+ interface GTIndexedSelectOption extends PluralOrSelectOption {
4
+ value: Array<LiteralElement>;
5
+ }
6
+ export interface GTIndexedSelectElement extends SelectElement {
7
+ value: `${typeof VAR_IDENTIFIER}${number}`;
8
+ options: {
9
+ other: GTIndexedSelectOption;
10
+ [key: string]: PluralOrSelectOption;
11
+ };
12
+ }
13
+ interface GTUnindexedSelectOption extends PluralOrSelectOption {
14
+ value: Array<LiteralElement>;
15
+ }
16
+ export interface GTUnindexedSelectElement extends SelectElement {
17
+ value: typeof VAR_IDENTIFIER;
18
+ options: {
19
+ other: GTUnindexedSelectOption;
20
+ [key: string]: PluralOrSelectOption;
21
+ };
22
+ }
23
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ export declare class ApiError extends Error {
2
+ code: number;
3
+ message: string;
4
+ constructor(error: string, code: number, message: string);
5
+ getCode(): number;
6
+ getMessage(): string;
7
+ }
@@ -0,0 +1,33 @@
1
+ var __extends = (this && this.__extends) || (function () {
2
+ var extendStatics = function (d, b) {
3
+ extendStatics = Object.setPrototypeOf ||
4
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
6
+ return extendStatics(d, b);
7
+ };
8
+ return function (d, b) {
9
+ if (typeof b !== "function" && b !== null)
10
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
11
+ extendStatics(d, b);
12
+ function __() { this.constructor = d; }
13
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
14
+ };
15
+ })();
16
+ var ApiError = /** @class */ (function (_super) {
17
+ __extends(ApiError, _super);
18
+ function ApiError(error, code, message) {
19
+ var _this = _super.call(this, error) || this;
20
+ _this.name = 'ApiError';
21
+ _this.code = code;
22
+ _this.message = message;
23
+ return _this;
24
+ }
25
+ ApiError.prototype.getCode = function () {
26
+ return this.code;
27
+ };
28
+ ApiError.prototype.getMessage = function () {
29
+ return this.message;
30
+ };
31
+ return ApiError;
32
+ }(Error));
33
+ export { ApiError };
package/dist/errors.cjs CHANGED
@@ -1,3 +1,3 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_ApiError = require("./ApiError-CZ45tkW6.cjs");
2
+ const require_ApiError = require("./ApiError-D-IBuHj6.cjs");
3
3
  exports.ApiError = require_ApiError.ApiError;
@@ -0,0 +1 @@
1
+ export * from './errors/ApiError';
package/dist/errors.js ADDED
@@ -0,0 +1 @@
1
+ export * from './errors/ApiError';
package/dist/errors.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import { t as ApiError } from "./ApiError-IYfaOR30.mjs";
1
+ import { t as ApiError } from "./ApiError-0DxxIHLp.mjs";
2
2
  export { ApiError };