auto-translate-json-library 1.0.2 → 1.0.3

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/extension.js ADDED
@@ -0,0 +1,310 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.deactivate = exports.activate = void 0;
36
+ // The module 'vscode' contains the VS Code extensibility API
37
+ // Import the module and reference it with the alias vscode in your code below
38
+ const vscode = __importStar(require("vscode"));
39
+ const files_1 = require("./files");
40
+ const folderFiles_1 = require("./folderFiles");
41
+ const aws_1 = require("./aws");
42
+ const azure_1 = require("./azure");
43
+ const google_1 = require("./google");
44
+ const util_1 = require("./util");
45
+ const deepl_1 = require("./deepl");
46
+ const NAME = 'AutoTranslateJSON';
47
+ // this method is called when your extension is activated
48
+ // your extension is activated the very first time the command is executed
49
+ function activate(context) {
50
+ // Use the console to output diagnostic information (console.log) and errors (console.error)
51
+ // This line of code will only be executed once when your extension is activated
52
+ console.log('Extension "auto-translate-json" is active');
53
+ vscode.commands.registerCommand('extension.autotranslate', (resource) => __awaiter(this, void 0, void 0, function* () {
54
+ var _a;
55
+ // check that we have a google api key
56
+ const googleApiKey = vscode.workspace
57
+ .getConfiguration()
58
+ .get('auto-translate-json.googleApiKey');
59
+ const awsAccessKeyId = vscode.workspace
60
+ .getConfiguration()
61
+ .get('auto-translate-json.awsAccessKeyId');
62
+ const awsSecretAccessKey = vscode.workspace
63
+ .getConfiguration()
64
+ .get('auto-translate-json.awsSecretAccessKey');
65
+ const awsRegion = vscode.workspace
66
+ .getConfiguration()
67
+ .get('auto-translate-json.awsRegion');
68
+ const azureSecretKey = vscode.workspace
69
+ .getConfiguration()
70
+ .get('auto-translate-json.azureSecretKey');
71
+ const deepLProSecretKey = vscode.workspace
72
+ .getConfiguration()
73
+ .get('auto-translate-json.deepLProSecretKey');
74
+ const deepLFreeSecretKey = vscode.workspace
75
+ .getConfiguration()
76
+ .get('auto-translate-json.deepLFreeSecretKey');
77
+ const azureRegion = vscode.workspace
78
+ .getConfiguration()
79
+ .get('auto-translate-json.azureRegion');
80
+ if (!googleApiKey &&
81
+ !awsAccessKeyId &&
82
+ !awsSecretAccessKey &&
83
+ !awsRegion &&
84
+ !azureSecretKey &&
85
+ !azureRegion &&
86
+ !deepLProSecretKey &&
87
+ !deepLFreeSecretKey) {
88
+ showWarning('You must provide a Google, AWS or Azure parameters first in the extension settings.');
89
+ return;
90
+ }
91
+ // set the delimiters for named arguments
92
+ const startDelimiter = vscode.workspace
93
+ .getConfiguration()
94
+ .get('auto-translate-json.startDelimiter');
95
+ if (startDelimiter) {
96
+ util_1.Util.startDelimiter = startDelimiter;
97
+ }
98
+ const endDelimiter = vscode.workspace
99
+ .getConfiguration()
100
+ .get('auto-translate-json.endDelimiter');
101
+ if (endDelimiter) {
102
+ util_1.Util.endDelimiter = endDelimiter;
103
+ }
104
+ let translateEngine;
105
+ if (googleApiKey) {
106
+ translateEngine = new google_1.GoogleTranslate(googleApiKey);
107
+ }
108
+ else if (awsAccessKeyId && awsSecretAccessKey && awsRegion) {
109
+ translateEngine = new aws_1.AWSTranslate(awsAccessKeyId, awsSecretAccessKey, awsRegion);
110
+ }
111
+ else if (azureSecretKey && azureRegion) {
112
+ translateEngine = new azure_1.AzureTranslate(azureSecretKey, azureRegion);
113
+ }
114
+ else if (deepLFreeSecretKey) {
115
+ translateEngine = new deepl_1.DeepLTranslate(deepLFreeSecretKey, 'free');
116
+ }
117
+ else if (deepLProSecretKey) {
118
+ translateEngine = new deepl_1.DeepLTranslate(deepLProSecretKey, 'pro');
119
+ }
120
+ else {
121
+ showWarning('You must provide a Google, AWS or Azure parameters first in the extension settings.');
122
+ return;
123
+ }
124
+ // inform user if running the extension from the command bar
125
+ if (resource === undefined) {
126
+ showMessage('You must run this extension by right clicking on a .json file', '');
127
+ return;
128
+ }
129
+ const fileMode = (_a = vscode.workspace.getConfiguration().get('auto-translate-json.mode')) !== null && _a !== void 0 ? _a : 'file';
130
+ const files = readFiles(resource.fsPath, fileMode);
131
+ if (files === null) {
132
+ return;
133
+ }
134
+ // enforce source locale if provided in settings
135
+ const configLocale = vscode.workspace
136
+ .getConfiguration()
137
+ .get('auto-translate-json.sourceLocale');
138
+ if (!configLocale || configLocale !== files.sourceLocale) {
139
+ showWarning('You must use the ' +
140
+ configLocale +
141
+ '.json file due to your Source Locale setting.');
142
+ return;
143
+ }
144
+ // ask user to pick options
145
+ const keepTranslations = yield askToPreserveTranslations();
146
+ if (keepTranslations === null) {
147
+ showWarning('You must select a translations option');
148
+ return;
149
+ }
150
+ const keepExtras = yield askToKeepExtra();
151
+ if (keepExtras === null) {
152
+ showWarning('You must select a keep option');
153
+ return;
154
+ }
155
+ // load source JSON
156
+ let source;
157
+ try {
158
+ source = yield files.loadJsonFromLocale(files.sourceLocale);
159
+ }
160
+ catch (error) {
161
+ if (error instanceof Error) {
162
+ showError(error, 'Source file malformed');
163
+ }
164
+ return;
165
+ }
166
+ // Iterate target Locales
167
+ files.targetLocales.forEach((targetLocale) => __awaiter(this, void 0, void 0, function* () {
168
+ try {
169
+ const isValid = yield translateEngine.isValidLocale(targetLocale);
170
+ if (!isValid) {
171
+ throw Error(targetLocale + ' is not supported. Skipping.');
172
+ }
173
+ const targetOriginal = yield files.loadJsonFromLocale(targetLocale);
174
+ // Iterate source terms
175
+ const targetNew = yield recurseNode(source, targetOriginal, keepTranslations, keepExtras, files.sourceLocale, targetLocale, translateEngine);
176
+ // save target
177
+ files.saveJsonToLocale(targetLocale, targetNew);
178
+ const feedback = "Translated locale '" + targetLocale + "'";
179
+ console.log(feedback);
180
+ vscode.window.showInformationMessage(feedback);
181
+ }
182
+ catch (error) {
183
+ if (error instanceof Error) {
184
+ showError(error);
185
+ }
186
+ return;
187
+ }
188
+ }));
189
+ }));
190
+ const readFiles = (filePath, mode) => {
191
+ try {
192
+ const files = mode === 'file' ? new files_1.Files(filePath) : new folderFiles_1.FolderFiles(filePath);
193
+ // log locale info
194
+ showMessage('Source locale = ' + files.sourceLocale);
195
+ showMessage('Target locales = ' + files.targetLocales);
196
+ return files;
197
+ }
198
+ catch (error) {
199
+ if (error instanceof Error) {
200
+ showError(error, 'Opening Files: ');
201
+ }
202
+ return null;
203
+ }
204
+ };
205
+ function recurseNode(source, original, keepTranslations, keepExtras, sourceLocale, locale, translateEngine, isArray = false) {
206
+ var _a;
207
+ return __awaiter(this, void 0, void 0, function* () {
208
+ const destination = isArray ? [] : {};
209
+ // defaults
210
+ if (keepTranslations === null) {
211
+ keepTranslations = true;
212
+ }
213
+ if (keepExtras === null) {
214
+ keepExtras = true;
215
+ }
216
+ for (const term in source) {
217
+ const node = source[term];
218
+ if (node instanceof Object && node !== null) {
219
+ destination[term] = yield recurseNode(node, (_a = original[term]) !== null && _a !== void 0 ? _a : {}, keepTranslations, keepExtras, sourceLocale, locale, translateEngine, node instanceof Array);
220
+ }
221
+ else {
222
+ // if we already have a translation, keep it
223
+ if (keepTranslations && original[term]) {
224
+ destination[term] = original[term];
225
+ }
226
+ else if (typeof node === 'number' || typeof node === 'boolean') {
227
+ // numbers and booleans do not need translations
228
+ destination[term] = node;
229
+ }
230
+ else {
231
+ const translation = yield translateEngine
232
+ .translateText(node, sourceLocale, locale)
233
+ .catch((err) => showError(err));
234
+ destination[term] = translation;
235
+ }
236
+ }
237
+ }
238
+ if (keepExtras) {
239
+ // add back in any terms that were not in source
240
+ for (const term in original) {
241
+ if (!destination[term]) {
242
+ destination[term] = original[term];
243
+ }
244
+ }
245
+ }
246
+ return destination;
247
+ });
248
+ }
249
+ }
250
+ exports.activate = activate;
251
+ function showError(error, prefix = '') {
252
+ let message = error.toString();
253
+ if (error.message) {
254
+ message = NAME + ': ' + prefix + error.message;
255
+ }
256
+ else {
257
+ message = NAME + ': ' + prefix + message;
258
+ }
259
+ console.error(message);
260
+ vscode.window.showErrorMessage(message);
261
+ }
262
+ function showWarning(message, prefix = '') {
263
+ message = NAME + ': ' + prefix + message;
264
+ console.log(message);
265
+ vscode.window.showWarningMessage(message);
266
+ }
267
+ function showMessage(message, prefix = '') {
268
+ message = NAME + ': ' + prefix + message;
269
+ console.log(message);
270
+ vscode.window.showInformationMessage(message);
271
+ }
272
+ function askToPreserveTranslations() {
273
+ return __awaiter(this, void 0, void 0, function* () {
274
+ let keepTranslations = null;
275
+ const optionKeep = 'Preserve previous translations (default)';
276
+ const optionReplace = 'Retranslate previous translations';
277
+ yield vscode.window
278
+ .showQuickPick([optionKeep, optionReplace])
279
+ .then((selection) => {
280
+ if (selection === optionReplace) {
281
+ keepTranslations = false;
282
+ }
283
+ if (selection === optionKeep) {
284
+ keepTranslations = true;
285
+ }
286
+ });
287
+ return keepTranslations;
288
+ });
289
+ }
290
+ function askToKeepExtra() {
291
+ return __awaiter(this, void 0, void 0, function* () {
292
+ let keepExtra = null;
293
+ const optionKeep = 'Keep extra translations (default)';
294
+ const optionReplace = 'Remove extra translations';
295
+ yield vscode.window
296
+ .showQuickPick([optionKeep, optionReplace])
297
+ .then((selection) => {
298
+ if (selection === optionReplace) {
299
+ keepExtra = false;
300
+ }
301
+ if (selection === optionKeep) {
302
+ keepExtra = true;
303
+ }
304
+ });
305
+ return keepExtra;
306
+ });
307
+ }
308
+ // this method is called when your extension is deactivated
309
+ function deactivate() { }
310
+ exports.deactivate = deactivate;
package/files.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ export interface IFiles {
2
+ sourceLocale: string;
3
+ targetLocales: Array<string>;
4
+ loadJsonFromLocale(locale: string): Promise<any>;
5
+ saveJsonToLocale(locale: string, file: any): void;
6
+ }
7
+ export declare const readFileAsync: (filename: string) => Promise<string>;
8
+ export declare const loadJsonFromLocale: (fileName: string) => Promise<any>;
9
+ export declare const saveJsonToLocale: (filename: string, file: any) => void;
10
+ export declare class Files implements IFiles {
11
+ folderPath: string;
12
+ sourceLocale: string;
13
+ targetLocales: Array<string>;
14
+ constructor(filePath: string);
15
+ private getLocaleFromFilename;
16
+ private getTargetLocales;
17
+ loadJsonFromLocale(locale: string): Promise<any>;
18
+ saveJsonToLocale(locale: string, file: any): void;
19
+ }
package/files.js ADDED
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.Files = exports.saveJsonToLocale = exports.loadJsonFromLocale = exports.readFileAsync = void 0;
36
+ const path = __importStar(require("path"));
37
+ const fs = __importStar(require("fs"));
38
+ const readFileAsync = (filename) => new Promise((resolve, reject) => {
39
+ fs.readFile(filename, (error, data) => {
40
+ error ? reject(error) : resolve(data.toString());
41
+ });
42
+ });
43
+ exports.readFileAsync = readFileAsync;
44
+ const loadJsonFromLocale = (fileName) => __awaiter(void 0, void 0, void 0, function* () {
45
+ let data = yield (0, exports.readFileAsync)(fileName);
46
+ // handle empty files
47
+ if (!data) {
48
+ data = '{}';
49
+ }
50
+ return JSON.parse(data);
51
+ });
52
+ exports.loadJsonFromLocale = loadJsonFromLocale;
53
+ const saveJsonToLocale = (filename, file) => {
54
+ let data = JSON.stringify(file, null, ' ');
55
+ fs.writeFileSync(filename, data, 'utf8');
56
+ };
57
+ exports.saveJsonToLocale = saveJsonToLocale;
58
+ class Files {
59
+ constructor(filePath) {
60
+ this.folderPath = path.dirname(filePath);
61
+ let fileName = path.basename(filePath);
62
+ this.sourceLocale = this.getLocaleFromFilename(fileName);
63
+ this.targetLocales = this.getTargetLocales();
64
+ }
65
+ getLocaleFromFilename(fileName) {
66
+ return fileName.replace('.json', '');
67
+ }
68
+ getTargetLocales() {
69
+ let locales = new Array();
70
+ let files = fs.readdirSync(this.folderPath);
71
+ files.forEach((file) => {
72
+ let locale = this.getLocaleFromFilename(file);
73
+ if (locale !== this.sourceLocale) {
74
+ locales.push(locale);
75
+ }
76
+ });
77
+ return locales;
78
+ }
79
+ loadJsonFromLocale(locale) {
80
+ let filename = this.folderPath + '/' + locale + '.json';
81
+ return (0, exports.loadJsonFromLocale)(filename);
82
+ }
83
+ saveJsonToLocale(locale, file) {
84
+ let filename = this.folderPath + '/' + locale + '.json';
85
+ (0, exports.saveJsonToLocale)(filename, file);
86
+ }
87
+ }
88
+ exports.Files = Files;
@@ -0,0 +1,12 @@
1
+ import { IFiles } from './files';
2
+ export declare class FolderFiles implements IFiles {
3
+ folderPath: string;
4
+ sourceLocale: string;
5
+ targetLocales: Array<string>;
6
+ fileName: string;
7
+ constructor(filePath: string);
8
+ private getTargetLocales;
9
+ private createFileName;
10
+ loadJsonFromLocale(locale: string): Promise<any>;
11
+ saveJsonToLocale(locale: string, file: any): void;
12
+ }
package/folderFiles.js ADDED
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.FolderFiles = void 0;
27
+ const path = __importStar(require("path"));
28
+ const fs = __importStar(require("fs"));
29
+ const files_1 = require("./files");
30
+ class FolderFiles {
31
+ constructor(filePath) {
32
+ const localeDir = path.dirname(filePath);
33
+ this.folderPath = path.dirname(localeDir);
34
+ this.fileName = path.basename(filePath);
35
+ this.sourceLocale = path.basename(localeDir);
36
+ this.targetLocales = this.getTargetLocales();
37
+ console.log(this.folderPath, this.fileName, this.sourceLocale, this.targetLocales);
38
+ }
39
+ getTargetLocales() {
40
+ let files = fs
41
+ .readdirSync(this.folderPath)
42
+ .map((folder) => path.basename(folder))
43
+ .map((locale) => (locale !== this.sourceLocale ? locale : ''))
44
+ .filter((x) => x); // do not want empty strings
45
+ return files;
46
+ }
47
+ createFileName(locale) {
48
+ return `${this.folderPath}/${locale}/${this.fileName}`;
49
+ }
50
+ loadJsonFromLocale(locale) {
51
+ return (0, files_1.loadJsonFromLocale)(this.createFileName(locale));
52
+ }
53
+ saveJsonToLocale(locale, file) {
54
+ (0, files_1.saveJsonToLocale)(this.createFileName(locale), file);
55
+ }
56
+ }
57
+ exports.FolderFiles = FolderFiles;
package/google.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { ITranslate } from './translate.interface';
2
+ export declare class GoogleTranslate implements ITranslate {
3
+ private apikey;
4
+ private googleTranslate;
5
+ constructor(apikey: string);
6
+ isValidLocale(targetLocale: string): boolean;
7
+ translateText(text: string, _sourceLocale: string, targetLocale: string): Promise<string>;
8
+ }
package/google.js ADDED
@@ -0,0 +1,163 @@
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.GoogleTranslate = void 0;
13
+ //const {Translate} = require('@google-cloud/translate').v2;
14
+ const v2_1 = require("@google-cloud/translate/build/src/v2");
15
+ const util_1 = require("./util");
16
+ const supportedLanguages = [
17
+ 'af',
18
+ 'sq',
19
+ 'am',
20
+ 'ar',
21
+ 'hy',
22
+ 'az',
23
+ 'eu',
24
+ 'be',
25
+ 'bn',
26
+ 'bs',
27
+ 'bg',
28
+ 'ca',
29
+ 'ceb',
30
+ 'zh-CN',
31
+ 'zh',
32
+ 'zh-TW',
33
+ 'co',
34
+ 'hr',
35
+ 'cs',
36
+ 'da',
37
+ 'nl',
38
+ 'en',
39
+ 'eo',
40
+ 'et',
41
+ 'fi',
42
+ 'fr',
43
+ 'fy',
44
+ 'gl',
45
+ 'ka',
46
+ 'de',
47
+ 'el',
48
+ 'gu',
49
+ 'ht',
50
+ 'ha',
51
+ 'haw',
52
+ 'he',
53
+ 'iw',
54
+ 'hi',
55
+ 'hmn',
56
+ 'hu',
57
+ 'is',
58
+ 'ig',
59
+ 'id',
60
+ 'ga',
61
+ 'it',
62
+ 'ja',
63
+ 'jv',
64
+ 'kn',
65
+ 'kk',
66
+ 'km',
67
+ 'rw',
68
+ 'ko',
69
+ 'ku',
70
+ 'ky',
71
+ 'lo',
72
+ 'lv',
73
+ 'lt',
74
+ 'lb',
75
+ 'mk',
76
+ 'mg',
77
+ 'ms',
78
+ 'ml',
79
+ 'mt',
80
+ 'mi',
81
+ 'mr',
82
+ 'mn',
83
+ 'my',
84
+ 'ne',
85
+ 'no',
86
+ 'ny',
87
+ 'or',
88
+ 'ps',
89
+ 'fa',
90
+ 'pl',
91
+ 'pt',
92
+ 'pa',
93
+ 'ro',
94
+ 'ru',
95
+ 'sm',
96
+ 'gd',
97
+ 'sr',
98
+ 'st',
99
+ 'sn',
100
+ 'sd',
101
+ 'si',
102
+ 'sk',
103
+ 'sl',
104
+ 'so',
105
+ 'es',
106
+ 'su',
107
+ 'sw',
108
+ 'sv',
109
+ 'tl',
110
+ 'tg',
111
+ 'ta',
112
+ 'tt',
113
+ 'te',
114
+ 'th',
115
+ 'tr',
116
+ 'tk',
117
+ 'uk',
118
+ 'ur',
119
+ 'ug',
120
+ 'uz',
121
+ 'vi',
122
+ 'cy',
123
+ 'xh',
124
+ 'yi',
125
+ 'yo',
126
+ 'zu'
127
+ ];
128
+ class GoogleTranslate {
129
+ constructor(apikey) {
130
+ this.apikey = apikey;
131
+ this.googleTranslate = new v2_1.Translate({ key: this.apikey });
132
+ // this.googleTranslate.getSupportedLanguages();
133
+ }
134
+ isValidLocale(targetLocale) {
135
+ return supportedLanguages.includes(targetLocale);
136
+ }
137
+ translateText(text, _sourceLocale, targetLocale) {
138
+ return __awaiter(this, void 0, void 0, function* () {
139
+ let args;
140
+ ({ args, text } = util_1.Util.replaceContextVariables(text));
141
+ let result = '';
142
+ try {
143
+ const response = yield this.googleTranslate.translate(text, targetLocale);
144
+ result = response[0];
145
+ }
146
+ catch (error) {
147
+ if (error instanceof Error) {
148
+ let message = error.message;
149
+ if (error.message === 'Invalid Value') {
150
+ message = 'Invalid Locale ' + targetLocale;
151
+ }
152
+ console.log(message);
153
+ return message;
154
+ }
155
+ return 'error';
156
+ }
157
+ // replace arguments with numbers
158
+ result = util_1.Util.replaceArgumentsWithNumbers(args, result);
159
+ return result;
160
+ });
161
+ }
162
+ }
163
+ exports.GoogleTranslate = GoogleTranslate;
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { Configuration } from './config';
2
+ import { translate } from './lib';
3
+ export { Configuration, translate };