auto-translate-json-library 2.0.2 → 2.0.5

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 (40) hide show
  1. package/README.MD +240 -3
  2. package/build/package.json +30 -16
  3. package/build/src/bin/cli.d.ts +1 -1
  4. package/build/src/bin/cli.js +82 -53
  5. package/build/src/config.d.ts +12 -1
  6. package/build/src/files.js +2 -2
  7. package/build/src/huggingface-local.d.ts +11 -0
  8. package/build/src/huggingface-local.js +71 -0
  9. package/build/src/huggingface.d.ts +12 -0
  10. package/build/src/huggingface.js +76 -0
  11. package/build/src/index.js +17 -0
  12. package/build/src/lib.js +9 -1
  13. package/build/src/openai.js +1 -0
  14. package/package.json +30 -16
  15. package/build/tests/arb-handler.test.d.ts +0 -1
  16. package/build/tests/arb-handler.test.js +0 -694
  17. package/build/tests/cli-integration.test.d.ts +0 -1
  18. package/build/tests/cli-integration.test.js +0 -352
  19. package/build/tests/csv-handler.test.d.ts +0 -1
  20. package/build/tests/csv-handler.test.js +0 -507
  21. package/build/tests/error-handling.test.d.ts +0 -1
  22. package/build/tests/error-handling.test.js +0 -456
  23. package/build/tests/files-integration.test.d.ts +0 -1
  24. package/build/tests/files-integration.test.js +0 -421
  25. package/build/tests/json-handler.test.d.ts +0 -1
  26. package/build/tests/json-handler.test.js +0 -526
  27. package/build/tests/po-handler.test.d.ts +0 -1
  28. package/build/tests/po-handler.test.js +0 -768
  29. package/build/tests/properties-handler.test.d.ts +0 -1
  30. package/build/tests/properties-handler.test.js +0 -316
  31. package/build/tests/setup.d.ts +0 -1
  32. package/build/tests/setup.js +0 -5
  33. package/build/tests/xliff-handler.test.d.ts +0 -1
  34. package/build/tests/xliff-handler.test.js +0 -459
  35. package/build/tests/xmb-xtb-handler.test.d.ts +0 -1
  36. package/build/tests/xmb-xtb-handler.test.js +0 -758
  37. package/build/tests/xml-handler.test.d.ts +0 -1
  38. package/build/tests/xml-handler.test.js +0 -570
  39. package/build/tests/yaml-handler.test.d.ts +0 -1
  40. package/build/tests/yaml-handler.test.js +0 -156
