auto-translate-json-library 1.2.3 → 1.2.4

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/README.MD CHANGED
@@ -1,3 +1,12 @@
1
+ # About project
2
+
3
+ This project is used in, (and refactored from) excellent vs code extension
4
+ Auto Translate JSON
5
+ <https://marketplace.visualstudio.com/items?itemName=JeffJorczak.auto-translate-json>
6
+
7
+ It open posibility to use Auto Taransation JSON
8
+ not just in vs code but as a library and command line interface
9
+
1
10
  # Use as library
2
11
 
3
12
  ```shell
package/package.json CHANGED
@@ -1,72 +1,67 @@
1
1
  {
2
- "name": "auto-translate-json-library",
3
- "version": "1.2.3",
4
- "description": "Automatically translates JSON language files to other languages using Google Translate,AWS,Azure,DeepL or OpenAI",
5
- "main": "./index.js",
6
- "types": "./index.d.ts",
7
- "scripts": {
8
- "prepare": "tsc",
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",
18
- "start": "ts-node src/index.ts"
19
- },
20
- "keywords": [
21
- "translate",
22
- "language",
23
- "json",
24
- "flutter",
25
- "angular",
26
- "ngx-translate",
27
- "transloco",
28
- "auto-translate-json",
29
- "google",
30
- "Azure",
31
- "DeepL",
32
- "OpenAI"
33
- ],
34
- "author": "codegrue",
35
- "repository": {
36
- "type": "git",
37
- "url": "https://github.com/topce/auto-translate-json-library"
38
- },
39
- "contributors": [
40
- "codegrue",
41
- "topce"
42
- ],
43
- "license": "MIT",
44
- "husky": {
45
- "hooks": {
46
- "pre-commit": "pretty-quick --staged"
2
+ "name": "auto-translate-json-library",
3
+ "version": "1.2.4",
4
+ "description": "Automatically translates JSON language files to other languages using Google Translate,AWS,Azure,DeepL or OpenAI",
5
+ "main": "./index.js",
6
+ "bin": {
7
+ "atj": "./bin/cli.js"
8
+ },
9
+ "types": "./index.d.ts",
10
+ "scripts": {
11
+ "prepare": "tsc",
12
+ "build": "tsc",
13
+ "lint": "biome check ./src",
14
+ "lint-fix": "biome check ./src --apply",
15
+ "lint-fix-all": "biome check --apply-unsafe ./src",
16
+ "format": "biome format ./src",
17
+ "format-fix": "biome format ./src --write",
18
+ "start": "ts-node src/index.ts"
19
+ },
20
+ "keywords": [
21
+ "translate",
22
+ "language",
23
+ "json",
24
+ "flutter",
25
+ "angular",
26
+ "ngx-translate",
27
+ "transloco",
28
+ "auto-translate-json",
29
+ "google",
30
+ "Azure",
31
+ "DeepL",
32
+ "OpenAI"
33
+ ],
34
+ "author": "codegrue",
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "https://github.com/topce/auto-translate-json-library"
38
+ },
39
+ "contributors": [
40
+ "codegrue",
41
+ "gajo357",
42
+ "topce",
43
+ "BenGMiles",
44
+ "stonega"
45
+ ],
46
+ "license": "MIT",
47
+ "devDependencies": {
48
+ "@topce/typescript": "5.3.1",
49
+ "@types/minimist": "1.2.2",
50
+ "@types/node": "20.6.5",
51
+ "@types/yargs": "17.0.25",
52
+ "@biomejs/biome": "1.2.2",
53
+ "ts-node": "10.9.1"
54
+ },
55
+ "dependencies": {
56
+ "@aws-sdk/client-translate": "3.418.0",
57
+ "@google-cloud/translate": "8.0.2",
58
+ "axios": "1.5.0",
59
+ "dotenv": "16.3.1",
60
+ "form-data": "4.0.0",
61
+ "minimist": "1.2.8",
62
+ "openai": "4.10.0",
63
+ "picocolors": "1.0.0",
64
+ "uuid": "9.0.1",
65
+ "yargs": "17.7.2"
47
66
  }
48
- },
49
- "devDependencies": {
50
- "@types/jest": "29.5.2",
51
- "@types/node": "20.3.1",
52
- "@types/yargs": "17.0.24",
53
- "husky": "8.0.3",
54
- "jest": "^29.5.0",
55
- "prettier": "2.8.8",
56
- "pretty-quick": "3.1.3",
57
- "rome": "12.1.3",
58
- "ts-jest": "29.1.0",
59
- "ts-node": "10.9.1",
60
- "@topce/typescript": "5.1.6"
61
- },
62
- "dependencies": {
63
- "@aws-sdk/client-translate": "3.359.0",
64
- "@google-cloud/translate": "7.2.1",
65
- "axios": "1.4.0",
66
- "dotenv": "16.3.1",
67
- "form-data": "4.0.0",
68
- "openai": "3.3.0",
69
- "uuid": "9.0.0",
70
- "yargs": "17.7.2"
71
- }
72
67
  }
package/src/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/src/cli.js ADDED
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const path_1 = __importDefault(require("path"));
7
+ const minimist_1 = __importDefault(require("minimist"));
8
+ const picocolors_1 = __importDefault(require("picocolors"));
9
+ const package_json_1 = require("../package.json");
10
+ const lib_1 = require("./lib");
11
+ const arguments_ = process.argv.slice(2);
12
+ const flags = (0, minimist_1.default)(arguments_, {
13
+ alias: {
14
+ mode: ["m"],
15
+ engine: ["e"],
16
+ keepTranslations: ["kt"],
17
+ keepExtraTranslations: ["ket"],
18
+ sourceLocale: ["s"],
19
+ },
20
+ string: ["mode", "engine", "sourceLocale"],
21
+ boolean: ["keepTranslations", "keepExtraTranslations"],
22
+ default: {
23
+ engine: "google",
24
+ sourceLocale: "en",
25
+ keepTranslations: true,
26
+ keepExtraTranslations: true,
27
+ mode: "file",
28
+ },
29
+ });
30
+ console.log(picocolors_1.default.green(`🔨 Auto translate json cli v${package_json_1.version}`));
31
+ const inputPath = flags._[0];
32
+ const { mode, engine, sourceLocale, keepTranslations, keepExtraTranslations } = flags;
33
+ const config = {};
34
+ // set translate engine config
35
+ switch (engine) {
36
+ case "google": {
37
+ const googleApiKey = process.env.ATJ_GOOGLE_API_KEY;
38
+ if (!googleApiKey) {
39
+ console.error(picocolors_1.default.red("❌ Google api key not found in environment variable ATJ_GOOGLE_API_KEY"));
40
+ process.exit(1);
41
+ }
42
+ config.translationKeyInfo = {
43
+ kind: "google",
44
+ apiKey: googleApiKey,
45
+ };
46
+ break;
47
+ }
48
+ case "aws": {
49
+ const awsAccessKeyId = process.env.ATJ_AWS_ACCESS_KEY_ID;
50
+ const awsSecretAccessKey = process.env.ATJ_AWS_SECRET_ACCESS_KEY;
51
+ const awsRegion = process.env.ATJ_AWS_REGION;
52
+ if (!awsAccessKeyId || !awsSecretAccessKey || !awsRegion) {
53
+ console.error(picocolors_1.default.red("❌ AWS credentials not found in environment variables ATJ_AWS_ACCESS_KEY_ID, ATJ_AWS_SECRET_ACCESS_KEY, ATJ_AWS_REGION"));
54
+ process.exit(1);
55
+ }
56
+ config.translationKeyInfo = {
57
+ kind: "aws",
58
+ accessKeyId: awsAccessKeyId,
59
+ secretAccessKey: awsSecretAccessKey,
60
+ region: awsRegion,
61
+ };
62
+ break;
63
+ }
64
+ case "azure": {
65
+ const azureSecretKey = process.env.ATJ_AZURE_SECRET_KEY;
66
+ const azureRegion = process.env.ATJ_AZURE_REGION;
67
+ if (!azureSecretKey || !azureRegion) {
68
+ console.error(picocolors_1.default.red("❌ Azure credentials not found in environment variables ATJ_AZURE_SECRET_KEY, ATJ_AZURE_REGION"));
69
+ process.exit(1);
70
+ }
71
+ config.translationKeyInfo = {
72
+ kind: "azure",
73
+ secretKey: azureSecretKey,
74
+ region: azureRegion,
75
+ };
76
+ break;
77
+ }
78
+ case "openai": {
79
+ const openaiApiKey = process.env.ATJ_OPENAI_API_KEY;
80
+ if (!openaiApiKey) {
81
+ console.error(picocolors_1.default.red("❌ Openai api key not found in environment variable ATJ_OPENAI_API_KEY"));
82
+ process.exit(1);
83
+ }
84
+ config.translationKeyInfo = {
85
+ kind: "openai",
86
+ apiKey: openaiApiKey,
87
+ };
88
+ break;
89
+ }
90
+ case "deepLPro": {
91
+ const deepLProSecretKey = process.env.ATJ_DEEPL_PRO_SECRET_KEY;
92
+ if (!deepLProSecretKey) {
93
+ console.log(picocolors_1.default.red("❌ DeepL pro api key not found in environment variable ATJ_DEEPL_PRO_SECRET_KEY"));
94
+ process.exit(1);
95
+ }
96
+ config.translationKeyInfo = {
97
+ kind: "deepLPro",
98
+ secretKey: deepLProSecretKey,
99
+ };
100
+ break;
101
+ }
102
+ case "deepLFree": {
103
+ const deepLFreeSecretKey = process.env.ATJ_DEEPL_FREE_SECRET_KEY;
104
+ if (!deepLFreeSecretKey) {
105
+ console.error(picocolors_1.default.red("❌ DeepL free api key not found in environment variable ATJ_DEEPL_FREE_SECRET_KEY"));
106
+ process.exit(1);
107
+ }
108
+ config.translationKeyInfo = {
109
+ kind: "deepLFree",
110
+ secretKey: deepLFreeSecretKey,
111
+ };
112
+ break;
113
+ }
114
+ default:
115
+ console.error(picocolors_1.default.red(`❌ Engine ${engine} not supported`));
116
+ process.exit(1);
117
+ }
118
+ config.sourceLocale = sourceLocale;
119
+ config.keepTranslations = keepTranslations ? "keep" : "retranslate";
120
+ config.keepExtraTranslations = keepExtraTranslations ? "keep" : "remove";
121
+ config.mode = mode;
122
+ const sourcePath = path_1.default.join(process.cwd(), inputPath);
123
+ console.log(picocolors_1.default.green(`🌐 Translating ${sourcePath}`));
124
+ (0, lib_1.translate)(sourcePath, config).catch((error) => {
125
+ console.error(picocolors_1.default.red("❌ Translate error:\n"), error);
126
+ process.exit(1);
127
+ });
@@ -33,9 +33,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
33
33
  };
34
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
35
  exports.Files = exports.saveJsonToLocale = exports.loadJsonFromLocale = exports.readFileAsync = void 0;
36
- const path = __importStar(require("path"));
37
36
  const fs = __importStar(require("fs"));
37
+ const path = __importStar(require("path"));
38
38
  const readFileAsync = (filename) => new Promise((resolve, reject) => {
39
+ const exist = fs.existsSync(filename);
40
+ if (!exist)
41
+ fs.writeFileSync(filename, "");
39
42
  fs.readFile(filename, (error, data) => {
40
43
  error ? reject(error) : resolve(data.toString());
41
44
  });
@@ -1,8 +1,10 @@
1
1
  import { ITranslate } from "./translate.interface";
2
2
  export declare class GoogleTranslate implements ITranslate {
3
- private apikey;
4
3
  private googleTranslate;
5
- constructor(apikey: string);
4
+ private supportedLanguages;
5
+ private constructor();
6
+ static initialize(apiKey: string): Promise<GoogleTranslate>;
7
+ private static getSupportedLanguages;
6
8
  isValidLocale(targetLocale: string): boolean;
7
9
  translateText(text: string, _sourceLocale: string, targetLocale: string): Promise<string>;
8
10
  }
@@ -10,129 +10,28 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.GoogleTranslate = void 0;
13
- //const {Translate} = require('@google-cloud/translate').v2;
14
13
  const v2_1 = require("@google-cloud/translate/build/src/v2");
15
14
  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
15
  class GoogleTranslate {
129
- constructor(apikey) {
130
- this.apikey = apikey;
131
- this.googleTranslate = new v2_1.Translate({ key: this.apikey });
132
- // this.googleTranslate.getSupportedLanguages();
16
+ constructor(googleTranslate, supportedLanguages = []) {
17
+ this.googleTranslate = googleTranslate;
18
+ this.supportedLanguages = supportedLanguages;
19
+ }
20
+ static initialize(apiKey) {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ const googleTranslate = new v2_1.Translate({ key: apiKey });
23
+ const supportedLanguages = yield GoogleTranslate.getSupportedLanguages(googleTranslate);
24
+ return new GoogleTranslate(googleTranslate, supportedLanguages);
25
+ });
26
+ }
27
+ static getSupportedLanguages(translate) {
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ const [languages] = yield translate.getLanguages();
30
+ return languages.map((language, _index, _array) => language.code.toLowerCase());
31
+ });
133
32
  }
134
33
  isValidLocale(targetLocale) {
135
- return supportedLanguages.includes(targetLocale);
34
+ return this.supportedLanguages.includes(targetLocale);
136
35
  }
137
36
  translateText(text, _sourceLocale, targetLocale) {
138
37
  return __awaiter(this, void 0, void 0, function* () {
@@ -16,10 +16,10 @@ const files_1 = require("./files");
16
16
  const folderFiles_1 = require("./folderFiles");
17
17
  const aws_1 = require("./aws");
18
18
  const azure_1 = require("./azure");
19
- const google_1 = require("./google");
20
- const util_1 = require("./util");
21
19
  const deepl_1 = require("./deepl");
20
+ const google_1 = require("./google");
22
21
  const openai_1 = require("./openai");
22
+ const util_1 = require("./util");
23
23
  function translate(sourceFile, config) {
24
24
  return __awaiter(this, void 0, void 0, function* () {
25
25
  // set the delimiters for named arguments
@@ -33,7 +33,7 @@ function translate(sourceFile, config) {
33
33
  }
34
34
  let translateEngine;
35
35
  if (config.translationKeyInfo.kind === "google") {
36
- translateEngine = new google_1.GoogleTranslate(config.translationKeyInfo.apiKey);
36
+ translateEngine = yield google_1.GoogleTranslate.initialize(config.translationKeyInfo.apiKey);
37
37
  }
38
38
  else if (config.translationKeyInfo.kind === "aws") {
39
39
  translateEngine = new aws_1.AWSTranslate(config.translationKeyInfo.accessKeyId, config.translationKeyInfo.secretAccessKey, config.translationKeyInfo.region);
@@ -120,7 +120,7 @@ const readFiles = (filePath, mode) => {
120
120
  function recurseNode(source, original, keepTranslations, keepExtras, sourceLocale, locale, translateEngine, ignorePrefix = "", isArray = false) {
121
121
  var _a;
122
122
  return __awaiter(this, void 0, void 0, function* () {
123
- // rome-ignore lint/suspicious/noExplicitAny: <explanation>
123
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
124
124
  const destination = isArray ? [] : {};
125
125
  // defaults
126
126
  if (keepTranslations === null) {
@@ -1,6 +1,5 @@
1
1
  import { ITranslate } from "./translate.interface";
2
2
  export declare class OpenAITranslate implements ITranslate {
3
- private apiKey;
4
3
  private openai;
5
4
  constructor(apiKey: string);
6
5
  isValidLocale(targetLocale: string): boolean;
@@ -10,8 +10,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.OpenAITranslate = void 0;
13
- const util_1 = require("./util");
14
13
  const openai_1 = require("openai");
14
+ const util_1 = require("./util");
15
15
  const supportedLanguages = {
16
16
  af: "Afrikaans",
17
17
  sq: "Albanian",
@@ -124,11 +124,10 @@ const supportedLanguages = {
124
124
  };
125
125
  class OpenAITranslate {
126
126
  constructor(apiKey) {
127
- this.apiKey = apiKey;
128
- const configuration = new openai_1.Configuration({
127
+ const configuration = {
129
128
  apiKey: apiKey,
130
- });
131
- this.openai = new openai_1.OpenAIApi(configuration);
129
+ };
130
+ this.openai = new openai_1.OpenAI(configuration);
132
131
  }
133
132
  isValidLocale(targetLocale) {
134
133
  return targetLocale in supportedLanguages;
@@ -141,18 +140,19 @@ class OpenAITranslate {
141
140
  let result = "";
142
141
  let args;
143
142
  ({ args, text } = util_1.Util.replaceContextVariables(text));
144
- const prompt = `Translate this into ${supportedLanguages[targetLocale]}:\n${text}\n.`;
145
- const response = yield this.openai.createCompletion({
143
+ const prompt = `You will be provided with a sentence in English, and your task is to translate it into ${supportedLanguages[targetLocale]}:\n${text}\n.`;
144
+ const response = yield this.openai.chat.completions.create({
146
145
  model: "text-davinci-003",
147
- prompt: prompt,
148
- temperature: 0.3,
149
- max_tokens: 1000,
146
+ messages: [{ role: "system", content: prompt }],
147
+ temperature: 0.2,
148
+ max_tokens: 250,
150
149
  top_p: 1.0,
150
+ n: 1,
151
151
  frequency_penalty: 0.0,
152
152
  presence_penalty: 0.0,
153
153
  });
154
- if (response.data.choices[0].text !== undefined) {
155
- result = util_1.Util.replaceArgumentsWithNumbers(args, response.data.choices[0].text);
154
+ if (response.choices[0].message.content !== null) {
155
+ result = util_1.Util.replaceArgumentsWithNumbers(args, response.choices[0].message.content);
156
156
  result = result.replace(/^\n+|\n+$/g, "");
157
157
  }
158
158
  else {
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Util = void 0;
4
+ // biome-ignore lint/complexity/noStaticOnlyClass: <explanation>
4
5
  class Util {
5
6
  static set startDelimiter(value) {
6
7
  Util._startDelimiter = value;
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -24,8 +24,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.FolderFiles = void 0;
27
- const path = __importStar(require("path"));
28
27
  const fs = __importStar(require("fs"));
28
+ const path = __importStar(require("path"));
29
29
  const files_1 = require("./files");
30
30
  class FolderFiles {
31
31
  constructor(filePath) {
File without changes
File without changes
File without changes
File without changes