auto-translate-json-library 1.3.5 → 1.3.7

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/package.json CHANGED
@@ -1,71 +1,71 @@
1
1
  {
2
- "name": "auto-translate-json-library",
3
- "version": "1.3.5",
4
- "description": "Automatically translates JSON language files to other languages using Google Translate,AWS,Azure,DeepL or OpenAI",
5
- "main": "./build/src/index.js",
6
- "bin": {
7
- "auto-translate-json-library": "./build/src/bin/cli.js",
8
- "atj": "./build/src/bin/cli.js"
9
- },
10
- "types": "./build/src/index.d.ts",
11
- "scripts": {
12
- "prepare": "tsc",
13
- "build": "tsc",
14
- "build:run": "ts-node ./build/src/index.js --pivotTranslation=./tests/translations/en.json",
15
- "debug": "ts-node src/index.ts --pivotTranslation=./tests/translations/en.json",
16
- "lint": "biome check ./src",
17
- "lint-fix": "biome check ./src --apply",
18
- "lint-fix-all": "biome check --apply-unsafe ./src",
19
- "format": "biome format ./src",
20
- "format-fix": "biome format ./src --write",
21
- "start": "ts-node src/index.ts"
22
- },
23
- "keywords": [
24
- "translate",
25
- "language",
26
- "json",
27
- "flutter",
28
- "angular",
29
- "ngx-translate",
30
- "transloco",
31
- "auto-translate-json",
32
- "google",
33
- "Azure",
34
- "DeepL",
35
- "OpenAI"
36
- ],
37
- "author": "codegrue",
38
- "repository": {
39
- "type": "git",
40
- "url": "https://github.com/topce/auto-translate-json-library"
41
- },
42
- "contributors": [
43
- "codegrue",
44
- "gajo357",
45
- "topce",
46
- "BenGMiles",
47
- "stonega",
48
- "alexei-petru"
49
- ],
50
- "license": "MIT",
51
- "devDependencies": {
52
- "@biomejs/biome": "1.2.2",
53
- "@topce/typescript": "5.3.1",
54
- "@types/minimist": "1.2.3",
55
- "@types/node": "20.8.6",
56
- "@types/yargs": "17.0.28",
57
- "ts-node": "10.9.1"
58
- },
59
- "dependencies": {
60
- "@aws-sdk/client-translate": "3.428.0",
61
- "@google-cloud/translate": "8.0.2",
62
- "axios": "1.5.1",
63
- "dotenv": "16.3.1",
64
- "form-data": "4.0.0",
65
- "minimist": "1.2.8",
66
- "openai": "4.12.1",
67
- "picocolors": "1.0.0",
68
- "uuid": "9.0.1",
69
- "yargs": "17.7.2"
70
- }
2
+ "name": "auto-translate-json-library",
3
+ "version": "1.3.7",
4
+ "description": "Automatically translates JSON language files to other languages using Google Translate,AWS,Azure,DeepL,OpenAI or local OpenAI compatible server",
5
+ "main": "./build/src/index.js",
6
+ "bin": {
7
+ "auto-translate-json-library": "./build/src/bin/cli.js",
8
+ "atj": "./build/src/bin/cli.js"
9
+ },
10
+ "types": "./build/src/index.d.ts",
11
+ "scripts": {
12
+ "prepare": "tsc",
13
+ "build": "tsc",
14
+ "build:run": "ts-node ./build/src/index.js --pivotTranslation=./tests/translations/en.json",
15
+ "debug": "ts-node src/index.ts --pivotTranslation=./tests/translations/en.json",
16
+ "lint": "biome check ./src",
17
+ "lint-fix": "biome check ./src --apply",
18
+ "lint-fix-all": "biome check --apply-unsafe ./src",
19
+ "format": "biome format ./src",
20
+ "format-fix": "biome format ./src --write",
21
+ "start": "ts-node src/index.ts"
22
+ },
23
+ "keywords": [
24
+ "translate",
25
+ "language",
26
+ "json",
27
+ "flutter",
28
+ "angular",
29
+ "ngx-translate",
30
+ "transloco",
31
+ "auto-translate-json",
32
+ "google",
33
+ "Azure",
34
+ "DeepL",
35
+ "OpenAI"
36
+ ],
37
+ "author": "codegrue",
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "https://github.com/topce/auto-translate-json-library"
41
+ },
42
+ "contributors": [
43
+ "codegrue",
44
+ "gajo357",
45
+ "topce",
46
+ "BenGMiles",
47
+ "stonega",
48
+ "alexei-petru"
49
+ ],
50
+ "license": "MIT",
51
+ "devDependencies": {
52
+ "@biomejs/biome": "1.5.3",
53
+ "@topce/typescript": "5.3.1",
54
+ "@types/minimist": "1.2.5",
55
+ "@types/node": "20.11.24",
56
+ "@types/yargs": "17.0.32",
57
+ "ts-node": "10.9.2"
58
+ },
59
+ "dependencies": {
60
+ "@aws-sdk/client-translate": "3.525.0",
61
+ "@google-cloud/translate": "8.1.0",
62
+ "axios": "1.6.7",
63
+ "dotenv": "16.4.5",
64
+ "form-data": "4.0.0",
65
+ "minimist": "1.2.8",
66
+ "openai": "4.28.4",
67
+ "picocolors": "1.0.0",
68
+ "uuid": "9.0.1",
69
+ "yargs": "17.7.2"
70
+ }
71
71
  }
