generaltranslation 1.3.0 → 1.3.2
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/index.js +15 -1
- package/package.json +1 -1
- package/translation/react/translateReactChildren.js +25 -0
package/index.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
// ----- IMPORTS ----- //
|
|
5
5
|
|
|
6
6
|
const { _getLanguageObject, _getLanguageCode, _getLanguageName, _isSameLanguage } = require('./codes/codes.js');
|
|
7
|
+
const _translateReactChildren = require('./translation/react/translateReactChildren.js');
|
|
7
8
|
|
|
8
9
|
// TO DO
|
|
9
10
|
// - Translation API
|
|
@@ -35,7 +36,20 @@ class GT {
|
|
|
35
36
|
this.baseURL = baseURL;
|
|
36
37
|
}
|
|
37
38
|
|
|
38
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Asynchronously translates the given React content to the specified target language.
|
|
41
|
+
*
|
|
42
|
+
*
|
|
43
|
+
* @param {Object} params - The parameters for the translation.
|
|
44
|
+
* @param {Object|Array} params.content - The React children content to be translated.
|
|
45
|
+
* @param {string} params.targetLanguage - The target language for translation.
|
|
46
|
+
* @param {...any} metadata - Additional metadata that might be needed for the translation process.
|
|
47
|
+
*
|
|
48
|
+
* @returns {Promise<Object|Array>} - A promise that resolves to the translated React children content.
|
|
49
|
+
*/
|
|
50
|
+
async translateReactChildren({ content, targetLanguage, ...metadata }) {
|
|
51
|
+
return await _translateReactChildren(content, targetLanguage, this, metadata)
|
|
52
|
+
}
|
|
39
53
|
|
|
40
54
|
}
|
|
41
55
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
async function _translateReactChildren(content, targetLanguage, this, metadata) {
|
|
2
|
+
try {
|
|
3
|
+
const response = await fetch(`${this.baseURL}`, {
|
|
4
|
+
method: 'POST',
|
|
5
|
+
headers: {
|
|
6
|
+
'Content-Type': 'application/json',
|
|
7
|
+
'gtx-api-key': this.apiKey,
|
|
8
|
+
},
|
|
9
|
+
body: JSON.stringify({
|
|
10
|
+
content: content,
|
|
11
|
+
targetLanguage: targetLanguage,
|
|
12
|
+
metadata: { ...metadata }
|
|
13
|
+
})
|
|
14
|
+
});
|
|
15
|
+
if (!response.ok) {
|
|
16
|
+
throw new Error(`${response.status}: ${await response.text()}`)
|
|
17
|
+
}
|
|
18
|
+
return await response.json();
|
|
19
|
+
} catch (error) {
|
|
20
|
+
console.error(error)
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = _translateReactChildren;
|