@@ -0,0 +1,71 @@
1
+ import { Util } from "./util.js";
2
+ const supportedLanguages = {
3
+ en: "English",
4
+ fr: "French",
5
+ de: "German",
6
+ es: "Spanish",
7
+ it: "Italian",
8
+ pt: "Portuguese",
9
+ nl: "Dutch",
10
+ pl: "Polish",
11
+ cs: "Czech",
12
+ sk: "Slovak",
13
+ sl: "Slovenian",
14
+ hr: "Croatian",
15
+ hu: "Hungarian",
16
+ ro: "Romanian",
17
+ bg: "Bulgarian",
18
+ el: "Greek",
19
+ ru: "Russian",
20
+ uk: "Ukrainian",
21
+ tr: "Turkish",
22
+ ar: "Arabic",
23
+ he: "Hebrew",
24
+ hi: "Hindi",
25
+ th: "Thai",
26
+ vi: "Vietnamese",
27
+ id: "Indonesian",
28
+ ms: "Malay",
29
+ ja: "Japanese",
30
+ ko: "Korean",
31
+ "zh-CN": "Chinese (Simplified)",
32
+ "zh-TW": "Chinese (Traditional)",
33
+ };
34
+ export class HuggingFaceLocalTranslate {
35
+ model;
36
+ pipelinePromise = null;
37
+ constructor(model) {
38
+ this.model = model;
39
+ }
40
+ isValidLocale(_targetLocale) {
41
+ // Local ONNX models support many locales; validation happens at runtime
42
+ return true;
43
+ }
44
+ async getPipeline() {
45
+ if (!this.pipelinePromise) {
46
+ // Dynamically import to avoid issues when the package is not installed
47
+ const { pipeline } = await import("@huggingface/transformers");
48
+ this.pipelinePromise = pipeline("translation", this.model);
49
+ }
50
+ return this.pipelinePromise;
51
+ }
52
+ sanitizeResult(result, args) {
53
+ return Util.replaceArgumentsWithNumbers(args, result.replace(/^\n+|\n+$/g, "").trim());
54
+ }
55
+ async translateWithTranslationPipeline(text, sourceLocale, targetLocale, args) {
56
+ const translator = await this.getPipeline();
57
+ const output = await translator(text, {
58
+ src_lang: sourceLocale,
59
+ tgt_lang: targetLocale,
60
+ });
61
+ const result = Array.isArray(output) && output.length > 0
62
+ ? output[0].translation_text
63
+ : output.translation_text;
64
+ return this.sanitizeResult(result, args);
65
+ }
66
+ async translateText(text, sourceLocale, targetLocale, _context) {
67
+ let args;
68
+ ({ args, text } = Util.replaceContextVariables(text));
69
+ return this.translateWithTranslationPipeline(text, sourceLocale, targetLocale, args);
70
+ }
71
+ }
@@ -0,0 +1,12 @@
1
+ import { type InferenceProviderOrPolicy } from "@huggingface/inference";
2
+ import type { ITranslate } from "./translate.interface.js";
3
+ export declare class HuggingFaceTranslate implements ITranslate {
4
+ private model;
5
+ private provider?;
6
+ private client;
7
+ constructor(apiKey: string, model: string, provider?: InferenceProviderOrPolicy | undefined);
8
+ isValidLocale(_targetLocale: string): boolean;
9
+ private getLanguageName;
10
+ private sanitizeResult;
11
+ translateText(text: string, sourceLocale: string, targetLocale: string, _context?: string): Promise<string>;
12
+ }
@@ -0,0 +1,76 @@
1
+ import { InferenceClient } from "@huggingface/inference";
2
+ import { Util } from "./util.js";
3
+ const supportedLanguages = {
4
+ en: "English",
5
+ fr: "French",
6
+ de: "German",
7
+ es: "Spanish",
8
+ it: "Italian",
9
+ pt: "Portuguese",
10
+ nl: "Dutch",
11
+ pl: "Polish",
12
+ cs: "Czech",
13
+ sk: "Slovak",
14
+ sl: "Slovenian",
15
+ hr: "Croatian",
16
+ hu: "Hungarian",
17
+ ro: "Romanian",
18
+ bg: "Bulgarian",
19
+ el: "Greek",
20
+ ru: "Russian",
21
+ uk: "Ukrainian",
22
+ tr: "Turkish",
23
+ ar: "Arabic",
24
+ he: "Hebrew",
25
+ hi: "Hindi",
26
+ th: "Thai",
27
+ vi: "Vietnamese",
28
+ id: "Indonesian",
29
+ ms: "Malay",
30
+ ja: "Japanese",
31
+ ko: "Korean",
32
+ "zh-CN": "Chinese (Simplified)",
33
+ "zh-TW": "Chinese (Traditional)",
34
+ };
35
+ export class HuggingFaceTranslate {
36
+ model;
37
+ provider;
38
+ client;
39
+ constructor(apiKey, model, provider) {
40
+ this.model = model;
41
+ this.provider = provider;
42
+ this.client = new InferenceClient(apiKey);
43
+ }
44
+ isValidLocale(_targetLocale) {
45
+ // HF translation models support many locales; validation happens at runtime
46
+ return true;
47
+ }
48
+ getLanguageName(locale) {
49
+ return supportedLanguages[locale] ?? locale;
50
+ }
51
+ sanitizeResult(result, args) {
52
+ const trimmedResult = result.replace(/^\n+|\n+$/g, "").trim();
53
+ return Util.replaceArgumentsWithNumbers(args, trimmedResult);
54
+ }
55
+ async translateText(text, sourceLocale, targetLocale, _context) {
56
+ let args;
57
+ ({ args, text } = Util.replaceContextVariables(text));
58
+ const response = await this.client.translation({
59
+ model: this.model,
60
+ provider: this.provider,
61
+ inputs: text,
62
+ parameters: {
63
+ src_lang: sourceLocale,
64
+ tgt_lang: targetLocale,
65
+ },
66
+ });
67
+ let result;
68
+ if (Array.isArray(response)) {
69
+ result = response[0].translation_text;
70
+ }
71
+ else {
72
+ result = response.translation_text;
73
+ }
74
+ return this.sanitizeResult(result, args);
75
+ }
76
+ }
@@ -123,6 +123,23 @@ function setConfigurationFromEnvironment() {
123
123
  presencePenalty: Number(process.env.ATJ_OPEN_AI_PRESENCE_PENALTY ?? "0"),
124
124
  };
