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,550 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Global validation rules that apply to all formats
|
|
3
|
+
*/
|
|
4
|
+
export const globalValidationRules = [
|
|
5
|
+
{
|
|
6
|
+
code: "EMPTY_TRANSLATION_FILE",
|
|
7
|
+
name: "Empty Translation File",
|
|
8
|
+
description: "Check for empty translation files",
|
|
9
|
+
severity: "warning",
|
|
10
|
+
validate: (data) => {
|
|
11
|
+
const issues = [];
|
|
12
|
+
const keys = Object.keys(data).filter((key) => !key.startsWith("_"));
|
|
13
|
+
if (keys.length === 0) {
|
|
14
|
+
issues.push({
|
|
15
|
+
code: "EMPTY_TRANSLATION_FILE",
|
|
16
|
+
message: "Translation file contains no translatable content",
|
|
17
|
+
severity: "warning",
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return issues;
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
code: "DUPLICATE_KEYS",
|
|
25
|
+
name: "Duplicate Translation Keys",
|
|
26
|
+
description: "Check for duplicate translation keys",
|
|
27
|
+
severity: "error",
|
|
28
|
+
validate: (data) => {
|
|
29
|
+
const issues = [];
|
|
30
|
+
const seenKeys = new Set();
|
|
31
|
+
const duplicates = new Set();
|
|
32
|
+
const checkKeys = (obj, path = "") => {
|
|
33
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
34
|
+
if (key.startsWith("_"))
|
|
35
|
+
continue;
|
|
36
|
+
const fullKey = path ? `${path}.${key}` : key;
|
|
37
|
+
if (seenKeys.has(fullKey)) {
|
|
38
|
+
duplicates.add(fullKey);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
seenKeys.add(fullKey);
|
|
42
|
+
}
|
|
43
|
+
if (typeof value === "object" &&
|
|
44
|
+
value !== null &&
|
|
45
|
+
!Array.isArray(value)) {
|
|
46
|
+
checkKeys(value, fullKey);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
checkKeys(data);
|
|
51
|
+
for (const duplicate of duplicates) {
|
|
52
|
+
issues.push({
|
|
53
|
+
code: "DUPLICATE_KEYS",
|
|
54
|
+
message: `Duplicate translation key found: ${duplicate}`,
|
|
55
|
+
severity: "error",
|
|
56
|
+
path: duplicate,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return issues;
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
code: "INVALID_KEY_FORMAT",
|
|
64
|
+
name: "Invalid Key Format",
|
|
65
|
+
description: "Check for invalid translation key formats",
|
|
66
|
+
severity: "warning",
|
|
67
|
+
validate: (data) => {
|
|
68
|
+
const issues = [];
|
|
69
|
+
const validateKeyFormat = (obj, path = "") => {
|
|
70
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
71
|
+
if (key.startsWith("_"))
|
|
72
|
+
continue;
|
|
73
|
+
const fullKey = path ? `${path}.${key}` : key;
|
|
74
|
+
// Check for problematic key patterns
|
|
75
|
+
if (key.includes(" ")) {
|
|
76
|
+
issues.push({
|
|
77
|
+
code: "KEY_CONTAINS_SPACES",
|
|
78
|
+
message: `Translation key "${fullKey}" contains spaces`,
|
|
79
|
+
severity: "warning",
|
|
80
|
+
path: fullKey,
|
|
81
|
+
suggestion: "Consider using underscores or camelCase instead of spaces",
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (key.startsWith("-") || key.endsWith("-")) {
|
|
85
|
+
issues.push({
|
|
86
|
+
code: "KEY_INVALID_DASH",
|
|
87
|
+
message: `Translation key "${fullKey}" starts or ends with dash`,
|
|
88
|
+
severity: "warning",
|
|
89
|
+
path: fullKey,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
if (/[^a-zA-Z0-9._\-\[\]]/.test(key)) {
|
|
93
|
+
issues.push({
|
|
94
|
+
code: "KEY_SPECIAL_CHARACTERS",
|
|
95
|
+
message: `Translation key "${fullKey}" contains special characters`,
|
|
96
|
+
severity: "info",
|
|
97
|
+
path: fullKey,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
if (typeof value === "object" &&
|
|
101
|
+
value !== null &&
|
|
102
|
+
!Array.isArray(value)) {
|
|
103
|
+
validateKeyFormat(value, fullKey);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
validateKeyFormat(data);
|
|
108
|
+
return issues;
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
];
|
|
112
|
+
/**
|
|
113
|
+
* JSON format validation rules
|
|
114
|
+
*/
|
|
115
|
+
export const jsonValidationRules = [
|
|
116
|
+
{
|
|
117
|
+
code: "JSON_CIRCULAR_REFERENCE",
|
|
118
|
+
name: "JSON Circular Reference",
|
|
119
|
+
description: "Check for circular references in JSON data",
|
|
120
|
+
severity: "error",
|
|
121
|
+
validate: (data) => {
|
|
122
|
+
const issues = [];
|
|
123
|
+
try {
|
|
124
|
+
JSON.stringify(data);
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
if (error instanceof TypeError && error.message.includes("circular")) {
|
|
128
|
+
issues.push({
|
|
129
|
+
code: "JSON_CIRCULAR_REFERENCE",
|
|
130
|
+
message: "JSON data contains circular references",
|
|
131
|
+
severity: "error",
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return issues;
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
code: "JSON_DEEP_NESTING",
|
|
140
|
+
name: "JSON Deep Nesting",
|
|
141
|
+
description: "Check for excessively deep nesting in JSON",
|
|
142
|
+
severity: "warning",
|
|
143
|
+
validate: (data) => {
|
|
144
|
+
const issues = [];
|
|
145
|
+
const maxDepth = 10;
|
|
146
|
+
const checkDepth = (obj, currentDepth = 0, path = "") => {
|
|
147
|
+
if (currentDepth > maxDepth) {
|
|
148
|
+
issues.push({
|
|
149
|
+
code: "JSON_DEEP_NESTING",
|
|
150
|
+
message: `JSON structure is deeply nested (depth > ${maxDepth}) at ${path}`,
|
|
151
|
+
severity: "warning",
|
|
152
|
+
path: path,
|
|
153
|
+
});
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
157
|
+
if (key.startsWith("_"))
|
|
158
|
+
continue;
|
|
159
|
+
const fullPath = path ? `${path}.${key}` : key;
|
|
160
|
+
if (typeof value === "object" &&
|
|
161
|
+
value !== null &&
|
|
162
|
+
!Array.isArray(value)) {
|
|
163
|
+
checkDepth(value, currentDepth + 1, fullPath);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
checkDepth(data);
|
|
168
|
+
return issues;
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
];
|
|
172
|
+
/**
|
|
173
|
+
* XLIFF format validation rules
|
|
174
|
+
*/
|
|
175
|
+
export const xliffValidationRules = [
|
|
176
|
+
{
|
|
177
|
+
code: "XLIFF_MISSING_VERSION",
|
|
178
|
+
name: "XLIFF Missing Version",
|
|
179
|
+
description: "Check for missing XLIFF version",
|
|
180
|
+
severity: "warning",
|
|
181
|
+
validate: (data, context) => {
|
|
182
|
+
const issues = [];
|
|
183
|
+
const enhancedData = data;
|
|
184
|
+
if (!enhancedData._metadata?.version) {
|
|
185
|
+
issues.push({
|
|
186
|
+
code: "XLIFF_MISSING_VERSION",
|
|
187
|
+
message: "XLIFF file should specify version information",
|
|
188
|
+
severity: "warning",
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
return issues;
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
code: "XLIFF_INVALID_VERSION",
|
|
196
|
+
name: "XLIFF Invalid Version",
|
|
197
|
+
description: "Check for invalid XLIFF version",
|
|
198
|
+
severity: "error",
|
|
199
|
+
validate: (data) => {
|
|
200
|
+
const issues = [];
|
|
201
|
+
const enhancedData = data;
|
|
202
|
+
const version = enhancedData._metadata?.version;
|
|
203
|
+
if (version && !["1.2", "2.0", "2.1"].includes(version)) {
|
|
204
|
+
issues.push({
|
|
205
|
+
code: "XLIFF_INVALID_VERSION",
|
|
206
|
+
message: `Unsupported XLIFF version: ${version}`,
|
|
207
|
+
severity: "error",
|
|
208
|
+
suggestion: "Supported versions are 1.2, 2.0, and 2.1",
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
return issues;
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
code: "XLIFF_MISSING_LANGUAGES",
|
|
216
|
+
name: "XLIFF Missing Languages",
|
|
217
|
+
description: "Check for missing source/target language information",
|
|
218
|
+
severity: "warning",
|
|
219
|
+
validate: (data) => {
|
|
220
|
+
const issues = [];
|
|
221
|
+
const enhancedData = data;
|
|
222
|
+
if (!enhancedData._metadata?.sourceLanguage) {
|
|
223
|
+
issues.push({
|
|
224
|
+
code: "XLIFF_MISSING_SOURCE_LANGUAGE",
|
|
225
|
+
message: "XLIFF file should specify source language",
|
|
226
|
+
severity: "warning",
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
if (!enhancedData._metadata?.targetLanguage) {
|
|
230
|
+
issues.push({
|
|
231
|
+
code: "XLIFF_MISSING_TARGET_LANGUAGE",
|
|
232
|
+
message: "XLIFF file should specify target language",
|
|
233
|
+
severity: "warning",
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
return issues;
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
];
|
|
240
|
+
/**
|
|
241
|
+
* ARB format validation rules
|
|
242
|
+
*/
|
|
243
|
+
export const arbValidationRules = [
|
|
244
|
+
{
|
|
245
|
+
code: "ARB_MISSING_LOCALE",
|
|
246
|
+
name: "ARB Missing Locale",
|
|
247
|
+
description: "Check for missing @@locale metadata",
|
|
248
|
+
severity: "warning",
|
|
249
|
+
validate: (data) => {
|
|
250
|
+
const issues = [];
|
|
251
|
+
const enhancedData = data;
|
|
252
|
+
if (!enhancedData._metadata?.arbMetadata?.["@@locale"]) {
|
|
253
|
+
issues.push({
|
|
254
|
+
code: "ARB_MISSING_LOCALE",
|
|
255
|
+
message: "ARB file should contain @@locale metadata",
|
|
256
|
+
severity: "warning",
|
|
257
|
+
suggestion: "Add @@locale metadata to specify the language code",
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
return issues;
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
code: "ARB_INVALID_LOCALE",
|
|
265
|
+
name: "ARB Invalid Locale",
|
|
266
|
+
description: "Check for invalid locale format",
|
|
267
|
+
severity: "warning",
|
|
268
|
+
validate: (data) => {
|
|
269
|
+
const issues = [];
|
|
270
|
+
const enhancedData = data;
|
|
271
|
+
const locale = enhancedData._metadata?.arbMetadata?.["@@locale"];
|
|
272
|
+
if (locale && !/^[a-z]{2,3}(_[A-Z]{2})?$/.test(locale)) {
|
|
273
|
+
issues.push({
|
|
274
|
+
code: "ARB_INVALID_LOCALE",
|
|
275
|
+
message: `Invalid locale format: ${locale}`,
|
|
276
|
+
severity: "warning",
|
|
277
|
+
suggestion: 'Use format like "en", "en_US", "es_ES"',
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
return issues;
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
{
|
|
284
|
+
code: "ARB_ICU_SYNTAX_ERROR",
|
|
285
|
+
name: "ARB ICU Syntax Error",
|
|
286
|
+
description: "Check for ICU message format syntax errors",
|
|
287
|
+
severity: "error",
|
|
288
|
+
validate: (data) => {
|
|
289
|
+
const issues = [];
|
|
290
|
+
const validateIcuSyntax = (obj, path = "") => {
|
|
291
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
292
|
+
if (key.startsWith("_") || key.startsWith("@"))
|
|
293
|
+
continue;
|
|
294
|
+
const fullPath = path ? `${path}.${key}` : key;
|
|
295
|
+
if (typeof value === "string") {
|
|
296
|
+
const icuErrors = validateIcuMessage(value);
|
|
297
|
+
for (const error of icuErrors) {
|
|
298
|
+
issues.push({
|
|
299
|
+
code: "ARB_ICU_SYNTAX_ERROR",
|
|
300
|
+
message: `ICU syntax error in "${fullPath}": ${error}`,
|
|
301
|
+
severity: "error",
|
|
302
|
+
path: fullPath,
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
else if (typeof value === "object" &&
|
|
307
|
+
value !== null &&
|
|
308
|
+
!Array.isArray(value)) {
|
|
309
|
+
validateIcuSyntax(value, fullPath);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
validateIcuSyntax(data);
|
|
314
|
+
return issues;
|
|
315
|
+
},
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
code: "ARB_ORPHANED_METADATA",
|
|
319
|
+
name: "ARB Orphaned Metadata",
|
|
320
|
+
description: "Check for metadata without corresponding resources",
|
|
321
|
+
severity: "warning",
|
|
322
|
+
validate: (data) => {
|
|
323
|
+
const issues = [];
|
|
324
|
+
const resourceKeys = new Set();
|
|
325
|
+
const metadataKeys = new Set();
|
|
326
|
+
// Collect resource and metadata keys
|
|
327
|
+
for (const key of Object.keys(data)) {
|
|
328
|
+
if (key.startsWith("@") && !key.startsWith("@@")) {
|
|
329
|
+
metadataKeys.add(key.substring(1));
|
|
330
|
+
}
|
|
331
|
+
else if (!key.startsWith("@") && !key.startsWith("_")) {
|
|
332
|
+
resourceKeys.add(key);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
// Check for orphaned metadata
|
|
336
|
+
for (const metadataKey of metadataKeys) {
|
|
337
|
+
if (!resourceKeys.has(metadataKey)) {
|
|
338
|
+
issues.push({
|
|
339
|
+
code: "ARB_ORPHANED_METADATA",
|
|
340
|
+
message: `Resource metadata @${metadataKey} has no corresponding resource`,
|
|
341
|
+
severity: "warning",
|
|
342
|
+
path: `@${metadataKey}`,
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return issues;
|
|
347
|
+
},
|
|
348
|
+
},
|
|
349
|
+
];
|
|
350
|
+
/**
|
|
351
|
+
* PO format validation rules
|
|
352
|
+
*/
|
|
353
|
+
export const poValidationRules = [
|
|
354
|
+
{
|
|
355
|
+
code: "PO_MISSING_HEADER",
|
|
356
|
+
name: "PO Missing Header",
|
|
357
|
+
description: "Check for missing PO file header information",
|
|
358
|
+
severity: "warning",
|
|
359
|
+
validate: (data, context) => {
|
|
360
|
+
const issues = [];
|
|
361
|
+
const enhancedData = data;
|
|
362
|
+
// Check if we have header information in metadata
|
|
363
|
+
if (!enhancedData._metadata?.originalStructure?.headers) {
|
|
364
|
+
issues.push({
|
|
365
|
+
code: "PO_MISSING_HEADER",
|
|
366
|
+
message: "PO file should contain header information",
|
|
367
|
+
severity: "warning",
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
return issues;
|
|
371
|
+
},
|
|
372
|
+
},
|
|
373
|
+
{
|
|
374
|
+
code: "PO_UNTRANSLATED_STRINGS",
|
|
375
|
+
name: "PO Untranslated Strings",
|
|
376
|
+
description: "Check for untranslated strings in PO files",
|
|
377
|
+
severity: "info",
|
|
378
|
+
validate: (data) => {
|
|
379
|
+
const issues = [];
|
|
380
|
+
let untranslatedCount = 0;
|
|
381
|
+
for (const [key, value] of Object.entries(data)) {
|
|
382
|
+
if (key.startsWith("_"))
|
|
383
|
+
continue;
|
|
384
|
+
if (typeof value === "string" && value.trim() === "") {
|
|
385
|
+
untranslatedCount++;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
if (untranslatedCount > 0) {
|
|
389
|
+
issues.push({
|
|
390
|
+
code: "PO_UNTRANSLATED_STRINGS",
|
|
391
|
+
message: `Found ${untranslatedCount} untranslated strings`,
|
|
392
|
+
severity: "info",
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
return issues;
|
|
396
|
+
},
|
|
397
|
+
},
|
|
398
|
+
];
|
|
399
|
+
/**
|
|
400
|
+
* YAML format validation rules
|
|
401
|
+
*/
|
|
402
|
+
export const yamlValidationRules = [
|
|
403
|
+
{
|
|
404
|
+
code: "YAML_MIXED_TYPES",
|
|
405
|
+
name: "YAML Mixed Types",
|
|
406
|
+
description: "Check for mixed data types in YAML values",
|
|
407
|
+
severity: "warning",
|
|
408
|
+
validate: (data) => {
|
|
409
|
+
const issues = [];
|
|
410
|
+
const checkTypes = (obj, path = "") => {
|
|
411
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
412
|
+
if (key.startsWith("_"))
|
|
413
|
+
continue;
|
|
414
|
+
const fullPath = path ? `${path}.${key}` : key;
|
|
415
|
+
if (typeof value !== "string" && typeof value !== "object") {
|
|
416
|
+
issues.push({
|
|
417
|
+
code: "YAML_NON_STRING_VALUE",
|
|
418
|
+
message: `Non-string value at ${fullPath} (${typeof value})`,
|
|
419
|
+
severity: "warning",
|
|
420
|
+
path: fullPath,
|
|
421
|
+
suggestion: "Only string values should be translated",
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
else if (typeof value === "object" &&
|
|
425
|
+
value !== null &&
|
|
426
|
+
!Array.isArray(value)) {
|
|
427
|
+
checkTypes(value, fullPath);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
checkTypes(data);
|
|
432
|
+
return issues;
|
|
433
|
+
},
|
|
434
|
+
},
|
|
435
|
+
];
|
|
436
|
+
/**
|
|
437
|
+
* Properties format validation rules
|
|
438
|
+
*/
|
|
439
|
+
export const propertiesValidationRules = [
|
|
440
|
+
{
|
|
441
|
+
code: "PROPERTIES_ENCODING_ISSUE",
|
|
442
|
+
name: "Properties Encoding Issue",
|
|
443
|
+
description: "Check for potential encoding issues in Properties files",
|
|
444
|
+
severity: "warning",
|
|
445
|
+
validate: (data) => {
|
|
446
|
+
const issues = [];
|
|
447
|
+
for (const [key, value] of Object.entries(data)) {
|
|
448
|
+
if (key.startsWith("_"))
|
|
449
|
+
continue;
|
|
450
|
+
if (typeof value === "string") {
|
|
451
|
+
// Check for unescaped Unicode characters
|
|
452
|
+
if (/[^\x00-\x7F]/.test(value) && !/\\u[0-9a-fA-F]{4}/.test(value)) {
|
|
453
|
+
issues.push({
|
|
454
|
+
code: "PROPERTIES_UNESCAPED_UNICODE",
|
|
455
|
+
message: `Unescaped Unicode characters in "${key}"`,
|
|
456
|
+
severity: "warning",
|
|
457
|
+
path: key,
|
|
458
|
+
suggestion: "Consider using Unicode escapes (\\uXXXX) for non-ASCII characters",
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return issues;
|
|
464
|
+
},
|
|
465
|
+
},
|
|
466
|
+
];
|
|
467
|
+
/**
|
|
468
|
+
* CSV format validation rules
|
|
469
|
+
*/
|
|
470
|
+
export const csvValidationRules = [
|
|
471
|
+
{
|
|
472
|
+
code: "CSV_INCONSISTENT_COLUMNS",
|
|
473
|
+
name: "CSV Inconsistent Columns",
|
|
474
|
+
description: "Check for inconsistent column structure in CSV",
|
|
475
|
+
severity: "error",
|
|
476
|
+
validate: (data, context) => {
|
|
477
|
+
const issues = [];
|
|
478
|
+
const enhancedData = data;
|
|
479
|
+
// This would need to be implemented based on CSV-specific metadata
|
|
480
|
+
// For now, just check basic structure
|
|
481
|
+
if (Object.keys(data).length === 0) {
|
|
482
|
+
issues.push({
|
|
483
|
+
code: "CSV_NO_DATA",
|
|
484
|
+
message: "CSV file contains no data",
|
|
485
|
+
severity: "error",
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
return issues;
|
|
489
|
+
},
|
|
490
|
+
},
|
|
491
|
+
];
|
|
492
|
+
/**
|
|
493
|
+
* Helper function to validate ICU message syntax
|
|
494
|
+
*/
|
|
495
|
+
function validateIcuMessage(message) {
|
|
496
|
+
const errors = [];
|
|
497
|
+
// Check bracket matching
|
|
498
|
+
let depth = 0;
|
|
499
|
+
let inQuotes = false;
|
|
500
|
+
for (let i = 0; i < message.length; i++) {
|
|
501
|
+
const char = message[i];
|
|
502
|
+
const prevChar = i > 0 ? message[i - 1] : "";
|
|
503
|
+
if (char === "'" && prevChar !== "\\") {
|
|
504
|
+
inQuotes = !inQuotes;
|
|
505
|
+
}
|
|
506
|
+
else if (!inQuotes) {
|
|
507
|
+
if (char === "{") {
|
|
508
|
+
depth++;
|
|
509
|
+
}
|
|
510
|
+
else if (char === "}") {
|
|
511
|
+
depth--;
|
|
512
|
+
if (depth < 0) {
|
|
513
|
+
errors.push("Mismatched closing bracket");
|
|
514
|
+
break;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
if (depth !== 0) {
|
|
520
|
+
errors.push("Unmatched opening brackets");
|
|
521
|
+
}
|
|
522
|
+
// Check for unmatched quotes
|
|
523
|
+
const unescapedQuotes = message.match(/(?<!\\)'/g);
|
|
524
|
+
if (unescapedQuotes && unescapedQuotes.length % 2 !== 0) {
|
|
525
|
+
errors.push("Unmatched single quotes");
|
|
526
|
+
}
|
|
527
|
+
// Check for invalid placeholder syntax
|
|
528
|
+
if (/\{[^}]*[{}][^}]*\}/.test(message)) {
|
|
529
|
+
errors.push("Invalid placeholder syntax");
|
|
530
|
+
}
|
|
531
|
+
return errors;
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Export all format-specific rules
|
|
535
|
+
*/
|
|
536
|
+
export const formatSpecificRules = {
|
|
537
|
+
json: jsonValidationRules,
|
|
538
|
+
xliff: xliffValidationRules,
|
|
539
|
+
arb: arbValidationRules,
|
|
540
|
+
po: poValidationRules,
|
|
541
|
+
pot: poValidationRules, // PO and POT share the same rules
|
|
542
|
+
yaml: yamlValidationRules,
|
|
543
|
+
yml: yamlValidationRules, // YAML and YML share the same rules
|
|
544
|
+
properties: propertiesValidationRules,
|
|
545
|
+
csv: csvValidationRules,
|
|
546
|
+
tsv: csvValidationRules, // CSV and TSV share the same rules
|
|
547
|
+
xml: [], // Basic XML rules would go here
|
|
548
|
+
xmb: [], // XMB-specific rules would go here
|
|
549
|
+
xtb: [], // XTB-specific rules would go here
|
|
550
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { IFormatHandler, ValidationResult } from "../format.interface.js";
|
|
2
|
+
import type { TranslationFile } from "../translate.interface.js";
|
|
3
|
+
import { EnhancedValidationResult } from "./enhanced-validation-result.js";
|
|
4
|
+
export interface FormatValidationRule {
|
|
5
|
+
code: string;
|
|
6
|
+
name: string;
|
|
7
|
+
description: string;
|
|
8
|
+
severity: "error" | "warning" | "info";
|
|
9
|
+
validate: (data: TranslationFile, context?: ValidationContext) => ValidationIssue[];
|
|
10
|
+
}
|
|
11
|
+
export interface ValidationContext {
|
|
12
|
+
format: string;
|
|
13
|
+
filePath?: string;
|
|
14
|
+
originalContent?: string;
|
|
15
|
+
handler?: IFormatHandler;
|
|
16
|
+
metadata?: any;
|
|
17
|
+
}
|
|
18
|
+
export interface ValidationIssue {
|
|
19
|
+
code: string;
|
|
20
|
+
message: string;
|
|
21
|
+
severity: "error" | "warning" | "info";
|
|
22
|
+
line?: number;
|
|
23
|
+
column?: number;
|
|
24
|
+
path?: string;
|
|
25
|
+
suggestion?: string;
|
|
26
|
+
}
|
|
27
|
+
export declare class FormatValidator {
|
|
28
|
+
private static rules;
|
|
29
|
+
private static globalRules;
|
|
30
|
+
/**
|
|
31
|
+
* Register format-specific validation rules
|
|
32
|
+
*/
|
|
33
|
+
static registerFormatRules(format: string, rules: FormatValidationRule[]): void;
|
|
34
|
+
/**
|
|
35
|
+
* Register global validation rules that apply to all formats
|
|
36
|
+
*/
|
|
37
|
+
static registerGlobalRules(rules: FormatValidationRule[]): void;
|
|
38
|
+
/**
|
|
39
|
+
* Validate translation file data using format-specific and global rules
|
|
40
|
+
*/
|
|
41
|
+
static validate(data: TranslationFile, context: ValidationContext): ValidationResult;
|
|
42
|
+
/**
|
|
43
|
+
* Validate translation file data and return enhanced result with detailed messaging
|
|
44
|
+
*/
|
|
45
|
+
static validateWithEnhancedResult(data: TranslationFile, context: ValidationContext): EnhancedValidationResult;
|
|
46
|
+
/**
|
|
47
|
+
* Create enhanced validation result from issues
|
|
48
|
+
*/
|
|
49
|
+
private static createEnhancedValidationResult;
|
|
50
|
+
/**
|
|
51
|
+
* Get all registered rules for a format
|
|
52
|
+
*/
|
|
53
|
+
static getFormatRules(format: string): FormatValidationRule[];
|
|
54
|
+
/**
|
|
55
|
+
* Get all global rules
|
|
56
|
+
*/
|
|
57
|
+
static getGlobalRules(): FormatValidationRule[];
|
|
58
|
+
/**
|
|
59
|
+
* Clear all rules (useful for testing)
|
|
60
|
+
*/
|
|
61
|
+
static clearRules(): void;
|
|
62
|
+
/**
|
|
63
|
+
* Validate structure integrity for translation files
|
|
64
|
+
*/
|
|
65
|
+
static validateStructureIntegrity(data: TranslationFile, context: ValidationContext): ValidationResult;
|
|
66
|
+
private static validateTranslatableContent;
|
|
67
|
+
private static validateStringContent;
|
|
68
|
+
private static containsHtmlTags;
|
|
69
|
+
private static createValidationResult;
|
|
70
|
+
}
|