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