125
125
  }
126
+ // Hugging Face cloud mode (requires API key)
127
+ if (process.env.ATJ_HUGGING_FACE_API_KEY) {
128
+ config.translationKeyInfo = {
129
+ kind: "huggingface",
130
+ apiKey: process.env.ATJ_HUGGING_FACE_API_KEY,
131
+ model: process.env.ATJ_HUGGING_FACE_MODEL ??
132
+ "Helsinki-NLP/opus-mt-en-fr",
133
+ provider: process.env.ATJ_HUGGING_FACE_PROVIDER,
134
+ };
135
+ }
136
+ // Hugging Face local mode (no API key, ONNX on-device — takes priority)
137
+ if (process.env.ATJ_HUGGING_FACE_LOCAL_MODEL) {
138
+ config.translationKeyInfo = {
139
+ kind: "huggingface-local",
140
+ model: process.env.ATJ_HUGGING_FACE_LOCAL_MODEL,
141
+ };
142
+ }
126
143
  if (process.env.ATJ_START_DELIMITER) {
127
144
  config.startDelimiter = process.env.ATJ_START_DELIMITER;
128
145
  }
package/build/src/lib.js CHANGED
@@ -6,6 +6,8 @@ import { DeepLTranslate } from "./deepl.js";
6
6
  import { Files } from "./files.js";
7
7
  import { FolderFiles } from "./folderFiles.js";
8
8
  import { GoogleTranslate } from "./google.js";
9
+ import { HuggingFaceTranslate } from "./huggingface.js";
10
+ import { HuggingFaceLocalTranslate } from "./huggingface-local.js";
9
11
  import { OpenAITranslate } from "./openai.js";
10
12
  import { Util } from "./util.js";
