auto-translate-json-library 1.0.5 → 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.
Files changed (3) hide show
  1. package/package.json +6 -1
  2. package/README.MD +0 -113
  3. package/extension.js +0 -310
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auto-translate-json-library",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
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",
@@ -11,7 +11,12 @@
11
11
  },
12
12
  "keywords": [],
13
13
  "author": "codegrue",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/topce/auto-translate-json-library"
17
+ },
14
18
  "contributors": [
19
+ "codegrue",
15
20
  "topce"
16
21
  ],
17
22
  "license": "MIT",
package/README.MD DELETED
@@ -1,113 +0,0 @@
1
- # Use as library
2
-
3
- ```shell
4
- npm i auto-translate-json-library
5
- ```
6
-
7
- ```Typescript
8
- import { translate, Configuration } from 'auto-translate-json-library';
9
-
10
- let config: Configuration = {} as Configuration;
11
-
12
- // set google api key
13
- config.translationKeyInfo = {
14
- kind: 'google',
15
- apiKey: googleApiKey
16
- };
17
- const pivotTranslation="./en.json";
18
- await translate(pivotTranslation, config) ;
19
- // translation for example fr.json in same folder as pivotTranslation should contain the translations
20
- ```
21
-
22
- # Use as command line tool
23
-
24
- first install it
25
-
26
- ```shell
27
- npm i auto-translate-json-library
28
- ```
29
-
30
- do not forget to set translation engine parameters in environment variables or .env file
31
-
32
- ```shell
33
-
34
- ```shell
35
- node node_modules\auto-translate-json-library\index.js --pivotTranslation=./translations/en.json
36
- ```
37
-
38
- if you clone repo you can use the following command
39
-
40
- ```shell
41
- npm start --pivotTranslation=./test/en.json
42
- ```
43
-
44
- ```
45
- # CLI PARAMETERS
46
-
47
- mode,
48
- file or folder,
49
- default is file
50
-
51
- startDelimiter,
52
- start delimiter,
53
- default is {
54
-
55
- endDelimiter,
56
- end delimiter,
57
- default is }
58
-
59
- keepExtraTranslations,
60
- keep extra translations (not present in pivot translation),
61
- default is keep,
62
- it could be keep or remove
63
-
64
- keepTranslations,
65
- keep existing translations,
66
- default is keep
67
- it could be keep or retranslate
68
-
69
- sourceLocale
70
- source locale
71
- it could be en or fr ...
72
- if specified it must match with pivot translation
73
-
74
- pivotTranslation
75
- pivot translation file that will be used as source for translations
76
-
77
- # ENVIRONMENT VARIABLES
78
-
79
- google parameters
80
- ATJ_GOOGLE_API_KEY=
81
-
82
- aws parameters
83
- ATJ_AWS_ACCESS_KEY_ID=
84
- ATJ_AWS_SECRET_ACCESS_KEY=
85
- ATJ_AWS_REGION=
86
-
87
- azure parameters
88
- ATJ_AZURE_SECRET_KEY=
89
- ATJ_AZURE_REGION=
90
-
91
- deepl pro parameters
92
- ATJ_DEEPL_PRO_SECRET_KEY=
93
- deepl free parameters
94
- ATJ_DEEPL_FREE_SECRET_KEY=
95
-
96
- start delimiter
97
- ATJ_START_DELIMITER={{
98
- end delimiter
99
- ATJ_END_DELIMITER=}}
100
-
101
- file or folder
102
- ATJ_MODE=file
103
-
104
- source locale
105
- ATJ_SOURCE_LOCALE=en
106
-
107
- keep or retranslate existing translations
108
- ATJ_KEEP_TRANSLATIONS=keep
109
-
110
- keep or remove extra translations
111
- ATJ_KEEP_EXTRA_TRANSLATIONS=keep
112
-
113
- You can also use .env file to store environment variables
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;