generaltranslation 8.2.12 → 8.2.13
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.
- package/CHANGELOG.md +8 -0
- package/dist/LocaleConfig.d.ts +59 -0
- package/dist/LocaleConfig.js +225 -0
- package/dist/backwards-compatability/dataConversion.d.ts +23 -0
- package/dist/backwards-compatability/dataConversion.js +207 -0
- package/dist/backwards-compatability/oldHashJsxChildren.d.ts +23 -0
- package/dist/backwards-compatability/oldHashJsxChildren.js +79 -0
- package/dist/backwards-compatability/oldTypes.d.ts +21 -0
- package/dist/backwards-compatability/oldTypes.js +1 -0
- package/dist/backwards-compatability/typeChecking.d.ts +21 -0
- package/dist/backwards-compatability/typeChecking.js +61 -0
- package/dist/cache/IntlCache.d.ts +26 -0
- package/dist/cache/IntlCache.js +84 -0
- package/dist/cache/types.d.ts +32 -0
- package/dist/cache/types.js +1 -0
- package/dist/{core-CST4J4WU.cjs → core-7RP541eY.cjs} +2 -2
- package/dist/core-7RP541eY.cjs.map +1 -0
- package/dist/{core-D-E8qhLn.d.cts → core-I9pWGafA.d.mts} +2 -2
- package/dist/{core-CGhlXrrK.d.mts → core-TLJoDpJP.d.cts} +2 -2
- package/dist/{core-7RJsqw6c.mjs → core-isLphYAZ.mjs} +2 -2
- package/dist/core-isLphYAZ.mjs.map +1 -0
- package/dist/core.cjs +1 -1
- package/dist/core.d.cts +1 -1
- package/dist/core.d.mts +1 -1
- package/dist/core.d.ts +128 -0
- package/dist/core.js +137 -0
- package/dist/core.mjs +1 -1
- package/dist/derive/condenseVars.d.ts +7 -0
- package/dist/derive/condenseVars.js +30 -0
- package/dist/derive/declareVar.d.ts +26 -0
- package/dist/derive/declareVar.js +38 -0
- package/dist/derive/decodeVars.d.ts +9 -0
- package/dist/derive/decodeVars.js +52 -0
- package/dist/derive/derive.d.ts +53 -0
- package/dist/derive/derive.js +55 -0
- package/dist/derive/extractVars.d.ts +9 -0
- package/dist/derive/extractVars.js +34 -0
- package/dist/derive/index.d.ts +7 -0
- package/dist/derive/index.js +7 -0
- package/dist/derive/indexVars.d.ts +6 -0
- package/dist/derive/indexVars.js +52 -0
- package/dist/derive/utils/constants.d.ts +2 -0
- package/dist/derive/utils/constants.js +2 -0
- package/dist/derive/utils/regex.d.ts +2 -0
- package/dist/derive/utils/regex.js +4 -0
- package/dist/derive/utils/sanitizeVar.d.ts +12 -0
- package/dist/derive/utils/sanitizeVar.js +38 -0
- package/dist/derive/utils/traverseHelpers.d.ts +4 -0
- package/dist/derive/utils/traverseHelpers.js +22 -0
- package/dist/derive/utils/traverseIcu.d.ts +20 -0
- package/dist/derive/utils/traverseIcu.js +49 -0
- package/dist/derive/utils/types.d.ts +23 -0
- package/dist/derive/utils/types.js +1 -0
- package/dist/errors/ApiError.d.ts +7 -0
- package/dist/errors/ApiError.js +33 -0
- package/dist/errors/formattingErrors.d.ts +1 -0
- package/dist/errors/formattingErrors.js +3 -0
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +1 -0
- package/dist/formatting/custom-formats/CutoffFormat/CutoffFormat.d.ts +59 -0
- package/dist/formatting/custom-formats/CutoffFormat/CutoffFormat.js +147 -0
- package/dist/formatting/custom-formats/CutoffFormat/constants.d.ts +4 -0
- package/dist/formatting/custom-formats/CutoffFormat/constants.js +30 -0
- package/dist/formatting/custom-formats/CutoffFormat/types.d.ts +48 -0
- package/dist/formatting/custom-formats/CutoffFormat/types.js +2 -0
- package/dist/formatting/format.d.ts +1 -0
- package/dist/formatting/format.js +257 -0
- package/dist/id/hashSource.d.ts +25 -0
- package/dist/id/hashSource.js +98 -0
- package/dist/id/hashTemplate.d.ts +4 -0
- package/dist/id/hashTemplate.js +6 -0
- package/dist/id/types.d.ts +7 -0
- package/dist/id/types.js +1 -0
- package/dist/id.d.cts +1 -1
- package/dist/id.d.mts +1 -1
- package/dist/id.d.ts +3 -0
- package/dist/id.js +3 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +1019 -0
- package/dist/index.js +1664 -0
- package/dist/index.mjs +1 -1
- package/dist/internal.cjs +1 -1
- package/dist/internal.cjs.map +1 -1
- package/dist/internal.d.cts +2 -2
- package/dist/internal.d.mts +2 -2
- package/dist/internal.d.ts +19 -0
- package/dist/internal.js +17 -0
- package/dist/internal.mjs +1 -1
- package/dist/internal.mjs.map +1 -1
- package/dist/locales/customLocaleMapping.d.ts +11 -0
- package/dist/locales/customLocaleMapping.js +23 -0
- package/dist/locales/determineLocale.d.ts +1 -0
- package/dist/locales/determineLocale.js +72 -0
- package/dist/locales/getLocaleDirection.d.ts +1 -0
- package/dist/locales/getLocaleDirection.js +89 -0
- package/dist/locales/getLocaleEmoji.d.ts +2 -0
- package/dist/locales/getLocaleEmoji.js +319 -0
- package/dist/locales/getLocaleName.d.ts +1 -0
- package/dist/locales/getLocaleName.js +45 -0
- package/dist/locales/getLocaleProperties.d.ts +32 -0
- package/dist/locales/getLocaleProperties.js +220 -0
- package/dist/locales/getPluralForm.d.ts +9 -0
- package/dist/locales/getPluralForm.js +63 -0
- package/dist/locales/getRegionProperties.d.ts +7 -0
- package/dist/locales/getRegionProperties.js +61 -0
- package/dist/locales/isSameDialect.d.ts +1 -0
- package/dist/locales/isSameDialect.js +41 -0
- package/dist/locales/isSameLanguage.d.ts +1 -0
- package/dist/locales/isSameLanguage.js +20 -0
- package/dist/locales/isSupersetLocale.d.ts +1 -0
- package/dist/locales/isSupersetLocale.js +22 -0
- package/dist/locales/isValidLocale.d.ts +1 -0
- package/dist/locales/isValidLocale.js +75 -0
- package/dist/locales/requiresTranslation.d.ts +1 -0
- package/dist/locales/requiresTranslation.js +32 -0
- package/dist/locales/resolveAliasLocale.d.ts +8 -0
- package/dist/locales/resolveAliasLocale.js +21 -0
- package/dist/locales/resolveCanonicalLocale.d.ts +8 -0
- package/dist/locales/resolveCanonicalLocale.js +13 -0
- package/dist/logging/errors.d.ts +10 -0
- package/dist/logging/errors.js +29 -0
- package/dist/logging/logger.d.ts +121 -0
- package/dist/logging/logger.js +259 -0
- package/dist/logging/warnings.d.ts +2 -0
- package/dist/logging/warnings.js +2 -0
- package/dist/projects/getProjectData.d.ts +1 -0
- package/dist/projects/getProjectData.js +87 -0
- package/dist/settings/plurals.d.ts +3 -0
- package/dist/settings/plurals.js +14 -0
- package/dist/settings/settings.d.ts +2 -0
- package/dist/settings/settings.js +2 -0
- package/dist/settings/settingsUrls.d.ts +3 -0
- package/dist/settings/settingsUrls.js +3 -0
- package/dist/translate/api.d.ts +1 -0
- package/dist/translate/api.js +1 -0
- package/dist/translate/awaitJobs.d.ts +19 -0
- package/dist/translate/awaitJobs.js +112 -0
- package/dist/translate/checkJobStatus.d.ts +8 -0
- package/dist/translate/checkJobStatus.js +55 -0
- package/dist/translate/createBranch.d.ts +10 -0
- package/dist/translate/createBranch.js +53 -0
- package/dist/translate/createTag.d.ts +19 -0
- package/dist/translate/createTag.js +67 -0
- package/dist/translate/downloadFileBatch.d.ts +1 -0
- package/dist/translate/downloadFileBatch.js +77 -0
- package/dist/translate/enqueueFiles.d.ts +8 -0
- package/dist/translate/enqueueFiles.js +96 -0
- package/dist/translate/getOrphanedFiles.d.ts +8 -0
- package/dist/translate/getOrphanedFiles.js +97 -0
- package/dist/translate/processFileMoves.d.ts +25 -0
- package/dist/translate/processFileMoves.js +90 -0
- package/dist/translate/publishFiles.d.ts +17 -0
- package/dist/translate/publishFiles.js +56 -0
- package/dist/translate/queryBranchData.d.ts +3 -0
- package/dist/translate/queryBranchData.js +53 -0
- package/dist/translate/queryFileData.d.ts +42 -0
- package/dist/translate/queryFileData.js +71 -0
- package/dist/translate/querySourceFile.d.ts +1 -0
- package/dist/translate/querySourceFile.js +67 -0
- package/dist/translate/setupProject.d.ts +11 -0
- package/dist/translate/setupProject.js +63 -0
- package/dist/translate/submitUserEditDiffs.d.ts +12 -0
- package/dist/translate/submitUserEditDiffs.js +68 -0
- package/dist/translate/translateMany.d.ts +1 -0
- package/dist/translate/translateMany.js +103 -0
- package/dist/translate/uploadSourceFiles.d.ts +1 -0
- package/dist/translate/uploadSourceFiles.js +87 -0
- package/dist/translate/uploadTranslations.d.ts +1 -0
- package/dist/translate/uploadTranslations.js +101 -0
- package/dist/translate/utils/apiRequest.d.ts +1 -0
- package/dist/translate/utils/apiRequest.js +130 -0
- package/dist/translate/utils/batch.d.ts +52 -0
- package/dist/translate/utils/batch.js +126 -0
- package/dist/translate/utils/fetchWithTimeout.d.ts +1 -0
- package/dist/translate/utils/fetchWithTimeout.js +93 -0
- package/dist/translate/utils/generateRequestHeaders.d.ts +2 -0
- package/dist/translate/utils/generateRequestHeaders.js +26 -0
- package/dist/translate/utils/handleFetchError.d.ts +1 -0
- package/dist/translate/utils/handleFetchError.js +12 -0
- package/dist/translate/utils/validateFileFormatTransform.d.ts +16 -0
- package/dist/translate/utils/validateFileFormatTransform.js +29 -0
- package/dist/translate/utils/validateResponse.d.ts +1 -0
- package/dist/translate/utils/validateResponse.js +72 -0
- package/dist/{types-BsGsgJWF.d.mts → types-AHtYZIP-.d.mts} +34 -11
- package/dist/{types-V5rqd1WR.d.cts → types-Bf8_Apq_.d.cts} +34 -11
- package/dist/types-dir/api/branch.d.ts +10 -0
- package/dist/types-dir/api/branch.js +1 -0
- package/dist/types-dir/api/checkFileTranslations.d.ts +37 -0
- package/dist/types-dir/api/checkFileTranslations.js +1 -0
- package/dist/types-dir/api/downloadFile.d.ts +3 -0
- package/dist/types-dir/api/downloadFile.js +1 -0
- package/dist/types-dir/api/downloadFileBatch.d.ts +35 -0
- package/dist/types-dir/api/downloadFileBatch.js +1 -0
- package/dist/types-dir/api/enqueueEntries.d.ts +21 -0
- package/dist/types-dir/api/enqueueEntries.js +1 -0
- package/dist/types-dir/api/enqueueFiles.d.ts +70 -0
- package/dist/types-dir/api/enqueueFiles.js +1 -0
- package/dist/types-dir/api/entry.d.ts +39 -0
- package/dist/types-dir/api/entry.js +1 -0
- package/dist/types-dir/api/fetchTranslations.d.ts +12 -0
- package/dist/types-dir/api/fetchTranslations.js +1 -0
- package/dist/types-dir/api/file.d.ts +57 -0
- package/dist/types-dir/api/file.js +1 -0
- package/dist/types-dir/api/json.d.ts +5 -0
- package/dist/types-dir/api/json.js +1 -0
- package/dist/types-dir/api/project.d.ts +7 -0
- package/dist/types-dir/api/project.js +1 -0
- package/dist/types-dir/api/translate.d.ts +34 -0
- package/dist/types-dir/api/translate.js +1 -0
- package/dist/types-dir/api/translateMany.d.ts +5 -0
- package/dist/types-dir/api/translateMany.js +1 -0
- package/dist/types-dir/api/translationStatus.d.ts +9 -0
- package/dist/types-dir/api/translationStatus.js +1 -0
- package/dist/types-dir/api/uploadFiles.d.ts +44 -0
- package/dist/types-dir/api/uploadFiles.js +1 -0
- package/dist/types-dir/jsx/content.d.ts +61 -0
- package/dist/types-dir/jsx/content.js +11 -0
- package/dist/types-dir/jsx/variables.d.ts +9 -0
- package/dist/types-dir/jsx/variables.js +1 -0
- package/dist/types-dir/transformations.d.ts +8 -0
- package/dist/types-dir/transformations.js +1 -0
- package/dist/types.d.cts +2 -2
- package/dist/types.d.mts +2 -2
- package/dist/types.d.ts +129 -0
- package/dist/types.js +2 -0
- package/dist/utils/base64.d.ts +2 -0
- package/dist/utils/base64.js +28 -0
- package/dist/utils/isSupportedFileFormatTransform.d.ts +8 -0
- package/dist/utils/isSupportedFileFormatTransform.js +26 -0
- package/dist/utils/isVariable.d.ts +2 -0
- package/dist/utils/isVariable.js +22 -0
- package/dist/utils/minify.d.ts +2 -0
- package/dist/utils/minify.js +10 -0
- package/dist/utils/stableStringify.d.ts +1 -0
- package/dist/utils/stableStringify.js +40 -0
- package/package.json +3 -3
- package/dist/core-7RJsqw6c.mjs.map +0 -1
- package/dist/core-CST4J4WU.cjs.map +0 -1
|
@@ -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,6 @@
|
|
|
1
|
+
import { IcuMessage } from '../types-dir/jsx/content';
|
|
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,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,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 };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const createInvalidCutoffStyleError: (style: string) => string;
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './errors/ApiError';
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './errors/ApiError';
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { CutoffFormat, CutoffFormatOptions, PostpendedCutoffParts, PrependedCutoffParts, ResolvedCutoffFormatOptions } from './types';
|
|
2
|
+
export declare class CutoffFormatConstructor implements CutoffFormat {
|
|
3
|
+
private locale;
|
|
4
|
+
private options;
|
|
5
|
+
private additionLength;
|
|
6
|
+
/**
|
|
7
|
+
* Constructor
|
|
8
|
+
* @param {Intl.LocalesArgument} locales - The locales to use for formatting.
|
|
9
|
+
* @param {CutoffFormatOptions} options - The options for formatting.
|
|
10
|
+
* @param {number} [options.maxChars] - The maximum number of characters to display.
|
|
11
|
+
* - Undefined values are treated as no cutoff.
|
|
12
|
+
* - Negative values follow .slice() behavior and terminator will be added before the value.
|
|
13
|
+
* - 0 will result in an empty string.
|
|
14
|
+
* - If cutoff results in an empty string, no terminator is added.
|
|
15
|
+
* @param {CutoffFormatStyle} [options.style='ellipsis'] - The style of the terminator.
|
|
16
|
+
* @param {string} [options.terminator] - Optional override the terminator to use.
|
|
17
|
+
* @param {string} [options.separator] - Optional override the separator to use between the terminator and the value.
|
|
18
|
+
* - If no terminator is provided, then separator is ignored.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const format = new CutoffFormat('en', { maxChars: 5 });
|
|
22
|
+
* format.format('Hello, world!'); // 'Hello...'
|
|
23
|
+
*
|
|
24
|
+
* const format = new CutoffFormat('en', { maxChars: -3 });
|
|
25
|
+
* format.format('Hello, world!'); // '...ld!'
|
|
26
|
+
*/
|
|
27
|
+
constructor(locales: Intl.LocalesArgument, options?: CutoffFormatOptions);
|
|
28
|
+
/**
|
|
29
|
+
* Format a value according to the cutoff options, returning a formatted string.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} value - The string value to format with cutoff behavior.
|
|
32
|
+
* @returns {string} The formatted string with terminator applied if cutoff occurs.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const formatter = new CutoffFormatConstructor('en', { maxChars: 8, style: 'ellipsis' });
|
|
36
|
+
* formatter.format('Hello, world!'); // Returns 'Hello, w...'
|
|
37
|
+
*/
|
|
38
|
+
format(value: string): string;
|
|
39
|
+
/**
|
|
40
|
+
* Format a value to parts according to the cutoff options, returning an array of string parts.
|
|
41
|
+
* This method breaks down the formatted result into individual components for more granular control.
|
|
42
|
+
*
|
|
43
|
+
* @param {string} value - The string value to format with cutoff behavior.
|
|
44
|
+
* @returns {PrependedCutoffParts | PostpendedCutoffParts} An array of string parts representing the formatted result.
|
|
45
|
+
* - For positive maxChars: [cutoffValue, separator?, terminator?]
|
|
46
|
+
* - For negative maxChars: [terminator?, separator?, cutoffValue]
|
|
47
|
+
* - For no cutoff: [originalValue]
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* const formatter = new CutoffFormatConstructor('en', { maxChars: 5, style: 'ellipsis' });
|
|
51
|
+
* formatter.formatToParts('Hello, world!'); // Returns ['Hello', '...']
|
|
52
|
+
*/
|
|
53
|
+
formatToParts(value: string): PrependedCutoffParts | PostpendedCutoffParts;
|
|
54
|
+
/**
|
|
55
|
+
* Get the resolved options
|
|
56
|
+
* @returns {ResolvedCutoffFormatOptions} The resolved options.
|
|
57
|
+
*/
|
|
58
|
+
resolvedOptions(): ResolvedCutoffFormatOptions;
|
|
59
|
+
}
|