auto-translate-json-library 1.5.5 → 2.0.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/README.MD +1141 -148
- package/build/package.json +96 -22
- package/build/src/aws.d.ts +1 -1
- package/build/src/aws.js +27 -38
- package/build/src/azure.d.ts +1 -1
- package/build/src/azure.js +50 -64
- package/build/src/bin/cli.d.ts +1 -1
- package/build/src/bin/cli.js +196 -37
- package/build/src/config.d.ts +1 -0
- package/build/src/config.js +1 -2
- package/build/src/deepl.d.ts +1 -1
- package/build/src/deepl.js +18 -64
- package/build/src/files.d.ts +11 -2
- package/build/src/files.js +356 -69
- package/build/src/folderFiles.d.ts +6 -3
- package/build/src/folderFiles.js +33 -44
- package/build/src/format/arb-handler.d.ts +33 -0
- package/build/src/format/arb-handler.js +679 -0
- package/build/src/format/base-format-handler.d.ts +75 -0
- package/build/src/format/base-format-handler.js +166 -0
- package/build/src/format/csv-handler.d.ts +38 -0
- package/build/src/format/csv-handler.js +604 -0
- package/build/src/format/index.d.ts +17 -0
- package/build/src/format/index.js +48 -0
- package/build/src/format/json-handler.d.ts +17 -0
- package/build/src/format/json-handler.js +294 -0
- package/build/src/format/po-handler.d.ts +17 -0
- package/build/src/format/po-handler.js +338 -0
- package/build/src/format/po-utils.d.ts +86 -0
- package/build/src/format/po-utils.js +385 -0
- package/build/src/format/pot-handler.d.ts +22 -0
- package/build/src/format/pot-handler.js +331 -0
- package/build/src/format/properties-handler.d.ts +26 -0
- package/build/src/format/properties-handler.js +500 -0
- package/build/src/format/tsv-handler.d.ts +13 -0
- package/build/src/format/tsv-handler.js +67 -0
- package/build/src/format/xliff-handler.d.ts +24 -0
- package/build/src/format/xliff-handler.js +433 -0
- package/build/src/format/xmb-handler.d.ts +33 -0
- package/build/src/format/xmb-handler.js +484 -0
- package/build/src/format/xmb-xtb-utils.d.ts +43 -0
- package/build/src/format/xmb-xtb-utils.js +180 -0
- package/build/src/format/xml-handler.d.ts +28 -0
- package/build/src/format/xml-handler.js +561 -0
- package/build/src/format/xtb-handler.d.ts +33 -0
- package/build/src/format/xtb-handler.js +518 -0
- package/build/src/format/yaml-handler.d.ts +26 -0
- package/build/src/format/yaml-handler.js +522 -0
- package/build/src/format-detector.d.ts +7 -0
- package/build/src/format-detector.js +58 -0
- package/build/src/format-handler-factory.d.ts +9 -0
- package/build/src/format-handler-factory.js +18 -0
- package/build/src/format.interface.d.ts +56 -0
- package/build/src/format.interface.js +1 -0
- package/build/src/google.d.ts +1 -1
- package/build/src/google.js +32 -49
- package/build/src/index.d.ts +4 -3
- package/build/src/index.js +15 -17
- package/build/src/lib.d.ts +1 -1
- package/build/src/lib.js +188 -133
- package/build/src/openai.d.ts +2 -2
- package/build/src/openai.js +59 -50
- package/build/src/translate.interface.d.ts +1 -1
- package/build/src/translate.interface.js +1 -2
- package/build/src/util.js +4 -9
- package/build/src/validation/enhanced-validation-result.d.ts +96 -0
- package/build/src/validation/enhanced-validation-result.js +198 -0
- package/build/src/validation/error-messages.d.ts +88 -0
- package/build/src/validation/error-messages.js +436 -0
- package/build/src/validation/error-recovery.d.ts +78 -0
- package/build/src/validation/error-recovery.js +593 -0
- package/build/src/validation/example-usage.d.ts +31 -0
- package/build/src/validation/example-usage.js +148 -0
- package/build/src/validation/format-rules.d.ts +37 -0
- package/build/src/validation/format-rules.js +550 -0
- package/build/src/validation/format-validator.d.ts +70 -0
- package/build/src/validation/format-validator.js +279 -0
- package/build/src/validation/index.d.ts +13 -0
- package/build/src/validation/index.js +30 -0
- package/build/src/validation/validation-service.d.ts +56 -0
- package/build/src/validation/validation-service.js +262 -0
- package/build/tests/arb-handler.test.d.ts +1 -0
- package/build/tests/arb-handler.test.js +694 -0
- package/build/tests/cli-integration.test.d.ts +1 -0
- package/build/tests/cli-integration.test.js +352 -0
- package/build/tests/csv-handler.test.d.ts +1 -0
- package/build/tests/csv-handler.test.js +507 -0
- package/build/tests/error-handling.test.d.ts +1 -0
- package/build/tests/error-handling.test.js +456 -0
- package/build/tests/files-integration.test.d.ts +1 -0
- package/build/tests/files-integration.test.js +421 -0
- package/build/tests/json-handler.test.d.ts +1 -0
- package/build/tests/json-handler.test.js +526 -0
- package/build/tests/po-handler.test.d.ts +1 -0
- package/build/tests/po-handler.test.js +768 -0
- package/build/tests/properties-handler.test.d.ts +1 -0
- package/build/tests/properties-handler.test.js +316 -0
- package/build/tests/setup.d.ts +1 -0
- package/build/tests/setup.js +5 -0
- package/build/tests/xliff-handler.test.d.ts +1 -0
- package/build/tests/xliff-handler.test.js +459 -0
- package/build/tests/xmb-xtb-handler.test.d.ts +1 -0
- package/build/tests/xmb-xtb-handler.test.js +758 -0
- package/build/tests/xml-handler.test.d.ts +1 -0
- package/build/tests/xml-handler.test.js +570 -0
- package/build/tests/yaml-handler.test.d.ts +1 -0
- package/build/tests/yaml-handler.test.js +156 -0
- package/package.json +96 -22
package/build/src/files.js
CHANGED
|
@@ -1,51 +1,80 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { XMLBuilder, XMLParser } from "fast-xml-parser";
|
|
4
|
+
import { FormatDetector } from "./format-detector.js";
|
|
5
|
+
import { FormatHandlerFactory } from "./format-handler-factory.js";
|
|
6
|
+
// Import format handlers to ensure they are registered
|
|
7
|
+
import "./format/index.js";
|
|
8
|
+
// Helper function for format-specific error recovery suggestions
|
|
9
|
+
function getFormatSpecificSuggestions(format, error) {
|
|
10
|
+
const errorMessage = error.message.toLowerCase();
|
|
11
|
+
switch (format) {
|
|
12
|
+
case "json":
|
|
13
|
+
if (errorMessage.includes("syntax") ||
|
|
14
|
+
errorMessage.includes("unexpected")) {
|
|
15
|
+
return "Check for missing commas, quotes, or brackets. Use a JSON validator to identify syntax issues.";
|
|
16
|
+
}
|
|
17
|
+
if (errorMessage.includes("circular")) {
|
|
18
|
+
return "Remove circular references from the data structure.";
|
|
19
|
+
}
|
|
20
|
+
break;
|
|
21
|
+
case "xml":
|
|
22
|
+
case "android-xml":
|
|
23
|
+
case "ios-xml":
|
|
24
|
+
if (errorMessage.includes("syntax") || errorMessage.includes("parsing")) {
|
|
25
|
+
return "Check for unclosed tags, missing quotes in attributes, or invalid XML characters.";
|
|
26
|
+
}
|
|
27
|
+
break;
|
|
28
|
+
case "xliff":
|
|
29
|
+
if (errorMessage.includes("validation")) {
|
|
30
|
+
return "Ensure XLIFF file has proper structure with <file>, <trans-unit>, <source>, and <target> elements.";
|
|
31
|
+
}
|
|
32
|
+
break;
|
|
33
|
+
case "po":
|
|
34
|
+
case "pot":
|
|
35
|
+
if (errorMessage.includes("syntax") || errorMessage.includes("parsing")) {
|
|
36
|
+
return "Check for proper msgid/msgstr pairs and correct PO file encoding.";
|
|
37
|
+
}
|
|
38
|
+
break;
|
|
39
|
+
case "yaml":
|
|
40
|
+
if (errorMessage.includes("syntax") ||
|
|
41
|
+
errorMessage.includes("indentation")) {
|
|
42
|
+
return "Check YAML indentation (use spaces, not tabs) and ensure proper key-value syntax.";
|
|
43
|
+
}
|
|
44
|
+
break;
|
|
45
|
+
case "properties":
|
|
46
|
+
if (errorMessage.includes("encoding")) {
|
|
47
|
+
return "Check file encoding (should be UTF-8 or ISO-8859-1) and Unicode escape sequences.";
|
|
48
|
+
}
|
|
49
|
+
break;
|
|
50
|
+
case "csv":
|
|
51
|
+
case "tsv":
|
|
52
|
+
if (errorMessage.includes("parsing")) {
|
|
53
|
+
return "Check for proper delimiter usage and ensure quoted fields are properly escaped.";
|
|
54
|
+
}
|
|
55
|
+
break;
|
|
56
|
+
case "arb":
|
|
57
|
+
if (errorMessage.includes("validation")) {
|
|
58
|
+
return "Ensure ARB file has proper JSON structure with @@locale metadata and valid resource entries.";
|
|
59
|
+
}
|
|
60
|
+
break;
|
|
61
|
+
case "xmb":
|
|
62
|
+
case "xtb":
|
|
63
|
+
if (errorMessage.includes("validation")) {
|
|
64
|
+
return "Check for proper XMB/XTB structure with <messagebundle>/<translationbundle> root elements.";
|
|
65
|
+
}
|
|
66
|
+
break;
|
|
67
|
+
default:
|
|
68
|
+
if (errorMessage.includes("unknown") ||
|
|
69
|
+
errorMessage.includes("unsupported")) {
|
|
70
|
+
const supportedFormats = FormatDetector.getSupportedFormats().join(", ");
|
|
71
|
+
return `Supported formats: ${supportedFormats}. Consider using --format parameter to specify format explicitly.`;
|
|
72
|
+
}
|
|
73
|
+
break;
|
|
7
74
|
}
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
-
exports.Files = exports.saveJsonToLocale = exports.loadJsonFromLocale = exports.readFileAsync = void 0;
|
|
46
|
-
const fs = __importStar(require("node:fs"));
|
|
47
|
-
const path = __importStar(require("node:path"));
|
|
48
|
-
const readFileAsync = (filename) => new Promise((resolve, reject) => {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
export const readFileAsync = (filename) => new Promise((resolve, reject) => {
|
|
49
78
|
const exist = fs.existsSync(filename);
|
|
50
79
|
if (!exist)
|
|
51
80
|
fs.writeFileSync(filename, "");
|
|
@@ -53,35 +82,210 @@ const readFileAsync = (filename) => new Promise((resolve, reject) => {
|
|
|
53
82
|
error ? reject(error) : resolve(data.toString());
|
|
54
83
|
});
|
|
55
84
|
});
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
85
|
+
export const loadJsonFromLocale = async (fileName) => {
|
|
86
|
+
try {
|
|
87
|
+
let data = await readFileAsync(fileName);
|
|
88
|
+
// handle empty files
|
|
89
|
+
if (!data) {
|
|
90
|
+
// return an empty object for both JSON and XML
|
|
91
|
+
return {};
|
|
92
|
+
}
|
|
93
|
+
// Try to use new format system first
|
|
94
|
+
const format = FormatDetector.detectFormat(fileName, data);
|
|
95
|
+
const handler = FormatHandlerFactory.getHandler(format);
|
|
96
|
+
if (handler) {
|
|
97
|
+
try {
|
|
98
|
+
// Validate structure before parsing
|
|
99
|
+
const tempResult = handler.parse(data);
|
|
100
|
+
const validation = handler.validateStructure(tempResult);
|
|
101
|
+
if (!validation.isValid) {
|
|
102
|
+
const errorMessages = validation.errors
|
|
103
|
+
.map((e) => `${e.code}: ${e.message}`)
|
|
104
|
+
.join(", ");
|
|
105
|
+
throw new Error(`${format.toUpperCase()} validation failed: ${errorMessages}`);
|
|
106
|
+
}
|
|
107
|
+
if (validation.warnings.length > 0) {
|
|
108
|
+
const warningMessages = validation.warnings
|
|
109
|
+
.map((w) => `${w.code}: ${w.message}`)
|
|
110
|
+
.join(", ");
|
|
111
|
+
console.warn(`${format.toUpperCase()} validation warnings for ${fileName}: ${warningMessages}`);
|
|
112
|
+
}
|
|
113
|
+
// For formats that need metadata for proper reconstruction (like YAML), preserve it
|
|
114
|
+
if (format === 'yaml' || format === 'yml') {
|
|
115
|
+
return tempResult;
|
|
116
|
+
}
|
|
117
|
+
// Remove metadata for backward compatibility
|
|
118
|
+
const { _metadata, ...cleanResult } = tempResult;
|
|
119
|
+
return cleanResult;
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
if (error instanceof SyntaxError) {
|
|
123
|
+
throw new Error(`${format.toUpperCase()} syntax error in ${fileName}: ${error.message}`);
|
|
124
|
+
}
|
|
125
|
+
else if (error instanceof Error &&
|
|
126
|
+
error.message.includes("validation failed")) {
|
|
127
|
+
throw error; // Re-throw validation errors as-is
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
throw new Error(`Failed to parse ${format.toUpperCase()} file ${fileName}: ${error}`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// Legacy fallback parsing with enhanced error handling
|
|
135
|
+
const ext = path.extname(fileName).toLowerCase();
|
|
136
|
+
if (ext === ".xml") {
|
|
137
|
+
try {
|
|
138
|
+
const parser = new XMLParser({
|
|
139
|
+
ignoreAttributes: false,
|
|
140
|
+
attributeNamePrefix: "@_",
|
|
141
|
+
textNodeName: "#text",
|
|
142
|
+
allowBooleanAttributes: true,
|
|
143
|
+
parseTagValue: true,
|
|
144
|
+
parseAttributeValue: true,
|
|
145
|
+
trimValues: false,
|
|
146
|
+
});
|
|
147
|
+
return parser.parse(data);
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
throw new Error(`XML parsing error in ${fileName}: ${error}. Please check XML syntax and structure.`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// default to JSON with enhanced error handling
|
|
154
|
+
try {
|
|
155
|
+
return JSON.parse(data);
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
if (error instanceof SyntaxError) {
|
|
159
|
+
throw new Error(`JSON syntax error in ${fileName}: ${error.message}. Please check JSON formatting.`);
|
|
160
|
+
}
|
|
161
|
+
throw new Error(`Failed to parse JSON file ${fileName}: ${error}`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
// Provide format-specific recovery suggestions
|
|
166
|
+
const format = FormatDetector.detectFormat(fileName);
|
|
167
|
+
const suggestions = getFormatSpecificSuggestions(format, error);
|
|
168
|
+
throw new Error(`${error}${suggestions ? ` Suggestions: ${suggestions}` : ""}`);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
export const saveJsonToLocale = (filename, file) => {
|
|
172
|
+
try {
|
|
173
|
+
// Try to use new format system first
|
|
174
|
+
const format = FormatDetector.detectFormat(filename);
|
|
175
|
+
const handler = FormatHandlerFactory.getHandler(format);
|
|
176
|
+
if (handler) {
|
|
177
|
+
try {
|
|
178
|
+
// Validate structure before serializing
|
|
179
|
+
const validation = handler.validateStructure(file);
|
|
180
|
+
if (!validation.isValid) {
|
|
181
|
+
const errorMessages = validation.errors
|
|
182
|
+
.map((e) => `${e.code}: ${e.message}`)
|
|
183
|
+
.join(", ");
|
|
184
|
+
throw new Error(`${format.toUpperCase()} validation failed: ${errorMessages}`);
|
|
185
|
+
}
|
|
186
|
+
if (validation.warnings.length > 0) {
|
|
187
|
+
const warningMessages = validation.warnings
|
|
188
|
+
.map((w) => `${w.code}: ${w.message}`)
|
|
189
|
+
.join(", ");
|
|
190
|
+
console.warn(`${format.toUpperCase()} validation warnings for ${filename}: ${warningMessages}`);
|
|
191
|
+
}
|
|
192
|
+
const data = handler.serialize(file, {
|
|
193
|
+
preserveFormatting: true,
|
|
194
|
+
indentation: 2,
|
|
195
|
+
xmlDeclaration: true,
|
|
196
|
+
});
|
|
197
|
+
fs.writeFileSync(filename, data, "utf8");
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
if (error instanceof Error &&
|
|
202
|
+
error.message.includes("validation failed")) {
|
|
203
|
+
throw error; // Re-throw validation errors as-is
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
throw new Error(`Failed to serialize ${format.toUpperCase()} file ${filename}: ${error}`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
// Legacy fallback serialization with enhanced error handling
|
|
211
|
+
const ext = path.extname(filename).toLowerCase();
|
|
212
|
+
let data;
|
|
213
|
+
if (ext === ".xml") {
|
|
214
|
+
try {
|
|
215
|
+
const builder = new XMLBuilder({
|
|
216
|
+
ignoreAttributes: false,
|
|
217
|
+
attributeNamePrefix: "@_",
|
|
218
|
+
textNodeName: "#text",
|
|
219
|
+
format: true,
|
|
220
|
+
indentBy: " ",
|
|
221
|
+
suppressEmptyNode: true,
|
|
222
|
+
});
|
|
223
|
+
data = builder.build(file);
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
throw new Error(`XML serialization error for ${filename}: ${error}. Please check data structure compatibility with XML format.`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
try {
|
|
231
|
+
data = JSON.stringify(file, null, " ");
|
|
232
|
+
}
|
|
233
|
+
catch (error) {
|
|
234
|
+
throw new Error(`JSON serialization error for ${filename}: ${error}. Please check for circular references or unsupported data types.`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
fs.writeFileSync(filename, data, "utf8");
|
|
238
|
+
}
|
|
239
|
+
catch (error) {
|
|
240
|
+
// Provide format-specific recovery suggestions
|
|
241
|
+
const format = FormatDetector.detectFormat(filename);
|
|
242
|
+
const suggestions = getFormatSpecificSuggestions(format, error);
|
|
243
|
+
throw new Error(`${error}${suggestions ? ` Suggestions: ${suggestions}` : ""}`);
|
|
62
244
|
}
|
|
63
|
-
return JSON.parse(data);
|
|
64
|
-
});
|
|
65
|
-
exports.loadJsonFromLocale = loadJsonFromLocale;
|
|
66
|
-
const saveJsonToLocale = (filename, file) => {
|
|
67
|
-
const data = JSON.stringify(file, null, " ");
|
|
68
|
-
fs.writeFileSync(filename, data, "utf8");
|
|
69
245
|
};
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
246
|
+
export class Files {
|
|
247
|
+
folderPath;
|
|
248
|
+
sourceLocale;
|
|
249
|
+
targetLocales;
|
|
250
|
+
fileExt;
|
|
251
|
+
formatOverride;
|
|
252
|
+
detectedFormat;
|
|
253
|
+
constructor(filePath, formatOverride) {
|
|
73
254
|
this.folderPath = path.dirname(filePath);
|
|
74
255
|
const fileName = path.basename(filePath);
|
|
256
|
+
this.fileExt = path.extname(fileName);
|
|
257
|
+
this.formatOverride = formatOverride;
|
|
75
258
|
this.sourceLocale = this.getLocaleFromFilename(fileName);
|
|
76
259
|
this.targetLocales = this.getTargetLocales();
|
|
260
|
+
// Detect format for the source file
|
|
261
|
+
this.detectedFormat = this.detectFileFormat(filePath);
|
|
77
262
|
}
|
|
78
263
|
getLocaleFromFilename(fileName) {
|
|
79
|
-
return
|
|
264
|
+
return path.parse(fileName).name;
|
|
265
|
+
}
|
|
266
|
+
detectFileFormat(filePath) {
|
|
267
|
+
if (this.formatOverride) {
|
|
268
|
+
return this.formatOverride;
|
|
269
|
+
}
|
|
270
|
+
// Try to read a small portion of the file for content-based detection
|
|
271
|
+
try {
|
|
272
|
+
if (fs.existsSync(filePath)) {
|
|
273
|
+
const content = fs.readFileSync(filePath, "utf8").substring(0, 1000); // Read first 1KB
|
|
274
|
+
return FormatDetector.detectFormat(filePath, content);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
catch (error) {
|
|
278
|
+
// If file reading fails, fall back to extension-based detection
|
|
279
|
+
console.warn(`Could not read file for format detection: ${error}`);
|
|
280
|
+
}
|
|
281
|
+
return FormatDetector.detectFormat(filePath);
|
|
80
282
|
}
|
|
81
283
|
getTargetLocales() {
|
|
82
284
|
const locales = [];
|
|
83
285
|
const files = fs.readdirSync(this.folderPath);
|
|
84
286
|
for (const file of files) {
|
|
287
|
+
if (path.extname(file) !== this.fileExt)
|
|
288
|
+
continue; // only same extension
|
|
85
289
|
const locale = this.getLocaleFromFilename(file);
|
|
86
290
|
if (locale !== this.sourceLocale) {
|
|
87
291
|
locales.push(locale);
|
|
@@ -89,13 +293,96 @@ class Files {
|
|
|
89
293
|
}
|
|
90
294
|
return locales;
|
|
91
295
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
296
|
+
getDetectedFormat() {
|
|
297
|
+
return this.detectedFormat;
|
|
298
|
+
}
|
|
299
|
+
getFormatOverride() {
|
|
300
|
+
return this.formatOverride;
|
|
301
|
+
}
|
|
302
|
+
async loadJsonFromLocale(locale) {
|
|
303
|
+
const filename = `${this.folderPath}/${locale}${this.fileExt}`;
|
|
304
|
+
try {
|
|
305
|
+
let data = await readFileAsync(filename);
|
|
306
|
+
// Handle empty files
|
|
307
|
+
if (!data) {
|
|
308
|
+
return {};
|
|
309
|
+
}
|
|
310
|
+
// Use format detection for this specific file
|
|
311
|
+
const format = this.formatOverride || FormatDetector.detectFormat(filename, data);
|
|
312
|
+
const handler = FormatHandlerFactory.getHandler(format);
|
|
313
|
+
if (handler) {
|
|
314
|
+
try {
|
|
315
|
+
const result = handler.parse(data);
|
|
316
|
+
// Remove metadata for backward compatibility
|
|
317
|
+
const { _metadata, ...cleanResult } = result;
|
|
318
|
+
return cleanResult;
|
|
319
|
+
}
|
|
320
|
+
catch (error) {
|
|
321
|
+
throw new Error(`Failed to parse ${format} file ${filename}: ${error}`);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
// Legacy fallback parsing for backward compatibility
|
|
325
|
+
const ext = path.extname(filename).toLowerCase();
|
|
326
|
+
if (ext === ".xml") {
|
|
327
|
+
const parser = new XMLParser({
|
|
328
|
+
ignoreAttributes: false,
|
|
329
|
+
attributeNamePrefix: "@_",
|
|
330
|
+
textNodeName: "#text",
|
|
331
|
+
allowBooleanAttributes: true,
|
|
332
|
+
parseTagValue: true,
|
|
333
|
+
parseAttributeValue: true,
|
|
334
|
+
trimValues: false,
|
|
335
|
+
});
|
|
336
|
+
return parser.parse(data);
|
|
337
|
+
}
|
|
338
|
+
// Default to JSON
|
|
339
|
+
return JSON.parse(data);
|
|
340
|
+
}
|
|
341
|
+
catch (error) {
|
|
342
|
+
throw new Error(`Failed to load translation file for locale ${locale}: ${error}`);
|
|
343
|
+
}
|
|
95
344
|
}
|
|
96
345
|
saveJsonToLocale(locale, file) {
|
|
97
|
-
const filename = `${this.folderPath}/${locale}.
|
|
98
|
-
|
|
346
|
+
const filename = `${this.folderPath}/${locale}${this.fileExt}`;
|
|
347
|
+
try {
|
|
348
|
+
// Use format detection for this specific file
|
|
349
|
+
const format = this.formatOverride || FormatDetector.detectFormat(filename);
|
|
350
|
+
const handler = FormatHandlerFactory.getHandler(format);
|
|
351
|
+
if (handler) {
|
|
352
|
+
try {
|
|
353
|
+
const data = handler.serialize(file, {
|
|
354
|
+
preserveFormatting: true,
|
|
355
|
+
indentation: 2,
|
|
356
|
+
xmlDeclaration: true,
|
|
357
|
+
});
|
|
358
|
+
fs.writeFileSync(filename, data, "utf8");
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
catch (error) {
|
|
362
|
+
throw new Error(`Failed to serialize ${format} file ${filename}: ${error}`);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
// Legacy fallback serialization for backward compatibility
|
|
366
|
+
const ext = path.extname(filename).toLowerCase();
|
|
367
|
+
let data;
|
|
368
|
+
if (ext === ".xml") {
|
|
369
|
+
const builder = new XMLBuilder({
|
|
370
|
+
ignoreAttributes: false,
|
|
371
|
+
attributeNamePrefix: "@_",
|
|
372
|
+
textNodeName: "#text",
|
|
373
|
+
format: true,
|
|
374
|
+
indentBy: " ",
|
|
375
|
+
suppressEmptyNode: true,
|
|
376
|
+
});
|
|
377
|
+
data = builder.build(file);
|
|
378
|
+
}
|
|
379
|
+
else {
|
|
380
|
+
data = JSON.stringify(file, null, " ");
|
|
381
|
+
}
|
|
382
|
+
fs.writeFileSync(filename, data, "utf8");
|
|
383
|
+
}
|
|
384
|
+
catch (error) {
|
|
385
|
+
throw new Error(`Failed to save translation file for locale ${locale}: ${error}`);
|
|
386
|
+
}
|
|
99
387
|
}
|
|
100
388
|
}
|
|
101
|
-
exports.Files = Files;
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { type IFiles } from "./files";
|
|
2
|
-
import type { TranslationFile } from "./translate.interface";
|
|
1
|
+
import { type IFiles } from "./files.js";
|
|
2
|
+
import type { TranslationFile } from "./translate.interface.js";
|
|
3
3
|
export declare class FolderFiles implements IFiles {
|
|
4
4
|
folderPath: string;
|
|
5
5
|
sourceLocale: string;
|
|
6
6
|
targetLocales: Array<string>;
|
|
7
7
|
fileName: string;
|
|
8
|
-
|
|
8
|
+
private formatOverride?;
|
|
9
|
+
constructor(filePath: string, formatOverride?: string);
|
|
10
|
+
getDetectedFormat(): string | undefined;
|
|
11
|
+
getFormatOverride(): string | undefined;
|
|
9
12
|
private getTargetLocales;
|
|
10
13
|
private createFileName;
|
|
11
14
|
loadJsonFromLocale(locale: string): Promise<TranslationFile>;
|
package/build/src/folderFiles.js
CHANGED
|
@@ -1,51 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.FolderFiles = void 0;
|
|
37
|
-
const fs = __importStar(require("node:fs"));
|
|
38
|
-
const path = __importStar(require("node:path"));
|
|
39
|
-
const files_1 = require("./files");
|
|
40
|
-
class FolderFiles {
|
|
41
|
-
constructor(filePath) {
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { loadJsonFromLocale, saveJsonToLocale } from "./files.js";
|
|
4
|
+
import { FormatDetector } from "./format-detector.js";
|
|
5
|
+
export class FolderFiles {
|
|
6
|
+
folderPath;
|
|
7
|
+
sourceLocale;
|
|
8
|
+
targetLocales;
|
|
9
|
+
fileName;
|
|
10
|
+
formatOverride;
|
|
11
|
+
constructor(filePath, formatOverride) {
|
|
42
12
|
const localeDir = path.dirname(filePath);
|
|
43
13
|
this.folderPath = path.dirname(localeDir);
|
|
44
14
|
this.fileName = path.basename(filePath);
|
|
45
15
|
this.sourceLocale = path.basename(localeDir);
|
|
16
|
+
this.formatOverride = formatOverride;
|
|
46
17
|
this.targetLocales = this.getTargetLocales();
|
|
47
18
|
console.log(this.folderPath, this.fileName, this.sourceLocale, this.targetLocales);
|
|
48
19
|
}
|
|
20
|
+
getDetectedFormat() {
|
|
21
|
+
// For folder mode, we detect format from the source file
|
|
22
|
+
const sourceFilePath = this.createFileName(this.sourceLocale);
|
|
23
|
+
try {
|
|
24
|
+
if (fs.existsSync(sourceFilePath)) {
|
|
25
|
+
const content = fs
|
|
26
|
+
.readFileSync(sourceFilePath, "utf8")
|
|
27
|
+
.substring(0, 1000);
|
|
28
|
+
return FormatDetector.detectFormat(sourceFilePath, content);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
console.warn(`Could not detect format for ${sourceFilePath}: ${error}`);
|
|
33
|
+
}
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
getFormatOverride() {
|
|
37
|
+
return this.formatOverride;
|
|
38
|
+
}
|
|
49
39
|
getTargetLocales() {
|
|
50
40
|
const files = fs
|
|
51
41
|
.readdirSync(this.folderPath)
|
|
@@ -58,10 +48,9 @@ class FolderFiles {
|
|
|
58
48
|
return `${this.folderPath}/${locale}/${this.fileName}`;
|
|
59
49
|
}
|
|
60
50
|
loadJsonFromLocale(locale) {
|
|
61
|
-
return
|
|
51
|
+
return loadJsonFromLocale(this.createFileName(locale));
|
|
62
52
|
}
|
|
63
53
|
saveJsonToLocale(locale, file) {
|
|
64
|
-
|
|
54
|
+
saveJsonToLocale(this.createFileName(locale), file);
|
|
65
55
|
}
|
|
66
56
|
}
|
|
67
|
-
exports.FolderFiles = FolderFiles;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { EnhancedTranslationFile, FormatOptions, IFormatHandler, ValidationResult } from "../format.interface.js";
|
|
2
|
+
import type { TranslationFile } from "../translate.interface.js";
|
|
3
|
+
export declare class ArbHandler implements IFormatHandler {
|
|
4
|
+
canHandle(filePath: string, content?: string): boolean;
|
|
5
|
+
parse(content: string): EnhancedTranslationFile;
|
|
6
|
+
serialize(data: EnhancedTranslationFile, options?: FormatOptions): string;
|
|
7
|
+
getFileExtension(): string;
|
|
8
|
+
validateStructure(data: TranslationFile): ValidationResult;
|
|
9
|
+
private isValidArbStructure;
|
|
10
|
+
private hasArbResourcePattern;
|
|
11
|
+
private extractArbComponents;
|
|
12
|
+
private validateAndPreserveResourceMetadata;
|
|
13
|
+
private preservePlaceholderMetadata;
|
|
14
|
+
private sanitizeResourceMetadata;
|
|
15
|
+
private validateArbMetadata;
|
|
16
|
+
private validateResourceStructure;
|
|
17
|
+
private validateResourceMetadata;
|
|
18
|
+
private validatePlaceholders;
|
|
19
|
+
private validateIcuMessages;
|
|
20
|
+
private validateIcuMessageSyntax;
|
|
21
|
+
private validateBracketMatching;
|
|
22
|
+
private validateIcuSyntaxErrors;
|
|
23
|
+
private validateResourceMetadataConsistency;
|
|
24
|
+
private validatePlaceholderConsistency;
|
|
25
|
+
private isValidLocaleCode;
|
|
26
|
+
private analyzeIcuMessages;
|
|
27
|
+
private analyzeIcuMessage;
|
|
28
|
+
private extractIcuPlaceholders;
|
|
29
|
+
private extractPluralForms;
|
|
30
|
+
private extractSelectOptions;
|
|
31
|
+
private validateIcuMessageIntegrity;
|
|
32
|
+
private getIndentation;
|
|
33
|
+
}
|