generaltranslation 2.0.53 → 2.0.54

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. package/dist/mjs/codes/15924/CodeToScript.json +215 -0
  2. package/dist/mjs/codes/15924/ScriptToCode.json +215 -0
  3. package/dist/mjs/codes/3166/CodeToRegion.json +296 -0
  4. package/dist/mjs/codes/3166/RegionToCode.json +296 -0
  5. package/dist/mjs/codes/639-1/CodeToLanguage.json +185 -0
  6. package/dist/mjs/codes/639-1/LanguageToCode.json +227 -0
  7. package/dist/mjs/codes/639-3/CodeToLanguageTriletter.json +186 -0
  8. package/dist/mjs/codes/639-3/LanguageToCodeTriletter.json +228 -0
  9. package/dist/mjs/codes/codes.d.ts +8 -0
  10. package/dist/mjs/codes/codes.js +306 -0
  11. package/dist/mjs/codes/getLanguageDirection.d.ts +1 -0
  12. package/dist/mjs/codes/getLanguageDirection.js +68 -0
  13. package/dist/mjs/codes/predefined/Predefined.json +28 -0
  14. package/dist/mjs/index.d.ts +153 -0
  15. package/dist/mjs/index.js +149 -0
  16. package/dist/mjs/translation/_bundleTranslation.d.ts +22 -0
  17. package/dist/mjs/translation/_bundleTranslation.js +51 -0
  18. package/dist/mjs/translation/_intl.d.ts +1 -0
  19. package/dist/mjs/translation/_intl.js +60 -0
  20. package/dist/mjs/translation/_translate.d.ts +1 -0
  21. package/dist/mjs/translation/_translate.js +59 -0
  22. package/dist/mjs/translation/_translateReactChildren.d.ts +1 -0
  23. package/dist/mjs/translation/_translateReactChildren.js +64 -0
  24. package/dist/mjs/translation/_updateRemoteDictionary.d.ts +17 -0
  25. package/dist/mjs/translation/_updateRemoteDictionary.js +38 -0
  26. package/package.json +12 -5
  27. /package/dist/{codes → cjs/codes}/15924/CodeToScript.json +0 -0
  28. /package/dist/{codes → cjs/codes}/15924/ScriptToCode.json +0 -0
  29. /package/dist/{codes → cjs/codes}/3166/CodeToRegion.json +0 -0
  30. /package/dist/{codes → cjs/codes}/3166/RegionToCode.json +0 -0
  31. /package/dist/{codes → cjs/codes}/639-1/CodeToLanguage.json +0 -0
  32. /package/dist/{codes → cjs/codes}/639-1/LanguageToCode.json +0 -0
  33. /package/dist/{codes → cjs/codes}/639-3/CodeToLanguageTriletter.json +0 -0
  34. /package/dist/{codes → cjs/codes}/639-3/LanguageToCodeTriletter.json +0 -0
  35. /package/dist/{codes → cjs/codes}/codes.d.ts +0 -0
  36. /package/dist/{codes → cjs/codes}/codes.js +0 -0
  37. /package/dist/{codes → cjs/codes}/getLanguageDirection.d.ts +0 -0
  38. /package/dist/{codes → cjs/codes}/getLanguageDirection.js +0 -0
  39. /package/dist/{codes → cjs/codes}/predefined/Predefined.json +0 -0
  40. /package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
  41. /package/dist/{index.js → cjs/index.js} +0 -0
  42. /package/dist/{translation → cjs/translation}/_bundleTranslation.d.ts +0 -0
  43. /package/dist/{translation → cjs/translation}/_bundleTranslation.js +0 -0
  44. /package/dist/{translation → cjs/translation}/_intl.d.ts +0 -0
  45. /package/dist/{translation → cjs/translation}/_intl.js +0 -0
  46. /package/dist/{translation → cjs/translation}/_translate.d.ts +0 -0
  47. /package/dist/{translation → cjs/translation}/_translate.js +0 -0
  48. /package/dist/{translation → cjs/translation}/_translateReactChildren.d.ts +0 -0
  49. /package/dist/{translation → cjs/translation}/_translateReactChildren.js +0 -0
  50. /package/dist/{translation → cjs/translation}/_updateRemoteDictionary.d.ts +0 -0
  51. /package/dist/{translation → cjs/translation}/_updateRemoteDictionary.js +0 -0