11
13
  export async function translate(sourceFile, config) {
@@ -37,8 +39,14 @@ export async function translate(sourceFile, config) {
37
39
  else if (config.translationKeyInfo.kind === "openai") {
38
40
  translateEngine = new 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);
39
41
  }
42
+ else if (config.translationKeyInfo.kind === "huggingface") {
43
+ translateEngine = new HuggingFaceTranslate(config.translationKeyInfo.apiKey, config.translationKeyInfo.model, config.translationKeyInfo.provider);
44
+ }
45
+ else if (config.translationKeyInfo.kind === "huggingface-local") {
46
+ translateEngine = new HuggingFaceLocalTranslate(config.translationKeyInfo.model);
47
+ }
40
48
  else {
41
- console.warn("You must provide a Google, AWS, Azure, deepL, openai parameters first in the extension settings.");
49
+ console.warn("You must provide a Google, AWS, Azure, deepL, openai, huggingface, or huggingface-local parameters first in the extension settings.");
42
50
  return;
43
51
  }
44
52
  const fileMode = config.mode ?? "file";
@@ -130,6 +130,7 @@ export class OpenAITranslate {
130
130
  const configuration = {
131
131
  apiKey: apiKey,
132
132
  baseURL: baseUrl,
133
+ timeout: 120000, // 2 minutes timeout for local models like Ollama
133
134
  };
134
135
  this.openai = new OpenAI(configuration);
135
136
  }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "auto-translate-json-library",
3
- "version": "2.0.2",
3
+ "version": "2.0.5",
4
4
  "type": "module",
5
- "description": "Automatically translates JSON language files to other languages using Google Translate,AWS,Azure,DeepL,OpenAI or local OpenAI compatible server",
5
+ "description": "Automatically translates JSON language files to other languages using Google Translate, AWS, Azure, DeepL, OpenAI, Hugging Face (cloud & local) or local OpenAI compatible server",
6
6
  "main": "./build/src/index.js",
7
7
  "bin": {
8
8
  "auto-translate-json-library": "build/src/bin/cli.js",
@@ -10,15 +10,21 @@
10
10
  },
11
11
  "types": "./build/src/index.d.ts",
12
12
  "files": [
13
- "build",
14
- "README.md"
13
+ "build/src",
14
+ "build/package.json",
15
+ "README.MD"
15
16
  ],
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
16
20
  "scripts": {
17
21
  "prepare": "tsc",
22
+ "prepack": "npm run build",
18
23
  "build": "tsc",
19
24
  "build:go": "tsgo --skipLibCheck",
20
25
  "build:run": "ts-node ./build/src/index.js --pivotTranslation=./tests/translations/en.json",
21
26
  "debug": "ts-node src/index.ts --pivotTranslation=./tests/translations/en.json",
27
+ "release:check": "npm test && npm pack --dry-run",
22
28
  "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
23
29
  "test:watch": "jest --watch",
24
30
  "test:coverage": "jest --coverage",
@@ -99,7 +105,13 @@
99
105
  "automation",
100
106
  "batch-translation",
101
107
  "multi-format",
102
- "gettext"
108
+ "gettext",
109
+ "huggingface",
110
+ "hugging-face",
111
+ "helsinki-nlp",
112
+ "nllb",
113
+ "transformers-js",
114
+ "local-translation"
103
115
  ],
104
116
  "author": "codegrue",
105
117
  "repository": {
@@ -116,34 +128,36 @@
116
128
  ],
117
129
  "license": "MIT",
118
130
  "devDependencies": {
119
- "@biomejs/biome": "2.3.11",
120
- "@topce/native-preview": "7.0.0-dev.20250824",
121
- "@types/gettext-parser": "8.0.0",
131
+ "@biomejs/biome": "2.4.7",
132
+ "@topce/native-preview": "7.0.0-dev.20260215",
133
+ "@types/gettext-parser": "9.0.0",
122
134
  "@types/jest": "30.0.0",
123
135
  "@types/minimist": "1.2.5",
124
- "@types/node": "25.0.3",
136
+ "@types/node": "25.5.0",
125
137
  "@types/properties-parser": "0.3.3",
126
138
  "@types/yargs": "17.0.35",
127
- "jest": "30.2.0",
139
+ "jest": "30.3.0",
128
140
  "ts-jest": "29.4.6",
129
141
  "ts-node": "10.9.2",
130
142
  "typescript": "5.9.3"
131
143
  },
132
144
  "dependencies": {
133
- "@aws-sdk/client-translate": "3.962.0",
145
+ "@aws-sdk/client-translate": "3.1011.0",
134
146
  "@google-cloud/translate": "9.3.0",
147
+ "@huggingface/inference": "^4.13.15",
148
+ "@huggingface/transformers": "^3.8.1",
135
149
  "@types/js-yaml": "4.0.9",
136
- "axios": "1.13.2",
150
+ "axios": "1.13.6",
137
151
  "csv-parser": "3.2.0",
138
152
  "csv-writer": "1.6.0",
139
153
  "deepl-node": "1.24.0",
140
- "dotenv": "17.2.3",
141
- "fast-xml-parser": "5.3.3",
154
+ "dotenv": "17.3.1",
155
+ "fast-xml-parser": "5.5.6",
142
156
  "form-data": "4.0.5",
143
- "gettext-parser": "9.0.0",
157
+ "gettext-parser": "9.0.2",
144
158
  "js-yaml": "4.1.1",
145
159
  "minimist": "1.2.8",
146
- "openai": "6.15.0",
160
+ "openai": "6.32.0",
147
161
  "picocolors": "1.1.1",
148
162
  "properties-parser": "0.6.0",
149
163
  "uuid": "13.0.0",
@@ -1 +0,0 @@
1
- export {};