@@ -3,6 +3,7 @@
3
3
  var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
5
  };
6
+ var _a, _b, _c, _d, _e, _f, _g, _h;
6
7
  Object.defineProperty(exports, "__esModule", { value: true });
7
8
  const path_1 = __importDefault(require("path"));
8
9
  const minimist_1 = __importDefault(require("minimist"));
@@ -115,6 +116,14 @@ switch (engine) {
115
116
  config.translationKeyInfo = {
116
117
  kind: "openai",
117
118
  apiKey: openaiApiKey,
119
+ baseUrl: (_a = process.env.ATJ_OPEN_AI_BASE_URL) !== null && _a !== void 0 ? _a : "https://api.openai.com/v1",
120
+ model: (_b = process.env.ATJ_OPEN_AI_MODEL) !== null && _b !== void 0 ? _b : "gpt-3.5-turbo",
121
+ maxTokens: Number((_c = process.env.ATJ_OPEN_AI_MAX_TOKENS) !== null && _c !== void 0 ? _c : "256"),
122
+ temperature: Number((_d = process.env.ATJ_OPEN_AI_TEMPERATURE) !== null && _d !== void 0 ? _d : "0"),
123
+ topP: Number((_e = process.env.ATJ_OPEN_AI_TOP_P) !== null && _e !== void 0 ? _e : "1.0"),
124
+ n: Number((_f = process.env.ATJ_OPEN_AI_N) !== null && _f !== void 0 ? _f : "1"),
125
+ frequencyPenalty: Number((_g = process.env.ATJ_OPEN_AI_FREQUENCY_PENALTY) !== null && _g !== void 0 ? _g : "0"),
126
+ presencePenalty: Number((_h = process.env.ATJ_OPEN_AI_PRESENCE_PENALTY) !== null && _h !== void 0 ? _h : "0"),
118
127
  };
119
128
  break;
120
129
  }