@@ -0,0 +1,153 @@
1
+ import { LanguageObject } from './codes/codes';
2
+ import { Request } from './translation/_bundleTranslation';
3
+ import { Update } from './translation/_updateRemoteDictionary';
4
+ /**
5
+ * Type representing the constructor parameters for the GT class.
6
+ */
7
+ type GTConstructorParams = {
8
+ apiKey?: string;
9
+ defaultLanguage?: string;
10
+ projectID?: string;
11
+ baseURL?: string;
12
+ };
13
+ /**
14
+ * GT is the core driver for the General Translation library.
15
+ */
16
+ declare class GT {
17
+ apiKey: string;
18
+ defaultLanguage: string;
19
+ projectID: string;
20
+ baseURL: string;
21
+ /**
22
+ * Constructs an instance of the GT class.
23
+ *
24
+ * @param {GTConstructorParams} [params] - The parameters for initializing the GT instance.
25
+ * @param {string} [params.apiKey=''] - The API key for accessing the translation service.
26
+ * @param {string} [params.defaultLanguage='en'] - The default language for translations.
27
+ * @param {string} [params.projectID=''] - The project ID for the translation service.
28
+ * @param {string} [params.baseURL='https://prod.gtx.dev'] - The base URL for the translation service.
29
+ */
30
+ constructor({ apiKey, defaultLanguage, projectID, baseURL }?: GTConstructorParams);
31
+ /**
32
+ * Translates a string into a target language.
33
+ * @param {string} content - A string to translate.
34
+ * @param {string} targetLanguage - The target language for the translation.
35
+ * @param {{ notes?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
36
+ * @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
37
+ */
38
+ translate(content: string, targetLanguage: string, metadata?: {
39
+ notes?: string;
40
+ [key: string]: any;
41
+ }): Promise<{
42
+ translation: string;
43
+ error?: Error | unknown;
44
+ }>;
45
+ /**
46
+ * Translates a string and caches for use in a public project.
47
+ * @param {string} content - A string to translate.
48
+ * @param {string} targetLanguage - The target language for the translation.
49
+ * @param {string} projectID - The ID of the project.
50
+ * @param {dictionaryName?: string, context?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
51
+ * @returns {Promise<{ translation: string, error?: Error | unknown }>} The translated content with optional error information.
52
+ */
53
+ intl(content: string, targetLanguage: string, projectID?: string, metadata?: {
54
+ dictionaryName?: string;
55
+ context?: string;
56
+ [key: string]: any;
57
+ }): Promise<{
58
+ translation: string;
59
+ error?: Error | unknown;
60
+ }>;
61
+ /**
62
+ * Translates the content of React children elements.
63
+ *
64
+ * @param {Object} params - The parameters for the translation.
65
+ * @param {any} params.content - The React children content to be translated.
66
+ * @param {string} params.targetLanguage - The target language for the translation.
67
+ * @param {Object} params.metadata - Additional metadata for the translation process.
68
+ *
69
+ * @returns {Promise<any>} - A promise that resolves to the translated content.
70
+ */
71
+ translateReactChildren(content: any, targetLanguage: string, metadata?: {
72
+ [key: string]: any;
73
+ }): Promise<{
74
+ translation: any | null;
75
+ error?: Error | unknown;
76
+ }>;
77
+ /**
78
+ * Bundles multiple translation requests and sends them to the server.
79
+ * @param requests - Array of requests to be processed and sent.
80
+ * @returns A promise that resolves to an array of processed results.
81
+ */
82
+ bundleTranslation(requests: Request[]): Promise<Array<any | null>>;
83
+ /**
84
+ * Pushes updates to a remotely cached translation dictionary.
85
+ * @param {Update[]} updates - Array of updates with optional targetLanguage.
86
+ * @param {string[]} [languages] - Array of languages to be updated.
87
+ * @param {string} [projectID=this.projectID] - The ID of the project. Defaults to the instance's projectID.
88
+ * @param {boolean} [replace=false] - Whether to replace the existing dictionary. Defaults to false.
89
+ * @returns {Promise<string[]>} A promise that resolves to an array of strings indicating the languages which have been updated.
90
+ */
91
+ updateRemoteDictionary(updates: Update[], languages?: string[], projectID?: string, replace?: boolean): Promise<string[]>;
92
+ }
93
+ export default GT;
94
+ /**
95
+ * Gets the writing direction for a given BCP 47 language code.
96
+ * @param {string} code - The BCP 47 language code to check.
97
+ * @returns {string} The language direction ('ltr' for left-to-right or 'rtl' for right-to-left).
98
+ */
99
+ export declare const getLanguageDirection: (code: string) => string;
100
+ /**
101
+ * Checks if a given BCP 47 language code is valid.
102
+ * @param {string} code - The BCP 47 language code to validate.
103
+ * @returns {boolean} True if the BCP 47 code is valid, false otherwise.
104
+ */
105
+ export declare const isValidLanguageCode: (code: string) => boolean;
106
+ /**
107
+ * Standardizes a BCP 47 language code to ensure correct formatting.
108
+ * @param {string} code - The BCP 47 language code to standardize.
109
+ * @returns {string} The standardized BCP 47 language code.
110
+ */
111
+ export declare const standardizeLanguageCode: (code: string) => string;
112
+ /**
113
+ * Gets a language object from a BCP 47 language code.
114
+ * @param {string} code - The BCP 47 language code to convert.
115
+ * @returns {LanguageObject | null} The language object or null if the BCP 47 code is invalid.
116
+ */
117
+ export declare function getLanguageObject(code: string): LanguageObject | null;
118
+ /**
119
+ * Gets language objects from multiple BCP 47 language codes.
120
+ * @param {string[]} codes - The BCP 47 language codes to convert.
121
+ * @returns {(LanguageObject | null)[]} An array of language objects or null for each invalid BCP 47 code.
122
+ */
123
+ export declare function getLanguageObject(codes: string[]): (LanguageObject | null)[];
124
+ /**
125
+ * Gets a BCP 47 language code from a language name.
126
+ * @param {string} language - The language name to convert.
127
+ * @returns {string} The corresponding BCP 47 language code.
128
+ */
129
+ export declare function getLanguageCode(language: string): string;
130
+ /**
131
+ * Gets BCP 47 language codes from multiple language names.
132
+ * @param {string[]} languages - The language names to convert.
133
+ * @returns {string[]} The corresponding BCP 47 language codes.
134
+ */
135
+ export declare function getLanguageCode(languages: string[]): string[];
136
+ /**
137
+ * Gets a language name from a BCP 47 language code.
138
+ * @param {string} code - The BCP 47 language code to convert.
139
+ * @returns {string} The corresponding language name.
140
+ */
141
+ export declare function getLanguageName(code: string): string;
142
+ /**
143
+ * Gets language names from multiple BCP 47 language codes.
144
+ * @param {string[]} codes - The BCP 47 language codes to convert.
145
+ * @returns {string[]} The corresponding language names.
146
+ */
147
+ export declare function getLanguageName(codes: string[]): string[];
148
+ /**
149
+ * Checks if multiple BCP 47 language codes represent the same language.
150
+ * @param {string[]} codes - The BCP 47 language codes to compare.
151
+ * @returns {boolean} True if all BCP 47 codes represent the same language, false otherwise.
152
+ */
153
+ export declare function isSameLanguage(...codes: string[]): boolean;
@@ -0,0 +1,149 @@
1
+ // `generaltranslation` language toolkit
2
+ // © 2024, General Translation, Inc.
3
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5
+ return new (P || (P = Promise))(function (resolve, reject) {
6
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
10
+ });
11
+ };
12
+ // ----- IMPORTS ----- //
13
+ import { _isValidLanguageCode, _standardizeLanguageCode, _getLanguageObject, _getLanguageCode, _getLanguageName, _isSameLanguage } from './codes/codes';
14
+ import _getLanguageDirection from './codes/getLanguageDirection';
15
+ import _bundleTranslation from './translation/_bundleTranslation';
16
+ import _intl from './translation/_intl';
17
+ import _translate from './translation/_translate';
18
+ import _translateReactChildren from './translation/_translateReactChildren';
19
+ import _updateRemoteDictionary from './translation/_updateRemoteDictionary';
20
+ // ----- CORE CLASS ----- //
21
+ const getDefaultFromEnv = (VARIABLE) => {
22
+ if (typeof process !== 'undefined' && process.env) {
23
+ return process.env[VARIABLE] || '';
24
+ }
25
+ return '';
26
+ };
27
+ /**
28
+ * GT is the core driver for the General Translation library.
29
+ */
30
+ class GT {
31
+ /**
32
+ * Constructs an instance of the GT class.
33
+ *
34
+ * @param {GTConstructorParams} [params] - The parameters for initializing the GT instance.
35
+ * @param {string} [params.apiKey=''] - The API key for accessing the translation service.
36
+ * @param {string} [params.defaultLanguage='en'] - The default language for translations.
37
+ * @param {string} [params.projectID=''] - The project ID for the translation service.
38
+ * @param {string} [params.baseURL='https://prod.gtx.dev'] - The base URL for the translation service.
39
+ */
40
+ constructor({ apiKey = '', defaultLanguage = 'en', projectID = '', baseURL = 'https://prod.gtx.dev' } = {}) {
41
+ this.apiKey = apiKey || getDefaultFromEnv('GT_API_KEY');
42
+ this.projectID = projectID || getDefaultFromEnv('GT_PROJECT_ID');
43
+ this.defaultLanguage = defaultLanguage.toLowerCase();
44
+ this.baseURL = baseURL;
45
+ }
46
+ /**
47
+ * Translates a string into a target language.
48
+ * @param {string} content - A string to translate.
49
+ * @param {string} targetLanguage - The target language for the translation.
50
+ * @param {{ notes?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
51
+ * @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
52
+ */
53
+ translate(content, targetLanguage, metadata) {
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ return yield _translate(this, content, targetLanguage, Object.assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata));
56
+ });
57
+ }
58
+ /**
59
+ * Translates a string and caches for use in a public project.
60
+ * @param {string} content - A string to translate.
61
+ * @param {string} targetLanguage - The target language for the translation.
62
+ * @param {string} projectID - The ID of the project.
63
+ * @param {dictionaryName?: string, context?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
64
+ * @returns {Promise<{ translation: string, error?: Error | unknown }>} The translated content with optional error information.
65
+ */
66
+ intl(content, targetLanguage, projectID, metadata) {
67
+ return __awaiter(this, void 0, void 0, function* () {
68
+ return yield _intl(this, content, targetLanguage, projectID || this.projectID, Object.assign({ projectID: projectID || this.projectID, defaultLanguage: this.defaultLanguage }, metadata));
69
+ });
70
+ }
71
+ /**
72
+ * Translates the content of React children elements.
73
+ *
74
+ * @param {Object} params - The parameters for the translation.
75
+ * @param {any} params.content - The React children content to be translated.
76
+ * @param {string} params.targetLanguage - The target language for the translation.
77
+ * @param {Object} params.metadata - Additional metadata for the translation process.
78
+ *
79
+ * @returns {Promise<any>} - A promise that resolves to the translated content.
80
+ */
81
+ translateReactChildren(content, targetLanguage, metadata) {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ return yield _translateReactChildren(this, content, targetLanguage, Object.assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata));
84
+ });
85
+ }
86
+ /**
87
+ * Bundles multiple translation requests and sends them to the server.
88
+ * @param requests - Array of requests to be processed and sent.
89
+ * @returns A promise that resolves to an array of processed results.
90
+ */
91
+ bundleTranslation(requests) {
92
+ return __awaiter(this, void 0, void 0, function* () {
93
+ return _bundleTranslation(this, requests);
94
+ });
95
+ }
96
+ /**
97
+ * Pushes updates to a remotely cached translation dictionary.
98
+ * @param {Update[]} updates - Array of updates with optional targetLanguage.
99
+ * @param {string[]} [languages] - Array of languages to be updated.
100
+ * @param {string} [projectID=this.projectID] - The ID of the project. Defaults to the instance's projectID.
101
+ * @param {boolean} [replace=false] - Whether to replace the existing dictionary. Defaults to false.
102
+ * @returns {Promise<string[]>} A promise that resolves to an array of strings indicating the languages which have been updated.
103
+ */
104
+ updateRemoteDictionary(updates_1) {
105
+ return __awaiter(this, arguments, void 0, function* (updates, languages = [], projectID = this.projectID, replace = false) {
106
+ return _updateRemoteDictionary(this, updates, languages, projectID, replace);
107
+ });
108
+ }
109
+ }
110
+ // ----- EXPORTS ----- //
111
+ // Export the class
112
+ export default GT;
113
+ // Export the functions
114
+ /**
115
+ * Gets the writing direction for a given BCP 47 language code.
116
+ * @param {string} code - The BCP 47 language code to check.
117
+ * @returns {string} The language direction ('ltr' for left-to-right or 'rtl' for right-to-left).
118
+ */
119
+ export const getLanguageDirection = (code) => _getLanguageDirection(code);
120
+ /**
121
+ * Checks if a given BCP 47 language code is valid.
122
+ * @param {string} code - The BCP 47 language code to validate.
123
+ * @returns {boolean} True if the BCP 47 code is valid, false otherwise.
124
+ */
125
+ export const isValidLanguageCode = (code) => _isValidLanguageCode(code);
126
+ /**
127
+ * Standardizes a BCP 47 language code to ensure correct formatting.
128
+ * @param {string} code - The BCP 47 language code to standardize.
129
+ * @returns {string} The standardized BCP 47 language code.
130
+ */
131
+ export const standardizeLanguageCode = (code) => _standardizeLanguageCode(code);
132
+ export function getLanguageObject(codes) {
133
+ return Array.isArray(codes) ? _getLanguageObject(codes) : _getLanguageObject(codes);
134
+ }
135
+ export function getLanguageCode(languages) {
136
+ return _getLanguageCode(languages);
137
+ }
138
+ export function getLanguageName(codes) {
139
+ return _getLanguageName(codes);
140
+ }
141
+ /**
142
+ * Checks if multiple BCP 47 language codes represent the same language.
143
+ * @param {string[]} codes - The BCP 47 language codes to compare.
144
+ * @returns {boolean} True if all BCP 47 codes represent the same language, false otherwise.
145
+ */
146
+ export function isSameLanguage(...codes) {
147
+ return _isSameLanguage(...codes);
148
+ }
149
+ ;
@@ -0,0 +1,22 @@
1
+ export type Request = {
2
+ type: 'translate';
3
+ data: {
4
+ content: string;
5
+ targetLanguage: string;
6
+ metadata: Record<string, any>;
7
+ };
8
+ } | {
9
+ type: 'intl';
10
+ data: {
11
+ content: string;
12
+ targetLanguage: string;
13
+ metadata: Record<string, any>;
14
+ };
15
+ } | {
16
+ type: 'react';
17
+ data: {
18
+ children: object | string;
19
+ targetLanguage: string;
20
+ metadata: Record<string, any>;
21
+ };
22
+ };
@@ -0,0 +1,51 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ /**
11
+ * Bundles multiple requests and sends them to the server.
12
+ * @param {{ baseURL: string, apiKey: string }} gt - Contains the baseURL and apiKey for the server request.
13
+ * @param {Request[]} requests - Array of requests to be processed and sent.
14
+ * @param {{ timeout?: number }} options - Additional options for the request, including timeout.
15
+ * @returns {Promise<Array<any | null>>} A promise that resolves to an array of processed results.
16
+ * @internal
17
+ */
18
+ export default function _bundleTranslation(gt, requests) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ var _a, _b, _c;
21
+ const controller = new AbortController();
22
+ const signal = controller.signal;
23
+ if ((_c = (_b = (_a = requests[0]) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.metadata) === null || _c === void 0 ? void 0 : _c.timeout) {
24
+ setTimeout(() => controller.abort(), requests[0].data.metadata.timeout);
25
+ }
26
+ try {
27
+ const response = yield fetch(`${gt.baseURL}/bundle`, {
28
+ method: 'POST',
29
+ headers: {
30
+ 'Content-Type': 'application/json',
31
+ 'gtx-api-key': gt.apiKey,
32
+ },
33
+ body: JSON.stringify(requests),
34
+ signal
35
+ });
36
+ if (!response.ok) {
37
+ throw new Error(`${response.status}: ${yield response.text()}`);
38
+ }
39
+ const resultArray = yield response.json();
40
+ return resultArray;
41
+ }
42
+ catch (error) {
43
+ if (error instanceof Error && error.name === 'AbortError') {
44
+ console.error('Request timed out');
45
+ return Array.from(requests, () => ({ translation: null, error: 'Request timed out' }));
46
+ }
47
+ console.error(error);
48
+ return Array.from(requests, () => ({ translation: null, error: error }));
49
+ }
50
+ });
51
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,60 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ /**
11
+ * Translates a single piece of content and caches for use in a public project.
12
+ * @param {{ baseURL: string, apiKey: string }} gt - The translation service configuration.
13
+ * @param {string} content - The content to translate.
14
+ * @param {string} targetLanguage - The target language for the translation.
15
+ * @param {string} projectID - The ID of the project
16
+ * @param {{ dictionaryName?: string, notes?: string, timeout?: number, [key: string]: any }} metadata - Additional metadata for the translation request.
17
+ * @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
18
+ * @internal
19
+ */
20
+ export default function _translate(gt, content, targetLanguage, projectID, metadata) {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ const controller = new AbortController();
23
+ const signal = controller.signal;
24
+ if (metadata.timeout) {
25
+ setTimeout(() => controller.abort(), metadata.timeout);
26
+ }
27
+ try {
28
+ const response = yield fetch(`${gt.baseURL}/translate`, {
29
+ method: 'POST',
30
+ headers: {
31
+ 'Content-Type': 'application/json',
32
+ 'gtx-api-key': gt.apiKey,
33
+ },
34
+ body: JSON.stringify({
35
+ content, targetLanguage, projectID, metadata
36
+ }),
37
+ signal
38
+ });
39
+ if (!response.ok) {
40
+ throw new Error(`${response.status}: ${yield response.text()}`);
41
+ }
42
+ const result = yield response.json();
43
+ return result;
44
+ }
45
+ catch (error) {
46
+ if (error instanceof Error && error.name === 'AbortError') {
47
+ console.error('Request timed out');
48
+ return {
49
+ translation: content,
50
+ error: 'Request timed out'
51
+ };
52
+ }
53
+ console.error(error);
54
+ return {
55
+ translation: content,
56
+ error: error
57
+ };
58
+ }
59
+ });
60
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,59 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ /**
11
+ * Translates a string.
12
+ * @param {{ baseURL: string, apiKey: string }} gt - The translation service configuration.
13
+ * @param {string} content - The content to translate.
14
+ * @param {string} targetLanguage - The target language for the translation.
15
+ * @param {{ notes?: string, timeout?: number, [key: string]: any }} metadata - Additional metadata for the translation request.
16
+ * @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
17
+ * @internal
18
+ */
19
+ export default function _translate(gt, content, targetLanguage, metadata) {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ const controller = new AbortController();
22
+ const signal = controller.signal;
23
+ if (metadata.timeout) {
24
+ setTimeout(() => controller.abort(), metadata.timeout);
25
+ }
26
+ try {
27
+ const response = yield fetch(`${gt.baseURL}/translate`, {
28
+ method: 'POST',
29
+ headers: {
30
+ 'Content-Type': 'application/json',
31
+ 'gtx-api-key': gt.apiKey,
32
+ },
33
+ body: JSON.stringify({
34
+ content, targetLanguage, metadata
35
+ }),
36
+ signal
37
+ });
38
+ if (!response.ok) {
39
+ throw new Error(`${response.status}: ${yield response.text()}`);
40
+ }
41
+ const result = yield response.json();
42
+ return result;
43
+ }
44
+ catch (error) {
45
+ if (error instanceof Error && error.name === 'AbortError') {
46
+ console.error('Request timed out');
47
+ return {
48
+ translation: content,
49
+ error: 'Request timed out'
50
+ };
51
+ }
52
+ console.error(error);
53
+ return {
54
+ translation: content,
55
+ error: error
56
+ };
57
+ }
58
+ });
59
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,64 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ /**
11
+ * Translates the given content into the target language using a specified API.
12
+ *
13
+ * @param {{ baseURL: string, apiKey: string }} gt - An object containing baseURL and apiKey for the API.
14
+ * @param {any} content - The content to be translated. This can be of any type.
15
+ * @param {string} targetLanguage - The target language code (e.g., 'en', 'fr') for the translation.
16
+ * @param {{ [key: string]: any }} metadata - Additional metadata to be sent with the translation request.
17
+ *
18
+ * @returns {Promise<{ translation: any | null, error?: Error | unknown }>} - A promise that resolves to the translated content as an object, or null if an error occurs.
19
+ *
20
+ * @throws {Error} - Throws an error if the response from the API is not ok (status code not in the range 200-299).
21
+ * @internal
22
+ **/
23
+ export default function _translateReactChildren(gt, content, targetLanguage, metadata) {
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ const controller = new AbortController();
26
+ const signal = controller.signal;
27
+ if (metadata.timeout) {
28
+ setTimeout(() => controller.abort(), metadata.timeout);
29
+ }
30
+ try {
31
+ const response = yield fetch(`${gt.baseURL}/react`, {
32
+ method: 'POST',
33
+ headers: {
34
+ 'Content-Type': 'application/json',
35
+ 'gtx-api-key': gt.apiKey,
36
+ },
37
+ body: JSON.stringify({
38
+ content: content,
39
+ targetLanguage: targetLanguage,
40
+ metadata: metadata
41
+ }),
42
+ signal
43
+ });
44
+ if (!response.ok) {
45
+ throw new Error(`${response.status}: ${yield response.text()}`);
46
+ }
47
+ return yield response.json();
48
+ }
49
+ catch (error) {
50
+ if (error instanceof Error && error.name === 'AbortError') {
51
+ console.error('Request timed out');
52
+ return {
53
+ translation: null,
54
+ error: 'Request timed out'
55
+ };
56
+ }
57
+ console.error(error);
58
+ return {
59
+ translation: null,
60
+ error: error
61
+ };
62
+ }
63
+ });
64
+ }
@@ -0,0 +1,17 @@
1
+ export type Update = {
2
+ type: 'react';
3
+ data: {
4
+ children: object | string;
5
+ metadata: Record<string, any>;
6
+ };
7
+ } | {
8
+ type: 'intl';
9
+ data: {
10
+ content: string;
11
+ metadata: Record<string, any>;
12
+ };
13
+ };
14
+ export default function _updateRemoteDictionary(gt: {
15
+ baseURL: string;
16
+ apiKey: string;
17
+ }, updates: Update[], languages: string[], projectID: string, replace: boolean): Promise<string[]>;
@@ -0,0 +1,38 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ export default function _updateRemoteDictionary(gt, updates, languages, projectID, replace) {
11
+ return __awaiter(this, void 0, void 0, function* () {
12
+ try {
13
+ const response = yield fetch(`${gt.baseURL}/update`, {
14
+ method: 'POST',
15
+ headers: {
16
+ 'Content-Type': 'application/json',
17
+ 'gtx-api-key': gt.apiKey,
18
+ },
19
+ body: JSON.stringify({
20
+ updates, languages, projectID, replace
21
+ })
22
+ });
23
+ if (!response.ok) {
24
+ throw new Error(`${response.status}: ${yield response.text()}`);
25
+ }
26
+ const result = yield response.json();
27
+ return result.languages;
28
+ }
29
+ catch (error) {
30
+ if (error instanceof Error && error.name === 'AbortError') {
31
+ console.error('Request timed out');
32
+ return [];
33
+ }
34
+ console.error(error);
35
+ return [];
36
+ }
37
+ });
38
+ }
package/package.json CHANGED
@@ -1,15 +1,22 @@
1
1
  {
2
2
  "name": "generaltranslation",
3
- "version": "2.0.53",
3
+ "version": "2.0.54",
4
4
  "description": "A language toolkit for AI developers",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
5
+ "main": "dist/mjs/index.js",
6
+ "types": "dist/mjs/index.d.ts",
7
7
  "files": [
8
8
  "dist"
9
9
  ],
10
10
  "scripts": {
11
- "build": "tsc",
12
- "prepublishOnly": "npm run build"
11
+ "transpile": "tsc; tsc --project tsconfig.cjs.json;",
12
+ "prepublishOnly": "npm run transpile"
13
+ },
14
+ "exports": {
15
+ ".": {
16
+ "import": "./dist/mjs/index.js",
17
+ "require": "./dist/cjs/index.js",
18
+ "types": "./dist/mjs/index.d.ts"
19
+ }
13
20
  },
14
21
  "repository": {
15
22
  "type": "git",