auto-translate-json-library 1.1.5 → 1.2.0
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/aws.d.ts +10 -10
- package/aws.js +131 -131
- package/azure.d.ts +9 -9
- package/azure.js +169 -169
- package/config.d.ts +37 -33
- package/config.js +2 -2
- package/deepl.d.ts +9 -9
- package/deepl.js +82 -82
- package/files.d.ts +19 -19
- package/files.js +88 -88
- package/folderFiles.d.ts +12 -12
- package/folderFiles.js +57 -57
- package/google.d.ts +8 -8
- package/google.js +163 -163
- package/index.d.ts +3 -3
- package/index.js +126 -120
- package/lib.d.ts +2 -2
- package/lib.js +166 -162
- package/openai.d.ts +9 -0
- package/openai.js +167 -0
- package/package.json +26 -12
- package/translate.interface.d.ts +4 -4
- package/translate.interface.js +2 -2
- package/util.d.ts +12 -12
- package/util.js +41 -41
package/lib.js
CHANGED
|
@@ -1,162 +1,166 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.translate = void 0;
|
|
13
|
-
// The module 'vscode' contains the VS Code extensibility API
|
|
14
|
-
// Import the module and reference it with the alias vscode in your code below
|
|
15
|
-
const files_1 = require("./files");
|
|
16
|
-
const folderFiles_1 = require("./folderFiles");
|
|
17
|
-
const aws_1 = require("./aws");
|
|
18
|
-
const azure_1 = require("./azure");
|
|
19
|
-
const google_1 = require("./google");
|
|
20
|
-
const util_1 = require("./util");
|
|
21
|
-
const deepl_1 = require("./deepl");
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if (
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
files.
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
destination[term] =
|
|
143
|
-
}
|
|
144
|
-
else {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.translate = void 0;
|
|
13
|
+
// The module 'vscode' contains the VS Code extensibility API
|
|
14
|
+
// Import the module and reference it with the alias vscode in your code below
|
|
15
|
+
const files_1 = require("./files");
|
|
16
|
+
const folderFiles_1 = require("./folderFiles");
|
|
17
|
+
const aws_1 = require("./aws");
|
|
18
|
+
const azure_1 = require("./azure");
|
|
19
|
+
const google_1 = require("./google");
|
|
20
|
+
const util_1 = require("./util");
|
|
21
|
+
const deepl_1 = require("./deepl");
|
|
22
|
+
const openai_1 = require("./openai");
|
|
23
|
+
const NAME = 'AutoTranslateJSON';
|
|
24
|
+
function translate(sourceFile, config) {
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
// set the delimiters for named arguments
|
|
27
|
+
const startDelimiter = config.startDelimiter || '{';
|
|
28
|
+
if (startDelimiter) {
|
|
29
|
+
util_1.Util.startDelimiter = startDelimiter;
|
|
30
|
+
}
|
|
31
|
+
const endDelimiter = config.endDelimiter || '}';
|
|
32
|
+
if (endDelimiter) {
|
|
33
|
+
util_1.Util.endDelimiter = endDelimiter;
|
|
34
|
+
}
|
|
35
|
+
let translateEngine;
|
|
36
|
+
if (config.translationKeyInfo.kind === 'google') {
|
|
37
|
+
translateEngine = new google_1.GoogleTranslate(config.translationKeyInfo.apiKey);
|
|
38
|
+
}
|
|
39
|
+
else if (config.translationKeyInfo.kind === 'aws') {
|
|
40
|
+
translateEngine = new aws_1.AWSTranslate(config.translationKeyInfo.accessKeyId, config.translationKeyInfo.secretAccessKey, config.translationKeyInfo.region);
|
|
41
|
+
}
|
|
42
|
+
else if (config.translationKeyInfo.kind === 'azure') {
|
|
43
|
+
translateEngine = new azure_1.AzureTranslate(config.translationKeyInfo.secretKey, config.translationKeyInfo.region);
|
|
44
|
+
}
|
|
45
|
+
else if (config.translationKeyInfo.kind === 'deepLFree') {
|
|
46
|
+
translateEngine = new deepl_1.DeepLTranslate(config.translationKeyInfo.secretKey, 'free');
|
|
47
|
+
}
|
|
48
|
+
else if (config.translationKeyInfo.kind === 'deepLPro') {
|
|
49
|
+
translateEngine = new deepl_1.DeepLTranslate(config.translationKeyInfo.secretKey, 'pro');
|
|
50
|
+
}
|
|
51
|
+
else if (config.translationKeyInfo.kind === 'openai') {
|
|
52
|
+
translateEngine = new openai_1.OpenAITranslate(config.translationKeyInfo.apiKey);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
console.warn('You must provide a Google, AWS, Azure, deepL, openai parameters first in the extension settings.');
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const fileMode = config.mode;
|
|
59
|
+
const files = readFiles(sourceFile, fileMode);
|
|
60
|
+
if (files === null) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
// enforce source locale if provided in settings
|
|
64
|
+
if (config.sourceLocale !== files.sourceLocale) {
|
|
65
|
+
console.log('You must use the ' +
|
|
66
|
+
config.sourceLocale +
|
|
67
|
+
'.json file due to your Source Locale setting.');
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const keepTranslations = config.keepTranslations === 'keep';
|
|
71
|
+
const keepExtras = config.keepExtraTranslations === 'keep';
|
|
72
|
+
// load source JSON
|
|
73
|
+
let source;
|
|
74
|
+
try {
|
|
75
|
+
source = yield files.loadJsonFromLocale(files.sourceLocale);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
if (error instanceof Error) {
|
|
79
|
+
console.log(error, 'Source file malformed');
|
|
80
|
+
}
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
// Iterate target Locales
|
|
84
|
+
files.targetLocales.forEach((targetLocale) => __awaiter(this, void 0, void 0, function* () {
|
|
85
|
+
try {
|
|
86
|
+
const isValid = yield translateEngine.isValidLocale(targetLocale);
|
|
87
|
+
if (!isValid) {
|
|
88
|
+
throw Error(targetLocale + ' is not supported. Skipping.');
|
|
89
|
+
}
|
|
90
|
+
const targetOriginal = yield files.loadJsonFromLocale(targetLocale);
|
|
91
|
+
// Iterate source terms
|
|
92
|
+
const targetNew = yield recurseNode(source, targetOriginal, keepTranslations, keepExtras, files.sourceLocale, targetLocale, translateEngine);
|
|
93
|
+
// save target
|
|
94
|
+
files.saveJsonToLocale(targetLocale, targetNew);
|
|
95
|
+
const feedback = "Translated locale '" + targetLocale + "'";
|
|
96
|
+
console.log(feedback);
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
if (error instanceof Error) {
|
|
100
|
+
console.error(error);
|
|
101
|
+
}
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
}));
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
exports.translate = translate;
|
|
108
|
+
const readFiles = (filePath, mode) => {
|
|
109
|
+
try {
|
|
110
|
+
const files = mode === 'file' ? new files_1.Files(filePath) : new folderFiles_1.FolderFiles(filePath);
|
|
111
|
+
// log locale info
|
|
112
|
+
console.log('Source locale = ' + files.sourceLocale);
|
|
113
|
+
console.log('Target locales = ' + files.targetLocales);
|
|
114
|
+
return files;
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
if (error instanceof Error) {
|
|
118
|
+
console.error(error);
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
function recurseNode(source, original, keepTranslations, keepExtras, sourceLocale, locale, translateEngine, isArray = false) {
|
|
124
|
+
var _a;
|
|
125
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
126
|
+
const destination = isArray ? [] : {};
|
|
127
|
+
// defaults
|
|
128
|
+
if (keepTranslations === null) {
|
|
129
|
+
keepTranslations = true;
|
|
130
|
+
}
|
|
131
|
+
if (keepExtras === null) {
|
|
132
|
+
keepExtras = true;
|
|
133
|
+
}
|
|
134
|
+
for (const term in source) {
|
|
135
|
+
const node = source[term];
|
|
136
|
+
if (node instanceof Object && node !== null) {
|
|
137
|
+
destination[term] = yield recurseNode(node, (_a = original[term]) !== null && _a !== void 0 ? _a : {}, keepTranslations, keepExtras, sourceLocale, locale, translateEngine, node instanceof Array);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
// if we already have a translation, keep it
|
|
141
|
+
if (keepTranslations && original[term]) {
|
|
142
|
+
destination[term] = original[term];
|
|
143
|
+
}
|
|
144
|
+
else if (typeof node === 'number' || typeof node === 'boolean') {
|
|
145
|
+
// numbers and booleans do not need translations
|
|
146
|
+
destination[term] = node;
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
const translation = yield translateEngine
|
|
150
|
+
.translateText(node, sourceLocale, locale)
|
|
151
|
+
.catch((err) => console.error(err));
|
|
152
|
+
destination[term] = translation;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (keepExtras) {
|
|
157
|
+
// add back in any terms that were not in source
|
|
158
|
+
for (const term in original) {
|
|
159
|
+
if (!destination[term]) {
|
|
160
|
+
destination[term] = original[term];
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return destination;
|
|
165
|
+
});
|
|
166
|
+
}
|
package/openai.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ITranslate } from "./translate.interface";
|
|
2
|
+
export declare class OpenAITranslate implements ITranslate {
|
|
3
|
+
private apiKey;
|
|
4
|
+
private endpoint;
|
|
5
|
+
private openai;
|
|
6
|
+
constructor(apiKey: string);
|
|
7
|
+
isValidLocale(targetLocale: string): boolean;
|
|
8
|
+
translateText(text: string, sourceLocale: string, targetLocale: string): Promise<string>;
|
|
9
|
+
}
|
package/openai.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.OpenAITranslate = void 0;
|
|
13
|
+
const util_1 = require("./util");
|
|
14
|
+
const openai_1 = require("openai");
|
|
15
|
+
const supportedLanguages = {
|
|
16
|
+
'af': 'Afrikaans',
|
|
17
|
+
'sq': 'Albanian',
|
|
18
|
+
'am': 'Amharic',
|
|
19
|
+
'ar': 'Arabic',
|
|
20
|
+
'hy': 'Armenian',
|
|
21
|
+
'az': 'Azerbaijani',
|
|
22
|
+
'eu': 'Basque',
|
|
23
|
+
'be': 'Belarusian',
|
|
24
|
+
'bn': 'Bengali',
|
|
25
|
+
'bs': 'Bosnian',
|
|
26
|
+
'bg': 'Bulgarian',
|
|
27
|
+
'ca': 'Catalan',
|
|
28
|
+
'ceb': 'Cebuano',
|
|
29
|
+
'zh-CN': 'Chinese (Simplified)',
|
|
30
|
+
'zh-TW': 'Chinese (Traditional)',
|
|
31
|
+
'co': 'Corsican',
|
|
32
|
+
'hr': 'Croatian',
|
|
33
|
+
'cs': 'Czech',
|
|
34
|
+
'da': 'Danish',
|
|
35
|
+
'nl': 'Dutch',
|
|
36
|
+
'en': 'English',
|
|
37
|
+
'eo': 'Esperanto',
|
|
38
|
+
'et': 'Estonian',
|
|
39
|
+
'fi': 'Finnish',
|
|
40
|
+
'fr': 'French',
|
|
41
|
+
'fy': 'Frisian',
|
|
42
|
+
'gl': 'Galician',
|
|
43
|
+
'ka': 'Georgian',
|
|
44
|
+
'de': 'German',
|
|
45
|
+
'el': 'Greek',
|
|
46
|
+
'gu': 'Gujarati',
|
|
47
|
+
'ht': 'Haitian Creole',
|
|
48
|
+
'ha': 'Hausa',
|
|
49
|
+
'haw': 'Hawaiian',
|
|
50
|
+
'he': 'Hebrew',
|
|
51
|
+
'hi': 'Hindi',
|
|
52
|
+
'hmn': 'Hmong',
|
|
53
|
+
'hu': 'Hungarian',
|
|
54
|
+
'is': 'Icelandic',
|
|
55
|
+
'ig': 'Igbo',
|
|
56
|
+
'id': 'Indonesian',
|
|
57
|
+
'ga': 'Irish',
|
|
58
|
+
'it': 'Italian',
|
|
59
|
+
'ja': 'Japanese',
|
|
60
|
+
'jv': 'Javanese',
|
|
61
|
+
'kn': 'Kannada',
|
|
62
|
+
'kk': 'Kazakh',
|
|
63
|
+
'km': 'Khmer',
|
|
64
|
+
'ko': 'Korean',
|
|
65
|
+
'ku': 'Kurdish',
|
|
66
|
+
'ky': 'Kyrgyz',
|
|
67
|
+
'lo': 'Lao',
|
|
68
|
+
'la': 'Latin',
|
|
69
|
+
'lv': 'Latvian',
|
|
70
|
+
'lt': 'Lithuanian',
|
|
71
|
+
'lb': 'Luxembourgish',
|
|
72
|
+
'mk': 'Macedonian',
|
|
73
|
+
'mg': 'Malagasy',
|
|
74
|
+
'ms': 'Malay',
|
|
75
|
+
'ml': 'Malayalam',
|
|
76
|
+
'mt': 'Maltese',
|
|
77
|
+
'mi': 'Maori',
|
|
78
|
+
'mr': 'Marathi',
|
|
79
|
+
'mn': 'Mongolian',
|
|
80
|
+
'my': 'Myanmar (Burmese)',
|
|
81
|
+
'ne': 'Nepali',
|
|
82
|
+
'no': 'Norwegian',
|
|
83
|
+
'ny': 'Nyanja (Chichewa)',
|
|
84
|
+
'or': 'Odia (Oriya)',
|
|
85
|
+
'ps': 'Pashto',
|
|
86
|
+
'fa': 'Persian',
|
|
87
|
+
'pl': 'Polish',
|
|
88
|
+
'pt': 'Portuguese',
|
|
89
|
+
'pa': 'Punjabi',
|
|
90
|
+
'ro': 'Romanian',
|
|
91
|
+
'ru': 'Russian',
|
|
92
|
+
'sm': 'Samoan',
|
|
93
|
+
'gd': 'Scots Gaelic',
|
|
94
|
+
'sr': 'Serbian',
|
|
95
|
+
'st': 'Sesotho',
|
|
96
|
+
'sn': 'Shona',
|
|
97
|
+
'sd': 'Sindhi',
|
|
98
|
+
'si': 'Sinhala (Sinhalese)',
|
|
99
|
+
'sk': 'Slovak',
|
|
100
|
+
'sl': 'Slovenian',
|
|
101
|
+
'so': 'Somali',
|
|
102
|
+
'es': 'Spanish',
|
|
103
|
+
'su': 'Sundanese',
|
|
104
|
+
'sw': 'Swahili',
|
|
105
|
+
'sv': 'Swedish',
|
|
106
|
+
'tl': 'Tagalog (Filipino)',
|
|
107
|
+
'tg': 'Tajik',
|
|
108
|
+
'ta': 'Tamil',
|
|
109
|
+
'tt': 'Tatar',
|
|
110
|
+
'te': 'Telugu',
|
|
111
|
+
'th': 'Thai',
|
|
112
|
+
'tr': 'Turkish',
|
|
113
|
+
'tk': 'Turkmen',
|
|
114
|
+
'uk': 'Ukrainian',
|
|
115
|
+
'ur': 'Urdu',
|
|
116
|
+
'ug': 'Uyghur',
|
|
117
|
+
'uz': 'Uzbek',
|
|
118
|
+
'vi': 'Vietnamese',
|
|
119
|
+
'cy': 'Welsh',
|
|
120
|
+
'xh': 'Xhosa',
|
|
121
|
+
'yi': 'Yiddish',
|
|
122
|
+
'yo': 'Yoruba',
|
|
123
|
+
'zu': 'Zulu'
|
|
124
|
+
};
|
|
125
|
+
class OpenAITranslate {
|
|
126
|
+
constructor(apiKey) {
|
|
127
|
+
this.apiKey = apiKey;
|
|
128
|
+
this.endpoint = 'https://api.cognitive.microsofttranslator.com';
|
|
129
|
+
const configuration = new openai_1.Configuration({
|
|
130
|
+
apiKey: apiKey,
|
|
131
|
+
});
|
|
132
|
+
this.openai = new openai_1.OpenAIApi(configuration);
|
|
133
|
+
}
|
|
134
|
+
isValidLocale(targetLocale) {
|
|
135
|
+
return targetLocale in supportedLanguages;
|
|
136
|
+
}
|
|
137
|
+
translateText(text, sourceLocale, targetLocale) {
|
|
138
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
139
|
+
if (sourceLocale !== 'en') {
|
|
140
|
+
throw Error(sourceLocale + ' is not supported.Currently we support just English ');
|
|
141
|
+
}
|
|
142
|
+
let result = '';
|
|
143
|
+
let args;
|
|
144
|
+
({ args, text } = util_1.Util.replaceContextVariables(text));
|
|
145
|
+
const prompt = `Translate this into ${supportedLanguages[targetLocale]}:\n${text}\n.`;
|
|
146
|
+
const response = yield this.openai.createCompletion({
|
|
147
|
+
model: "text-davinci-003",
|
|
148
|
+
prompt: prompt,
|
|
149
|
+
temperature: 0.3,
|
|
150
|
+
max_tokens: 1000,
|
|
151
|
+
top_p: 1.0,
|
|
152
|
+
frequency_penalty: 0.0,
|
|
153
|
+
presence_penalty: 0.0,
|
|
154
|
+
});
|
|
155
|
+
if (response.data.choices[0].text !== undefined) {
|
|
156
|
+
result = util_1.Util.replaceArgumentsWithNumbers(args, response.data.choices[0].text);
|
|
157
|
+
result = result.replace(/^\n+|\n+$/g, '');
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
console.error(`can not translate text with prompt :${prompt}`);
|
|
161
|
+
result = util_1.Util.replaceArgumentsWithNumbers(args, result);
|
|
162
|
+
}
|
|
163
|
+
return result;
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
exports.OpenAITranslate = OpenAITranslate;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "auto-translate-json-library",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Automatically translates JSON language files to other languages using Google Translate,AWS,Azure or
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "Automatically translates JSON language files to other languages using Google Translate,AWS,Azure,DeepL or OpenAI",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
7
7
|
"scripts": {
|
|
@@ -9,7 +9,20 @@
|
|
|
9
9
|
"build": "tsc",
|
|
10
10
|
"start": "ts-node src/index.ts"
|
|
11
11
|
},
|
|
12
|
-
"keywords": [
|
|
12
|
+
"keywords": [
|
|
13
|
+
"translate",
|
|
14
|
+
"language",
|
|
15
|
+
"json",
|
|
16
|
+
"flutter",
|
|
17
|
+
"angular",
|
|
18
|
+
"ngx-translate",
|
|
19
|
+
"transloco",
|
|
20
|
+
"auto-translate-json",
|
|
21
|
+
"google",
|
|
22
|
+
"Azure",
|
|
23
|
+
"DeepL",
|
|
24
|
+
"OpenAI"
|
|
25
|
+
],
|
|
13
26
|
"author": "codegrue",
|
|
14
27
|
"repository": {
|
|
15
28
|
"type": "git",
|
|
@@ -26,21 +39,22 @@
|
|
|
26
39
|
}
|
|
27
40
|
},
|
|
28
41
|
"devDependencies": {
|
|
29
|
-
"@types/node": "
|
|
30
|
-
"@types/yargs": "17.0.
|
|
31
|
-
"husky": "8.0.
|
|
32
|
-
"prettier": "2.
|
|
42
|
+
"@types/node": "20.1.4",
|
|
43
|
+
"@types/yargs": "17.0.24",
|
|
44
|
+
"husky": "8.0.3",
|
|
45
|
+
"prettier": "2.8.8",
|
|
33
46
|
"pretty-quick": "3.1.3",
|
|
34
47
|
"ts-node": "10.9.1",
|
|
35
|
-
"typescript": "
|
|
48
|
+
"typescript": "5.0.4"
|
|
36
49
|
},
|
|
37
50
|
"dependencies": {
|
|
38
|
-
"@aws-sdk/client-translate": "3.
|
|
39
|
-
"@google-cloud/translate": "
|
|
40
|
-
"axios": "1.
|
|
51
|
+
"@aws-sdk/client-translate": "3.332.0",
|
|
52
|
+
"@google-cloud/translate": "7.2.1",
|
|
53
|
+
"axios": "1.4.0",
|
|
41
54
|
"dotenv": "16.0.3",
|
|
42
55
|
"form-data": "4.0.0",
|
|
56
|
+
"openai": "3.2.1",
|
|
43
57
|
"uuid": "9.0.0",
|
|
44
|
-
"yargs": "17.
|
|
58
|
+
"yargs": "17.7.2"
|
|
45
59
|
}
|
|
46
60
|
}
|
package/translate.interface.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export interface ITranslate {
|
|
2
|
-
isValidLocale(targetLocale: string): boolean;
|
|
3
|
-
translateText(text: string, sourceLocale: string, targetLocale: string): Promise<string>;
|
|
4
|
-
}
|
|
1
|
+
export interface ITranslate {
|
|
2
|
+
isValidLocale(targetLocale: string): boolean;
|
|
3
|
+
translateText(text: string, sourceLocale: string, targetLocale: string): Promise<string>;
|
|
4
|
+
}
|
package/translate.interface.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
package/util.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
export declare class Util {
|
|
2
|
-
private static _startDelimiter;
|
|
3
|
-
private static _endDelimiter;
|
|
4
|
-
private static pattern;
|
|
5
|
-
static set startDelimiter(value: string);
|
|
6
|
-
static set endDelimiter(value: string);
|
|
7
|
-
static replaceArgumentsWithNumbers(args: RegExpMatchArray | null, result: string): string;
|
|
8
|
-
static replaceContextVariables(text: string): {
|
|
9
|
-
args: RegExpMatchArray | null;
|
|
10
|
-
text: string;
|
|
11
|
-
};
|
|
12
|
-
}
|
|
1
|
+
export declare class Util {
|
|
2
|
+
private static _startDelimiter;
|
|
3
|
+
private static _endDelimiter;
|
|
4
|
+
private static pattern;
|
|
5
|
+
static set startDelimiter(value: string);
|
|
6
|
+
static set endDelimiter(value: string);
|
|
7
|
+
static replaceArgumentsWithNumbers(args: RegExpMatchArray | null, result: string): string;
|
|
8
|
+
static replaceContextVariables(text: string): {
|
|
9
|
+
args: RegExpMatchArray | null;
|
|
10
|
+
text: string;
|
|
11
|
+
};
|
|
12
|
+
}
|