generaltranslation 1.1.26 → 1.1.28
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/client/getUserLanguage.js +1 -1
- package/codes/codes.js +4 -4
- package/index.js +16 -3
- package/package.json +1 -1
- package/{prompts/translate.js → translate/prompt.js} +4 -4
- package/translate/site.js +47 -0
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// e.g. 'en-US', not necessarily 'en'
|
|
3
3
|
// generaltranslation handles this well, but other libraries may not
|
|
4
4
|
// (it's a skill issue I'm afraid)
|
|
5
|
-
export default function _getUserLanguage({defaultLanguage = ''} = {}) {
|
|
5
|
+
export default function _getUserLanguage({ defaultLanguage = '' } = {}) {
|
|
6
6
|
if (typeof window !== 'undefined' && window?.navigator) {
|
|
7
7
|
return navigator?.language || navigator?.userLanguage || defaultLanguage || '';
|
|
8
8
|
} else if (defaultLanguage) {
|
package/codes/codes.js
CHANGED
|
@@ -11,14 +11,14 @@ import LanguageToCodeTriletter from './639-3/LanguageToCodeTriletter.js'
|
|
|
11
11
|
|
|
12
12
|
// Returns the name of a language from an ISO 639 code or an array of codes
|
|
13
13
|
const _mapCodeToLanguage = code => {
|
|
14
|
-
if (code
|
|
14
|
+
if (code?.length === 2) {
|
|
15
15
|
return CodeToLanguage[code]
|
|
16
16
|
}
|
|
17
|
-
if (code
|
|
17
|
+
if (code?.length === 3) {
|
|
18
18
|
CodeToLanguageTriletter[code]
|
|
19
19
|
}
|
|
20
20
|
else {
|
|
21
|
-
return CodeToLanguage[code
|
|
21
|
+
return CodeToLanguage[code?.slice(0, 2)] || '';
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
export const _getLanguageName = codes => {
|
|
@@ -28,7 +28,7 @@ export const _getLanguageName = codes => {
|
|
|
28
28
|
// Returns an ISO 639 code from a language name or an array of language names
|
|
29
29
|
// Preferentially returns two-letter codes
|
|
30
30
|
const _mapLanguageToCode = language => {
|
|
31
|
-
const lowerCaseLanguage = language
|
|
31
|
+
const lowerCaseLanguage = language?.toLowerCase();
|
|
32
32
|
return LanguageToCode[lowerCaseLanguage] || LanguageToCodeTriletter[lowerCaseLanguage] || '';
|
|
33
33
|
}
|
|
34
34
|
export const _getLanguageCode = languages => {
|
package/index.js
CHANGED
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
import _getUserLanguage from "./client/getUserLanguage.js";
|
|
7
7
|
import { _getLanguageCode, _getLanguageName } from "./codes/codes.js"
|
|
8
8
|
import { _getModelInfo, _getModelList, _getModelLanguages, _isLanguageSupported, _getModelsByDeveloper, _getModelsByLanguage } from "./models/models.js"
|
|
9
|
-
import _translatePrompt from "./
|
|
9
|
+
import _translatePrompt from "./translate/prompt.js"
|
|
10
|
+
import _createI18N from "./translate/site.js";
|
|
10
11
|
|
|
11
12
|
// ----- CORE CLASS ----- //
|
|
12
13
|
|
|
@@ -23,9 +24,21 @@ export default class GT {
|
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
// Prompt I18N
|
|
26
|
-
|
|
27
|
+
async translatePrompt(prompt, language) {
|
|
27
28
|
return await _translatePrompt({
|
|
28
|
-
content: prompt, language: language,
|
|
29
|
+
content: prompt, language: language, config: this
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Site I18N
|
|
34
|
+
async createI18N({ projectID, html, strings, defaultLanguage, userLanguage }) {
|
|
35
|
+
return await _createI18N({
|
|
36
|
+
projectID: projectID,
|
|
37
|
+
html: html,
|
|
38
|
+
strings: strings,
|
|
39
|
+
defaultLanguage: defaultLanguage,
|
|
40
|
+
userLanguage: userLanguage,
|
|
41
|
+
config: this
|
|
29
42
|
});
|
|
30
43
|
}
|
|
31
44
|
|
package/package.json
CHANGED
|
@@ -65,15 +65,15 @@ const _constructPrompt = ({ content, untranslated = null}) => {
|
|
|
65
65
|
// Get a translation via General Translation API
|
|
66
66
|
// Returns string
|
|
67
67
|
const _translatePrompt = async ({
|
|
68
|
-
content, language,
|
|
68
|
+
content, language, config
|
|
69
69
|
}) => {
|
|
70
70
|
|
|
71
|
-
const apiKey =
|
|
71
|
+
const apiKey = config?.apiKey;
|
|
72
72
|
if (!apiKey) {
|
|
73
73
|
throw new Error('Missing API Key!')
|
|
74
74
|
};
|
|
75
75
|
|
|
76
|
-
const defaultLanguage =
|
|
76
|
+
const defaultLanguage = config?.defaultLanguage;
|
|
77
77
|
if (language === defaultLanguage) {
|
|
78
78
|
return _constructPrompt({ content: content });
|
|
79
79
|
};
|
|
@@ -81,7 +81,7 @@ const _translatePrompt = async ({
|
|
|
81
81
|
const { processed, untranslated } = _processPrompt({ content });
|
|
82
82
|
|
|
83
83
|
try {
|
|
84
|
-
const response = await fetch(`${
|
|
84
|
+
const response = await fetch(`${config?.baseURL}/prompt`, {
|
|
85
85
|
method: 'POST',
|
|
86
86
|
headers: {
|
|
87
87
|
'Content-Type': 'application/json',
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const _createI18N = async ({
|
|
2
|
+
projectID,
|
|
3
|
+
defaultLanguage,
|
|
4
|
+
userLanguage,
|
|
5
|
+
content,
|
|
6
|
+
metadata,
|
|
7
|
+
config
|
|
8
|
+
}) => {
|
|
9
|
+
|
|
10
|
+
const apiKey = config?.apiKey;
|
|
11
|
+
if (!apiKey) {
|
|
12
|
+
throw new Error('Missing API Key!')
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
if (!projectID) {
|
|
16
|
+
throw new Error('Missing project ID!')
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
const response = await fetch(`${config?.baseURL}/site`, {
|
|
21
|
+
method: 'POST',
|
|
22
|
+
headers: {
|
|
23
|
+
'Content-Type': 'application/json',
|
|
24
|
+
'gtx-api-key': apiKey
|
|
25
|
+
},
|
|
26
|
+
body: JSON.stringify({
|
|
27
|
+
projectID: projectID,
|
|
28
|
+
content: content,
|
|
29
|
+
defaultLanguage: defaultLanguage,
|
|
30
|
+
userLanguage: userLanguage,
|
|
31
|
+
metadata: metadata
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
if (!response.ok) {
|
|
35
|
+
const result = await response.text();
|
|
36
|
+
throw new Error(`${result || response.status}`);
|
|
37
|
+
} else {
|
|
38
|
+
const result = await response.json();
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.error(error)
|
|
43
|
+
return content;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
}
|
|
47
|
+
export default _createI18N;
|