auto-translate-json-library 1.0.3 → 1.0.6
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 +111 -67
- package/package.json +15 -6
- package/extension.js +0 -310
package/index.js
CHANGED
|
@@ -1,76 +1,120 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.translate = void 0;
|
|
4
|
+
const process_1 = require("process");
|
|
4
5
|
const lib_1 = require("./lib");
|
|
5
6
|
Object.defineProperty(exports, "translate", { enumerable: true, get: function () { return lib_1.translate; } });
|
|
6
7
|
let config = {};
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
setDefaultConfiguration();
|
|
9
|
+
setConfigurationFromEnvironment();
|
|
10
|
+
setConfigurationFromCLI();
|
|
11
|
+
// console.log(config);
|
|
12
|
+
function setConfigurationFromCLI() {
|
|
13
|
+
const argv = require('yargs').argv;
|
|
14
|
+
if (argv.mode === 'file') {
|
|
15
|
+
config.mode = 'file';
|
|
16
|
+
}
|
|
17
|
+
else if (argv.mode === 'folder') {
|
|
18
|
+
config.mode = 'folder';
|
|
19
|
+
}
|
|
20
|
+
if (argv.startDelimiter !== undefined) {
|
|
21
|
+
config.startDelimiter = argv.startDelimiter;
|
|
22
|
+
}
|
|
23
|
+
if (argv.endDelimiter !== undefined) {
|
|
24
|
+
config.endDelimiter = argv.endDelimiter;
|
|
25
|
+
}
|
|
26
|
+
if (argv.keepExtraTranslations === 'keep') {
|
|
27
|
+
config.keepExtraTranslations = 'keep';
|
|
28
|
+
}
|
|
29
|
+
else if (argv.keepExtraTranslations === 'remove') {
|
|
30
|
+
config.keepExtraTranslations = 'remove';
|
|
31
|
+
}
|
|
32
|
+
if (argv.keepTranslations === 'keep') {
|
|
33
|
+
config.keepTranslations = 'keep';
|
|
34
|
+
}
|
|
35
|
+
else if (argv.keepTranslations === 'retranslate') {
|
|
36
|
+
config.keepTranslations = 'retranslate';
|
|
37
|
+
}
|
|
38
|
+
if (argv.sourceLocale !== undefined) {
|
|
39
|
+
config.sourceLocale = argv.sourceLocale;
|
|
40
|
+
}
|
|
41
|
+
if (argv.pivotTranslation !== undefined) {
|
|
42
|
+
(0, lib_1.translate)(argv.pivotTranslation, config);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
console.log('Please provide a pivot translation');
|
|
46
|
+
(0, process_1.exit)(1);
|
|
47
|
+
}
|
|
13
48
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
config.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
region: process.env.ATJ_AWS_REGION,
|
|
22
|
-
};
|
|
49
|
+
function setDefaultConfiguration() {
|
|
50
|
+
config.mode = 'file';
|
|
51
|
+
config.startDelimiter = '{';
|
|
52
|
+
config.endDelimiter = '}';
|
|
53
|
+
config.keepExtraTranslations = 'keep';
|
|
54
|
+
config.keepTranslations = 'keep';
|
|
55
|
+
config.sourceLocale = 'en';
|
|
23
56
|
}
|
|
24
|
-
|
|
25
|
-
config
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
if (process.env.
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
if (process.env.
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (
|
|
75
|
-
|
|
57
|
+
function setConfigurationFromEnvironment() {
|
|
58
|
+
require('dotenv').config();
|
|
59
|
+
if (process.env.ATJ_GOOGLE_API_KEY) {
|
|
60
|
+
config.translationKeyInfo = {
|
|
61
|
+
kind: 'google',
|
|
62
|
+
apiKey: process.env.ATJ_GOOGLE_API_KEY,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
if (process.env.ATJ_AWS_ACCESS_KEY_ID &&
|
|
66
|
+
process.env.ATJ_AWS_SECRET_ACCESS_KEY &&
|
|
67
|
+
process.env.ATJ_AWS_REGION) {
|
|
68
|
+
config.translationKeyInfo = {
|
|
69
|
+
kind: 'aws',
|
|
70
|
+
accessKeyId: process.env.ATJ_AWS_ACCESS_KEY_ID,
|
|
71
|
+
secretAccessKey: process.env.ATJ_AWS_SECRET_ACCESS_KEY,
|
|
72
|
+
region: process.env.ATJ_AWS_REGION,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
if (process.env.ATJ_AZURE_SECRET_KEY && process.env.ATJ_AZURE_REGION) {
|
|
76
|
+
config.translationKeyInfo = {
|
|
77
|
+
kind: 'azure',
|
|
78
|
+
secretKey: process.env.ATJ_AZURE_SECRET_KEY,
|
|
79
|
+
region: process.env.ATJ_AZURE_REGION,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
if (process.env.ATJ_DEEPL_PRO_SECRET_KEY) {
|
|
83
|
+
config.translationKeyInfo = {
|
|
84
|
+
kind: 'deepLPro',
|
|
85
|
+
secretKey: process.env.ATJ_DEEPL_PRO_SECRET_KEY,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (process.env.ATJ_DEEPL_FREE_SECRET_KEY) {
|
|
89
|
+
config.translationKeyInfo = {
|
|
90
|
+
kind: 'deepLFree',
|
|
91
|
+
secretKey: process.env.ATJ_DEEPL_FREE_SECRET_KEY,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
if (process.env.ATJ_START_DELIMITER) {
|
|
95
|
+
config.startDelimiter = process.env.ATJ_START_DELIMITER;
|
|
96
|
+
}
|
|
97
|
+
if (process.env.ATJ_END_DELIMITER) {
|
|
98
|
+
config.endDelimiter = process.env.ATJ_END_DELIMITER;
|
|
99
|
+
}
|
|
100
|
+
if (process.env.ATJ_END_DELIMITER) {
|
|
101
|
+
config.endDelimiter = process.env.ATJ_END_DELIMITER;
|
|
102
|
+
}
|
|
103
|
+
if (process.env.ATJ_MODE &&
|
|
104
|
+
(process.env.ATJ_MODE === 'file' || process.env.ATJ_MODE === 'folder')) {
|
|
105
|
+
config.mode = process.env.ATJ_MODE;
|
|
106
|
+
}
|
|
107
|
+
if (process.env.ATJ_SOURCE_LOCALE) {
|
|
108
|
+
config.sourceLocale = process.env.ATJ_SOURCE_LOCALE;
|
|
109
|
+
}
|
|
110
|
+
if (process.env.ATJ_KEEP_TRANSLATIONS &&
|
|
111
|
+
(process.env.ATJ_KEEP_TRANSLATIONS === 'keep' ||
|
|
112
|
+
process.env.ATJ_KEEP_TRANSLATIONS === 'retranslate')) {
|
|
113
|
+
config.keepTranslations = process.env.ATJ_KEEP_TRANSLATIONS;
|
|
114
|
+
}
|
|
115
|
+
if (process.env.ATJ_KEEP_EXTRA_TRANSLATIONS &&
|
|
116
|
+
(process.env.ATJ_KEEP_EXTRA_TRANSLATIONS === 'keep' ||
|
|
117
|
+
process.env.ATJ_KEEP_EXTRA_TRANSLATIONS === 'remove')) {
|
|
118
|
+
config.keepExtraTranslations = process.env.ATJ_KEEP_EXTRA_TRANSLATIONS;
|
|
119
|
+
}
|
|
76
120
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "auto-translate-json-library",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "",
|
|
3
|
+
"version": "1.0.6",
|
|
4
|
+
"description": "Automatically translates JSON language files to other languages using Google Translate,AWS,Azure or DeepL",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
7
7
|
"scripts": {
|
|
@@ -10,7 +10,15 @@
|
|
|
10
10
|
"start": "ts-node src/index.ts"
|
|
11
11
|
},
|
|
12
12
|
"keywords": [],
|
|
13
|
-
"author": "",
|
|
13
|
+
"author": "codegrue",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/topce/auto-translate-json-library"
|
|
17
|
+
},
|
|
18
|
+
"contributors": [
|
|
19
|
+
"codegrue",
|
|
20
|
+
"topce"
|
|
21
|
+
],
|
|
14
22
|
"license": "MIT",
|
|
15
23
|
"husky": {
|
|
16
24
|
"hooks": {
|
|
@@ -19,6 +27,7 @@
|
|
|
19
27
|
},
|
|
20
28
|
"devDependencies": {
|
|
21
29
|
"@types/node": "^14.18.21",
|
|
30
|
+
"@types/yargs": "^17.0.10",
|
|
22
31
|
"husky": "^8.0.1",
|
|
23
32
|
"prettier": "^2.7.1",
|
|
24
33
|
"pretty-quick": "^3.1.3",
|
|
@@ -26,12 +35,12 @@
|
|
|
26
35
|
"typescript": "^4.7.4"
|
|
27
36
|
},
|
|
28
37
|
"dependencies": {
|
|
29
|
-
"@aws-sdk/client-translate": "^3.
|
|
30
|
-
"@google-cloud/translate": "^
|
|
38
|
+
"@aws-sdk/client-translate": "^3.121.0",
|
|
39
|
+
"@google-cloud/translate": "^7.0.0",
|
|
31
40
|
"axios": "^0.27.2",
|
|
32
41
|
"dotenv": "^16.0.1",
|
|
33
42
|
"form-data": "^4.0.0",
|
|
34
43
|
"uuid": "^8.3.2",
|
|
35
|
-
"
|
|
44
|
+
"yargs": "^17.5.1"
|
|
36
45
|
}
|
|
37
46
|
}
|
package/extension.js
DELETED
|
@@ -1,310 +0,0 @@
|
|
|
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;
|