generaltranslation 9.4.2 → 9.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -0
- package/README.md +2 -1
- package/dist/{ApiError-BcjcnTAr.cjs → ApiError-BCjEo_CM.cjs} +1 -1
- package/dist/{ApiError-BcjcnTAr.cjs.map → ApiError-BCjEo_CM.cjs.map} +1 -1
- package/dist/{ApiError-CUKcz3YS.mjs → ApiError-DxWKM0Xt.mjs} +1 -1
- package/dist/{ApiError-CUKcz3YS.mjs.map → ApiError-DxWKM0Xt.mjs.map} +1 -1
- package/dist/adapter/createGtApi.d.ts +196 -27
- package/dist/adapter/createGtApi.js +561 -117
- package/dist/adapter/modelProvider.d.ts +4 -0
- package/dist/adapter/modelProvider.js +6 -0
- package/dist/derive-Cv5_y8Tf.mjs +656 -0
- package/dist/derive-Cv5_y8Tf.mjs.map +1 -0
- package/dist/{derive-DBCCXTmL.d.cts → derive-J2NQLqZm.d.cts} +1 -1
- package/dist/derive-t_E3ftJN.cjs +733 -0
- package/dist/derive-t_E3ftJN.cjs.map +1 -0
- package/dist/diagnostics-CqAHg_Vc.d.cts +34 -0
- package/dist/diagnostics-DKA2SkZB.mjs +45 -0
- package/dist/diagnostics-DKA2SkZB.mjs.map +1 -0
- package/dist/{unwrapApiResult-BzGfPw75.mjs → diagnostics-DKtiPUvo.cjs} +12 -31
- package/dist/diagnostics-DKtiPUvo.cjs.map +1 -0
- package/dist/diagnostics-DYtVjOm5.d.mts +34 -0
- package/dist/diagnostics.cjs +4 -0
- package/dist/diagnostics.d.cts +2 -0
- package/dist/diagnostics.d.mts +2 -0
- package/dist/diagnostics.d.ts +1 -0
- package/dist/diagnostics.js +1 -0
- package/dist/diagnostics.mjs +2 -0
- package/dist/errors.cjs +1 -1
- package/dist/errors.mjs +1 -1
- package/dist/file-B37WkCqM.cjs.map +1 -1
- package/dist/file-Dga6cyM8.mjs.map +1 -1
- package/dist/{id-X2zyx5Jk.mjs → id-B2wA7-S-.mjs} +19 -3
- package/dist/id-B2wA7-S-.mjs.map +1 -0
- package/dist/{id-pdr3R1xs.cjs → id-DJ_59q21.cjs} +25 -3
- package/dist/id-DJ_59q21.cjs.map +1 -0
- package/dist/id.cjs +1 -1
- package/dist/id.d.cts +1 -1
- package/dist/id.d.mts +1 -1
- package/dist/id.mjs +1 -1
- package/dist/index.cjs +94 -753
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +14 -33
- package/dist/index.d.mts +13 -32
- package/dist/index.d.ts +20 -16
- package/dist/index.js +110 -98
- package/dist/index.mjs +88 -746
- package/dist/index.mjs.map +1 -1
- package/dist/internal.cjs +16 -186
- package/dist/internal.cjs.map +1 -1
- package/dist/internal.d.cts +200 -62
- package/dist/internal.d.mts +199 -61
- package/dist/internal.d.ts +3 -1
- package/dist/internal.js +2 -1
- package/dist/internal.mjs +6 -178
- package/dist/internal.mjs.map +1 -1
- package/dist/logging/errors.d.ts +0 -1
- package/dist/logging/errors.js +0 -6
- package/dist/logging/logger.js +6 -4
- package/dist/openapi.json +2344 -845
- package/dist/runtime.cjs +580 -2
- package/dist/runtime.cjs.map +1 -0
- package/dist/runtime.d.cts +12 -6
- package/dist/runtime.d.mts +12 -6
- package/dist/runtime.d.ts +10 -4
- package/dist/runtime.js +10 -65
- package/dist/runtime.mjs +579 -1
- package/dist/runtime.mjs.map +1 -0
- package/dist/runtimeTranslate-CVw143pQ.mjs +506 -0
- package/dist/runtimeTranslate-CVw143pQ.mjs.map +1 -0
- package/dist/runtimeTranslate-DJioRjb6.cjs +601 -0
- package/dist/runtimeTranslate-DJioRjb6.cjs.map +1 -0
- package/dist/translate/awaitJobs.d.ts +2 -2
- package/dist/translate/awaitJobs.js +1 -156
- package/dist/translate/checkJobStatus.d.ts +2 -1
- package/dist/translate/checkJobStatus.js +1 -55
- package/dist/translate/createBranch.d.ts +2 -6
- package/dist/translate/createBranch.js +1 -53
- package/dist/translate/enqueueFiles.js +1 -95
- package/dist/translate/getProjectInfo.d.ts +2 -6
- package/dist/translate/getProjectInfo.js +1 -54
- package/dist/translate/processFileMoves.d.ts +3 -13
- package/dist/translate/processFileMoves.js +1 -90
- package/dist/translate/publishFiles.d.ts +3 -15
- package/dist/translate/publishFiles.js +1 -56
- package/dist/translate/queryBranchData.js +1 -53
- package/dist/translate/queryFileData.d.ts +3 -41
- package/dist/translate/queryFileData.js +1 -71
- package/dist/translate/runtimeTranslate.d.ts +6 -0
- package/dist/translate/runtimeTranslate.js +297 -0
- package/dist/translate/setupProject.d.ts +2 -6
- package/dist/translate/setupProject.js +1 -63
- package/dist/translate/submitUserEditDiffs.js +1 -68
- package/dist/translate/utils/fetchWithTimeout.js +19 -12
- package/dist/{types-bCr8pV1Z.d.cts → types-Si4UstLL.d.mts} +41 -165
- package/dist/types-dir/__tests__/runtimeTranslate.type-test.js +18 -0
- package/dist/types-dir/api/branch.d.ts +3 -9
- package/dist/types-dir/api/checkFileTranslations.d.ts +2 -22
- package/dist/types-dir/api/downloadFileBatch.d.ts +2 -7
- package/dist/types-dir/api/entry.d.ts +3 -8
- package/dist/types-dir/api/file.d.ts +2 -1
- package/dist/types-dir/api/json.d.ts +1 -4
- package/dist/types-dir/api/project.d.ts +2 -7
- package/dist/types-dir/api/uploadAssets.d.ts +3 -10
- package/dist/types-dir/api/uploadAssets.js +0 -4
- package/dist/types-dir/config.d.ts +1 -0
- package/dist/{types-Dp1drjxq.d.mts → types-yByDbKnV.d.cts} +41 -165
- package/dist/types.d.cts +2 -2
- package/dist/types.d.mts +2 -2
- package/dist/types.d.ts +26 -7
- package/package.json +15 -2
- package/dist/derive-2CDqwrvS.cjs +0 -303
- package/dist/derive-2CDqwrvS.cjs.map +0 -1
- package/dist/derive-CTzR56T_.mjs +0 -233
- package/dist/derive-CTzR56T_.mjs.map +0 -1
- package/dist/id-X2zyx5Jk.mjs.map +0 -1
- package/dist/id-pdr3R1xs.cjs.map +0 -1
- package/dist/isVariable-8-Wy5FnX.mjs +0 -20
- package/dist/isVariable-8-Wy5FnX.mjs.map +0 -1
- package/dist/isVariable-I3nllgeI.cjs +0 -25
- package/dist/isVariable-I3nllgeI.cjs.map +0 -1
- package/dist/projects/getProjectData.js +0 -87
- package/dist/runtime-Bcp4F2ME.mjs +0 -974
- package/dist/runtime-Bcp4F2ME.mjs.map +0 -1
- package/dist/runtime-C25lwh9m.cjs +0 -1027
- package/dist/runtime-C25lwh9m.cjs.map +0 -1
- package/dist/translate/api.d.ts +0 -4
- package/dist/translate/api.js +0 -4
- package/dist/translate/createTag.d.ts +0 -19
- package/dist/translate/createTag.js +0 -67
- package/dist/translate/downloadFileBatch.d.ts +0 -1
- package/dist/translate/downloadFileBatch.js +0 -80
- package/dist/translate/getOrphanedFiles.d.ts +0 -8
- package/dist/translate/getOrphanedFiles.js +0 -97
- package/dist/translate/querySourceFile.d.ts +0 -1
- package/dist/translate/querySourceFile.js +0 -67
- package/dist/translate/translateMany.d.ts +0 -1
- package/dist/translate/translateMany.js +0 -124
- package/dist/translate/uploadFonts.d.ts +0 -1
- package/dist/translate/uploadFonts.js +0 -74
- package/dist/translate/uploadSourceFiles.d.ts +0 -1
- package/dist/translate/uploadSourceFiles.js +0 -90
- package/dist/translate/uploadTranslations.d.ts +0 -1
- package/dist/translate/uploadTranslations.js +0 -106
- package/dist/translate/utils/apiRequest.d.ts +0 -1
- package/dist/translate/utils/apiRequest.js +0 -167
- package/dist/translate/utils/generateRequestHeaders.d.ts +0 -2
- package/dist/translate/utils/generateRequestHeaders.js +0 -21
- package/dist/translate/utils/handleFetchError.d.ts +0 -1
- package/dist/translate/utils/handleFetchError.js +0 -12
- package/dist/unwrapApiResult-BzGfPw75.mjs.map +0 -1
- package/dist/unwrapApiResult-Dv6Mfw_K.cjs +0 -134
- package/dist/unwrapApiResult-Dv6Mfw_K.cjs.map +0 -1
- /package/dist/{projects/getProjectData.d.ts → types-dir/__tests__/runtimeTranslate.type-test.d.ts} +0 -0
package/dist/runtime.cjs
CHANGED
|
@@ -1,3 +1,581 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
3
|
-
|
|
2
|
+
const require_runtimeTranslate = require("./runtimeTranslate-DJioRjb6.cjs");
|
|
3
|
+
let _generaltranslation_format = require("@generaltranslation/format");
|
|
4
|
+
//#region src/runtime.ts
|
|
5
|
+
/**
|
|
6
|
+
* GTRuntime is the runtime core of the GT driver: locale management,
|
|
7
|
+
* formatting, and runtime translation requests.
|
|
8
|
+
*
|
|
9
|
+
* Browser-facing SDK code constructs this class (via `generaltranslation/runtime`)
|
|
10
|
+
* so production bundles do not ship the project/file management API client
|
|
11
|
+
* that lives on the GT class exported from the main entry.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const gt = new GTRuntime({
|
|
15
|
+
* sourceLocale: 'en-US',
|
|
16
|
+
* targetLocale: 'es-ES',
|
|
17
|
+
* locales: ['en-US', 'es-ES', 'fr-FR']
|
|
18
|
+
* });
|
|
19
|
+
*/
|
|
20
|
+
var GTRuntime = class {
|
|
21
|
+
/** Runtime-safe locale and formatting helpers */
|
|
22
|
+
get localeConfig() {
|
|
23
|
+
return this._localeConfig;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Constructs an instance of the GTRuntime class.
|
|
27
|
+
*
|
|
28
|
+
* @param {GTConstructorParams} [params] - The parameters for initializing the GTRuntime instance
|
|
29
|
+
* @throws {Error} If an invalid locale is provided
|
|
30
|
+
* @throws {Error} If any of the provided locales are invalid
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* const gt = new GTRuntime({
|
|
34
|
+
* apiKey: 'your-api-key',
|
|
35
|
+
* sourceLocale: 'en-US',
|
|
36
|
+
* targetLocale: 'es-ES',
|
|
37
|
+
* locales: ['en-US', 'es-ES', 'fr-FR']
|
|
38
|
+
* });
|
|
39
|
+
*/
|
|
40
|
+
constructor(params = {}) {
|
|
41
|
+
if (typeof process !== "undefined") {
|
|
42
|
+
this.apiKey ||= process.env?.GT_API_KEY;
|
|
43
|
+
this.devApiKey ||= process.env?.GT_DEV_API_KEY;
|
|
44
|
+
this.projectId ||= process.env?.GT_PROJECT_ID;
|
|
45
|
+
}
|
|
46
|
+
this.setConfig(params);
|
|
47
|
+
}
|
|
48
|
+
setConfig({ apiKey, devApiKey, sourceLocale, targetLocale, locales, projectId, customMapping, baseUrl, userTokenProvider }) {
|
|
49
|
+
const effectiveCustomMapping = customMapping ?? this.customMapping;
|
|
50
|
+
if (apiKey) this.apiKey = apiKey;
|
|
51
|
+
if (devApiKey) this.devApiKey = devApiKey;
|
|
52
|
+
if (projectId) this.projectId = projectId;
|
|
53
|
+
if (userTokenProvider) this.userTokenProvider = userTokenProvider;
|
|
54
|
+
if (sourceLocale) {
|
|
55
|
+
this.sourceLocale = sourceLocale;
|
|
56
|
+
if (!(0, _generaltranslation_format.isValidLocale)(this.sourceLocale, effectiveCustomMapping)) throw new Error(require_runtimeTranslate.invalidLocaleError(this.sourceLocale));
|
|
57
|
+
}
|
|
58
|
+
if (targetLocale) {
|
|
59
|
+
this.targetLocale = targetLocale;
|
|
60
|
+
if (!(0, _generaltranslation_format.isValidLocale)(this.targetLocale, effectiveCustomMapping)) throw new Error(require_runtimeTranslate.invalidLocaleError(this.targetLocale));
|
|
61
|
+
}
|
|
62
|
+
if (locales) {
|
|
63
|
+
const result = [];
|
|
64
|
+
const invalidLocales = [];
|
|
65
|
+
locales.forEach((locale) => {
|
|
66
|
+
if ((0, _generaltranslation_format.isValidLocale)(locale, effectiveCustomMapping)) result.push(locale);
|
|
67
|
+
else invalidLocales.push(locale);
|
|
68
|
+
});
|
|
69
|
+
if (invalidLocales.length > 0) throw new Error(require_runtimeTranslate.invalidLocalesError(invalidLocales));
|
|
70
|
+
this.locales = result;
|
|
71
|
+
}
|
|
72
|
+
if (baseUrl) this.baseUrl = baseUrl;
|
|
73
|
+
if (customMapping) {
|
|
74
|
+
this.customMapping = customMapping;
|
|
75
|
+
this.reverseCustomMapping = Object.fromEntries(Object.entries(customMapping).filter(([, value]) => value && typeof value === "object" && "code" in value).map(([key, value]) => [value.code, key]));
|
|
76
|
+
}
|
|
77
|
+
this._localeConfig = new _generaltranslation_format.LocaleConfig({
|
|
78
|
+
defaultLocale: this.sourceLocale,
|
|
79
|
+
locales: this.locales ?? [],
|
|
80
|
+
customMapping: this.customMapping
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/** Convert app identity only where a GT service expects a language code. */
|
|
84
|
+
resolveServiceLocale(locale) {
|
|
85
|
+
return this.standardizeLocale(this.resolveCanonicalLocale(locale));
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Recover configured spelling without negotiating an unrelated dialect.
|
|
89
|
+
* When several identities use the same service code and there is no request
|
|
90
|
+
* context to disambiguate them, the first configured identity wins.
|
|
91
|
+
*/
|
|
92
|
+
resolveServiceResponseLocale(locale) {
|
|
93
|
+
const canonical = this.standardizeLocale(locale);
|
|
94
|
+
return [
|
|
95
|
+
...this.locales ?? [],
|
|
96
|
+
this.sourceLocale,
|
|
97
|
+
this.targetLocale,
|
|
98
|
+
...Object.keys(this.customMapping ?? {})
|
|
99
|
+
].find((candidate) => candidate && this.resolveServiceLocale(candidate) === canonical) ?? locale;
|
|
100
|
+
}
|
|
101
|
+
_getTranslationConfig() {
|
|
102
|
+
return {
|
|
103
|
+
baseUrl: this.baseUrl,
|
|
104
|
+
apiKey: this.apiKey || this.devApiKey,
|
|
105
|
+
projectId: this.projectId || "",
|
|
106
|
+
userTokenProvider: this.userTokenProvider
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Translates a single source string to the target locale.
|
|
111
|
+
* Routes through {@link translateMany} under the hood.
|
|
112
|
+
*
|
|
113
|
+
* @param {string} source - The source string to translate.
|
|
114
|
+
* @param {object} options - Translation options including targetLocale and optional entry metadata.
|
|
115
|
+
* @param {number} [timeout] - Timeout in milliseconds; 0 or omitted selects the runtime default.
|
|
116
|
+
* @returns {Promise<TranslationResult | TranslationError>} The translated content.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* const result = await gt.translate('Hello, world!', { targetLocale: 'es' });
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* const result = await gt.translate('Hello, world!', {
|
|
123
|
+
* targetLocale: 'es',
|
|
124
|
+
* dataFormat: 'ICU',
|
|
125
|
+
* context: 'A formal greeting',
|
|
126
|
+
* });
|
|
127
|
+
*/
|
|
128
|
+
async translate(source, options, timeout) {
|
|
129
|
+
return require_runtimeTranslate.translate(source, options, {
|
|
130
|
+
...this._getTranslationConfig(),
|
|
131
|
+
customMapping: this.customMapping,
|
|
132
|
+
timeoutMs: timeout || void 0
|
|
133
|
+
}, this);
|
|
134
|
+
}
|
|
135
|
+
async translateMany(sources, options, timeout) {
|
|
136
|
+
return require_runtimeTranslate.translateMany(sources, options, {
|
|
137
|
+
...this._getTranslationConfig(),
|
|
138
|
+
customMapping: this.customMapping,
|
|
139
|
+
timeoutMs: timeout || void 0
|
|
140
|
+
}, this);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Formats a string with cutoff behavior, applying a terminator when the string exceeds the maximum character limit.
|
|
144
|
+
*
|
|
145
|
+
* This method uses the GT instance's rendering locales by default for locale-specific terminator selection,
|
|
146
|
+
* but can be overridden with custom locales in the options.
|
|
147
|
+
*
|
|
148
|
+
* @param {string} value - The string value to format with cutoff behavior.
|
|
149
|
+
* @param {Object} [options] - Configuration options for cutoff formatting.
|
|
150
|
+
* @param {string | string[]} [options.locales] - The locales to use for terminator selection. Defaults to instance's rendering locales.
|
|
151
|
+
* @param {number} [options.maxChars] - The maximum number of characters to display.
|
|
152
|
+
* - Undefined values are treated as no cutoff.
|
|
153
|
+
* - Negative values follow .slice() behavior and terminator will be added before the value.
|
|
154
|
+
* - 0 will result in an empty string.
|
|
155
|
+
* - If cutoff results in an empty string, no terminator is added.
|
|
156
|
+
* @param {CutoffFormatStyle} [options.style='ellipsis'] - The style of the terminator.
|
|
157
|
+
* @param {string} [options.terminator] - Optional override the terminator to use.
|
|
158
|
+
* @param {string} [options.separator] - Optional override the separator to use between the terminator and the value.
|
|
159
|
+
* - If no terminator is provided, then separator is ignored.
|
|
160
|
+
* @returns {string} The formatted string with terminator applied if cutoff occurs.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* const gt = new GTRuntime({ targetLocale: 'en-US' });
|
|
164
|
+
* gt.formatCutoff('Hello, world!', { maxChars: 8 });
|
|
165
|
+
* // Returns: 'Hello, w...'
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* gt.formatCutoff('Hello, world!', { maxChars: -3 });
|
|
169
|
+
* // Returns: '...ld!'
|
|
170
|
+
*/
|
|
171
|
+
formatCutoff(value, options) {
|
|
172
|
+
return this.localeConfig.formatCutoff(value, this.targetLocale, options);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Formats a message according to the specified locales and options.
|
|
176
|
+
*
|
|
177
|
+
* @param {string} message - The message to format.
|
|
178
|
+
* @param {string | string[]} [locales=libraryDefaultLocale] - The locales to use for formatting.
|
|
179
|
+
* @param {FormatVariables} [variables={}] - The variables to use for formatting.
|
|
180
|
+
* @param {StringFormat} [dataFormat='ICU'] - The format of the message.
|
|
181
|
+
* @returns {string} The formatted message.
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* gt.formatMessage('Hello {name}', { name: 'John' });
|
|
185
|
+
* // Returns: "Hello John"
|
|
186
|
+
*
|
|
187
|
+
* gt.formatMessage('Hello {name}', { name: 'John' }, { locales: ['fr'] });
|
|
188
|
+
* // Returns: "Bonjour John"
|
|
189
|
+
*/
|
|
190
|
+
formatMessage(message, options) {
|
|
191
|
+
return this.localeConfig.formatMessage(message, this.targetLocale, options);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Formats a number according to the specified locales and options.
|
|
195
|
+
*
|
|
196
|
+
* @param {number} number - The number to format.
|
|
197
|
+
* @param {Object} [options] - Additional options for number formatting.
|
|
198
|
+
* @param {string | string[]} [options.locales] - The locales to use for formatting.
|
|
199
|
+
* @param {Intl.NumberFormatOptions} [options] - Additional Intl.NumberFormat options.
|
|
200
|
+
* @returns {string} The formatted number.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* gt.formatNum(1234.56, { style: 'currency', currency: 'USD' });
|
|
204
|
+
* // Returns: "$1,234.56"
|
|
205
|
+
*/
|
|
206
|
+
formatNum(number, options) {
|
|
207
|
+
return this.localeConfig.formatNum(number, this.targetLocale, options);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Formats a date according to the specified locales and options.
|
|
211
|
+
*
|
|
212
|
+
* @param {Date} date - The date to format.
|
|
213
|
+
* @param {Object} [options] - Additional options for date formatting.
|
|
214
|
+
* @param {string | string[]} [options.locales] - The locales to use for formatting.
|
|
215
|
+
* @param {Intl.DateTimeFormatOptions} [options] - Additional Intl.DateTimeFormat options.
|
|
216
|
+
* @returns {string} The formatted date.
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* gt.formatDateTime(new Date(), { dateStyle: 'full', timeStyle: 'long' });
|
|
220
|
+
* // Returns: "Thursday, March 14, 2024 at 2:30:45 PM GMT-7"
|
|
221
|
+
*/
|
|
222
|
+
formatDateTime(date, options) {
|
|
223
|
+
return this.localeConfig.formatDateTime(date, this.targetLocale, options);
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Formats a currency value according to the specified locales and options.
|
|
227
|
+
*
|
|
228
|
+
* @param {number} value - The currency value to format.
|
|
229
|
+
* @param {string} currency - The currency code (e.g., 'USD', 'EUR')
|
|
230
|
+
* @param {Object} [options] - Additional options for currency formatting.
|
|
231
|
+
* @param {string | string[]} [options.locales] - The locales to use for formatting.
|
|
232
|
+
* @param {Intl.NumberFormatOptions} [options] - Additional Intl.NumberFormat options.
|
|
233
|
+
* @returns {string} The formatted currency value.
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* gt.formatCurrency(1234.56, 'USD', { style: 'currency' });
|
|
237
|
+
* // Returns: "$1,234.56"
|
|
238
|
+
*/
|
|
239
|
+
formatCurrency(value, currency, options) {
|
|
240
|
+
return this.localeConfig.formatCurrency(value, currency, this.targetLocale, options);
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Formats a list of items according to the specified locales and options.
|
|
244
|
+
*
|
|
245
|
+
* @param {Array<string | number>} array - The list of items to format.
|
|
246
|
+
* @param {Object} [options] - Additional options for list formatting.
|
|
247
|
+
* @param {string | string[]} [options.locales] - The locales to use for formatting.
|
|
248
|
+
* @param {Intl.ListFormatOptions} [options] - Additional Intl.ListFormat options.
|
|
249
|
+
* @returns {string} The formatted list.
|
|
250
|
+
*
|
|
251
|
+
* @example
|
|
252
|
+
* gt.formatList(['apple', 'banana', 'orange'], { type: 'conjunction' });
|
|
253
|
+
* // Returns: "apple, banana, and orange"
|
|
254
|
+
*/
|
|
255
|
+
formatList(array, options) {
|
|
256
|
+
return this.localeConfig.formatList(array, this.targetLocale, options);
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Formats a list of items according to the specified locales and options.
|
|
260
|
+
* @param {Array<T>} array - The list of items to format.
|
|
261
|
+
* @param {Object} [options] - Additional options for list formatting.
|
|
262
|
+
* @param {string | string[]} [options.locales] - The locales to use for formatting.
|
|
263
|
+
* @param {Intl.ListFormatOptions} [options] - Additional Intl.ListFormat options.
|
|
264
|
+
* @returns {Array<T | string>} The formatted list parts.
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* gt.formatListToParts(['apple', 42, { foo: 'bar' }], { type: 'conjunction', style: 'short', locales: ['en'] });
|
|
268
|
+
* // Returns: ['apple', ', ', 42, ' and ', '{ foo: "bar" }']
|
|
269
|
+
*/
|
|
270
|
+
formatListToParts(array, options) {
|
|
271
|
+
return this.localeConfig.formatListToParts(array, this.targetLocale, options);
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Formats a relative time value according to the specified locales and options.
|
|
275
|
+
*
|
|
276
|
+
* @param {number} value - The relative time value to format.
|
|
277
|
+
* @param {Intl.RelativeTimeFormatUnit} unit - The unit of time (e.g., 'second', 'minute', 'hour', 'day', 'week', 'month', 'year')
|
|
278
|
+
* @param {Object} options - Additional options for relative time formatting.
|
|
279
|
+
* @param {string | string[]} [options.locales] - The locales to use for formatting.
|
|
280
|
+
* @param {Intl.RelativeTimeFormatOptions} [options] - Additional Intl.RelativeTimeFormat options.
|
|
281
|
+
* @returns {string} The formatted relative time string.
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* gt.formatRelativeTime(-1, 'day', { locales: ['en-US'], numeric: 'auto' });
|
|
285
|
+
* // Returns: "yesterday"
|
|
286
|
+
*/
|
|
287
|
+
formatRelativeTime(value, unit, options) {
|
|
288
|
+
return this.localeConfig.formatRelativeTime(value, unit, this.targetLocale, options);
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Formats a relative time string from a Date, automatically selecting the best unit.
|
|
292
|
+
*
|
|
293
|
+
* @param {Date} date - The date to format relative to now.
|
|
294
|
+
* @param {Object} [options] - Additional options for relative time formatting.
|
|
295
|
+
* @param {string | string[]} [options.locales] - The locales to use for formatting.
|
|
296
|
+
* @returns {string} The formatted relative time string (e.g., "2 hours ago", "in 3 days")
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* gt.formatRelativeTimeFromDate(new Date(Date.now() - 3600000));
|
|
300
|
+
* // Returns: "1 hour ago"
|
|
301
|
+
*/
|
|
302
|
+
formatRelativeTimeFromDate(date, options) {
|
|
303
|
+
return this.localeConfig.formatRelativeTimeFromDate(date, this.targetLocale, options);
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Retrieves the display name of a locale code using Intl.DisplayNames, returning an empty string if no name is found.
|
|
307
|
+
*
|
|
308
|
+
* @param {string} [locale=this.targetLocale] - A BCP-47 locale code.
|
|
309
|
+
* @returns {string} The display name corresponding to the code.
|
|
310
|
+
* @throws {Error} If no target locale is provided.
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* gt.getLocaleName('es-ES');
|
|
314
|
+
* // Returns: "Spanish (Spain)"
|
|
315
|
+
*/
|
|
316
|
+
getLocaleName(locale = this.targetLocale) {
|
|
317
|
+
if (!locale) throw new Error(require_runtimeTranslate.noTargetLocaleProvidedError("getLocaleName"));
|
|
318
|
+
return this.localeConfig.getLocaleName(locale);
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Retrieves an emoji based on a given locale code.
|
|
322
|
+
* Uses the locale's region (if present) to select an emoji or falls back on default emojis.
|
|
323
|
+
*
|
|
324
|
+
* @param {string} [locale=this.targetLocale] - A BCP-47 locale code (e.g., 'en-US', 'fr-CA')
|
|
325
|
+
* @returns {string} The emoji representing the locale or its region.
|
|
326
|
+
* @throws {Error} If no target locale is provided.
|
|
327
|
+
*
|
|
328
|
+
* @example
|
|
329
|
+
* gt.getLocaleEmoji('es-ES');
|
|
330
|
+
* // Returns: "🇪🇸"
|
|
331
|
+
*/
|
|
332
|
+
getLocaleEmoji(locale = this.targetLocale) {
|
|
333
|
+
if (!locale) throw new Error(require_runtimeTranslate.noTargetLocaleProvidedError("getLocaleEmoji"));
|
|
334
|
+
return this.localeConfig.getLocaleEmoji(locale);
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Generates linguistic details for a given locale code.
|
|
338
|
+
*
|
|
339
|
+
* This function returns information about the locale,
|
|
340
|
+
* script, and region of a given language code both in a standard form and in a maximized form (with likely script and region).
|
|
341
|
+
* The function provides these names in both your default language and native forms, and an associated emoji.
|
|
342
|
+
*
|
|
343
|
+
* @param {string} [locale=this.targetLocale] - The locale code to get properties for (e.g., "de-AT").
|
|
344
|
+
* @returns {LocaleProperties} - An object containing detailed information about the locale.
|
|
345
|
+
*
|
|
346
|
+
* @property {string} code - The full locale code, e.g., "de-AT".
|
|
347
|
+
* @property {string} name - Language name in the default display language, e.g., "Austrian German".
|
|
348
|
+
* @property {string} nativeName - Language name in the locale's native language, e.g., "Österreichisches Deutsch".
|
|
349
|
+
* @property {string} languageCode - The base language code, e.g., "de".
|
|
350
|
+
* @property {string} languageName - The language name in the default display language, e.g., "German".
|
|
351
|
+
* @property {string} nativeLanguageName - The language name in the native language, e.g., "Deutsch".
|
|
352
|
+
* @property {string} nameWithRegionCode - Language name with region in the default language, e.g., "German (AT)".
|
|
353
|
+
* @property {string} nativeNameWithRegionCode - Language name with region in the native language, e.g., "Deutsch (AT)".
|
|
354
|
+
* @property {string} regionCode - The region code from maximization, e.g., "AT".
|
|
355
|
+
* @property {string} regionName - The region name in the default display language, e.g., "Austria".
|
|
356
|
+
* @property {string} nativeRegionName - The region name in the native language, e.g., "Österreich".
|
|
357
|
+
* @property {string} scriptCode - The script code from maximization, e.g., "Latn".
|
|
358
|
+
* @property {string} scriptName - The script name in the default display language, e.g., "Latin".
|
|
359
|
+
* @property {string} nativeScriptName - The script name in the native language, e.g., "Lateinisch".
|
|
360
|
+
* @property {string} maximizedCode - The maximized locale code, e.g., "de-Latn-AT".
|
|
361
|
+
* @property {string} maximizedName - Maximized locale name with likely script in the default language, e.g., "Austrian German (Latin)".
|
|
362
|
+
* @property {string} nativeMaximizedName - Maximized locale name in the native language, e.g., "Österreichisches Deutsch (Lateinisch)".
|
|
363
|
+
* @property {string} minimizedCode - Minimized locale code, e.g., "de-AT" (or "de" for "de-DE").
|
|
364
|
+
* @property {string} minimizedName - Minimized language name in the default language, e.g., "Austrian German".
|
|
365
|
+
* @property {string} nativeMinimizedName - Minimized language name in the native language, e.g., "Österreichisches Deutsch".
|
|
366
|
+
* @property {string} emoji - The emoji associated with the locale's region, if applicable.
|
|
367
|
+
*/
|
|
368
|
+
getLocaleProperties(locale = this.targetLocale) {
|
|
369
|
+
if (!locale) throw new Error(require_runtimeTranslate.noTargetLocaleProvidedError("getLocaleProperties"));
|
|
370
|
+
return this.localeConfig.getLocaleProperties(locale);
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Retrieves multiple properties for a given region code, including:
|
|
374
|
+
* - `code`: the original region code
|
|
375
|
+
* - `name`: the localized display name
|
|
376
|
+
* - `emoji`: the associated flag or symbol
|
|
377
|
+
*
|
|
378
|
+
* Behavior:
|
|
379
|
+
* - Accepts ISO 3166-1 alpha-2 or UN M.49 region codes (e.g., `"US"`, `"FR"`, `"419"`).
|
|
380
|
+
* - Uses the instance's `targetLocale` to localize the region name for the user.
|
|
381
|
+
* - If `customMapping` contains a `name` or `emoji` for the region, those override the default values.
|
|
382
|
+
* - Otherwise, uses `Intl.DisplayNames` to get the localized region name, falling back to `libraryDefaultLocale`.
|
|
383
|
+
* - Falls back to the region code as `name` if display name resolution fails.
|
|
384
|
+
* - Falls back to a default emoji if no emoji mapping is found in built-in data or `customMapping`.
|
|
385
|
+
*
|
|
386
|
+
* @param {string} [region=this.getLocaleProperties().regionCode] - The region code to look up (e.g., `"US"`, `"GB"`, `"DE"`).
|
|
387
|
+
* @param {CustomRegionMapping} [customMapping] - Optional mapping of region codes to custom names and/or emojis.
|
|
388
|
+
* @returns {{ code: string, name: string, emoji: string }} An object containing:
|
|
389
|
+
* - `code`: the input region code
|
|
390
|
+
* - `name`: the localized or custom region name
|
|
391
|
+
* - `emoji`: the matching emoji flag or symbol
|
|
392
|
+
*
|
|
393
|
+
* @throws {Error} If no target locale is available to determine region properties.
|
|
394
|
+
*
|
|
395
|
+
* @example
|
|
396
|
+
* const gt = new GTRuntime({ targetLocale: 'en-US' });
|
|
397
|
+
* gt.getRegionProperties('US');
|
|
398
|
+
* // => { code: 'US', name: 'United States', emoji: '🇺🇸' }
|
|
399
|
+
*
|
|
400
|
+
* @example
|
|
401
|
+
* const gt = new GTRuntime({ targetLocale: 'fr-FR' });
|
|
402
|
+
* gt.getRegionProperties('US');
|
|
403
|
+
* // => { code: 'US', name: 'États-Unis', emoji: '🇺🇸' }
|
|
404
|
+
*
|
|
405
|
+
* @example
|
|
406
|
+
* gt.getRegionProperties('US', { US: { name: 'USA', emoji: '🗽' } });
|
|
407
|
+
* // => { code: 'US', name: 'USA', emoji: '🗽' }
|
|
408
|
+
*/
|
|
409
|
+
getRegionProperties(region = this.getLocaleProperties().regionCode, customMapping) {
|
|
410
|
+
if (!customMapping) {
|
|
411
|
+
if (this.customMapping && !this.customRegionMapping) {
|
|
412
|
+
const customRegionMapping = {};
|
|
413
|
+
for (const [locale, lp] of Object.entries(this.customMapping)) if (lp && typeof lp === "object" && lp.regionCode && !customRegionMapping[lp.regionCode]) {
|
|
414
|
+
const { regionName: name, emoji } = lp;
|
|
415
|
+
customRegionMapping[lp.regionCode] = {
|
|
416
|
+
locale,
|
|
417
|
+
...name && { name },
|
|
418
|
+
...emoji && { emoji }
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
this.customRegionMapping = customRegionMapping;
|
|
422
|
+
}
|
|
423
|
+
customMapping = this.customRegionMapping;
|
|
424
|
+
}
|
|
425
|
+
return (0, _generaltranslation_format.getRegionProperties)(region, this.targetLocale, customMapping);
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Determines whether a translation is required based on the source and target locales.
|
|
429
|
+
*
|
|
430
|
+
* @param {string} [sourceLocale=this.sourceLocale] - The locale code for the original content.
|
|
431
|
+
* @param {string} [targetLocale=this.targetLocale] - The locale code to translate into.
|
|
432
|
+
* @param {string[]} [approvedLocales=this.locales] - Optional array of approved target locales.
|
|
433
|
+
* @returns {boolean} True if translation is required, false otherwise
|
|
434
|
+
* @throws {Error} If no source locale is provided.
|
|
435
|
+
* @throws {Error} If no target locale is provided.
|
|
436
|
+
*
|
|
437
|
+
* @example
|
|
438
|
+
* gt.requiresTranslation('en-US', 'es-ES');
|
|
439
|
+
* // Returns: true
|
|
440
|
+
*/
|
|
441
|
+
requiresTranslation(sourceLocale = this.sourceLocale, targetLocale = this.targetLocale, approvedLocales = this.locales, customMapping = this.customMapping) {
|
|
442
|
+
if (!sourceLocale) throw new Error(require_runtimeTranslate.noSourceLocaleProvidedError("requiresTranslation"));
|
|
443
|
+
if (!targetLocale) throw new Error(require_runtimeTranslate.noTargetLocaleProvidedError("requiresTranslation"));
|
|
444
|
+
if (customMapping === this.customMapping) return this.localeConfig.requiresTranslation(targetLocale, sourceLocale, approvedLocales);
|
|
445
|
+
return (0, _generaltranslation_format.requiresTranslation)(sourceLocale, targetLocale, approvedLocales, customMapping);
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Determines the best matching locale from the provided approved locales list.
|
|
449
|
+
*
|
|
450
|
+
* @param {string | string[]} locales - A single locale or array of locales in preference order.
|
|
451
|
+
* @param {string[]} [approvedLocales=this.locales] - Array of approved locales in preference order.
|
|
452
|
+
* @returns {string | undefined} The best matching locale, or undefined if no match is found.
|
|
453
|
+
*
|
|
454
|
+
* @example
|
|
455
|
+
* gt.determineLocale(['fr-CA', 'fr-FR'], ['en-US', 'fr-FR', 'es-ES']);
|
|
456
|
+
* // Returns: "fr-FR"
|
|
457
|
+
*/
|
|
458
|
+
determineLocale(locales, approvedLocales = this.locales || [], customMapping = this.customMapping) {
|
|
459
|
+
if (customMapping === this.customMapping) return this.localeConfig.determineLocale(locales, approvedLocales ?? []);
|
|
460
|
+
return new _generaltranslation_format.LocaleConfig({ customMapping }).determineLocale(locales, approvedLocales ?? []);
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Gets the text direction for a given locale code.
|
|
464
|
+
*
|
|
465
|
+
* @param {string} [locale=this.targetLocale] - A BCP-47 locale code.
|
|
466
|
+
* @returns {'ltr' | 'rtl'} 'rtl' if the locale is right-to-left; otherwise 'ltr'.
|
|
467
|
+
* @throws {Error} If no target locale is provided.
|
|
468
|
+
*
|
|
469
|
+
* @example
|
|
470
|
+
* gt.getLocaleDirection('ar-SA');
|
|
471
|
+
* // Returns: "rtl"
|
|
472
|
+
*/
|
|
473
|
+
getLocaleDirection(locale = this.targetLocale) {
|
|
474
|
+
if (!locale) throw new Error(require_runtimeTranslate.noTargetLocaleProvidedError("getLocaleDirection"));
|
|
475
|
+
return this.localeConfig.getLocaleDirection(locale);
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Checks if a given BCP 47 locale code is valid.
|
|
479
|
+
*
|
|
480
|
+
* @param {string} [locale=this.targetLocale] - The BCP 47 locale code to validate.
|
|
481
|
+
* @param {CustomMapping} [customMapping=this.customMapping] - The custom mapping to use for validation.
|
|
482
|
+
* @returns {boolean} True if the locale code is valid, false otherwise
|
|
483
|
+
* @throws {Error} If no target locale is provided.
|
|
484
|
+
*
|
|
485
|
+
* @example
|
|
486
|
+
* gt.isValidLocale('en-US');
|
|
487
|
+
* // Returns: true
|
|
488
|
+
*/
|
|
489
|
+
isValidLocale(locale = this.targetLocale, customMapping = this.customMapping) {
|
|
490
|
+
if (!locale) throw new Error(require_runtimeTranslate.noTargetLocaleProvidedError("isValidLocale"));
|
|
491
|
+
if (customMapping === this.customMapping) return this.localeConfig.isValidLocale(locale);
|
|
492
|
+
return (0, _generaltranslation_format.isValidLocale)(locale, customMapping);
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* Resolves the canonical locale for a given locale.
|
|
496
|
+
* @param locale - The locale to resolve the canonical locale for
|
|
497
|
+
* @param customMapping - The custom mapping to use for resolving the canonical locale
|
|
498
|
+
* @returns The canonical locale, or the input locale when no canonical mapping exists.
|
|
499
|
+
*/
|
|
500
|
+
resolveCanonicalLocale(locale = this.targetLocale, customMapping = this.customMapping) {
|
|
501
|
+
if (!locale) throw new Error(require_runtimeTranslate.noTargetLocaleProvidedError("resolveCanonicalLocale"));
|
|
502
|
+
if (customMapping === this.customMapping) return this.localeConfig.resolveCanonicalLocale(locale);
|
|
503
|
+
return (0, _generaltranslation_format.resolveCanonicalLocale)(locale, customMapping);
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Resolves the alias locale for a given locale.
|
|
507
|
+
* @param locale - The locale to resolve the alias locale for
|
|
508
|
+
* @param customMapping - The custom mapping to use for resolving the alias locale
|
|
509
|
+
* @returns The configured alias for a canonical locale, or the input locale when already an alias or no alias mapping exists.
|
|
510
|
+
*/
|
|
511
|
+
resolveAliasLocale(locale, customMapping = this.customMapping) {
|
|
512
|
+
if (!locale) throw new Error(require_runtimeTranslate.noTargetLocaleProvidedError("resolveAliasLocale"));
|
|
513
|
+
if (customMapping === this.customMapping) return this.localeConfig.resolveAliasLocale(locale);
|
|
514
|
+
return (0, _generaltranslation_format.resolveAliasLocale)(locale, customMapping);
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Standardizes a BCP 47 locale code to ensure correct formatting.
|
|
518
|
+
*
|
|
519
|
+
* @param {string} [locale=this.targetLocale] - The BCP 47 locale code to standardize.
|
|
520
|
+
* @returns {string} The standardized locale code, or the input string if it cannot be standardized.
|
|
521
|
+
* @throws {Error} If no target locale is provided.
|
|
522
|
+
*
|
|
523
|
+
* @example
|
|
524
|
+
* gt.standardizeLocale('en_us');
|
|
525
|
+
* // Returns: "en-US"
|
|
526
|
+
*/
|
|
527
|
+
standardizeLocale(locale = this.targetLocale) {
|
|
528
|
+
if (!locale) throw new Error(require_runtimeTranslate.noTargetLocaleProvidedError("standardizeLocale"));
|
|
529
|
+
return this.localeConfig.standardizeLocale(locale);
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Checks if multiple BCP 47 locale codes represent the same dialect.
|
|
533
|
+
*
|
|
534
|
+
* @param {...(string | string[])} locales - The BCP 47 locale codes to compare.
|
|
535
|
+
* @returns {boolean} True if all codes represent the same dialect, false otherwise
|
|
536
|
+
*
|
|
537
|
+
* @example
|
|
538
|
+
* gt.isSameDialect('en-US', 'en-GB');
|
|
539
|
+
* // Returns: false
|
|
540
|
+
*
|
|
541
|
+
* gt.isSameDialect('en', 'en-US');
|
|
542
|
+
* // Returns: true
|
|
543
|
+
*/
|
|
544
|
+
isSameDialect(...locales) {
|
|
545
|
+
return this.localeConfig.isSameDialect(...locales);
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Checks if multiple BCP 47 locale codes represent the same language.
|
|
549
|
+
*
|
|
550
|
+
* @param {...(string | string[])} locales - The BCP 47 locale codes to compare.
|
|
551
|
+
* @returns {boolean} True if all codes represent the same language, false otherwise
|
|
552
|
+
*
|
|
553
|
+
* @example
|
|
554
|
+
* gt.isSameLanguage('en-US', 'en-GB');
|
|
555
|
+
* // Returns: true
|
|
556
|
+
*/
|
|
557
|
+
isSameLanguage(...locales) {
|
|
558
|
+
return this.localeConfig.isSameLanguage(...locales);
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Checks if a locale is a superset of another locale.
|
|
562
|
+
*
|
|
563
|
+
* @param {string} superLocale - The locale to check if it is a superset
|
|
564
|
+
* @param {string} subLocale - The locale to check if it is a subset
|
|
565
|
+
* @returns {boolean} True if superLocale is a superset of subLocale, false otherwise
|
|
566
|
+
*
|
|
567
|
+
* @example
|
|
568
|
+
* gt.isSupersetLocale('en', 'en-US');
|
|
569
|
+
* // Returns: true
|
|
570
|
+
*
|
|
571
|
+
* gt.isSupersetLocale('en-US', 'en');
|
|
572
|
+
* // Returns: false
|
|
573
|
+
*/
|
|
574
|
+
isSupersetLocale(superLocale, subLocale) {
|
|
575
|
+
return this.localeConfig.isSupersetLocale(superLocale, subLocale);
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
//#endregion
|
|
579
|
+
exports.GTRuntime = GTRuntime;
|
|
580
|
+
|
|
581
|
+
//# sourceMappingURL=runtime.cjs.map
|