auto-translate-json-library 1.2.0 → 1.2.2

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/lib.js CHANGED
@@ -20,39 +20,38 @@ const google_1 = require("./google");
20
20
  const util_1 = require("./util");
21
21
  const deepl_1 = require("./deepl");
22
22
  const openai_1 = require("./openai");
23
- const NAME = 'AutoTranslateJSON';
24
23
  function translate(sourceFile, config) {
25
24
  return __awaiter(this, void 0, void 0, function* () {
26
25
  // set the delimiters for named arguments
27
- const startDelimiter = config.startDelimiter || '{';
26
+ const startDelimiter = config.startDelimiter || "{";
28
27
  if (startDelimiter) {
29
28
  util_1.Util.startDelimiter = startDelimiter;
30
29
  }
31
- const endDelimiter = config.endDelimiter || '}';
30
+ const endDelimiter = config.endDelimiter || "}";
32
31
  if (endDelimiter) {
33
32
  util_1.Util.endDelimiter = endDelimiter;
34
33
  }
35
34
  let translateEngine;
36
- if (config.translationKeyInfo.kind === 'google') {
35
+ if (config.translationKeyInfo.kind === "google") {
37
36
  translateEngine = new google_1.GoogleTranslate(config.translationKeyInfo.apiKey);
38
37
  }
39
- else if (config.translationKeyInfo.kind === 'aws') {
38
+ else if (config.translationKeyInfo.kind === "aws") {
40
39
  translateEngine = new aws_1.AWSTranslate(config.translationKeyInfo.accessKeyId, config.translationKeyInfo.secretAccessKey, config.translationKeyInfo.region);
41
40
  }
42
- else if (config.translationKeyInfo.kind === 'azure') {
41
+ else if (config.translationKeyInfo.kind === "azure") {
43
42
  translateEngine = new azure_1.AzureTranslate(config.translationKeyInfo.secretKey, config.translationKeyInfo.region);
44
43
  }
45
- else if (config.translationKeyInfo.kind === 'deepLFree') {
46
- translateEngine = new deepl_1.DeepLTranslate(config.translationKeyInfo.secretKey, 'free');
44
+ else if (config.translationKeyInfo.kind === "deepLFree") {
45
+ translateEngine = new deepl_1.DeepLTranslate(config.translationKeyInfo.secretKey, "free");
47
46
  }
48
- else if (config.translationKeyInfo.kind === 'deepLPro') {
49
- translateEngine = new deepl_1.DeepLTranslate(config.translationKeyInfo.secretKey, 'pro');
47
+ else if (config.translationKeyInfo.kind === "deepLPro") {
48
+ translateEngine = new deepl_1.DeepLTranslate(config.translationKeyInfo.secretKey, "pro");
50
49
  }
51
- else if (config.translationKeyInfo.kind === 'openai') {
50
+ else if (config.translationKeyInfo.kind === "openai") {
52
51
  translateEngine = new openai_1.OpenAITranslate(config.translationKeyInfo.apiKey);
53
52
  }
54
53
  else {
55
- console.warn('You must provide a Google, AWS, Azure, deepL, openai parameters first in the extension settings.');
54
+ console.warn("You must provide a Google, AWS, Azure, deepL, openai parameters first in the extension settings.");
56
55
  return;
57
56
  }
58
57
  const fileMode = config.mode;
@@ -62,13 +61,11 @@ function translate(sourceFile, config) {
62
61
  }
63
62
  // enforce source locale if provided in settings
64
63
  if (config.sourceLocale !== files.sourceLocale) {
65
- console.log('You must use the ' +
66
- config.sourceLocale +
67
- '.json file due to your Source Locale setting.');
64
+ console.log(`You must use the ${config.sourceLocale}.json file due to your Source Locale setting.`);
68
65
  return;
69
66
  }
70
- const keepTranslations = config.keepTranslations === 'keep';
71
- const keepExtras = config.keepExtraTranslations === 'keep';
67
+ const keepTranslations = config.keepTranslations === "keep";
68
+ const keepExtras = config.keepExtraTranslations === "keep";
72
69
  // load source JSON
73
70
  let source;
74
71
  try {
@@ -76,7 +73,7 @@ function translate(sourceFile, config) {
76
73
  }
77
74
  catch (error) {
78
75
  if (error instanceof Error) {
79
- console.log(error, 'Source file malformed');
76
+ console.log(error, "Source file malformed");
80
77
  }
81
78
  return;
82
79
  }
@@ -85,14 +82,14 @@ function translate(sourceFile, config) {
85
82
  try {
86
83
  const isValid = yield translateEngine.isValidLocale(targetLocale);
87
84
  if (!isValid) {
88
- throw Error(targetLocale + ' is not supported. Skipping.');
85
+ throw Error(`${targetLocale} is not supported. Skipping.`);
89
86
  }
90
87
  const targetOriginal = yield files.loadJsonFromLocale(targetLocale);
91
88
  // Iterate source terms
92
- const targetNew = yield recurseNode(source, targetOriginal, keepTranslations, keepExtras, files.sourceLocale, targetLocale, translateEngine);
89
+ const targetNew = yield recurseNode(source, targetOriginal, keepTranslations, keepExtras, files.sourceLocale, targetLocale, translateEngine, config.ignorePrefix);
93
90
  // save target
94
91
  files.saveJsonToLocale(targetLocale, targetNew);
95
- const feedback = "Translated locale '" + targetLocale + "'";
92
+ const feedback = `Translated locale '${targetLocale}'`;
96
93
  console.log(feedback);
97
94
  }
98
95
  catch (error) {
@@ -107,10 +104,10 @@ function translate(sourceFile, config) {
107
104
  exports.translate = translate;
108
105
  const readFiles = (filePath, mode) => {
109
106
  try {
110
- const files = mode === 'file' ? new files_1.Files(filePath) : new folderFiles_1.FolderFiles(filePath);
107
+ const files = mode === "file" ? new files_1.Files(filePath) : new folderFiles_1.FolderFiles(filePath);
111
108
  // log locale info
112
- console.log('Source locale = ' + files.sourceLocale);
113
- console.log('Target locales = ' + files.targetLocales);
109
+ console.log(`Source locale = ${files.sourceLocale}`);
110
+ console.log(`Target locales = ${files.targetLocales}`);
114
111
  return files;
115
112
  }
116
113
  catch (error) {
@@ -120,9 +117,10 @@ const readFiles = (filePath, mode) => {
120
117
  return null;
121
118
  }
122
119
  };
123
- function recurseNode(source, original, keepTranslations, keepExtras, sourceLocale, locale, translateEngine, isArray = false) {
120
+ function recurseNode(source, original, keepTranslations, keepExtras, sourceLocale, locale, translateEngine, ignorePrefix = "", isArray = false) {
124
121
  var _a;
125
122
  return __awaiter(this, void 0, void 0, function* () {
123
+ // rome-ignore lint/suspicious/noExplicitAny: <explanation>
126
124
  const destination = isArray ? [] : {};
127
125
  // defaults
128
126
  if (keepTranslations === null) {
@@ -134,22 +132,28 @@ function recurseNode(source, original, keepTranslations, keepExtras, sourceLocal
134
132
  for (const term in source) {
135
133
  const node = source[term];
136
134
  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);
135
+ destination[term] = yield recurseNode(node, (_a = original[term]) !== null && _a !== void 0 ? _a : {}, keepTranslations, keepExtras, sourceLocale, locale, translateEngine, ignorePrefix, node instanceof Array);
138
136
  }
139
137
  else {
140
138
  // if we already have a translation, keep it
141
139
  if (keepTranslations && original[term]) {
142
140
  destination[term] = original[term];
143
141
  }
144
- else if (typeof node === 'number' || typeof node === 'boolean') {
142
+ else if (typeof node === "number" || typeof node === "boolean") {
145
143
  // numbers and booleans do not need translations
146
144
  destination[term] = node;
147
145
  }
148
146
  else {
149
- const translation = yield translateEngine
150
- .translateText(node, sourceLocale, locale)
151
- .catch((err) => console.error(err));
152
- destination[term] = translation;
147
+ if (ignorePrefix === "" ||
148
+ (ignorePrefix !== "" && !term.startsWith(ignorePrefix))) {
149
+ const translation = yield translateEngine
150
+ .translateText(node, sourceLocale, locale)
151
+ .catch((err) => console.error(err));
152
+ destination[term] = translation;
153
+ }
154
+ else {
155
+ delete destination[term];
156
+ }
153
157
  }
154
158
  }
155
159
  }
package/openai.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { ITranslate } from "./translate.interface";
2
2
  export declare class OpenAITranslate implements ITranslate {
3
3
  private apiKey;
4
- private endpoint;
5
4
  private openai;
6
5
  constructor(apiKey: string);
7
6
  isValidLocale(targetLocale: string): boolean;
package/openai.js CHANGED
@@ -13,119 +13,118 @@ exports.OpenAITranslate = void 0;
13
13
  const util_1 = require("./util");
14
14
  const openai_1 = require("openai");
15
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'
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
124
  };
125
125
  class OpenAITranslate {
126
126
  constructor(apiKey) {
127
127
  this.apiKey = apiKey;
128
- this.endpoint = 'https://api.cognitive.microsofttranslator.com';
129
128
  const configuration = new openai_1.Configuration({
130
129
  apiKey: apiKey,
131
130
  });
@@ -136,10 +135,10 @@ class OpenAITranslate {
136
135
  }
137
136
  translateText(text, sourceLocale, targetLocale) {
138
137
  return __awaiter(this, void 0, void 0, function* () {
139
- if (sourceLocale !== 'en') {
140
- throw Error(sourceLocale + ' is not supported.Currently we support just English ');
138
+ if (sourceLocale !== "en") {
139
+ throw Error(`${sourceLocale} is not supported.Currently we support just English `);
141
140
  }
142
- let result = '';
141
+ let result = "";
143
142
  let args;
144
143
  ({ args, text } = util_1.Util.replaceContextVariables(text));
145
144
  const prompt = `Translate this into ${supportedLanguages[targetLocale]}:\n${text}\n.`;
@@ -154,7 +153,7 @@ class OpenAITranslate {
154
153
  });
155
154
  if (response.data.choices[0].text !== undefined) {
156
155
  result = util_1.Util.replaceArgumentsWithNumbers(args, response.data.choices[0].text);
157
- result = result.replace(/^\n+|\n+$/g, '');
156
+ result = result.replace(/^\n+|\n+$/g, "");
158
157
  }
159
158
  else {
160
159
  console.error(`can not translate text with prompt :${prompt}`);
package/package.json CHANGED
@@ -1,12 +1,20 @@
1
1
  {
2
2
  "name": "auto-translate-json-library",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
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": {
8
8
  "prepare": "tsc",
9
9
  "build": "tsc",
10
+ "lint": "rome check ./src",
11
+ "lint-fix": "rome check ./src --apply",
12
+ "lint-fix-all": "rome check --apply-unsafe ./src",
13
+ "format": "rome format ./src",
14
+ "format-fix": "rome format ./src --write",
15
+ "test": "jest",
16
+ "test:coverage": "jest --coverage",
17
+ "test:watch": "jest --watch",
10
18
  "start": "ts-node src/index.ts"
11
19
  },
12
20
  "keywords": [
@@ -39,21 +47,25 @@
39
47
  }
40
48
  },
41
49
  "devDependencies": {
42
- "@types/node": "20.1.4",
50
+ "@types/jest": "29.5.2",
51
+ "@types/node": "20.3.1",
43
52
  "@types/yargs": "17.0.24",
44
53
  "husky": "8.0.3",
54
+ "jest": "^29.5.0",
45
55
  "prettier": "2.8.8",
46
56
  "pretty-quick": "3.1.3",
57
+ "rome": "12.1.3",
58
+ "ts-jest": "29.1.0",
47
59
  "ts-node": "10.9.1",
48
- "typescript": "5.0.4"
60
+ "typescript": "5.1.3"
49
61
  },
50
62
  "dependencies": {
51
- "@aws-sdk/client-translate": "3.332.0",
63
+ "@aws-sdk/client-translate": "3.359.0",
52
64
  "@google-cloud/translate": "7.2.1",
53
65
  "axios": "1.4.0",
54
- "dotenv": "16.0.3",
66
+ "dotenv": "16.3.1",
55
67
  "form-data": "4.0.0",
56
- "openai": "3.2.1",
68
+ "openai": "3.3.0",
57
69
  "uuid": "9.0.0",
58
70
  "yargs": "17.7.2"
59
71
  }
@@ -2,3 +2,6 @@ export interface ITranslate {
2
2
  isValidLocale(targetLocale: string): boolean;
3
3
  translateText(text: string, sourceLocale: string, targetLocale: string): Promise<string>;
4
4
  }
5
+ export interface TranslationFile {
6
+ [key: string]: any;
7
+ }
package/util.js CHANGED
@@ -5,17 +5,17 @@ class Util {
5
5
  static set startDelimiter(value) {
6
6
  Util._startDelimiter = value;
7
7
  // update regex
8
- Util.pattern = new RegExp(Util._startDelimiter + '(.*?)' + Util._endDelimiter, 'g');
8
+ Util.pattern = new RegExp(`${Util._startDelimiter}(.*?)${Util._endDelimiter}`, "g");
9
9
  }
10
10
  static set endDelimiter(value) {
11
11
  Util._endDelimiter = value;
12
12
  // update regex
13
- Util.pattern = new RegExp(Util._startDelimiter + '(.*?)' + Util._endDelimiter, 'g');
13
+ Util.pattern = new RegExp(`${Util._startDelimiter}(.*?)${Util._endDelimiter}`, "g");
14
14
  }
15
15
  static replaceArgumentsWithNumbers(args, result) {
16
16
  if (args) {
17
17
  let i = 0;
18
- for (let arg of args) {
18
+ for (const arg of args) {
19
19
  result = result.replace(Util._startDelimiter + i + Util._endDelimiter, arg);
20
20
  i++;
21
21
  }
@@ -27,7 +27,7 @@ class Util {
27
27
  // replace arguments with numbers
28
28
  if (args) {
29
29
  let i = 0;
30
- for (let arg of args) {
30
+ for (const arg of args) {
31
31
  text = text.replace(arg, Util._startDelimiter + i + Util._endDelimiter);
32
32
  i++;
33
33
  }
@@ -35,7 +35,7 @@ class Util {
35
35
  return { args, text };
36
36
  }
37
37
  }
38
- Util._startDelimiter = '{';
39
- Util._endDelimiter = '}';
40
- Util.pattern = new RegExp(Util._startDelimiter + '(.*?)' + Util._endDelimiter, 'g');
41
38
  exports.Util = Util;
39
+ Util._startDelimiter = "{";
40
+ Util._endDelimiter = "}";
41
+ Util.pattern = new RegExp(`${Util._startDelimiter}(.*?)${Util._endDelimiter}`, "g");