auto-translate-json-library 1.3.5 → 1.3.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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auto-translate-json-library",
3
- "version": "1.3.5",
3
+ "version": "1.3.6",
4
4
  "description": "Automatically translates JSON language files to other languages using Google Translate,AWS,Azure,DeepL or OpenAI",
5
5
  "main": "./build/src/index.js",
6
6
  "bin": {
@@ -49,21 +49,21 @@
49
49
  ],
50
50
  "license": "MIT",
51
51
  "devDependencies": {
52
- "@biomejs/biome": "1.2.2",
52
+ "@biomejs/biome": "1.3.1",
53
53
  "@topce/typescript": "5.3.1",
54
- "@types/minimist": "1.2.3",
55
- "@types/node": "20.8.6",
56
- "@types/yargs": "17.0.28",
54
+ "@types/minimist": "1.2.4",
55
+ "@types/node": "20.8.9",
56
+ "@types/yargs": "17.0.29",
57
57
  "ts-node": "10.9.1"
58
58
  },
59
59
  "dependencies": {
60
- "@aws-sdk/client-translate": "3.428.0",
60
+ "@aws-sdk/client-translate": "3.438.0",
61
61
  "@google-cloud/translate": "8.0.2",
62
- "axios": "1.5.1",
62
+ "axios": "1.6.0",
63
63
  "dotenv": "16.3.1",
64
64
  "form-data": "4.0.0",
65
65
  "minimist": "1.2.8",
66
- "openai": "4.12.1",
66
+ "openai": "4.14.1",
67
67
  "picocolors": "1.0.0",
68
68
  "uuid": "9.0.1",
69
69
  "yargs": "17.7.2"
@@ -71,12 +71,12 @@ class Files {
71
71
  getTargetLocales() {
72
72
  const locales = new Array();
73
73
  const files = fs.readdirSync(this.folderPath);
74
- files.forEach((file, _index, _array) => {
74
+ for (const file of files) {
75
75
  const locale = this.getLocaleFromFilename(file);
76
76
  if (locale !== this.sourceLocale) {
77
77
  locales.push(locale);
78
78
  }
79
- });
79
+ }
80
80
  return locales;
81
81
  }
82
82
  loadJsonFromLocale(locale) {
package/build/src/lib.js CHANGED
@@ -78,7 +78,7 @@ function translate(sourceFile, config) {
78
78
  return;
79
79
  }
80
80
  // Iterate target Locales
81
- files.targetLocales.forEach((targetLocale, _index, _array) => __awaiter(this, void 0, void 0, function* () {
81
+ for (const targetLocale of files.targetLocales) {
82
82
  try {
83
83
  const isValid = yield translateEngine.isValidLocale(targetLocale);
84
84
  if (!isValid) {
@@ -98,7 +98,7 @@ function translate(sourceFile, config) {
98
98
  }
99
99
  return;
100
100
  }
101
- }));
101
+ }
102
102
  });
103
103
  }
104
104
  exports.translate = translate;
@@ -132,7 +132,7 @@ function recurseNode(source, original, keepTranslations, keepExtras, sourceLocal
132
132
  for (const term in source) {
133
133
  const node = source[term];
134
134
  if (node instanceof Object && node !== null) {
135
- destination[term] = yield recurseNode(node, (_a = original[term]) !== null && _a !== void 0 ? _a : {}, keepTranslations, keepExtras, sourceLocale, locale, translateEngine, ignorePrefix, node instanceof Array);
135
+ destination[term] = yield recurseNode(node, (_a = original[term]) !== null && _a !== void 0 ? _a : {}, keepTranslations, keepExtras, sourceLocale, locale, translateEngine, ignorePrefix, Array.isArray(node));
136
136
  }
137
137
  else {
138
138
  // if we already have a translation, keep it
@@ -140,11 +140,15 @@ class OpenAITranslate {
140
140
  let result = "";
141
141
  let args;
142
142
  ({ args, text } = util_1.Util.replaceContextVariables(text));
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.`;
143
+ const systemPrompt = `You will be provided with a sentence in English, and your task is to translate it into ${supportedLanguages[targetLocale]}`;
144
+ const userPrompt = text;
144
145
  const response = yield this.openai.chat.completions.create({
145
146
  model: "text-davinci-003",
146
- messages: [{ role: "system", content: prompt }],
147
- temperature: 0.2,
147
+ messages: [
148
+ { role: "system", content: systemPrompt },
149
+ { role: "user", content: userPrompt },
150
+ ],
151
+ temperature: 0,
148
152
  max_tokens: 250,
149
153
  top_p: 1.0,
150
154
  n: 1,
@@ -156,7 +160,10 @@ class OpenAITranslate {
156
160
  result = result.replace(/^\n+|\n+$/g, "");
157
161
  }
158
162
  else {
159
- console.error(`can not translate text with prompt :${prompt}`);
163
+ console.error(`can not translate text with
164
+ system prompt : ${systemPrompt}
165
+ and
166
+ user prompt : ${userPrompt} `);
160
167
  result = util_1.Util.replaceArgumentsWithNumbers(args, result);
161
168
  }
162
169
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "auto-translate-json-library",
3
- "version": "1.3.5",
3
+ "version": "1.3.6",
4
4
  "description": "Automatically translates JSON language files to other languages using Google Translate,AWS,Azure,DeepL or OpenAI",
5
5
  "main": "./build/src/index.js",
6
6
  "bin": {
@@ -49,21 +49,21 @@
49
49
  ],
50
50
  "license": "MIT",
51
51
  "devDependencies": {
52
- "@biomejs/biome": "1.2.2",
52
+ "@biomejs/biome": "1.3.1",
53
53
  "@topce/typescript": "5.3.1",
54
- "@types/minimist": "1.2.3",
55
- "@types/node": "20.8.6",
56
- "@types/yargs": "17.0.28",
54
+ "@types/minimist": "1.2.4",
55
+ "@types/node": "20.8.9",
56
+ "@types/yargs": "17.0.29",
57
57
  "ts-node": "10.9.1"
58
58
  },
59
59
  "dependencies": {
60
- "@aws-sdk/client-translate": "3.428.0",
60
+ "@aws-sdk/client-translate": "3.438.0",
61
61
  "@google-cloud/translate": "8.0.2",
62
- "axios": "1.5.1",
62
+ "axios": "1.6.0",
63
63
  "dotenv": "16.3.1",
64
64
  "form-data": "4.0.0",
65
65
  "minimist": "1.2.8",
66
- "openai": "4.12.1",
66
+ "openai": "4.14.1",
67
67
  "picocolors": "1.0.0",
68
68
  "uuid": "9.0.1",
69
69
  "yargs": "17.7.2"
@@ -1,141 +0,0 @@
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
-
10
- # Use as library
11
-
12
- ```shell
13
- npm i auto-translate-json-library
14
- ```
15
-
16
- ```Typescript
17
- import { translate, Configuration } from 'auto-translate-json-library';
18
-
19
- let config: Configuration = {} as Configuration;
20
-
21
- // set google api key
22
- config.translationKeyInfo = {
23
- kind: 'google',
24
- apiKey: googleApiKey
25
- };
26
- const pivotTranslation="./en.json";
27
- await translate(pivotTranslation, config) ;
28
- // translation for example fr.json in same folder as pivotTranslation should contain the translations
29
- ```
30
-
31
- # Use as command line tool
32
-
33
- first install it
34
-
35
- ```shell
36
- npm i auto-translate-json-library
37
- ```
38
-
39
- do not forget to set translation engine parameters in environment variables or .env file
40
-
41
- ```shell
42
-
43
- ```shell
44
- node node_modules\auto-translate-json-library\index.js --pivotTranslation=./translations/en.json
45
- ```
46
-
47
- if you clone repo you can use the following commands
48
-
49
- ```shell
50
- npm i
51
- npm run build
52
- node .\build\src\index.js --pivotTranslation=./tests/translations/en.json
53
- ```
54
-
55
- ```
56
- # CLI PARAMETERS
57
-
58
- mode,
59
- file or folder,
60
- default is file
61
-
62
- startDelimiter,
63
- start delimiter,
64
- default is {
65
-
66
- endDelimiter,
67
- end delimiter,
68
- default is }
69
-
70
- keepExtraTranslations,
71
- keep extra translations (not present in pivot translation),
72
- default is keep,
73
- it could be keep or remove
74
-
75
- keepTranslations,
76
- keep existing translations,
77
- default is keep
78
- it could be keep or retranslate
79
-
80
- sourceLocale
81
- source locale
82
- it could be en or fr ...
83
- if specified it must match with pivot translation
84
-
85
- pivotTranslation
86
- pivot translation file that will be used as source for translations
87
-
88
- ignorePrefix
89
- ignore translation that key starts with ignore prefix
90
-
91
- # ENVIRONMENT VARIABLES
92
-
93
- google parameters
94
- ATJ_GOOGLE_API_KEY=
95
-
96
- aws parameters
97
- ATJ_AWS_ACCESS_KEY_ID=
98
- ATJ_AWS_SECRET_ACCESS_KEY=
99
- ATJ_AWS_REGION=
100
-
101
- azure parameters
102
- ATJ_AZURE_SECRET_KEY=
103
- ATJ_AZURE_REGION=
104
-
105
- deepl pro parameters
106
- ATJ_DEEPL_PRO_SECRET_KEY=
107
- deepl free parameters
108
- ATJ_DEEPL_FREE_SECRET_KEY=
109
-
110
- OpenAI parameters
111
- ATJ_OPEN_AI_SECRET_KEY=
112
-
113
- start delimiter
114
- ATJ_START_DELIMITER={{
115
- end delimiter
116
- ATJ_END_DELIMITER=}}
117
-
118
- file or folder
119
- ATJ_MODE=file
120
-
121
- source locale
122
- ATJ_SOURCE_LOCALE=en
123
-
124
- keep or retranslate existing translations
125
- ATJ_KEEP_TRANSLATIONS=keep
126
-
127
- keep or remove extra translations
128
- ATJ_KEEP_EXTRA_TRANSLATIONS=keep
129
-
130
- ignore prefix translation keys to ignore
131
- ATJ_IGNORE_PREFIX=
132
- ```
133
-
134
- You can also use .env file to store environment variables
135
-
136
- There is also improved version of CLI for example:
137
-
138
- ```shell
139
- atj tests/translations/en.json -e google -m file
140
- ```
141
- since version 1.2.5
@@ -1 +0,0 @@
1
- import "dotenv/config";
package/build/src/cli.js DELETED
@@ -1,128 +0,0 @@
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
- require("dotenv/config");
12
- const arguments_ = process.argv.slice(2);
13
- const flags = (0, minimist_1.default)(arguments_, {
14
- alias: {
15
- mode: ["m"],
16
- engine: ["e"],
17
- keepTranslations: ["kt"],
18
- keepExtraTranslations: ["ket"],
19
- sourceLocale: ["s"],
20
- },
21
- string: ["mode", "engine", "sourceLocale"],
22
- boolean: ["keepTranslations", "keepExtraTranslations"],
23
- default: {
24
- engine: "google",
25
- sourceLocale: "en",
26
- keepTranslations: true,
27
- keepExtraTranslations: true,
28
- mode: "file",
29
- },
30
- });
31
- console.log(picocolors_1.default.green(`🔨 Auto translate json cli v${package_json_1.version}`));
32
- const inputPath = flags._[0];
33
- const { mode, engine, sourceLocale, keepTranslations, keepExtraTranslations } = flags;
34
- const config = {};
35
- // set translate engine config
36
- switch (engine) {
37
- case "google": {
38
- const googleApiKey = process.env.ATJ_GOOGLE_API_KEY;
39
- if (!googleApiKey) {
40
- console.error(picocolors_1.default.red("❌ Google api key not found in environment variable ATJ_GOOGLE_API_KEY"));
41
- process.exit(1);
42
- }
43
- config.translationKeyInfo = {
44
- kind: "google",
45
- apiKey: googleApiKey,
46
- };
47
- break;
48
- }
49
- case "aws": {
50
- const awsAccessKeyId = process.env.ATJ_AWS_ACCESS_KEY_ID;
51
- const awsSecretAccessKey = process.env.ATJ_AWS_SECRET_ACCESS_KEY;
52
- const awsRegion = process.env.ATJ_AWS_REGION;
53
- if (!awsAccessKeyId || !awsSecretAccessKey || !awsRegion) {
54
- 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"));
55
- process.exit(1);
56
- }
57
- config.translationKeyInfo = {
58
- kind: "aws",
59
- accessKeyId: awsAccessKeyId,
60
- secretAccessKey: awsSecretAccessKey,
61
- region: awsRegion,
62
- };
63
- break;
64
- }
65
- case "azure": {
66
- const azureSecretKey = process.env.ATJ_AZURE_SECRET_KEY;
67
- const azureRegion = process.env.ATJ_AZURE_REGION;
68
- if (!azureSecretKey || !azureRegion) {
69
- console.error(picocolors_1.default.red("❌ Azure credentials not found in environment variables ATJ_AZURE_SECRET_KEY, ATJ_AZURE_REGION"));
70
- process.exit(1);
71
- }
72
- config.translationKeyInfo = {
73
- kind: "azure",
74
- secretKey: azureSecretKey,
75
- region: azureRegion,
76
- };
77
- break;
78
- }
79
- case "openai": {
80
- const openaiApiKey = process.env.ATJ_OPENAI_API_KEY;
81
- if (!openaiApiKey) {
82
- console.error(picocolors_1.default.red("❌ Openai api key not found in environment variable ATJ_OPENAI_API_KEY"));
83
- process.exit(1);
84
- }
85
- config.translationKeyInfo = {
86
- kind: "openai",
87
- apiKey: openaiApiKey,
88
- };
89
- break;
90
- }
91
- case "deepLPro": {
92
- const deepLProSecretKey = process.env.ATJ_DEEPL_PRO_SECRET_KEY;
93
- if (!deepLProSecretKey) {
94
- console.log(picocolors_1.default.red("❌ DeepL pro api key not found in environment variable ATJ_DEEPL_PRO_SECRET_KEY"));
95
- process.exit(1);
96
- }
97
- config.translationKeyInfo = {
98
- kind: "deepLPro",
99
- secretKey: deepLProSecretKey,
100
- };
101
- break;
102
- }
103
- case "deepLFree": {
104
- const deepLFreeSecretKey = process.env.ATJ_DEEPL_FREE_SECRET_KEY;
105
- if (!deepLFreeSecretKey) {
106
- console.error(picocolors_1.default.red("❌ DeepL free api key not found in environment variable ATJ_DEEPL_FREE_SECRET_KEY"));
107
- process.exit(1);
108
- }
109
- config.translationKeyInfo = {
110
- kind: "deepLFree",
111
- secretKey: deepLFreeSecretKey,
112
- };
113
- break;
114
- }
115
- default:
116
- console.error(picocolors_1.default.red(`❌ Engine ${engine} not supported`));
117
- process.exit(1);
118
- }
119
- config.sourceLocale = sourceLocale;
120
- config.keepTranslations = keepTranslations ? "keep" : "retranslate";
121
- config.keepExtraTranslations = keepExtraTranslations ? "keep" : "remove";
122
- config.mode = mode;
123
- const sourcePath = path_1.default.join(process.cwd(), inputPath);
124
- console.log(picocolors_1.default.green(`🌐 Translating ${sourcePath}`));
125
- (0, lib_1.translate)(sourcePath, config).catch((error) => {
126
- console.error(picocolors_1.default.red("❌ Translate error:\n"), error);
127
- process.exit(1);
128
- });
@@ -1,67 +0,0 @@
1
- {
2
- "name": "auto-translate-json-library",
3
- "version": "1.2.6",
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
- "@biomejs/biome": "1.2.2",
49
- "@topce/typescript": "5.3.1",
50
- "@types/minimist": "1.2.2",
51
- "@types/node": "20.6.5",
52
- "@types/yargs": "17.0.25",
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"
66
- }
67
- }