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
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
import * as path from "node:path";
|
|
2
|
+
import * as propertiesParser from "properties-parser";
|
|
3
|
+
export class PropertiesHandler {
|
|
4
|
+
canHandle(filePath, content) {
|
|
5
|
+
const extension = path.extname(filePath).toLowerCase();
|
|
6
|
+
if (extension !== ".properties") {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
// If content is provided, check for valid Properties format
|
|
10
|
+
if (content) {
|
|
11
|
+
try {
|
|
12
|
+
propertiesParser.parse(content);
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
parse(content) {
|
|
22
|
+
try {
|
|
23
|
+
// Detect encoding from content before parsing
|
|
24
|
+
const encoding = this.detectEncoding(content);
|
|
25
|
+
// Handle Unicode unescaping if needed
|
|
26
|
+
const processedContent = this.unescapeUnicode(content, encoding);
|
|
27
|
+
// Parse the properties content
|
|
28
|
+
const parsed = propertiesParser.parse(processedContent);
|
|
29
|
+
if (!parsed || typeof parsed !== "object") {
|
|
30
|
+
throw new Error("Properties file is empty or invalid");
|
|
31
|
+
}
|
|
32
|
+
// Extract comments and formatting information from original content
|
|
33
|
+
const comments = this.extractComments(content);
|
|
34
|
+
const formatting = this.extractFormatting(content);
|
|
35
|
+
// Extract placeholder information
|
|
36
|
+
const placeholderInfo = this.analyzePlaceholders(parsed);
|
|
37
|
+
// Add metadata
|
|
38
|
+
const result = {
|
|
39
|
+
...parsed,
|
|
40
|
+
_metadata: {
|
|
41
|
+
format: "properties",
|
|
42
|
+
encoding: encoding,
|
|
43
|
+
originalStructure: parsed,
|
|
44
|
+
preserveComments: true,
|
|
45
|
+
preserveAttributes: false,
|
|
46
|
+
comments: comments,
|
|
47
|
+
formatting: formatting,
|
|
48
|
+
hasUnicodeEscapes: this.hasUnicodeEscapes(content),
|
|
49
|
+
placeholders: placeholderInfo,
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
throw new Error(`Failed to parse Properties: ${error instanceof Error ? error.message : String(error)}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
serialize(data, options) {
|
|
59
|
+
try {
|
|
60
|
+
// Remove metadata for serialization
|
|
61
|
+
const { _metadata, ...cleanData } = data;
|
|
62
|
+
// Apply encoding option with normalization
|
|
63
|
+
const rawEncoding = options?.encoding || _metadata?.encoding || "UTF-8";
|
|
64
|
+
const encoding = this.normalizeEncoding(rawEncoding);
|
|
65
|
+
// Build properties content with preserved formatting and comments
|
|
66
|
+
let result = "";
|
|
67
|
+
// Add encoding comment if not UTF-8 and preserving formatting
|
|
68
|
+
if (options?.preserveFormatting !== false && encoding !== "UTF-8") {
|
|
69
|
+
result += `# encoding: ${encoding}\n`;
|
|
70
|
+
}
|
|
71
|
+
// Add header comment if preserving formatting
|
|
72
|
+
if (options?.preserveFormatting !== false &&
|
|
73
|
+
_metadata?.comments?.header) {
|
|
74
|
+
result += _metadata.comments.header + "\n";
|
|
75
|
+
}
|
|
76
|
+
// Process each key-value pair
|
|
77
|
+
for (const [key, value] of Object.entries(cleanData)) {
|
|
78
|
+
// Add any comments for this key
|
|
79
|
+
if (options?.preserveFormatting !== false &&
|
|
80
|
+
_metadata?.comments?.keys?.[key]) {
|
|
81
|
+
result += _metadata.comments.keys[key] + "\n";
|
|
82
|
+
}
|
|
83
|
+
// Handle Unicode escaping based on encoding
|
|
84
|
+
const escapedKey = this.escapeKey(key, encoding);
|
|
85
|
+
const escapedValue = this.escapeValue(String(value), encoding);
|
|
86
|
+
// Use appropriate separator (= or :) with proper spacing
|
|
87
|
+
const separator = _metadata?.formatting?.separators?.[key] || "=";
|
|
88
|
+
const spacing = separator === "=" ? " = " : ": ";
|
|
89
|
+
result += `${escapedKey}${spacing}${escapedValue}\n`;
|
|
90
|
+
}
|
|
91
|
+
// Add footer comment if preserving formatting
|
|
92
|
+
if (options?.preserveFormatting !== false &&
|
|
93
|
+
_metadata?.comments?.footer) {
|
|
94
|
+
result += _metadata.comments.footer;
|
|
95
|
+
}
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
throw new Error(`Failed to serialize Properties: ${error instanceof Error ? error.message : String(error)}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
getFileExtension() {
|
|
103
|
+
return ".properties";
|
|
104
|
+
}
|
|
105
|
+
validateStructure(data) {
|
|
106
|
+
const errors = [];
|
|
107
|
+
const warnings = [];
|
|
108
|
+
// Basic Properties structure validation
|
|
109
|
+
if (!data || typeof data !== "object") {
|
|
110
|
+
errors.push({
|
|
111
|
+
code: "INVALID_STRUCTURE",
|
|
112
|
+
message: "Properties data must be an object",
|
|
113
|
+
});
|
|
114
|
+
return { isValid: false, errors, warnings };
|
|
115
|
+
}
|
|
116
|
+
if (Array.isArray(data)) {
|
|
117
|
+
errors.push({
|
|
118
|
+
code: "INVALID_ROOT_TYPE",
|
|
119
|
+
message: "Properties root cannot be an array",
|
|
120
|
+
});
|
|
121
|
+
return { isValid: false, errors, warnings };
|
|
122
|
+
}
|
|
123
|
+
// Validate key-value pairs
|
|
124
|
+
for (const [key, value] of Object.entries(data)) {
|
|
125
|
+
// Skip metadata
|
|
126
|
+
if (key === "_metadata")
|
|
127
|
+
continue;
|
|
128
|
+
// Validate key format
|
|
129
|
+
if (!this.isValidPropertyKey(key)) {
|
|
130
|
+
errors.push({
|
|
131
|
+
code: "INVALID_KEY_FORMAT",
|
|
132
|
+
message: `Invalid property key format: ${key}`,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
// Check for placeholder variables
|
|
136
|
+
if (typeof value === "string" && this.containsPlaceholders(value)) {
|
|
137
|
+
const placeholders = this.extractPlaceholders(value);
|
|
138
|
+
warnings.push({
|
|
139
|
+
code: "PLACEHOLDER_DETECTED",
|
|
140
|
+
message: `Placeholder variables detected in key ${key}: [${placeholders.join(", ")}]`,
|
|
141
|
+
});
|
|
142
|
+
// Validate placeholder syntax
|
|
143
|
+
for (const placeholder of placeholders) {
|
|
144
|
+
if (!this.isValidPlaceholderSyntax(placeholder)) {
|
|
145
|
+
errors.push({
|
|
146
|
+
code: "INVALID_PLACEHOLDER_SYNTAX",
|
|
147
|
+
message: `Invalid placeholder syntax in key ${key}: ${placeholder}`,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
// Validate value type
|
|
153
|
+
if (typeof value !== "string" &&
|
|
154
|
+
typeof value !== "number" &&
|
|
155
|
+
typeof value !== "boolean") {
|
|
156
|
+
warnings.push({
|
|
157
|
+
code: "NON_STRING_VALUE",
|
|
158
|
+
message: `Non-string value at key ${key} will be converted to string`,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
// Check for empty properties
|
|
163
|
+
const dataKeys = Object.keys(data).filter((key) => key !== "_metadata");
|
|
164
|
+
if (dataKeys.length === 0) {
|
|
165
|
+
warnings.push({
|
|
166
|
+
code: "EMPTY_PROPERTIES",
|
|
167
|
+
message: "Properties file appears to be empty",
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
isValid: errors.length === 0,
|
|
172
|
+
errors,
|
|
173
|
+
warnings,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
detectEncoding(content) {
|
|
177
|
+
// Check for BOM (Byte Order Mark)
|
|
178
|
+
if (content.startsWith("\uFEFF")) {
|
|
179
|
+
return "UTF-8"; // UTF-8 with BOM
|
|
180
|
+
}
|
|
181
|
+
// Check for Unicode escape sequences
|
|
182
|
+
if (/\\u[0-9a-fA-F]{4}/.test(content)) {
|
|
183
|
+
return "ISO-8859-1"; // Properties with Unicode escapes typically use ISO-8859-1
|
|
184
|
+
}
|
|
185
|
+
// Check for non-ASCII characters
|
|
186
|
+
if (/[^\x00-\x7F]/.test(content)) {
|
|
187
|
+
return "UTF-8"; // Contains non-ASCII, likely UTF-8
|
|
188
|
+
}
|
|
189
|
+
// Check for encoding declaration in comments
|
|
190
|
+
const encodingComment = content.match(/#.*encoding[:\s=]+([^\s\n\r]+)/i);
|
|
191
|
+
if (encodingComment) {
|
|
192
|
+
const declaredEncoding = encodingComment[1].toUpperCase();
|
|
193
|
+
if (declaredEncoding.includes("UTF-8") ||
|
|
194
|
+
declaredEncoding.includes("UTF8")) {
|
|
195
|
+
return "UTF-8";
|
|
196
|
+
}
|
|
197
|
+
if (declaredEncoding.includes("ISO-8859-1") ||
|
|
198
|
+
declaredEncoding.includes("LATIN-1")) {
|
|
199
|
+
return "ISO-8859-1";
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return "UTF-8"; // Default to UTF-8
|
|
203
|
+
}
|
|
204
|
+
hasUnicodeEscapes(content) {
|
|
205
|
+
return /\\u[0-9a-fA-F]{4}/.test(content);
|
|
206
|
+
}
|
|
207
|
+
unescapeUnicode(content, encoding) {
|
|
208
|
+
if (encoding === "ISO-8859-1" || this.hasUnicodeEscapes(content)) {
|
|
209
|
+
// Unescape Unicode sequences for proper parsing
|
|
210
|
+
return content.replace(/\\u([0-9a-fA-F]{4})/g, (match, hex) => {
|
|
211
|
+
return String.fromCharCode(parseInt(hex, 16));
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
return content;
|
|
215
|
+
}
|
|
216
|
+
normalizeEncoding(encoding) {
|
|
217
|
+
const normalized = encoding.toUpperCase().replace(/[-_]/g, "");
|
|
218
|
+
// Map common encoding variations
|
|
219
|
+
const encodingMap = {
|
|
220
|
+
UTF8: "UTF-8",
|
|
221
|
+
"UTF-8": "UTF-8",
|
|
222
|
+
ISO88591: "ISO-8859-1",
|
|
223
|
+
"ISO-8859-1": "ISO-8859-1",
|
|
224
|
+
LATIN1: "ISO-8859-1",
|
|
225
|
+
ASCII: "ASCII",
|
|
226
|
+
"US-ASCII": "ASCII",
|
|
227
|
+
};
|
|
228
|
+
return encodingMap[normalized] || "UTF-8";
|
|
229
|
+
}
|
|
230
|
+
extractComments(content) {
|
|
231
|
+
const comments = {
|
|
232
|
+
header: "",
|
|
233
|
+
footer: "",
|
|
234
|
+
keys: {},
|
|
235
|
+
};
|
|
236
|
+
const lines = content.split(/\r?\n/);
|
|
237
|
+
let currentKey = null;
|
|
238
|
+
let headerDone = false;
|
|
239
|
+
let pendingComments = [];
|
|
240
|
+
for (let i = 0; i < lines.length; i++) {
|
|
241
|
+
const line = lines[i].trim();
|
|
242
|
+
// Check if line is a comment
|
|
243
|
+
if (line.startsWith("#") || line.startsWith("!")) {
|
|
244
|
+
pendingComments.push(lines[i]); // Preserve original formatting
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
// Check if line is empty
|
|
248
|
+
if (line === "") {
|
|
249
|
+
if (pendingComments.length > 0) {
|
|
250
|
+
pendingComments.push(lines[i]);
|
|
251
|
+
}
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
// Check if line is a property
|
|
255
|
+
if (line.includes("=") || line.includes(":")) {
|
|
256
|
+
headerDone = true;
|
|
257
|
+
// Extract key
|
|
258
|
+
const separatorMatch = line.match(/^([^=:]+)[=:]/);
|
|
259
|
+
if (separatorMatch) {
|
|
260
|
+
currentKey = separatorMatch[1].trim();
|
|
261
|
+
// Associate pending comments with this key
|
|
262
|
+
if (pendingComments.length > 0) {
|
|
263
|
+
comments.keys[currentKey] = pendingComments.join("\n");
|
|
264
|
+
pendingComments = [];
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
// Handle remaining comments as footer
|
|
270
|
+
if (pendingComments.length > 0) {
|
|
271
|
+
if (!headerDone) {
|
|
272
|
+
comments.header = pendingComments.join("\n");
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
comments.footer = pendingComments.join("\n");
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return comments;
|
|
279
|
+
}
|
|
280
|
+
extractFormatting(content) {
|
|
281
|
+
const formatting = {
|
|
282
|
+
separators: {},
|
|
283
|
+
};
|
|
284
|
+
const lines = content.split(/\r?\n/);
|
|
285
|
+
for (const line of lines) {
|
|
286
|
+
const trimmedLine = line.trim();
|
|
287
|
+
// Skip comments and empty lines
|
|
288
|
+
if (trimmedLine.startsWith("#") ||
|
|
289
|
+
trimmedLine.startsWith("!") ||
|
|
290
|
+
trimmedLine === "") {
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
// Extract separator type (= or :)
|
|
294
|
+
const equalMatch = trimmedLine.match(/^([^=:]+)(=)/);
|
|
295
|
+
const colonMatch = trimmedLine.match(/^([^=:]+)(:)/);
|
|
296
|
+
if (equalMatch) {
|
|
297
|
+
const key = equalMatch[1].trim();
|
|
298
|
+
formatting.separators[key] = "=";
|
|
299
|
+
}
|
|
300
|
+
else if (colonMatch) {
|
|
301
|
+
const key = colonMatch[1].trim();
|
|
302
|
+
formatting.separators[key] = ":";
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return formatting;
|
|
306
|
+
}
|
|
307
|
+
escapeKey(key, encoding) {
|
|
308
|
+
// Keys need to escape spaces, =, :, and other special characters
|
|
309
|
+
let escaped = key.replace(/\\/g, "\\\\"); // Escape backslashes first
|
|
310
|
+
escaped = escaped.replace(/=/g, "\\=");
|
|
311
|
+
escaped = escaped.replace(/:/g, "\\:");
|
|
312
|
+
escaped = escaped.replace(/\s/g, "\\ "); // Escape spaces
|
|
313
|
+
escaped = escaped.replace(/\n/g, "\\n");
|
|
314
|
+
escaped = escaped.replace(/\r/g, "\\r");
|
|
315
|
+
escaped = escaped.replace(/\t/g, "\\t");
|
|
316
|
+
escaped = escaped.replace(/\f/g, "\\f"); // Form feed
|
|
317
|
+
// Handle Unicode escaping based on encoding
|
|
318
|
+
escaped = this.applyUnicodeEscaping(escaped, encoding);
|
|
319
|
+
return escaped;
|
|
320
|
+
}
|
|
321
|
+
escapeValue(value, encoding) {
|
|
322
|
+
// Values need to escape backslashes and newlines
|
|
323
|
+
let escaped = value.replace(/\\/g, "\\\\"); // Escape backslashes first
|
|
324
|
+
escaped = escaped.replace(/\n/g, "\\n");
|
|
325
|
+
escaped = escaped.replace(/\r/g, "\\r");
|
|
326
|
+
escaped = escaped.replace(/\t/g, "\\t");
|
|
327
|
+
escaped = escaped.replace(/\f/g, "\\f"); // Form feed
|
|
328
|
+
// Leading spaces in values need to be escaped
|
|
329
|
+
escaped = escaped.replace(/^(\s+)/, (match) => {
|
|
330
|
+
return match.replace(/\s/g, "\\ ");
|
|
331
|
+
});
|
|
332
|
+
// Handle Unicode escaping based on encoding
|
|
333
|
+
escaped = this.applyUnicodeEscaping(escaped, encoding);
|
|
334
|
+
return escaped;
|
|
335
|
+
}
|
|
336
|
+
applyUnicodeEscaping(text, encoding) {
|
|
337
|
+
switch (encoding) {
|
|
338
|
+
case "ISO-8859-1":
|
|
339
|
+
// For ISO-8859-1, escape all non-Latin-1 characters
|
|
340
|
+
return text.replace(/[^\x00-\xFF]/g, (char) => {
|
|
341
|
+
const code = char.charCodeAt(0);
|
|
342
|
+
return `\\u${code.toString(16).padStart(4, "0")}`;
|
|
343
|
+
});
|
|
344
|
+
case "ASCII":
|
|
345
|
+
// For ASCII, escape all non-ASCII characters
|
|
346
|
+
return text.replace(/[^\x00-\x7F]/g, (char) => {
|
|
347
|
+
const code = char.charCodeAt(0);
|
|
348
|
+
return `\\u${code.toString(16).padStart(4, "0")}`;
|
|
349
|
+
});
|
|
350
|
+
case "UTF-8":
|
|
351
|
+
default:
|
|
352
|
+
// For UTF-8, no Unicode escaping needed, but handle surrogate pairs properly
|
|
353
|
+
return text;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
escapeUnicode(text) {
|
|
357
|
+
return text.replace(/[^\x00-\x7F]/g, (char) => {
|
|
358
|
+
const code = char.charCodeAt(0);
|
|
359
|
+
return `\\u${code.toString(16).padStart(4, "0")}`;
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
isValidPropertyKey(key) {
|
|
363
|
+
// Property keys should not be empty and should not start with whitespace
|
|
364
|
+
if (!key || key.trim() !== key || key.trim() === "") {
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
// Keys should not contain unescaped = or : at the beginning
|
|
368
|
+
if (key.match(/^[^\\]*[=:]/)) {
|
|
369
|
+
return false;
|
|
370
|
+
}
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
containsPlaceholders(value) {
|
|
374
|
+
// Common placeholder patterns in Properties files
|
|
375
|
+
const patterns = [
|
|
376
|
+
/\{[^}]+\}/g, // {placeholder}
|
|
377
|
+
/\$\{[^}]+\}/g, // ${placeholder}
|
|
378
|
+
/%[^%\s]+%/g, // %placeholder%
|
|
379
|
+
/%\w+/g, // %placeholder
|
|
380
|
+
/\{[0-9]+\}/g, // {0}, {1}, etc. (MessageFormat)
|
|
381
|
+
/\[[^\]]+\]/g, // [placeholder]
|
|
382
|
+
/@\{[^}]+\}/g, // @{placeholder}
|
|
383
|
+
/\$[a-zA-Z_][a-zA-Z0-9_]*/g, // $variable
|
|
384
|
+
/#\{[^}]+\}/g, // #{placeholder}
|
|
385
|
+
];
|
|
386
|
+
return patterns.some((pattern) => pattern.test(value));
|
|
387
|
+
}
|
|
388
|
+
extractPlaceholders(value) {
|
|
389
|
+
const placeholders = [];
|
|
390
|
+
const patterns = [
|
|
391
|
+
/\{[^}]+\}/g, // {placeholder}
|
|
392
|
+
/\$\{[^}]+\}/g, // ${placeholder}
|
|
393
|
+
/%[^%\s]+%/g, // %placeholder%
|
|
394
|
+
/%\w+/g, // %placeholder
|
|
395
|
+
/\{[0-9]+\}/g, // {0}, {1}, etc. (MessageFormat)
|
|
396
|
+
/\[[^\]]+\]/g, // [placeholder]
|
|
397
|
+
/@\{[^}]+\}/g, // @{placeholder}
|
|
398
|
+
/\$[a-zA-Z_][a-zA-Z0-9_]*/g, // $variable
|
|
399
|
+
/#\{[^}]+\}/g, // #{placeholder}
|
|
400
|
+
];
|
|
401
|
+
for (const pattern of patterns) {
|
|
402
|
+
const matches = value.match(pattern);
|
|
403
|
+
if (matches) {
|
|
404
|
+
placeholders.push(...matches);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return [...new Set(placeholders)]; // Remove duplicates
|
|
408
|
+
}
|
|
409
|
+
validatePlaceholderIntegrity(original, translated) {
|
|
410
|
+
const originalPlaceholders = this.extractPlaceholders(original);
|
|
411
|
+
const translatedPlaceholders = this.extractPlaceholders(translated);
|
|
412
|
+
const issues = [];
|
|
413
|
+
// Check for missing placeholders
|
|
414
|
+
for (const placeholder of originalPlaceholders) {
|
|
415
|
+
if (!translatedPlaceholders.includes(placeholder)) {
|
|
416
|
+
issues.push(`Missing placeholder: ${placeholder}`);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
// Check for extra placeholders
|
|
420
|
+
for (const placeholder of translatedPlaceholders) {
|
|
421
|
+
if (!originalPlaceholders.includes(placeholder)) {
|
|
422
|
+
issues.push(`Extra placeholder: ${placeholder}`);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
// Check for placeholder count mismatches (for numbered placeholders)
|
|
426
|
+
const originalNumbered = originalPlaceholders.filter((p) => /\{[0-9]+\}/.test(p));
|
|
427
|
+
const translatedNumbered = translatedPlaceholders.filter((p) => /\{[0-9]+\}/.test(p));
|
|
428
|
+
if (originalNumbered.length !== translatedNumbered.length) {
|
|
429
|
+
issues.push(`Numbered placeholder count mismatch: original has ${originalNumbered.length}, translated has ${translatedNumbered.length}`);
|
|
430
|
+
}
|
|
431
|
+
return {
|
|
432
|
+
isValid: issues.length === 0,
|
|
433
|
+
issues,
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
analyzePlaceholders(data) {
|
|
437
|
+
const placeholderInfo = {};
|
|
438
|
+
for (const [key, value] of Object.entries(data)) {
|
|
439
|
+
if (typeof value === "string" && this.containsPlaceholders(value)) {
|
|
440
|
+
const placeholders = this.extractPlaceholders(value);
|
|
441
|
+
placeholderInfo[key] = {
|
|
442
|
+
placeholders: placeholders,
|
|
443
|
+
count: placeholders.length,
|
|
444
|
+
types: this.categorizePlaceholders(placeholders),
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return placeholderInfo;
|
|
449
|
+
}
|
|
450
|
+
categorizePlaceholders(placeholders) {
|
|
451
|
+
const types = {};
|
|
452
|
+
for (const placeholder of placeholders) {
|
|
453
|
+
let type = "unknown";
|
|
454
|
+
if (/\{[0-9]+\}/.test(placeholder)) {
|
|
455
|
+
type = "messageformat_numbered";
|
|
456
|
+
}
|
|
457
|
+
else if (/\{[^}]+\}/.test(placeholder)) {
|
|
458
|
+
type = "curly_braces";
|
|
459
|
+
}
|
|
460
|
+
else if (/\$\{[^}]+\}/.test(placeholder)) {
|
|
461
|
+
type = "shell_variable";
|
|
462
|
+
}
|
|
463
|
+
else if (/%[^%\s]+%/.test(placeholder)) {
|
|
464
|
+
type = "percent_wrapped";
|
|
465
|
+
}
|
|
466
|
+
else if (/%\w+/.test(placeholder)) {
|
|
467
|
+
type = "percent_prefix";
|
|
468
|
+
}
|
|
469
|
+
else if (/\[[^\]]+\]/.test(placeholder)) {
|
|
470
|
+
type = "square_brackets";
|
|
471
|
+
}
|
|
472
|
+
else if (/@\{[^}]+\}/.test(placeholder)) {
|
|
473
|
+
type = "at_curly";
|
|
474
|
+
}
|
|
475
|
+
else if (/\$[a-zA-Z_][a-zA-Z0-9_]*/.test(placeholder)) {
|
|
476
|
+
type = "dollar_variable";
|
|
477
|
+
}
|
|
478
|
+
else if (/#\{[^}]+\}/.test(placeholder)) {
|
|
479
|
+
type = "hash_curly";
|
|
480
|
+
}
|
|
481
|
+
types[type] = (types[type] || 0) + 1;
|
|
482
|
+
}
|
|
483
|
+
return types;
|
|
484
|
+
}
|
|
485
|
+
isValidPlaceholderSyntax(placeholder) {
|
|
486
|
+
// Define valid placeholder patterns
|
|
487
|
+
const validPatterns = [
|
|
488
|
+
/^\{[^}]+\}$/, // {placeholder}
|
|
489
|
+
/^\$\{[^}]+\}$/, // ${placeholder}
|
|
490
|
+
/^%[^%\s]+%$/, // %placeholder%
|
|
491
|
+
/^%\w+$/, // %placeholder
|
|
492
|
+
/^\{[0-9]+\}$/, // {0}, {1}, etc.
|
|
493
|
+
/^\[[^\]]+\]$/, // [placeholder]
|
|
494
|
+
/^@\{[^}]+\}$/, // @{placeholder}
|
|
495
|
+
/^\$[a-zA-Z_][a-zA-Z0-9_]*$/, // $variable
|
|
496
|
+
/^#\{[^}]+\}$/, // #{placeholder}
|
|
497
|
+
];
|
|
498
|
+
return validPatterns.some((pattern) => pattern.test(placeholder));
|
|
499
|
+
}
|
|
500
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { EnhancedTranslationFile, IFormatHandler, ValidationResult } from "../format.interface.js";
|
|
2
|
+
import type { TranslationFile } from "../translate.interface.js";
|
|
3
|
+
import { type CsvOptions } from "./csv-handler.js";
|
|
4
|
+
export declare class TsvHandler implements IFormatHandler {
|
|
5
|
+
private csvHandler;
|
|
6
|
+
constructor();
|
|
7
|
+
canHandle(filePath: string, content?: string): boolean;
|
|
8
|
+
parse(content: string): EnhancedTranslationFile;
|
|
9
|
+
serialize(data: EnhancedTranslationFile, options?: CsvOptions): string;
|
|
10
|
+
getFileExtension(): string;
|
|
11
|
+
validateStructure(data: TranslationFile): ValidationResult;
|
|
12
|
+
private isValidTsvContent;
|
|
13
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import * as path from "node:path";
|
|
2
|
+
import { CsvHandler } from "./csv-handler.js";
|
|
3
|
+
export class TsvHandler {
|
|
4
|
+
csvHandler;
|
|
5
|
+
constructor() {
|
|
6
|
+
this.csvHandler = new CsvHandler();
|
|
7
|
+
}
|
|
8
|
+
canHandle(filePath, content) {
|
|
9
|
+
const extension = path.extname(filePath).toLowerCase();
|
|
10
|
+
if (extension !== ".tsv") {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
// If content is provided, check for tab-separated format
|
|
14
|
+
if (content) {
|
|
15
|
+
return this.isValidTsvContent(content);
|
|
16
|
+
}
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
parse(content) {
|
|
20
|
+
// Use CSV handler with tab delimiter
|
|
21
|
+
const data = this.csvHandler.parse(content);
|
|
22
|
+
// Update metadata to indicate TSV format
|
|
23
|
+
if (data._metadata) {
|
|
24
|
+
data._metadata.format = "tsv";
|
|
25
|
+
data._metadata.csvDelimiter = "\t";
|
|
26
|
+
}
|
|
27
|
+
return data;
|
|
28
|
+
}
|
|
29
|
+
serialize(data, options) {
|
|
30
|
+
// Force tab delimiter for TSV
|
|
31
|
+
const tsvOptions = {
|
|
32
|
+
...options,
|
|
33
|
+
delimiter: "\t",
|
|
34
|
+
};
|
|
35
|
+
return this.csvHandler.serialize(data, tsvOptions);
|
|
36
|
+
}
|
|
37
|
+
getFileExtension() {
|
|
38
|
+
return ".tsv";
|
|
39
|
+
}
|
|
40
|
+
validateStructure(data) {
|
|
41
|
+
return this.csvHandler.validateStructure(data);
|
|
42
|
+
}
|
|
43
|
+
isValidTsvContent(content) {
|
|
44
|
+
try {
|
|
45
|
+
// Check if content has tab characters and consistent tab-separated structure
|
|
46
|
+
const lines = content.split("\n").filter((line) => line.trim());
|
|
47
|
+
if (lines.length === 0)
|
|
48
|
+
return false;
|
|
49
|
+
// Must contain tabs
|
|
50
|
+
if (!content.includes("\t"))
|
|
51
|
+
return false;
|
|
52
|
+
const firstLineFields = lines[0].split("\t").length;
|
|
53
|
+
// Check if most lines have consistent field count
|
|
54
|
+
let consistentLines = 0;
|
|
55
|
+
for (const line of lines.slice(0, Math.min(10, lines.length))) {
|
|
56
|
+
const fields = line.split("\t");
|
|
57
|
+
if (fields.length === firstLineFields) {
|
|
58
|
+
consistentLines++;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return consistentLines >= Math.ceil(lines.length * 0.7); // 70% consistency threshold
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { EnhancedTranslationFile, FormatOptions, IFormatHandler, ValidationResult } from "../format.interface.js";
|
|
2
|
+
import type { TranslationFile } from "../translate.interface.js";
|
|
3
|
+
export declare class XliffHandler 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
|
+
private detectXliffVersion;
|
|
13
|
+
private extractSourceLanguage;
|
|
14
|
+
private extractTargetLanguage;
|
|
15
|
+
private transformXliff12;
|
|
16
|
+
private transformXliff2x;
|
|
17
|
+
private extractTextContent;
|
|
18
|
+
private updateOriginalStructure;
|
|
19
|
+
private updateXliff12Structure;
|
|
20
|
+
private updateXliff2xStructure;
|
|
21
|
+
private reconstructXliff12Structure;
|
|
22
|
+
private validateXliff12;
|
|
23
|
+
private validateXliff2x;
|
|
24
|
+
}
|