auto-translate-json-library 1.5.4 → 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 +97 -21
- 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 -63
- 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 +97 -21
|
@@ -0,0 +1,561 @@
|
|
|
1
|
+
import * as path from "node:path";
|
|
2
|
+
import { XMLBuilder, XMLParser } from "fast-xml-parser";
|
|
3
|
+
export class XmlHandler {
|
|
4
|
+
parser;
|
|
5
|
+
builder;
|
|
6
|
+
builderOptions;
|
|
7
|
+
constructor() {
|
|
8
|
+
this.parser = new XMLParser({
|
|
9
|
+
ignoreAttributes: false,
|
|
10
|
+
attributeNamePrefix: "@_",
|
|
11
|
+
textNodeName: "#text",
|
|
12
|
+
allowBooleanAttributes: true,
|
|
13
|
+
parseTagValue: false, // Keep as strings to preserve original values
|
|
14
|
+
parseAttributeValue: false, // Keep attributes as strings
|
|
15
|
+
trimValues: true,
|
|
16
|
+
preserveOrder: false,
|
|
17
|
+
commentPropName: "#comment",
|
|
18
|
+
cdataPropName: "#cdata",
|
|
19
|
+
processEntities: true,
|
|
20
|
+
stopNodes: [], // Don't stop parsing on any nodes
|
|
21
|
+
alwaysCreateTextNode: false,
|
|
22
|
+
});
|
|
23
|
+
this.builderOptions = {
|
|
24
|
+
ignoreAttributes: false,
|
|
25
|
+
attributeNamePrefix: "@_",
|
|
26
|
+
textNodeName: "#text",
|
|
27
|
+
format: true,
|
|
28
|
+
indentBy: " ",
|
|
29
|
+
suppressEmptyNode: false,
|
|
30
|
+
commentPropName: "#comment",
|
|
31
|
+
cdataPropName: "#cdata",
|
|
32
|
+
processEntities: true,
|
|
33
|
+
suppressBooleanAttributes: false,
|
|
34
|
+
};
|
|
35
|
+
this.builder = new XMLBuilder(this.builderOptions);
|
|
36
|
+
}
|
|
37
|
+
canHandle(filePath, content) {
|
|
38
|
+
const extension = path.extname(filePath).toLowerCase();
|
|
39
|
+
if (extension !== ".xml") {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
// If content is provided, check for XML structure
|
|
43
|
+
if (content) {
|
|
44
|
+
const trimmed = content.trim();
|
|
45
|
+
if (trimmed.startsWith("<?xml") || trimmed.startsWith("<")) {
|
|
46
|
+
// Try to parse to validate it's actually XML
|
|
47
|
+
try {
|
|
48
|
+
this.parser.parse(content);
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
parse(content) {
|
|
60
|
+
try {
|
|
61
|
+
// Validate that content is actually XML-like
|
|
62
|
+
const trimmed = content.trim();
|
|
63
|
+
if (!trimmed.startsWith("<?xml") && !trimmed.startsWith("<")) {
|
|
64
|
+
throw new Error("Content does not appear to be valid XML");
|
|
65
|
+
}
|
|
66
|
+
// Basic validation for malformed XML
|
|
67
|
+
this.validateXmlStructure(content);
|
|
68
|
+
const parsed = this.parser.parse(content);
|
|
69
|
+
// Detect XML format type
|
|
70
|
+
const format = this.detectXmlFormat(content, parsed);
|
|
71
|
+
// Transform based on format
|
|
72
|
+
const transformed = this.transformParsedData(parsed, format);
|
|
73
|
+
// Add metadata
|
|
74
|
+
const result = {
|
|
75
|
+
...transformed,
|
|
76
|
+
_metadata: {
|
|
77
|
+
format: format,
|
|
78
|
+
originalStructure: parsed,
|
|
79
|
+
preserveComments: true,
|
|
80
|
+
preserveAttributes: true,
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
throw new Error(`Failed to parse XML: ${error instanceof Error ? error.message : String(error)}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
serialize(data, options) {
|
|
90
|
+
try {
|
|
91
|
+
// Use original structure if available, otherwise reconstruct
|
|
92
|
+
let xmlData;
|
|
93
|
+
if (data._metadata?.originalStructure) {
|
|
94
|
+
// Update the original structure with translated values
|
|
95
|
+
xmlData = this.updateOriginalStructure(data._metadata.originalStructure, data);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
// Reconstruct XML structure
|
|
99
|
+
xmlData = this.reconstructXmlStructure(data);
|
|
100
|
+
}
|
|
101
|
+
// Apply formatting options
|
|
102
|
+
let currentBuilder = this.builder;
|
|
103
|
+
if (options?.indentation) {
|
|
104
|
+
currentBuilder = new XMLBuilder({
|
|
105
|
+
...this.builderOptions,
|
|
106
|
+
indentBy: typeof options.indentation === "string"
|
|
107
|
+
? options.indentation
|
|
108
|
+
: " ".repeat(options.indentation),
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
let result = currentBuilder.build(xmlData);
|
|
112
|
+
// Add XML declaration if needed
|
|
113
|
+
if (options?.xmlDeclaration !== false && !result.startsWith("<?xml")) {
|
|
114
|
+
result = `<?xml version="1.0" encoding="UTF-8"?>\n${result}`;
|
|
115
|
+
}
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
throw new Error(`Failed to serialize XML: ${error instanceof Error ? error.message : String(error)}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
getFileExtension() {
|
|
123
|
+
return ".xml";
|
|
124
|
+
}
|
|
125
|
+
validateStructure(data) {
|
|
126
|
+
const errors = [];
|
|
127
|
+
const warnings = [];
|
|
128
|
+
// Basic XML structure validation
|
|
129
|
+
if (!data || typeof data !== "object") {
|
|
130
|
+
errors.push({
|
|
131
|
+
code: "INVALID_STRUCTURE",
|
|
132
|
+
message: "XML data must be an object",
|
|
133
|
+
});
|
|
134
|
+
return { isValid: false, errors, warnings };
|
|
135
|
+
}
|
|
136
|
+
// Check if this is an EnhancedTranslationFile with metadata
|
|
137
|
+
const enhancedData = data;
|
|
138
|
+
if (enhancedData._metadata?.originalStructure) {
|
|
139
|
+
// Validate the original structure
|
|
140
|
+
const originalStructure = enhancedData._metadata.originalStructure;
|
|
141
|
+
if (originalStructure.resources !== undefined) {
|
|
142
|
+
// Android strings.xml format
|
|
143
|
+
this.validateAndroidFormat(originalStructure, errors, warnings);
|
|
144
|
+
}
|
|
145
|
+
else if (originalStructure.plist !== undefined) {
|
|
146
|
+
// iOS plist format
|
|
147
|
+
this.validateIosFormat(originalStructure, errors, warnings);
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
// Generic XML format
|
|
151
|
+
this.validateGenericXmlFormat(originalStructure, errors, warnings);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
// Validate the data directly (for test cases that pass raw structures)
|
|
156
|
+
if (data.resources !== undefined) {
|
|
157
|
+
// Android strings.xml format
|
|
158
|
+
this.validateAndroidFormat(data, errors, warnings);
|
|
159
|
+
}
|
|
160
|
+
else if (data.plist !== undefined) {
|
|
161
|
+
// iOS plist format
|
|
162
|
+
this.validateIosFormat(data, errors, warnings);
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
// Check if this looks like Android XML without proper structure
|
|
166
|
+
if (this.looksLikeAndroidXml(data)) {
|
|
167
|
+
this.validateAndroidFormat(data, errors, warnings);
|
|
168
|
+
}
|
|
169
|
+
// Check if this looks like iOS XML without proper structure
|
|
170
|
+
else if (this.looksLikeIosXml(data)) {
|
|
171
|
+
this.validateIosFormat(data, errors, warnings);
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
// Check if it's empty data (should be valid but with warnings)
|
|
175
|
+
const keys = Object.keys(data);
|
|
176
|
+
if (keys.length === 0) {
|
|
177
|
+
warnings.push({
|
|
178
|
+
code: "EMPTY_XML",
|
|
179
|
+
message: "XML file appears to be empty or contains no translatable content",
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
// Generic XML format or flattened data
|
|
184
|
+
this.validateGenericXmlFormat(data, errors, warnings);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return {
|
|
190
|
+
isValid: errors.length === 0,
|
|
191
|
+
errors,
|
|
192
|
+
warnings,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
detectXmlFormat(content, parsed) {
|
|
196
|
+
// Check content for XML format indicators
|
|
197
|
+
if (content.includes("<resources") || parsed.resources !== undefined) {
|
|
198
|
+
return "android-xml";
|
|
199
|
+
}
|
|
200
|
+
if (content.includes("<plist") || parsed.plist !== undefined) {
|
|
201
|
+
return "ios-xml";
|
|
202
|
+
}
|
|
203
|
+
return "generic-xml";
|
|
204
|
+
}
|
|
205
|
+
transformParsedData(parsed, format) {
|
|
206
|
+
switch (format) {
|
|
207
|
+
case "android-xml":
|
|
208
|
+
return this.transformAndroidXml(parsed);
|
|
209
|
+
case "ios-xml":
|
|
210
|
+
return this.transformIosXml(parsed);
|
|
211
|
+
default:
|
|
212
|
+
return this.transformGenericXml(parsed);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
transformAndroidXml(parsed) {
|
|
216
|
+
const result = {};
|
|
217
|
+
if (!parsed.resources) {
|
|
218
|
+
return result;
|
|
219
|
+
}
|
|
220
|
+
const resources = parsed.resources;
|
|
221
|
+
// If resources is empty or just an empty object, return empty result
|
|
222
|
+
if (typeof resources === "object" && Object.keys(resources).length === 0) {
|
|
223
|
+
return result;
|
|
224
|
+
}
|
|
225
|
+
// Handle string elements
|
|
226
|
+
if (resources.string) {
|
|
227
|
+
const strings = Array.isArray(resources.string)
|
|
228
|
+
? resources.string
|
|
229
|
+
: [resources.string];
|
|
230
|
+
for (const str of strings) {
|
|
231
|
+
if (str["@_name"]) {
|
|
232
|
+
const key = str["@_name"];
|
|
233
|
+
// Handle different value formats: #text, #cdata, or direct string
|
|
234
|
+
let value = str["#text"] || str["#cdata"];
|
|
235
|
+
// If no #text or #cdata, check if the string element itself is the value
|
|
236
|
+
if (value === undefined) {
|
|
237
|
+
// Check if str is a simple object with just attributes
|
|
238
|
+
const nonAttributeKeys = Object.keys(str).filter((k) => !k.startsWith("@_"));
|
|
239
|
+
if (nonAttributeKeys.length === 0) {
|
|
240
|
+
// Empty element
|
|
241
|
+
value = "";
|
|
242
|
+
}
|
|
243
|
+
else if (nonAttributeKeys.length === 1 &&
|
|
244
|
+
nonAttributeKeys[0] === "#text") {
|
|
245
|
+
value = str["#text"];
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
// Complex content, try to get text content
|
|
249
|
+
value = str["#text"] || "";
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
// Ensure we have a string value
|
|
253
|
+
if (value === undefined || value === null) {
|
|
254
|
+
value = "";
|
|
255
|
+
}
|
|
256
|
+
if (typeof value === "string") {
|
|
257
|
+
result[key] = value;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
// Handle group elements (nested structure)
|
|
263
|
+
if (resources.group) {
|
|
264
|
+
const groups = Array.isArray(resources.group)
|
|
265
|
+
? resources.group
|
|
266
|
+
: [resources.group];
|
|
267
|
+
for (const group of groups) {
|
|
268
|
+
if (group["@_name"] && group.string) {
|
|
269
|
+
const groupName = group["@_name"];
|
|
270
|
+
result[groupName] = {};
|
|
271
|
+
const groupStrings = Array.isArray(group.string)
|
|
272
|
+
? group.string
|
|
273
|
+
: [group.string];
|
|
274
|
+
for (const str of groupStrings) {
|
|
275
|
+
if (str["@_name"]) {
|
|
276
|
+
const key = str["@_name"];
|
|
277
|
+
let value = str["#text"] || str["#cdata"];
|
|
278
|
+
if (value === undefined) {
|
|
279
|
+
const nonAttributeKeys = Object.keys(str).filter((k) => !k.startsWith("@_"));
|
|
280
|
+
if (nonAttributeKeys.length === 0) {
|
|
281
|
+
value = "";
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
value = str["#text"] || "";
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
if (value === undefined || value === null) {
|
|
288
|
+
value = "";
|
|
289
|
+
}
|
|
290
|
+
if (typeof value === "string") {
|
|
291
|
+
result[groupName][key] = value;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return result;
|
|
299
|
+
}
|
|
300
|
+
transformIosXml(parsed) {
|
|
301
|
+
const result = {};
|
|
302
|
+
if (!parsed.plist || !parsed.plist.dict) {
|
|
303
|
+
return result;
|
|
304
|
+
}
|
|
305
|
+
// iOS plist format has key-value pairs in dict
|
|
306
|
+
const dict = parsed.plist.dict;
|
|
307
|
+
if (dict.key && dict.string) {
|
|
308
|
+
const keys = Array.isArray(dict.key) ? dict.key : [dict.key];
|
|
309
|
+
const values = Array.isArray(dict.string) ? dict.string : [dict.string];
|
|
310
|
+
for (let i = 0; i < keys.length && i < values.length; i++) {
|
|
311
|
+
const key = keys[i]["#text"] || keys[i];
|
|
312
|
+
const value = values[i]["#text"] || values[i];
|
|
313
|
+
if (typeof key === "string" && typeof value === "string") {
|
|
314
|
+
result[key] = value;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return result;
|
|
319
|
+
}
|
|
320
|
+
transformGenericXml(parsed) {
|
|
321
|
+
// For generic XML, flatten the structure while preserving hierarchy
|
|
322
|
+
// Skip the XML declaration and start from the root element
|
|
323
|
+
const rootKeys = Object.keys(parsed).filter((key) => !key.startsWith("?xml") && !key.startsWith("@_"));
|
|
324
|
+
if (rootKeys.length === 1) {
|
|
325
|
+
// If there's only one root element, flatten from that element
|
|
326
|
+
const rootElement = parsed[rootKeys[0]];
|
|
327
|
+
return this.flattenXmlStructure(rootElement);
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
// Multiple root elements, flatten the whole structure
|
|
331
|
+
return this.flattenXmlStructure(parsed);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
flattenXmlStructure(obj, prefix = "") {
|
|
335
|
+
const result = {};
|
|
336
|
+
if (!obj || typeof obj !== "object") {
|
|
337
|
+
return result;
|
|
338
|
+
}
|
|
339
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
340
|
+
if (key.startsWith("@_") || key.startsWith("#")) {
|
|
341
|
+
// Skip attributes and special properties for now
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
const newKey = prefix ? `${prefix}.${key}` : key;
|
|
345
|
+
if (typeof value === "string") {
|
|
346
|
+
result[newKey] = value;
|
|
347
|
+
}
|
|
348
|
+
else if (typeof value === "object" && value !== null) {
|
|
349
|
+
const objValue = value;
|
|
350
|
+
if (objValue["#text"] !== undefined) {
|
|
351
|
+
result[newKey] = objValue["#text"];
|
|
352
|
+
}
|
|
353
|
+
else if (objValue["#cdata"] !== undefined) {
|
|
354
|
+
result[newKey] = objValue["#cdata"];
|
|
355
|
+
}
|
|
356
|
+
else if (Array.isArray(objValue)) {
|
|
357
|
+
// Handle arrays - for now, just skip them in generic XML
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
// Recursively flatten nested objects
|
|
362
|
+
Object.assign(result, this.flattenXmlStructure(value, newKey));
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return result;
|
|
367
|
+
}
|
|
368
|
+
updateOriginalStructure(original, translated) {
|
|
369
|
+
// Deep clone the original structure
|
|
370
|
+
const updated = JSON.parse(JSON.stringify(original));
|
|
371
|
+
// Update translated values while preserving structure
|
|
372
|
+
this.updateTranslatedValues(updated, translated);
|
|
373
|
+
return updated;
|
|
374
|
+
}
|
|
375
|
+
updateTranslatedValues(structure, translations, path = "") {
|
|
376
|
+
if (Array.isArray(structure)) {
|
|
377
|
+
// Handle arrays (like string elements in Android XML)
|
|
378
|
+
for (const item of structure) {
|
|
379
|
+
if (item["@_name"] && typeof item["@_name"] === "string") {
|
|
380
|
+
const key = item["@_name"];
|
|
381
|
+
if (translations[key] !== undefined) {
|
|
382
|
+
// Update the text content
|
|
383
|
+
if (item["#text"] !== undefined) {
|
|
384
|
+
item["#text"] = translations[key];
|
|
385
|
+
}
|
|
386
|
+
else if (item["#cdata"] !== undefined) {
|
|
387
|
+
item["#cdata"] = translations[key];
|
|
388
|
+
}
|
|
389
|
+
else {
|
|
390
|
+
// If it's a direct string value
|
|
391
|
+
if (typeof item === "string") {
|
|
392
|
+
// This case is handled by the parent
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
item["#text"] = translations[key];
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
// Recursively update nested structures
|
|
401
|
+
this.updateTranslatedValues(item, translations, path);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
else if (typeof structure === "object" && structure !== null) {
|
|
405
|
+
// Special handling for iOS plist format
|
|
406
|
+
if (structure.key &&
|
|
407
|
+
structure.string &&
|
|
408
|
+
Array.isArray(structure.key) &&
|
|
409
|
+
Array.isArray(structure.string)) {
|
|
410
|
+
// Update iOS plist key-value pairs
|
|
411
|
+
for (let i = 0; i < structure.key.length && i < structure.string.length; i++) {
|
|
412
|
+
const keyObj = structure.key[i];
|
|
413
|
+
const stringObj = structure.string[i];
|
|
414
|
+
const keyName = keyObj["#text"] || keyObj;
|
|
415
|
+
if (typeof keyName === "string" &&
|
|
416
|
+
translations[keyName] !== undefined) {
|
|
417
|
+
if (stringObj["#text"] !== undefined) {
|
|
418
|
+
stringObj["#text"] = translations[keyName];
|
|
419
|
+
}
|
|
420
|
+
else if (typeof stringObj === "string") {
|
|
421
|
+
structure.string[i] = translations[keyName];
|
|
422
|
+
}
|
|
423
|
+
else {
|
|
424
|
+
stringObj["#text"] = translations[keyName];
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
for (const [key, value] of Object.entries(structure)) {
|
|
430
|
+
if (key.startsWith("@_") || key.startsWith("#")) {
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
433
|
+
const currentPath = path ? `${path}.${key}` : key;
|
|
434
|
+
if (typeof value === "object" && value !== null) {
|
|
435
|
+
this.updateTranslatedValues(value, translations, currentPath);
|
|
436
|
+
}
|
|
437
|
+
else if (typeof value === "string" && translations[currentPath]) {
|
|
438
|
+
structure[key] = translations[currentPath];
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
reconstructXmlStructure(data) {
|
|
444
|
+
// Remove metadata for reconstruction
|
|
445
|
+
const { _metadata, ...cleanData } = data;
|
|
446
|
+
// We need to separate strings and groups
|
|
447
|
+
const strings = [];
|
|
448
|
+
const groups = [];
|
|
449
|
+
for (const [key, value] of Object.entries(cleanData)) {
|
|
450
|
+
if (typeof value === "object" && value !== null) {
|
|
451
|
+
// handle group
|
|
452
|
+
const groupStrings = Object.entries(value).map(([gKey, gValue]) => ({
|
|
453
|
+
"@_name": gKey,
|
|
454
|
+
"#text": String(gValue),
|
|
455
|
+
}));
|
|
456
|
+
groups.push({
|
|
457
|
+
"@_name": key,
|
|
458
|
+
string: groupStrings,
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
else {
|
|
462
|
+
// handle string
|
|
463
|
+
strings.push({
|
|
464
|
+
"@_name": key,
|
|
465
|
+
"#text": String(value),
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
const resources = {};
|
|
470
|
+
if (strings.length > 0)
|
|
471
|
+
resources.string = strings;
|
|
472
|
+
if (groups.length > 0)
|
|
473
|
+
resources.group = groups;
|
|
474
|
+
return { resources };
|
|
475
|
+
}
|
|
476
|
+
validateAndroidFormat(data, errors, warnings) {
|
|
477
|
+
if (!data || typeof data !== "object" || data.resources === undefined) {
|
|
478
|
+
errors.push({
|
|
479
|
+
code: "MISSING_RESOURCES",
|
|
480
|
+
message: "Android XML must have a 'resources' root element",
|
|
481
|
+
});
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
const resources = data.resources;
|
|
485
|
+
// Check for string elements - resources can be empty string or empty object
|
|
486
|
+
if (typeof resources === "string" ||
|
|
487
|
+
(typeof resources === "object" && !resources.string && !resources.group)) {
|
|
488
|
+
warnings.push({
|
|
489
|
+
code: "NO_STRINGS",
|
|
490
|
+
message: "No string elements found in resources",
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
validateIosFormat(data, errors, warnings) {
|
|
495
|
+
if (!data || typeof data !== "object" || data.plist === undefined) {
|
|
496
|
+
errors.push({
|
|
497
|
+
code: "MISSING_PLIST",
|
|
498
|
+
message: "iOS XML must have a 'plist' root element",
|
|
499
|
+
});
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
if (typeof data.plist === "object" &&
|
|
503
|
+
data.plist !== null &&
|
|
504
|
+
!data.plist.dict) {
|
|
505
|
+
errors.push({
|
|
506
|
+
code: "MISSING_DICT",
|
|
507
|
+
message: "iOS plist must contain a 'dict' element",
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
validateGenericXmlFormat(data, errors, warnings) {
|
|
512
|
+
// Generic validation - just ensure it's a valid object
|
|
513
|
+
if (Object.keys(data).length === 0) {
|
|
514
|
+
warnings.push({
|
|
515
|
+
code: "EMPTY_XML",
|
|
516
|
+
message: "XML file appears to be empty or contains no translatable content",
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
validateXmlStructure(content) {
|
|
521
|
+
// Basic XML structure validation
|
|
522
|
+
const trimmed = content.trim();
|
|
523
|
+
// Check for basic XML structure issues
|
|
524
|
+
if (trimmed.includes("<") && !trimmed.includes(">")) {
|
|
525
|
+
throw new Error("Malformed XML: unclosed tag detected");
|
|
526
|
+
}
|
|
527
|
+
// Count opening and closing tags (basic check)
|
|
528
|
+
const openTags = (trimmed.match(/<[^/!?][^>]*>/g) || []).length;
|
|
529
|
+
const closeTags = (trimmed.match(/<\/[^>]+>/g) || []).length;
|
|
530
|
+
const selfClosingTags = (trimmed.match(/<[^>]*\/>/g) || []).length;
|
|
531
|
+
// For proper XML, opening tags should equal closing tags + self-closing tags
|
|
532
|
+
// Allow for XML declaration and DOCTYPE
|
|
533
|
+
const xmlDeclaration = trimmed.match(/<\?xml[^>]*\?>/g) || [];
|
|
534
|
+
const doctype = trimmed.match(/<!DOCTYPE[^>]*>/g) || [];
|
|
535
|
+
const comments = trimmed.match(/<!--[^>]*-->/g) || [];
|
|
536
|
+
const expectedClosingTags = openTags -
|
|
537
|
+
selfClosingTags -
|
|
538
|
+
xmlDeclaration.length -
|
|
539
|
+
doctype.length -
|
|
540
|
+
comments.length;
|
|
541
|
+
if (expectedClosingTags > closeTags) {
|
|
542
|
+
throw new Error("Malformed XML: unclosed tags detected");
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
looksLikeAndroidXml(data) {
|
|
546
|
+
// Check if it has Android XML characteristics without proper structure
|
|
547
|
+
return ((data.string !== undefined &&
|
|
548
|
+
Array.isArray(data.string) &&
|
|
549
|
+
data.string.some((item) => item && typeof item === "object" && item["@_name"])) ||
|
|
550
|
+
(data.group !== undefined &&
|
|
551
|
+
Array.isArray(data.group) &&
|
|
552
|
+
data.group.some((item) => item && typeof item === "object" && item["@_name"])));
|
|
553
|
+
}
|
|
554
|
+
looksLikeIosXml(data) {
|
|
555
|
+
// Check if it has iOS XML characteristics without proper structure
|
|
556
|
+
return (data.dict !== undefined &&
|
|
557
|
+
typeof data.dict === "object" &&
|
|
558
|
+
data.dict !== null &&
|
|
559
|
+
(data.dict.key !== undefined || data.dict.string !== undefined));
|
|
560
|
+
}
|
|
561
|
+
}
|
|
@@ -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 XtbHandler implements IFormatHandler {
|
|
4
|
+
private parser;
|
|
5
|
+
private builder;
|
|
6
|
+
constructor();
|
|
7
|
+
canHandle(filePath: string, content?: string): boolean;
|
|
8
|
+
parse(content: string): EnhancedTranslationFile;
|
|
9
|
+
serialize(data: EnhancedTranslationFile, options?: FormatOptions): string;
|
|
10
|
+
getFileExtension(): string;
|
|
11
|
+
validateStructure(data: TranslationFile): ValidationResult;
|
|
12
|
+
/**
|
|
13
|
+
* Update existing XTB file with new translations while preserving message references
|
|
14
|
+
*/
|
|
15
|
+
updateXtbTranslations(existingXtbContent: string, newTranslations: TranslationFile): string;
|
|
16
|
+
private preservePlaceholders;
|
|
17
|
+
private validatePlaceholders;
|
|
18
|
+
/**
|
|
19
|
+
* Extract and validate placeholder information from translation text
|
|
20
|
+
*/
|
|
21
|
+
private extractPlaceholderInfo;
|
|
22
|
+
/**
|
|
23
|
+
* Validate translation integrity by checking placeholder consistency with original message
|
|
24
|
+
*/
|
|
25
|
+
validateTranslationIntegrity(originalText: string, translatedText: string): ValidationResult;
|
|
26
|
+
private extractTranslationMetadata;
|
|
27
|
+
private updateOriginalStructure;
|
|
28
|
+
private reconstructXtbStructure;
|
|
29
|
+
/**
|
|
30
|
+
* Extract text content from mixed XML content (text + elements)
|
|
31
|
+
*/
|
|
32
|
+
private extractMixedContent;
|
|
33
|
+
}
|