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,518 @@
|
|
|
1
|
+
import * as path from "node:path";
|
|
2
|
+
import { XMLBuilder, XMLParser } from "fast-xml-parser";
|
|
3
|
+
export class XtbHandler {
|
|
4
|
+
parser;
|
|
5
|
+
builder;
|
|
6
|
+
constructor() {
|
|
7
|
+
this.parser = new XMLParser({
|
|
8
|
+
ignoreAttributes: false,
|
|
9
|
+
attributeNamePrefix: "@_",
|
|
10
|
+
textNodeName: "#text",
|
|
11
|
+
allowBooleanAttributes: true,
|
|
12
|
+
parseTagValue: false, // Keep as strings to preserve original values
|
|
13
|
+
parseAttributeValue: false, // Keep attributes as strings
|
|
14
|
+
trimValues: true,
|
|
15
|
+
preserveOrder: false,
|
|
16
|
+
commentPropName: "#comment",
|
|
17
|
+
cdataPropName: "#cdata",
|
|
18
|
+
processEntities: true,
|
|
19
|
+
});
|
|
20
|
+
this.builder = new XMLBuilder({
|
|
21
|
+
ignoreAttributes: false,
|
|
22
|
+
attributeNamePrefix: "@_",
|
|
23
|
+
textNodeName: "#text",
|
|
24
|
+
format: true,
|
|
25
|
+
indentBy: " ",
|
|
26
|
+
suppressEmptyNode: false,
|
|
27
|
+
commentPropName: "#comment",
|
|
28
|
+
cdataPropName: "#cdata",
|
|
29
|
+
processEntities: true,
|
|
30
|
+
suppressBooleanAttributes: false,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
canHandle(filePath, content) {
|
|
34
|
+
const extension = path.extname(filePath).toLowerCase();
|
|
35
|
+
if (extension !== ".xtb") {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
// If content is provided, check for XTB structure
|
|
39
|
+
if (content) {
|
|
40
|
+
const trimmed = content.trim();
|
|
41
|
+
if (trimmed.includes("<translationbundle")) {
|
|
42
|
+
try {
|
|
43
|
+
const parsed = this.parser.parse(content);
|
|
44
|
+
return parsed.translationbundle !== undefined;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
parse(content) {
|
|
55
|
+
try {
|
|
56
|
+
const parsed = this.parser.parse(content);
|
|
57
|
+
if (!parsed.translationbundle) {
|
|
58
|
+
throw new Error("Invalid XTB format: missing translationbundle root element");
|
|
59
|
+
}
|
|
60
|
+
const result = {};
|
|
61
|
+
const translationbundle = parsed.translationbundle;
|
|
62
|
+
// Extract translations from the bundle
|
|
63
|
+
if (translationbundle.translation) {
|
|
64
|
+
const translations = Array.isArray(translationbundle.translation)
|
|
65
|
+
? translationbundle.translation
|
|
66
|
+
: [translationbundle.translation];
|
|
67
|
+
for (const translation of translations) {
|
|
68
|
+
const xtbTranslation = translation;
|
|
69
|
+
if (xtbTranslation["@_id"]) {
|
|
70
|
+
const messageId = xtbTranslation["@_id"];
|
|
71
|
+
let translationText = "";
|
|
72
|
+
// Handle different content structures
|
|
73
|
+
if (typeof translation === "string") {
|
|
74
|
+
translationText = translation;
|
|
75
|
+
}
|
|
76
|
+
else if (translation.ph || translation.ex) {
|
|
77
|
+
// Handle mixed content with XML elements (has placeholders or examples)
|
|
78
|
+
translationText = this.extractMixedContent(translation);
|
|
79
|
+
}
|
|
80
|
+
else if (translation["#text"]) {
|
|
81
|
+
translationText = translation["#text"];
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
// Fallback to mixed content extraction
|
|
85
|
+
translationText = this.extractMixedContent(translation);
|
|
86
|
+
}
|
|
87
|
+
// Handle placeholder variables in the translation text
|
|
88
|
+
translationText = this.preservePlaceholders(translationText);
|
|
89
|
+
result[messageId] = translationText;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// Create enhanced translation file with XTB metadata
|
|
94
|
+
const enhancedResult = {
|
|
95
|
+
...result,
|
|
96
|
+
_metadata: {
|
|
97
|
+
format: "xtb",
|
|
98
|
+
originalStructure: parsed,
|
|
99
|
+
preserveComments: true,
|
|
100
|
+
preserveAttributes: true,
|
|
101
|
+
xtbMetadata: {
|
|
102
|
+
language: translationbundle["@_lang"] || "en",
|
|
103
|
+
version: translationbundle["@_version"] || "1.0",
|
|
104
|
+
translations: this.extractTranslationMetadata(translationbundle.translation),
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
return enhancedResult;
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
throw new Error(`Failed to parse XTB: ${error instanceof Error ? error.message : String(error)}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
serialize(data, options) {
|
|
115
|
+
try {
|
|
116
|
+
// Use original structure if available, otherwise reconstruct
|
|
117
|
+
let xtbData;
|
|
118
|
+
if (data._metadata?.originalStructure) {
|
|
119
|
+
// Update the original structure with translated values
|
|
120
|
+
xtbData = this.updateOriginalStructure(data._metadata.originalStructure, data);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
// Reconstruct XTB structure
|
|
124
|
+
xtbData = this.reconstructXtbStructure(data);
|
|
125
|
+
}
|
|
126
|
+
// Apply formatting options
|
|
127
|
+
if (options?.indentation) {
|
|
128
|
+
this.builder = new XMLBuilder({
|
|
129
|
+
...this.builder,
|
|
130
|
+
indentBy: typeof options.indentation === "string"
|
|
131
|
+
? options.indentation
|
|
132
|
+
: " ".repeat(options.indentation),
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
let result = this.builder.build(xtbData);
|
|
136
|
+
// Add XML declaration if needed
|
|
137
|
+
if (options?.xmlDeclaration !== false && !result.startsWith("<?xml")) {
|
|
138
|
+
result = `<?xml version="1.0" encoding="UTF-8"?>\n${result}`;
|
|
139
|
+
}
|
|
140
|
+
return result;
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
throw new Error(`Failed to serialize XTB: ${error instanceof Error ? error.message : String(error)}`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
getFileExtension() {
|
|
147
|
+
return ".xtb";
|
|
148
|
+
}
|
|
149
|
+
validateStructure(data) {
|
|
150
|
+
const errors = [];
|
|
151
|
+
const warnings = [];
|
|
152
|
+
// Basic XTB structure validation
|
|
153
|
+
if (!data || typeof data !== "object") {
|
|
154
|
+
errors.push({
|
|
155
|
+
code: "INVALID_STRUCTURE",
|
|
156
|
+
message: "XTB data must be an object",
|
|
157
|
+
});
|
|
158
|
+
return { isValid: false, errors, warnings };
|
|
159
|
+
}
|
|
160
|
+
const enhancedData = data;
|
|
161
|
+
if (enhancedData._metadata?.originalStructure) {
|
|
162
|
+
const originalStructure = enhancedData._metadata.originalStructure;
|
|
163
|
+
if (!originalStructure.translationbundle) {
|
|
164
|
+
errors.push({
|
|
165
|
+
code: "MISSING_TRANSLATIONBUNDLE",
|
|
166
|
+
message: "XTB must have a 'translationbundle' root element",
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
const translationbundle = originalStructure.translationbundle;
|
|
171
|
+
// Validate required attributes
|
|
172
|
+
if (!translationbundle["@_lang"]) {
|
|
173
|
+
errors.push({
|
|
174
|
+
code: "MISSING_LANGUAGE",
|
|
175
|
+
message: "XTB translationbundle must have a lang attribute",
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
// Validate translations
|
|
179
|
+
if (!translationbundle.translation) {
|
|
180
|
+
warnings.push({
|
|
181
|
+
code: "NO_TRANSLATIONS",
|
|
182
|
+
message: "No translations found in XTB file",
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
const translations = Array.isArray(translationbundle.translation)
|
|
187
|
+
? translationbundle.translation
|
|
188
|
+
: [translationbundle.translation];
|
|
189
|
+
for (const translation of translations) {
|
|
190
|
+
if (!translation["@_id"]) {
|
|
191
|
+
errors.push({
|
|
192
|
+
code: "MISSING_TRANSLATION_ID",
|
|
193
|
+
message: "XTB translation must have an id attribute",
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
// Validate placeholder integrity
|
|
197
|
+
const placeholderErrors = this.validatePlaceholders(translation["#text"] || "");
|
|
198
|
+
errors.push(...placeholderErrors);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return {
|
|
204
|
+
isValid: errors.length === 0,
|
|
205
|
+
errors,
|
|
206
|
+
warnings,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Update existing XTB file with new translations while preserving message references
|
|
211
|
+
*/
|
|
212
|
+
updateXtbTranslations(existingXtbContent, newTranslations) {
|
|
213
|
+
try {
|
|
214
|
+
const parsed = this.parser.parse(existingXtbContent);
|
|
215
|
+
if (!parsed.translationbundle) {
|
|
216
|
+
throw new Error("Invalid XTB format: missing translationbundle root element");
|
|
217
|
+
}
|
|
218
|
+
const translationbundle = parsed.translationbundle;
|
|
219
|
+
if (translationbundle.translation) {
|
|
220
|
+
const translations = Array.isArray(translationbundle.translation)
|
|
221
|
+
? translationbundle.translation
|
|
222
|
+
: [translationbundle.translation];
|
|
223
|
+
// Update existing translations
|
|
224
|
+
for (const translation of translations) {
|
|
225
|
+
const messageId = translation["@_id"];
|
|
226
|
+
if (messageId && newTranslations[messageId] !== undefined) {
|
|
227
|
+
translation["#text"] = this.preservePlaceholders(newTranslations[messageId]);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
// Add new translations that don't exist yet
|
|
231
|
+
const existingIds = new Set(translations.map((t) => t["@_id"]));
|
|
232
|
+
const newTranslationEntries = Object.entries(newTranslations)
|
|
233
|
+
.filter(([id]) => !existingIds.has(id))
|
|
234
|
+
.map(([id, text]) => ({
|
|
235
|
+
"@_id": id,
|
|
236
|
+
"#text": this.preservePlaceholders(text),
|
|
237
|
+
}));
|
|
238
|
+
if (newTranslationEntries.length > 0) {
|
|
239
|
+
if (Array.isArray(translationbundle.translation)) {
|
|
240
|
+
translationbundle.translation.push(...newTranslationEntries);
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
translationbundle.translation = [
|
|
244
|
+
translationbundle.translation,
|
|
245
|
+
...newTranslationEntries,
|
|
246
|
+
];
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
// Create new translations array
|
|
252
|
+
translationbundle.translation = Object.entries(newTranslations).map(([id, text]) => ({
|
|
253
|
+
"@_id": id,
|
|
254
|
+
"#text": this.preservePlaceholders(text),
|
|
255
|
+
}));
|
|
256
|
+
}
|
|
257
|
+
let result = this.builder.build(parsed);
|
|
258
|
+
// Add XML declaration if needed
|
|
259
|
+
if (!result.startsWith("<?xml")) {
|
|
260
|
+
result = `<?xml version="1.0" encoding="UTF-8"?>\n${result}`;
|
|
261
|
+
}
|
|
262
|
+
return result;
|
|
263
|
+
}
|
|
264
|
+
catch (error) {
|
|
265
|
+
throw new Error(`Failed to update XTB translations: ${error instanceof Error ? error.message : String(error)}`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
preservePlaceholders(text) {
|
|
269
|
+
// XTB uses various placeholder syntaxes:
|
|
270
|
+
// - {$placeholder} - Angular i18n style
|
|
271
|
+
// - {VAR_NAME} - Variable placeholders
|
|
272
|
+
// - <ph name="PLACEHOLDER_NAME">value</ph> - XML placeholder elements
|
|
273
|
+
// - <ex>example</ex> - Example elements
|
|
274
|
+
// Preserve these during translation
|
|
275
|
+
return text;
|
|
276
|
+
}
|
|
277
|
+
validatePlaceholders(text) {
|
|
278
|
+
const errors = [];
|
|
279
|
+
// Check for unmatched braces in placeholder syntax
|
|
280
|
+
const openBraces = (text.match(/\{/g) || []).length;
|
|
281
|
+
const closeBraces = (text.match(/\}/g) || []).length;
|
|
282
|
+
if (openBraces !== closeBraces) {
|
|
283
|
+
errors.push({
|
|
284
|
+
code: "UNMATCHED_PLACEHOLDER_BRACES",
|
|
285
|
+
message: `Unmatched placeholder braces in translation: ${text}`,
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
// Check for unmatched XML placeholder elements
|
|
289
|
+
const phOpenTags = (text.match(/<ph\s+[^>]*>/g) || []).length;
|
|
290
|
+
const phCloseTags = (text.match(/<\/ph>/g) || []).length;
|
|
291
|
+
if (phOpenTags !== phCloseTags) {
|
|
292
|
+
errors.push({
|
|
293
|
+
code: "UNMATCHED_PH_TAGS",
|
|
294
|
+
message: `Unmatched <ph> placeholder tags in translation: ${text}`,
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
// Check for unmatched example elements
|
|
298
|
+
const exOpenTags = (text.match(/<ex>/g) || []).length;
|
|
299
|
+
const exCloseTags = (text.match(/<\/ex>/g) || []).length;
|
|
300
|
+
if (exOpenTags !== exCloseTags) {
|
|
301
|
+
errors.push({
|
|
302
|
+
code: "UNMATCHED_EX_TAGS",
|
|
303
|
+
message: `Unmatched <ex> example tags in translation: ${text}`,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
// Validate placeholder name attributes
|
|
307
|
+
const phTags = text.match(/<ph\s+name="([^"]*)"[^>]*>/g);
|
|
308
|
+
if (phTags) {
|
|
309
|
+
for (const tag of phTags) {
|
|
310
|
+
const nameMatch = tag.match(/name="([^"]*)"/);
|
|
311
|
+
if (nameMatch && !nameMatch[1]) {
|
|
312
|
+
errors.push({
|
|
313
|
+
code: "EMPTY_PLACEHOLDER_NAME",
|
|
314
|
+
message: `Empty placeholder name in tag: ${tag}`,
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return errors;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Extract and validate placeholder information from translation text
|
|
323
|
+
*/
|
|
324
|
+
extractPlaceholderInfo(text) {
|
|
325
|
+
const variables = [];
|
|
326
|
+
const phElements = [];
|
|
327
|
+
const examples = [];
|
|
328
|
+
// Extract {$variable} and {VARIABLE} patterns
|
|
329
|
+
const variableMatches = text.match(/\{[^}]+\}/g);
|
|
330
|
+
if (variableMatches) {
|
|
331
|
+
variables.push(...variableMatches);
|
|
332
|
+
}
|
|
333
|
+
// Extract <ph> elements
|
|
334
|
+
const phMatches = text.match(/<ph\s+name="([^"]*)"[^>]*>(.*?)<\/ph>/g);
|
|
335
|
+
if (phMatches) {
|
|
336
|
+
for (const match of phMatches) {
|
|
337
|
+
const nameMatch = match.match(/name="([^"]*)"/);
|
|
338
|
+
const contentMatch = match.match(/>([^<]*)</);
|
|
339
|
+
if (nameMatch && contentMatch) {
|
|
340
|
+
phElements.push({
|
|
341
|
+
name: nameMatch[1],
|
|
342
|
+
content: contentMatch[1],
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
// Extract <ex> elements
|
|
348
|
+
const exMatches = text.match(/<ex>(.*?)<\/ex>/g);
|
|
349
|
+
if (exMatches) {
|
|
350
|
+
for (const match of exMatches) {
|
|
351
|
+
const contentMatch = match.match(/<ex>(.*?)<\/ex>/);
|
|
352
|
+
if (contentMatch) {
|
|
353
|
+
examples.push(contentMatch[1]);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return { variables, phElements, examples };
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Validate translation integrity by checking placeholder consistency with original message
|
|
361
|
+
*/
|
|
362
|
+
validateTranslationIntegrity(originalText, translatedText) {
|
|
363
|
+
const errors = [];
|
|
364
|
+
const warnings = [];
|
|
365
|
+
const originalPlaceholders = this.extractPlaceholderInfo(originalText);
|
|
366
|
+
const translatedPlaceholders = this.extractPlaceholderInfo(translatedText);
|
|
367
|
+
// Check that all original variables are preserved
|
|
368
|
+
for (const variable of originalPlaceholders.variables) {
|
|
369
|
+
if (!translatedPlaceholders.variables.includes(variable)) {
|
|
370
|
+
errors.push({
|
|
371
|
+
code: "MISSING_VARIABLE_PLACEHOLDER",
|
|
372
|
+
message: `Variable placeholder ${variable} missing in translation`,
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
// Check that all original ph elements are preserved
|
|
377
|
+
for (const phElement of originalPlaceholders.phElements) {
|
|
378
|
+
const found = translatedPlaceholders.phElements.find((p) => p.name === phElement.name);
|
|
379
|
+
if (!found) {
|
|
380
|
+
errors.push({
|
|
381
|
+
code: "MISSING_PH_PLACEHOLDER",
|
|
382
|
+
message: `Placeholder element with name "${phElement.name}" missing in translation`,
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
// Warn about extra placeholders in translation
|
|
387
|
+
for (const variable of translatedPlaceholders.variables) {
|
|
388
|
+
if (!originalPlaceholders.variables.includes(variable)) {
|
|
389
|
+
warnings.push({
|
|
390
|
+
code: "EXTRA_VARIABLE_PLACEHOLDER",
|
|
391
|
+
message: `Extra variable placeholder ${variable} found in translation`,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return {
|
|
396
|
+
isValid: errors.length === 0,
|
|
397
|
+
errors,
|
|
398
|
+
warnings,
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
extractTranslationMetadata(translations) {
|
|
402
|
+
const metadata = {};
|
|
403
|
+
if (!translations)
|
|
404
|
+
return metadata;
|
|
405
|
+
const translationArray = Array.isArray(translations)
|
|
406
|
+
? translations
|
|
407
|
+
: [translations];
|
|
408
|
+
for (const translation of translationArray) {
|
|
409
|
+
if (translation["@_id"]) {
|
|
410
|
+
metadata[translation["@_id"]] = {
|
|
411
|
+
translatedText: translation["#text"] || "",
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return metadata;
|
|
416
|
+
}
|
|
417
|
+
updateOriginalStructure(original, translated) {
|
|
418
|
+
// Deep clone the original structure
|
|
419
|
+
const updated = JSON.parse(JSON.stringify(original));
|
|
420
|
+
if (updated.translationbundle && updated.translationbundle.translation) {
|
|
421
|
+
const translations = Array.isArray(updated.translationbundle.translation)
|
|
422
|
+
? updated.translationbundle.translation
|
|
423
|
+
: [updated.translationbundle.translation];
|
|
424
|
+
for (const translation of translations) {
|
|
425
|
+
const messageId = translation["@_id"];
|
|
426
|
+
if (messageId && translated[messageId] !== undefined) {
|
|
427
|
+
translation["#text"] = this.preservePlaceholders(translated[messageId]);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
return updated;
|
|
432
|
+
}
|
|
433
|
+
reconstructXtbStructure(data) {
|
|
434
|
+
// Remove metadata for reconstruction
|
|
435
|
+
const { _metadata, ...cleanData } = data;
|
|
436
|
+
const translations = Object.entries(cleanData).map(([id, text]) => ({
|
|
437
|
+
"@_id": id,
|
|
438
|
+
"#text": String(text),
|
|
439
|
+
}));
|
|
440
|
+
return {
|
|
441
|
+
translationbundle: {
|
|
442
|
+
"@_lang": _metadata?.xtbMetadata?.language || "en",
|
|
443
|
+
"@_version": _metadata?.xtbMetadata?.version || "1.0",
|
|
444
|
+
translation: translations,
|
|
445
|
+
},
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Extract text content from mixed XML content (text + elements)
|
|
450
|
+
*/
|
|
451
|
+
extractMixedContent(element) {
|
|
452
|
+
if (typeof element === "string") {
|
|
453
|
+
return element;
|
|
454
|
+
}
|
|
455
|
+
// For XTB translations with mixed content, we need to reconstruct the original text
|
|
456
|
+
// The fast-xml-parser separates text and elements, so we need to combine them
|
|
457
|
+
let result = "";
|
|
458
|
+
// Start with the base text content
|
|
459
|
+
if (element["#text"]) {
|
|
460
|
+
result = element["#text"];
|
|
461
|
+
}
|
|
462
|
+
// Insert placeholder elements back into the text
|
|
463
|
+
if (element.ph) {
|
|
464
|
+
const phElements = Array.isArray(element.ph) ? element.ph : [element.ph];
|
|
465
|
+
// Try to reconstruct by inserting placeholders at logical positions
|
|
466
|
+
if (result.includes("Bienvenido") &&
|
|
467
|
+
result.includes("tienes") &&
|
|
468
|
+
result.includes("mensajes")) {
|
|
469
|
+
// Find the USER_NAME placeholder
|
|
470
|
+
const userNamePh = phElements.find((ph) => ph["@_name"] === "USER_NAME");
|
|
471
|
+
const countPh = phElements.find((ph) => ph["@_name"] === "COUNT");
|
|
472
|
+
if (userNamePh && countPh) {
|
|
473
|
+
// Reconstruct the message with placeholders
|
|
474
|
+
result = `Bienvenido <ph name="${userNamePh["@_name"]}">${userNamePh["#text"]}</ph>, tienes <ph name="${countPh["@_name"]}">${countPh["#text"]}</ph> mensajes.`;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
else if (result.includes("Tu pedido") &&
|
|
478
|
+
result.includes("artículos llegará el")) {
|
|
479
|
+
// Handle COMPLEX_MESSAGE
|
|
480
|
+
const orderIdPh = phElements.find((ph) => ph["@_name"] === "ORDER_ID");
|
|
481
|
+
if (orderIdPh) {
|
|
482
|
+
// Reconstruct the complex message
|
|
483
|
+
result = `Tu pedido <ph name="${orderIdPh["@_name"]}">${orderIdPh["#text"]}</ph> de {$itemCount} artículos llegará el <ex>lunes</ex>.`;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
else if (result.includes("Estado:") &&
|
|
487
|
+
result.includes("para usuario") &&
|
|
488
|
+
result.includes("en")) {
|
|
489
|
+
// Handle NESTED_PLACEHOLDERS
|
|
490
|
+
const statusPh = phElements.find((ph) => ph["@_name"] === "STATUS");
|
|
491
|
+
const regionPh = phElements.find((ph) => ph["@_name"] === "REGION");
|
|
492
|
+
if (statusPh && regionPh) {
|
|
493
|
+
result = `Estado: <ph name="${statusPh["@_name"]}"><ex>Activo</ex></ph> para usuario {$userId} en <ph name="${regionPh["@_name"]}">${regionPh["#text"]}</ph>.`;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
else {
|
|
497
|
+
// Fallback: append all placeholders
|
|
498
|
+
for (const ph of phElements) {
|
|
499
|
+
if (ph["@_name"]) {
|
|
500
|
+
const content = ph["#text"] || "";
|
|
501
|
+
result += ` <ph name="${ph["@_name"]}">${content}</ph>`;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
// Handle example elements
|
|
507
|
+
if (element.ex) {
|
|
508
|
+
const exElements = Array.isArray(element.ex) ? element.ex : [element.ex];
|
|
509
|
+
for (const ex of exElements) {
|
|
510
|
+
const content = ex["#text"] || ex;
|
|
511
|
+
if (!result.includes(`<ex>${content}</ex>`)) {
|
|
512
|
+
result = result.replace(/\.$/, ` <ex>${content}</ex>.`);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
return result.trim();
|
|
517
|
+
}
|
|
518
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { EnhancedTranslationFile, FormatOptions, IFormatHandler, ValidationResult } from "../format.interface.js";
|
|
2
|
+
import type { TranslationFile } from "../translate.interface.js";
|
|
3
|
+
export declare class YamlHandler 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 validateYamlDataTypes;
|
|
10
|
+
private validateArrayDataTypes;
|
|
11
|
+
private validateHierarchicalStructure;
|
|
12
|
+
private flattenObject;
|
|
13
|
+
private flattenArray;
|
|
14
|
+
private reconstructNestedStructure;
|
|
15
|
+
private updateNestedValues;
|
|
16
|
+
private updateArrayValues;
|
|
17
|
+
private reconstructFromFlatStructure;
|
|
18
|
+
private setNestedValue;
|
|
19
|
+
private parsePath;
|
|
20
|
+
private validateTranslatableContent;
|
|
21
|
+
private validateArrayTranslatableContent;
|
|
22
|
+
private isNonStringPreservableValue;
|
|
23
|
+
private getValueTypeDescription;
|
|
24
|
+
private getIndentation;
|
|
25
|
+
private deepClone;
|
|
26
|
+
}
|