generaltranslation 2.0.65 → 2.0.67

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.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = _determineLanguage;
4
+ var codes_1 = require("./codes");
5
+ /**
6
+ * Given a list of language and a list of approved language, sorted in preference order
7
+ * Determines which language of the given languages is the best match in the approvedLanguages, prioritizing exact matches and falling back to dialects of the same language
8
+ * @internal
9
+ */
10
+ function _determineLanguage(languages, approvedLanguages) {
11
+ if (typeof languages === 'string')
12
+ languages = [languages];
13
+ if (!approvedLanguages)
14
+ return languages[0];
15
+ var _loop_1 = function (language) {
16
+ var exactMatch = approvedLanguages.find(function (approvedLanguage) { return approvedLanguage === language; });
17
+ if (exactMatch)
18
+ return { value: exactMatch };
19
+ var sameLanguage = approvedLanguages.find(function (approvedLanguage) { return (0, codes_1._isSameLanguage)(approvedLanguage, language); });
20
+ if (sameLanguage)
21
+ return { value: sameLanguage };
22
+ };
23
+ for (var _i = 0, languages_1 = languages; _i < languages_1.length; _i++) {
24
+ var language = languages_1[_i];
25
+ var state_1 = _loop_1(language);
26
+ if (typeof state_1 === "object")
27
+ return state_1.value;
28
+ }
29
+ return undefined;
30
+ }
package/dist/index.d.ts CHANGED
@@ -32,14 +32,14 @@ declare class GT {
32
32
  * If `metadata.save` is provided, the translation is cached for use in a public project.
33
33
  *
34
34
  * @param {Content} content - The string or array of strings/variables to be translated.
35
- * @param {string} targetLanguage - The target language code (e.g., 'en', 'fr') for the translation.
35
+ * @param {string} language - The target language code (e.g., 'en', 'fr') for the translation.
36
36
  * @param {{ context?: string, save?: boolean, [key: string]: any }} [metadata] - Additional metadata for the translation request.
37
37
  * @param {string} [metadata.context] - Contextual information to assist with the translation.
38
38
  * @param {boolean} [metadata.save] - Whether to cache the translation for use in a public project.
39
39
  *
40
40
  * @returns {Promise<ContentTranslationResult>} A promise that resolves to the translated content, or an error if the translation fails.
41
41
  */
42
- translate(content: Content, targetLanguage: string, metadata?: {
42
+ translate(content: Content, language: string, metadata?: {
43
43
  context?: string;
44
44
  save?: boolean;
45
45
  [key: string]: any;
@@ -52,12 +52,12 @@ declare class GT {
52
52
  *
53
53
  * @param {Object} params - The parameters for the translation.
54
54
  * @param {ReactChildrenAsObject} params.children - The React children content to be translated.
55
- * @param {string} params.targetLanguage - The target language for the translation.
55
+ * @param {string} params.language - The target language for the translation.
56
56
  * @param {Object} params.metadata - Additional metadata for the translation process.
57
57
  *
58
58
  * @returns {Promise<ReactTranslationResult>} - A promise that resolves to the translated content.
59
59
  */
60
- translateReact(children: ReactChildrenAsObject, targetLanguage: string, metadata?: {
60
+ translateReact(children: ReactChildrenAsObject, language: string, metadata?: {
61
61
  context?: string;
62
62
  save?: boolean;
63
63
  [key: string]: any;
@@ -70,7 +70,7 @@ declare class GT {
70
70
  translateBundle(requests: Request[]): Promise<Array<ReactTranslationResult | ContentTranslationResult>>;
71
71
  /**
72
72
  * Pushes updates to a remotely cached translation dictionary.
73
- * @param {Update[]} updates - Array of updates with optional targetLanguage.
73
+ * @param {Update[]} updates - Array of updates.
74
74
  * @param {string[]} [languages] - Array of languages to be updated.
75
75
  * @param {string} [projectID=this.projectID] - The ID of the project. Defaults to the instance's projectID.
76
76
  * @param {boolean} [replace=false] - Whether to replace the existing dictionary. Defaults to false.
@@ -200,3 +200,22 @@ export declare function splitStringToContent(string: string): Content;
200
200
  export declare function renderContentToString<V extends Record<string, any>>(content: Content, languages?: string | string[], variables?: V, variableOptions?: {
201
201
  [key in keyof V]?: Intl.NumberFormatOptions | Intl.DateTimeFormatOptions;
202
202
  }): string;
203
+ /**
204
+ * Determines the best matching language from the approved languages list based on a provided
205
+ * list of preferred languages. The function prioritizes exact matches, but will also consider
206
+ * dialects of the same language if an exact match is not available.
207
+ *
208
+ * It also respects the order of preference in both the provided languages list and the
209
+ * approved languages list. A dialect match of a higher-preference language is considered better
210
+ * than an exact match of a lower-preference language.
211
+ *
212
+ * For example, if the `languages` list is ['en', 'fr'], and the `approvedLanguages` list is
213
+ * ['fr', 'en-GB'], it will prefer 'en-GB' over 'fr', even though 'fr' has an exact
214
+ * dialect match, because 'en' appears earlier in the `languages` list.
215
+ *
216
+ * @param {string | string[]} languages - A single language or an array of languages sorted in preference order.
217
+ * @param {string[]} approvedLanguages - An array of approved languages, also sorted by preference.
218
+ *
219
+ * @returns {string | undefined} - The best matching language from the approvedLanguages list, or undefined if no match is found.
220
+ */
221
+ export declare function determineLanguage(languages: string | string[], approvedLanguages: string[]): string | undefined;
package/dist/index.js CHANGED
@@ -62,6 +62,7 @@ exports.formatDateTime = formatDateTime;
62
62
  exports.formatCurrency = formatCurrency;
63
63
  exports.splitStringToContent = splitStringToContent;
64
64
  exports.renderContentToString = renderContentToString;
65
+ exports.determineLanguage = determineLanguage;
65
66
  // ----- IMPORTS ----- //
66
67
  var codes_1 = require("./codes/codes");
67
68
  var getLanguageDirection_1 = __importDefault(require("./codes/getLanguageDirection"));
@@ -71,6 +72,7 @@ var _translateReact_1 = __importDefault(require("./translation/react/_translateR
71
72
  var _updateProjectDictionary_1 = __importDefault(require("./translation/dictionaries/_updateProjectDictionary"));
72
73
  var _format_1 = require("./formatting/_format");
73
74
  var _string_content_1 = require("./formatting/_string_content");
75
+ var determineLanguage_1 = __importDefault(require("./codes/determineLanguage"));
74
76
  // ----- CORE CLASS ----- //
75
77
  var getDefaultFromEnv = function (VARIABLE) {
76
78
  if (typeof process !== 'undefined' && process.env) {
@@ -103,18 +105,18 @@ var GT = /** @class */ (function () {
103
105
  * If `metadata.save` is provided, the translation is cached for use in a public project.
104
106
  *
105
107
  * @param {Content} content - The string or array of strings/variables to be translated.
106
- * @param {string} targetLanguage - The target language code (e.g., 'en', 'fr') for the translation.
108
+ * @param {string} language - The target language code (e.g., 'en', 'fr') for the translation.
107
109
  * @param {{ context?: string, save?: boolean, [key: string]: any }} [metadata] - Additional metadata for the translation request.
108
110
  * @param {string} [metadata.context] - Contextual information to assist with the translation.
109
111
  * @param {boolean} [metadata.save] - Whether to cache the translation for use in a public project.
110
112
  *
111
113
  * @returns {Promise<ContentTranslationResult>} A promise that resolves to the translated content, or an error if the translation fails.
112
114
  */
113
- GT.prototype.translate = function (content, targetLanguage, metadata) {
115
+ GT.prototype.translate = function (content, language, metadata) {
114
116
  return __awaiter(this, void 0, void 0, function () {
115
117
  return __generator(this, function (_a) {
116
118
  switch (_a.label) {
117
- case 0: return [4 /*yield*/, (0, _translate_1.default)(this, content, targetLanguage, __assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata))];
119
+ case 0: return [4 /*yield*/, (0, _translate_1.default)(this, content, language, __assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata))];
118
120
  case 1: return [2 /*return*/, _a.sent()];
119
121
  }
120
122
  });
@@ -125,16 +127,16 @@ var GT = /** @class */ (function () {
125
127
  *
126
128
  * @param {Object} params - The parameters for the translation.
127
129
  * @param {ReactChildrenAsObject} params.children - The React children content to be translated.
128
- * @param {string} params.targetLanguage - The target language for the translation.
130
+ * @param {string} params.language - The target language for the translation.
129
131
  * @param {Object} params.metadata - Additional metadata for the translation process.
130
132
  *
131
133
  * @returns {Promise<ReactTranslationResult>} - A promise that resolves to the translated content.
132
134
  */
133
- GT.prototype.translateReact = function (children, targetLanguage, metadata) {
135
+ GT.prototype.translateReact = function (children, language, metadata) {
134
136
  return __awaiter(this, void 0, void 0, function () {
135
137
  return __generator(this, function (_a) {
136
138
  switch (_a.label) {
137
- case 0: return [4 /*yield*/, (0, _translateReact_1.default)(this, children, targetLanguage, __assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata))];
139
+ case 0: return [4 /*yield*/, (0, _translateReact_1.default)(this, children, language, __assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata))];
138
140
  case 1: return [2 /*return*/, _a.sent()];
139
141
  }
140
142
  });
@@ -154,7 +156,7 @@ var GT = /** @class */ (function () {
154
156
  };
155
157
  /**
156
158
  * Pushes updates to a remotely cached translation dictionary.
157
- * @param {Update[]} updates - Array of updates with optional targetLanguage.
159
+ * @param {Update[]} updates - Array of updates.
158
160
  * @param {string[]} [languages] - Array of languages to be updated.
159
161
  * @param {string} [projectID=this.projectID] - The ID of the project. Defaults to the instance's projectID.
160
162
  * @param {boolean} [replace=false] - Whether to replace the existing dictionary. Defaults to false.
@@ -278,3 +280,24 @@ function splitStringToContent(string) {
278
280
  function renderContentToString(content, languages, variables, variableOptions) {
279
281
  return (0, _string_content_1._renderContentToString)(content, languages, variables, variableOptions);
280
282
  }
283
+ /**
284
+ * Determines the best matching language from the approved languages list based on a provided
285
+ * list of preferred languages. The function prioritizes exact matches, but will also consider
286
+ * dialects of the same language if an exact match is not available.
287
+ *
288
+ * It also respects the order of preference in both the provided languages list and the
289
+ * approved languages list. A dialect match of a higher-preference language is considered better
290
+ * than an exact match of a lower-preference language.
291
+ *
292
+ * For example, if the `languages` list is ['en', 'fr'], and the `approvedLanguages` list is
293
+ * ['fr', 'en-GB'], it will prefer 'en-GB' over 'fr', even though 'fr' has an exact
294
+ * dialect match, because 'en' appears earlier in the `languages` list.
295
+ *
296
+ * @param {string | string[]} languages - A single language or an array of languages sorted in preference order.
297
+ * @param {string[]} approvedLanguages - An array of approved languages, also sorted by preference.
298
+ *
299
+ * @returns {string | undefined} - The best matching language from the approvedLanguages list, or undefined if no match is found.
300
+ */
301
+ function determineLanguage(languages, approvedLanguages) {
302
+ return (0, determineLanguage_1.default)(languages, approvedLanguages);
303
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "generaltranslation",
3
- "version": "2.0.65",
3
+ "version": "2.0.67",
4
4
  "description": "A language toolkit for AI developers",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",