@@ -34,5 +34,13 @@ type DeepLFreeTranslationKey = {
34
34
  type OpenAITranslationKey = {
35
35
  kind: "openai";
36
36
  apiKey: string;
37
+ baseUrl: string;
38
+ model: string;
39
+ maxTokens: number;
40
+ temperature: number;
41
+ topP: number;
42
+ n: number;
43
+ frequencyPenalty: number;
44
+ presencePenalty: number;
37
45
  };
38
46
  export {};
@@ -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) {
@@ -58,6 +58,7 @@ function setDefaultConfiguration() {
58
58
  config.ignorePrefix = "";
59
59
  }
60
60
  function setConfigurationFromEnvironment() {
61
+ var _a, _b, _c, _d, _e, _f, _g, _h;
61
62
  require("dotenv").config();
62
63
  if (process.env.ATJ_GOOGLE_API_KEY) {
63
64
  config.translationKeyInfo = {
@@ -98,6 +99,14 @@ function setConfigurationFromEnvironment() {
98
99
  config.translationKeyInfo = {
99
100
  kind: "openai",
100
101
  apiKey: process.env.ATJ_OPEN_AI_SECRET_KEY,
102
+ baseUrl: (_a = process.env.ATJ_OPEN_AI_BASE_URL) !== null && _a !== void 0 ? _a : "https://api.openai.com/v1",
103
+ model: (_b = process.env.ATJ_OPEN_AI_MODEL) !== null && _b !== void 0 ? _b : "gpt-3.5-turbo",
104
+ maxTokens: Number((_c = process.env.ATJ_OPEN_AI_MAX_TOKENS) !== null && _c !== void 0 ? _c : "256"),
105
+ temperature: Number((_d = process.env.ATJ_OPEN_AI_TEMPERATURE) !== null && _d !== void 0 ? _d : "0"),
106
+ topP: Number((_e = process.env.ATJ_OPEN_AI_TOP_P) !== null && _e !== void 0 ? _e : "1.0"),
107
+ n: Number((_f = process.env.ATJ_OPEN_AI_N) !== null && _f !== void 0 ? _f : "1"),
108
+ frequencyPenalty: Number((_g = process.env.ATJ_OPEN_AI_FREQUENCY_PENALTY) !== null && _g !== void 0 ? _g : "0"),
109
+ presencePenalty: Number((_h = process.env.ATJ_OPEN_AI_PRESENCE_PENALTY) !== null && _h !== void 0 ? _h : "0"),
101
110
  };
102
111
  }
103
112
  if (process.env.ATJ_START_DELIMITER) {
@@ -48,7 +48,7 @@ function translate(sourceFile, config) {
48
48
  translateEngine = new deepl_1.DeepLTranslate(config.translationKeyInfo.secretKey, "pro");
49
49
  }
50
50
  else if (config.translationKeyInfo.kind === "openai") {
51
- translateEngine = new openai_1.OpenAITranslate(config.translationKeyInfo.apiKey);
51
+ translateEngine = new openai_1.OpenAITranslate(config.translationKeyInfo.apiKey, config.translationKeyInfo.baseUrl, config.translationKeyInfo.model, config.translationKeyInfo.maxTokens, config.translationKeyInfo.temperature, config.translationKeyInfo.topP, config.translationKeyInfo.n, config.translationKeyInfo.presencePenalty, config.translationKeyInfo.frequencyPenalty);
52
52
  }
53
53
  else {
54
54
  console.warn("You must provide a Google, AWS, Azure, deepL, openai parameters first in the extension settings.");
@@ -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
@@ -0,0 +1,14 @@
1
+ import { ITranslate } from "./translate.interface";
2
+ export declare class OpenAITranslate implements ITranslate {
3
+ private model;
4
+ private maxTokens;
5
+ private temperature;
6
+ private topP;
7
+ private n;
8
+ private frequencyPenalty;
9
+ private presencePenalty;
10
+ private openai;
11
+ constructor(apiKey: string, baseUrl: string, model: string, maxTokens: number, temperature: number, topP: number, n: number, frequencyPenalty: number, presencePenalty: number);
12
+ isValidLocale(targetLocale: string): boolean;
13
+ translateText(text: string, sourceLocale: string, targetLocale: string): Promise<string>;
14
+ }
@@ -123,9 +123,17 @@ const supportedLanguages = {
123
123
  zu: "Zulu",
124
124
  };
125
125
  class OpenAITranslate {
126
- constructor(apiKey) {
126
+ constructor(apiKey, baseUrl, model, maxTokens, temperature, topP, n, frequencyPenalty, presencePenalty) {
127
+ this.model = model;
128
+ this.maxTokens = maxTokens;
129
+ this.temperature = temperature;
130
+ this.topP = topP;
131
+ this.n = n;
132
+ this.frequencyPenalty = frequencyPenalty;
133
+ this.presencePenalty = presencePenalty;
127
134
  const configuration = {
128
135
  apiKey: apiKey,
136
+ baseURL: baseUrl,
129
137
  };
130
138
  this.openai = new openai_1.OpenAI(configuration);
131
139
  }
@@ -140,23 +148,39 @@ class OpenAITranslate {
140
148
  let result = "";
141
149
  let args;
142
150
  ({ 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.`;
144
- const response = yield this.openai.chat.completions.create({
145
- model: "text-davinci-003",
146
- messages: [{ role: "system", content: prompt }],
147
- temperature: 0.2,
148
- max_tokens: 250,
149
- top_p: 1.0,
150
- n: 1,
151
- frequency_penalty: 0.0,
152
- presence_penalty: 0.0,
151
+ const systemPrompt = `You will be provided with a sentence in English, and your task is to translate it into ${supportedLanguages[targetLocale]}`;
152
+ const userPrompt = text;
153
+ let response = yield this.openai.chat.completions.create({
154
+ model: this.model,
155
+ messages: [
156
+ { role: "system", content: systemPrompt },
157
+ { role: "user", content: userPrompt },
158
+ ],
159
+ temperature: this.temperature,
160
+ max_tokens: this.maxTokens,
161
+ top_p: this.topP,
162
+ n: this.n,
163
+ frequency_penalty: this.frequencyPenalty,
164
+ presence_penalty: this.presencePenalty,
153
165
  });
166
+ try {
167
+ console.log(response.choices[0].message.content);
168
+ }
169
+ catch (_a) {
170
+ // disable type script compiler for next line
171
+ // @ts-ignore
172
+ response = JSON.parse(response);
173
+ console.log(response.choices[0].message.content);
174
+ }
154
175
  if (response.choices[0].message.content !== null) {
155
176
  result = util_1.Util.replaceArgumentsWithNumbers(args, response.choices[0].message.content);
156
177
  result = result.replace(/^\n+|\n+$/g, "");
157
178
  }
158
179
  else {
159
- console.error(`can not translate text with prompt :${prompt}`);
180
+ console.error(`can not translate text with
181
+ system prompt : ${systemPrompt}
182
+ and
183
+ user prompt : ${userPrompt} `);
160
184
  result = util_1.Util.replaceArgumentsWithNumbers(args, result);
161
185
  }
162
186
  return result;
package/README.MD DELETED
@@ -1,150 +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 opens posibility to use Auto Translation 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
- node node_modules\auto-translate-json-library\index.js --pivotTranslation=./translations/en.json
43
- ```
44
-
45
- if you clone repo you can use the following commands
46
-
47
- ```shell
48
- npm i
49
- npm run build
50
- node .\build\src\index.js --pivotTranslation=./tests/translations/en.json
51
- ```
52
-
53
- ## CLI PARAMETERS
54
-
55
- ```config
56
- mode,
57
- file or folder,
58
- default is file
59
-
60
- startDelimiter,
61
- start delimiter,
62
- default is {
63
-
64
- endDelimiter,
65
- end delimiter,
66
- default is }
67
-
68
- keepExtraTranslations,
69
- keep extra translations (not present in pivot translation),
70
- default is keep,
71
- it could be keep or remove
72
-
73
- keepTranslations,
74
- keep existing translations,
75
- default is keep
76
- it could be keep or retranslate
77
-
78
- sourceLocale
79
- source locale
80
- it could be en or fr ...
81
- if specified it must match with pivot translation
82
-
83
- pivotTranslation
84
- pivot translation file that will be used as source for translations
85
-
86
- ignorePrefix
87
- ignore translation that key starts with ignore prefix
88
-
89
- # ENVIRONMENT VARIABLES
90
-
91
- google parameters
92
- ATJ_GOOGLE_API_KEY=
93
-
94
- aws parameters
95
- ATJ_AWS_ACCESS_KEY_ID=
96
- ATJ_AWS_SECRET_ACCESS_KEY=
97
- ATJ_AWS_REGION=
98
-
99
- azure parameters
100
- ATJ_AZURE_SECRET_KEY=
101
- ATJ_AZURE_REGION=
102
-
103
- deepl pro parameters
104
- ATJ_DEEPL_PRO_SECRET_KEY=
105
- deepl free parameters
106
- ATJ_DEEPL_FREE_SECRET_KEY=
107
-
108
- OpenAI parameters
109
- ATJ_OPEN_AI_SECRET_KEY=
110
-
111
- start delimiter
112
- ATJ_START_DELIMITER={{
113
- end delimiter
114
- ATJ_END_DELIMITER=}}
115
-
116
- file or folder
117
- ATJ_MODE=file
118
-
119
- source locale
120
- ATJ_SOURCE_LOCALE=en
121
-
122
- keep or retranslate existing translations
123
- ATJ_KEEP_TRANSLATIONS=keep
124
-
125
- keep or remove extra translations
126
- ATJ_KEEP_EXTRA_TRANSLATIONS=keep
127
-
128
- ignore prefix translation keys to ignore
129
- ATJ_IGNORE_PREFIX=
130
- ```
131
-
132
- You can also use .env file to store environment variables
133
-
134
- There is also improved version of CLI (since version 1.3.2) for example:
135
-
136
- ```shell
137
- npm install -g auto-translate-library
138
- atj tests/translations/en.json -e google -m file
139
- ```
140
-
141
- You can also use it with npx
142
-
143
- ```shell
144
- npx auto-translate-library tests/translations/en.json -e google -m file
145
- ```
146
-
147
- ### Debug
148
- 1. Create .env file in main folder with the desired key/keys from # ENVIRONMENT VARIABLES
149
- 2. Add also in .env source locale "ATJ_SOURCE_LOCALE=en" to test from en
150
- 3. npm run debug
@@ -1,71 +0,0 @@
1
- {
2
- "name": "auto-translate-json-library",
3
- "version": "1.3.5",
4
- "description": "Automatically translates JSON language files to other languages using Google Translate,AWS,Azure,DeepL or OpenAI",
5
- "main": "./build/src/index.js",
6
- "bin": {
7
- "auto-translate-json-library": "./build/src/bin/cli.js",
8
- "atj": "./build/src/bin/cli.js"
9
- },
10
- "types": "./build/src/index.d.ts",
11
- "scripts": {
12
- "prepare": "tsc",
13
- "build": "tsc",
14
- "build:run": "ts-node ./build/src/index.js --pivotTranslation=./tests/translations/en.json",
15
- "debug": "ts-node src/index.ts --pivotTranslation=./tests/translations/en.json",
16
- "lint": "biome check ./src",
17
- "lint-fix": "biome check ./src --apply",
18
- "lint-fix-all": "biome check --apply-unsafe ./src",
19
- "format": "biome format ./src",
20
- "format-fix": "biome format ./src --write",
21
- "start": "ts-node src/index.ts"
22
- },
23
- "keywords": [
24
- "translate",
25
- "language",
26
- "json",
27
- "flutter",
28
- "angular",
29
- "ngx-translate",
30
- "transloco",
31
- "auto-translate-json",
32
- "google",
33
- "Azure",
34
- "DeepL",
35
- "OpenAI"
36
- ],
37
- "author": "codegrue",
38
- "repository": {
39
- "type": "git",
40
- "url": "https://github.com/topce/auto-translate-json-library"
41
- },
42
- "contributors": [
43
- "codegrue",
44
- "gajo357",
45
- "topce",
46
- "BenGMiles",
47
- "stonega",
48
- "alexei-petru"
49
- ],
50
- "license": "MIT",
51
- "devDependencies": {
52
- "@biomejs/biome": "1.2.2",
53
- "@topce/typescript": "5.3.1",
54
- "@types/minimist": "1.2.3",
55
- "@types/node": "20.8.6",
56
- "@types/yargs": "17.0.28",
57
- "ts-node": "10.9.1"
58
- },
59
- "dependencies": {
60
- "@aws-sdk/client-translate": "3.428.0",
61
- "@google-cloud/translate": "8.0.2",
62
- "axios": "1.5.1",
63
- "dotenv": "16.3.1",
64
- "form-data": "4.0.0",
65
- "minimist": "1.2.8",
66
- "openai": "4.12.1",
67
- "picocolors": "1.0.0",
68
- "uuid": "9.0.1",
69
- "yargs": "17.7.2"
70
- }
71
- }
@@ -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,7 +0,0 @@
1
- import { ITranslate } from "./translate.interface";
2
- export declare class OpenAITranslate implements ITranslate {
3
- private openai;
4
- constructor(apiKey: string);
5
- isValidLocale(targetLocale: string): boolean;
6
- translateText(text: string, sourceLocale: string, targetLocale: string): Promise<string>;
7
- }
@@ -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
- }
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
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