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,679 @@
|
|
|
1
|
+
import * as path from "node:path";
|
|
2
|
+
export class ArbHandler {
|
|
3
|
+
canHandle(filePath, content) {
|
|
4
|
+
const extension = path.extname(filePath).toLowerCase();
|
|
5
|
+
if (extension !== ".arb") {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
// If content is provided, check for ARB-specific structure
|
|
9
|
+
if (content) {
|
|
10
|
+
try {
|
|
11
|
+
const parsed = JSON.parse(content);
|
|
12
|
+
// ARB files should have @@locale metadata or resource metadata patterns
|
|
13
|
+
return this.isValidArbStructure(parsed);
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
parse(content) {
|
|
22
|
+
try {
|
|
23
|
+
const parsed = JSON.parse(content);
|
|
24
|
+
if (!this.isValidArbStructure(parsed)) {
|
|
25
|
+
throw new Error("Invalid ARB file structure");
|
|
26
|
+
}
|
|
27
|
+
// Separate ARB metadata from translatable content
|
|
28
|
+
const { metadata, resources, resourceMetadata } = this.extractArbComponents(parsed);
|
|
29
|
+
// Analyze ICU message formats in resources
|
|
30
|
+
const icuAnalysis = this.analyzeIcuMessages(resources);
|
|
31
|
+
// Create flattened structure for translation
|
|
32
|
+
const result = {
|
|
33
|
+
...resources,
|
|
34
|
+
_metadata: {
|
|
35
|
+
format: "arb",
|
|
36
|
+
originalStructure: parsed,
|
|
37
|
+
sourceLanguage: metadata["@@locale"],
|
|
38
|
+
preserveComments: false,
|
|
39
|
+
preserveAttributes: true,
|
|
40
|
+
arbMetadata: metadata,
|
|
41
|
+
resourceMetadata: resourceMetadata,
|
|
42
|
+
icuAnalysis: icuAnalysis,
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
throw new Error(`Failed to parse ARB: ${error instanceof Error ? error.message : String(error)}`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
serialize(data, options) {
|
|
52
|
+
try {
|
|
53
|
+
const { _metadata, ...translatedResources } = data;
|
|
54
|
+
// Reconstruct ARB structure with proper ordering
|
|
55
|
+
const result = {};
|
|
56
|
+
// 1. Add ARB metadata first (@@locale, @@last_modified, etc.)
|
|
57
|
+
if (_metadata?.arbMetadata) {
|
|
58
|
+
// Ensure @@locale comes first if present
|
|
59
|
+
if (_metadata.arbMetadata["@@locale"]) {
|
|
60
|
+
result["@@locale"] = _metadata.arbMetadata["@@locale"];
|
|
61
|
+
}
|
|
62
|
+
// Add other ARB metadata in sorted order
|
|
63
|
+
const otherMetadata = Object.entries(_metadata.arbMetadata)
|
|
64
|
+
.filter(([key]) => key !== "@@locale")
|
|
65
|
+
.sort(([a], [b]) => a.localeCompare(b));
|
|
66
|
+
for (const [key, value] of otherMetadata) {
|
|
67
|
+
result[key] = value;
|
|
68
|
+
}
|
|
69
|
+
// Update @@last_modified if preserving timestamps
|
|
70
|
+
if (options?.customSettings?.updateTimestamp !== false) {
|
|
71
|
+
result["@@last_modified"] = new Date().toISOString();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// 2. Add translated resources in sorted order
|
|
75
|
+
const sortedResources = Object.entries(translatedResources).sort(([a], [b]) => a.localeCompare(b));
|
|
76
|
+
for (const [key, value] of sortedResources) {
|
|
77
|
+
result[key] = value;
|
|
78
|
+
}
|
|
79
|
+
// 3. Add resource metadata (@resourceName entries) in sorted order
|
|
80
|
+
if (_metadata?.resourceMetadata) {
|
|
81
|
+
const sortedMetadata = Object.entries(_metadata.resourceMetadata).sort(([a], [b]) => a.localeCompare(b));
|
|
82
|
+
for (const [key, value] of sortedMetadata) {
|
|
83
|
+
// Ensure metadata is properly structured
|
|
84
|
+
result[key] = this.sanitizeResourceMetadata(value);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// Apply formatting options
|
|
88
|
+
const indentation = this.getIndentation(options);
|
|
89
|
+
return JSON.stringify(result, null, indentation);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
throw new Error(`Failed to serialize ARB: ${error instanceof Error ? error.message : String(error)}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
getFileExtension() {
|
|
96
|
+
return ".arb";
|
|
97
|
+
}
|
|
98
|
+
validateStructure(data) {
|
|
99
|
+
const errors = [];
|
|
100
|
+
const warnings = [];
|
|
101
|
+
// Basic structure validation
|
|
102
|
+
if (!data || typeof data !== "object") {
|
|
103
|
+
errors.push({
|
|
104
|
+
code: "INVALID_STRUCTURE",
|
|
105
|
+
message: "ARB data must be an object",
|
|
106
|
+
});
|
|
107
|
+
return { isValid: false, errors, warnings };
|
|
108
|
+
}
|
|
109
|
+
// Check for required ARB metadata
|
|
110
|
+
const hasLocaleMetadata = "@@locale" in data;
|
|
111
|
+
if (!hasLocaleMetadata) {
|
|
112
|
+
warnings.push({
|
|
113
|
+
code: "MISSING_LOCALE_METADATA",
|
|
114
|
+
message: "ARB file should contain @@locale metadata",
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
// Validate ARB metadata format
|
|
118
|
+
this.validateArbMetadata(data, errors, warnings);
|
|
119
|
+
// Validate resource structure
|
|
120
|
+
this.validateResourceStructure(data, errors, warnings);
|
|
121
|
+
// Validate ICU message formats
|
|
122
|
+
this.validateIcuMessages(data, errors, warnings);
|
|
123
|
+
return {
|
|
124
|
+
isValid: errors.length === 0,
|
|
125
|
+
errors,
|
|
126
|
+
warnings,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
isValidArbStructure(parsed) {
|
|
130
|
+
if (!parsed || typeof parsed !== "object") {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
// Check for ARB-specific patterns
|
|
134
|
+
const hasArbMetadata = Object.keys(parsed).some((key) => key.startsWith("@@"));
|
|
135
|
+
const hasResourceMetadata = Object.keys(parsed).some((key) => key.startsWith("@") && !key.startsWith("@@"));
|
|
136
|
+
// ARB files should have either ARB metadata or resource metadata
|
|
137
|
+
return (hasArbMetadata ||
|
|
138
|
+
hasResourceMetadata ||
|
|
139
|
+
this.hasArbResourcePattern(parsed));
|
|
140
|
+
}
|
|
141
|
+
hasArbResourcePattern(parsed) {
|
|
142
|
+
// Look for typical ARB resource patterns
|
|
143
|
+
const keys = Object.keys(parsed);
|
|
144
|
+
return keys.some((key) => {
|
|
145
|
+
const metadataKey = `@${key}`;
|
|
146
|
+
return metadataKey in parsed;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
extractArbComponents(parsed) {
|
|
150
|
+
const metadata = {};
|
|
151
|
+
const resources = {};
|
|
152
|
+
const resourceMetadata = {};
|
|
153
|
+
for (const [key, value] of Object.entries(parsed)) {
|
|
154
|
+
if (key.startsWith("@@")) {
|
|
155
|
+
// ARB metadata - preserve all ARB-specific metadata
|
|
156
|
+
metadata[key] = value;
|
|
157
|
+
}
|
|
158
|
+
else if (key.startsWith("@")) {
|
|
159
|
+
// Resource metadata - validate and preserve structure
|
|
160
|
+
resourceMetadata[key] = this.validateAndPreserveResourceMetadata(key, value);
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
// Translatable resource
|
|
164
|
+
if (typeof value === "string") {
|
|
165
|
+
resources[key] = value;
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
// Non-string values in ARB should be preserved as metadata
|
|
169
|
+
console.warn(`Non-string resource "${key}" found in ARB file, treating as metadata`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return { metadata, resources, resourceMetadata };
|
|
174
|
+
}
|
|
175
|
+
validateAndPreserveResourceMetadata(key, value) {
|
|
176
|
+
if (typeof value !== "object" || value === null) {
|
|
177
|
+
console.warn(`Invalid resource metadata for ${key}, creating empty metadata object`);
|
|
178
|
+
return {};
|
|
179
|
+
}
|
|
180
|
+
const metadata = {};
|
|
181
|
+
// Preserve known metadata properties
|
|
182
|
+
if (value.type !== undefined) {
|
|
183
|
+
metadata.type = String(value.type);
|
|
184
|
+
}
|
|
185
|
+
if (value.description !== undefined) {
|
|
186
|
+
metadata.description = String(value.description);
|
|
187
|
+
}
|
|
188
|
+
if (value.context !== undefined) {
|
|
189
|
+
metadata.context = String(value.context);
|
|
190
|
+
}
|
|
191
|
+
// Preserve placeholders with validation
|
|
192
|
+
if (value.placeholders && typeof value.placeholders === "object") {
|
|
193
|
+
metadata.placeholders = this.preservePlaceholderMetadata(value.placeholders);
|
|
194
|
+
}
|
|
195
|
+
// Preserve any additional properties (for extensibility)
|
|
196
|
+
for (const [prop, propValue] of Object.entries(value)) {
|
|
197
|
+
if (!["type", "description", "context", "placeholders"].includes(prop)) {
|
|
198
|
+
metadata[prop] = propValue;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return metadata;
|
|
202
|
+
}
|
|
203
|
+
preservePlaceholderMetadata(placeholders) {
|
|
204
|
+
const preserved = {};
|
|
205
|
+
for (const [placeholderName, placeholderData] of Object.entries(placeholders)) {
|
|
206
|
+
if (typeof placeholderData === "object" && placeholderData !== null) {
|
|
207
|
+
preserved[placeholderName] = { ...placeholderData };
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
console.warn(`Invalid placeholder metadata for ${placeholderName}`);
|
|
211
|
+
preserved[placeholderName] = {};
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return preserved;
|
|
215
|
+
}
|
|
216
|
+
sanitizeResourceMetadata(metadata) {
|
|
217
|
+
if (typeof metadata !== "object" || metadata === null) {
|
|
218
|
+
return {};
|
|
219
|
+
}
|
|
220
|
+
const sanitized = {};
|
|
221
|
+
// Ensure string properties are strings
|
|
222
|
+
if (metadata.type !== undefined) {
|
|
223
|
+
sanitized.type = String(metadata.type);
|
|
224
|
+
}
|
|
225
|
+
if (metadata.description !== undefined) {
|
|
226
|
+
sanitized.description = String(metadata.description);
|
|
227
|
+
}
|
|
228
|
+
if (metadata.context !== undefined) {
|
|
229
|
+
sanitized.context = String(metadata.context);
|
|
230
|
+
}
|
|
231
|
+
// Preserve placeholders structure
|
|
232
|
+
if (metadata.placeholders && typeof metadata.placeholders === "object") {
|
|
233
|
+
sanitized.placeholders = {};
|
|
234
|
+
for (const [name, data] of Object.entries(metadata.placeholders)) {
|
|
235
|
+
if (typeof data === "object" && data !== null) {
|
|
236
|
+
sanitized.placeholders[name] = { ...data };
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return sanitized;
|
|
241
|
+
}
|
|
242
|
+
validateArbMetadata(data, errors, warnings) {
|
|
243
|
+
for (const [key, value] of Object.entries(data)) {
|
|
244
|
+
if (key.startsWith("@@")) {
|
|
245
|
+
if (typeof value !== "string") {
|
|
246
|
+
errors.push({
|
|
247
|
+
code: "INVALID_METADATA_TYPE",
|
|
248
|
+
message: `ARB metadata ${key} must be a string, got ${typeof value}`,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
// Validate specific metadata
|
|
252
|
+
if (key === "@@locale" && typeof value === "string") {
|
|
253
|
+
if (!this.isValidLocaleCode(value)) {
|
|
254
|
+
warnings.push({
|
|
255
|
+
code: "INVALID_LOCALE_FORMAT",
|
|
256
|
+
message: `Locale code "${value}" may not be in standard format`,
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
validateResourceStructure(data, errors, warnings) {
|
|
264
|
+
const resourceKeys = new Set();
|
|
265
|
+
const metadataKeys = new Set();
|
|
266
|
+
// Collect resource and metadata keys
|
|
267
|
+
for (const key of Object.keys(data)) {
|
|
268
|
+
if (key.startsWith("@") && !key.startsWith("@@")) {
|
|
269
|
+
metadataKeys.add(key.substring(1));
|
|
270
|
+
}
|
|
271
|
+
else if (!key.startsWith("@")) {
|
|
272
|
+
resourceKeys.add(key);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
// Check for orphaned metadata (metadata without corresponding resource)
|
|
276
|
+
for (const metadataKey of metadataKeys) {
|
|
277
|
+
if (!resourceKeys.has(metadataKey)) {
|
|
278
|
+
warnings.push({
|
|
279
|
+
code: "ORPHANED_METADATA",
|
|
280
|
+
message: `Resource metadata @${metadataKey} has no corresponding resource`,
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
// Check for resources without metadata (informational)
|
|
285
|
+
for (const resourceKey of resourceKeys) {
|
|
286
|
+
if (!metadataKeys.has(resourceKey)) {
|
|
287
|
+
// This is not an error, just informational
|
|
288
|
+
const resourceValue = data[resourceKey];
|
|
289
|
+
if (typeof resourceValue === "string" &&
|
|
290
|
+
this.analyzeIcuMessage(resourceValue).hasIcuSyntax) {
|
|
291
|
+
warnings.push({
|
|
292
|
+
code: "MISSING_METADATA_FOR_ICU",
|
|
293
|
+
message: `Resource "${resourceKey}" contains ICU syntax but has no metadata`,
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
// Validate resource metadata structure
|
|
299
|
+
for (const [key, value] of Object.entries(data)) {
|
|
300
|
+
if (key.startsWith("@") && !key.startsWith("@@")) {
|
|
301
|
+
this.validateResourceMetadata(key, value, errors, warnings);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
// Validate resource-metadata consistency
|
|
305
|
+
this.validateResourceMetadataConsistency(data, errors, warnings);
|
|
306
|
+
}
|
|
307
|
+
validateResourceMetadata(key, metadata, errors, warnings) {
|
|
308
|
+
if (typeof metadata !== "object" || metadata === null) {
|
|
309
|
+
errors.push({
|
|
310
|
+
code: "INVALID_RESOURCE_METADATA",
|
|
311
|
+
message: `Resource metadata ${key} must be an object`,
|
|
312
|
+
});
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
// Validate known metadata properties
|
|
316
|
+
const validProperties = ["type", "description", "placeholders", "context"];
|
|
317
|
+
for (const prop of Object.keys(metadata)) {
|
|
318
|
+
if (!validProperties.includes(prop)) {
|
|
319
|
+
warnings.push({
|
|
320
|
+
code: "UNKNOWN_METADATA_PROPERTY",
|
|
321
|
+
message: `Unknown metadata property "${prop}" in ${key}`,
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
// Validate placeholders structure
|
|
326
|
+
if ("placeholders" in metadata) {
|
|
327
|
+
this.validatePlaceholders(key, metadata.placeholders, errors, warnings);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
validatePlaceholders(resourceKey, placeholders, errors, warnings) {
|
|
331
|
+
if (typeof placeholders !== "object" || placeholders === null) {
|
|
332
|
+
errors.push({
|
|
333
|
+
code: "INVALID_PLACEHOLDERS",
|
|
334
|
+
message: `Placeholders in ${resourceKey} must be an object`,
|
|
335
|
+
});
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
for (const [placeholderName, placeholderData] of Object.entries(placeholders)) {
|
|
339
|
+
if (typeof placeholderData !== "object" || placeholderData === null) {
|
|
340
|
+
errors.push({
|
|
341
|
+
code: "INVALID_PLACEHOLDER_DATA",
|
|
342
|
+
message: `Placeholder "${placeholderName}" in ${resourceKey} must be an object`,
|
|
343
|
+
});
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
// Validate placeholder properties
|
|
347
|
+
const validPlaceholderProps = ["type", "example", "description"];
|
|
348
|
+
for (const prop of Object.keys(placeholderData)) {
|
|
349
|
+
if (!validPlaceholderProps.includes(prop)) {
|
|
350
|
+
warnings.push({
|
|
351
|
+
code: "UNKNOWN_PLACEHOLDER_PROPERTY",
|
|
352
|
+
message: `Unknown placeholder property "${prop}" in ${resourceKey}.${placeholderName}`,
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
validateIcuMessages(data, errors, warnings) {
|
|
359
|
+
for (const [key, value] of Object.entries(data)) {
|
|
360
|
+
// Skip metadata keys
|
|
361
|
+
if (key.startsWith("@")) {
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
if (typeof value === "string") {
|
|
365
|
+
this.validateIcuMessageSyntax(key, value, errors, warnings);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
validateIcuMessageSyntax(key, message, errors, warnings) {
|
|
370
|
+
const icuInfo = this.analyzeIcuMessage(message);
|
|
371
|
+
if (!icuInfo.hasIcuSyntax) {
|
|
372
|
+
return; // No ICU syntax to validate
|
|
373
|
+
}
|
|
374
|
+
// Validate bracket matching
|
|
375
|
+
if (!this.validateBracketMatching(message)) {
|
|
376
|
+
errors.push({
|
|
377
|
+
code: "ICU_BRACKET_MISMATCH",
|
|
378
|
+
message: `ICU message in "${key}" has mismatched brackets`,
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
// Validate plural forms
|
|
382
|
+
if (icuInfo.messageType === "plural" && icuInfo.pluralForms) {
|
|
383
|
+
if (!icuInfo.pluralForms.includes("other")) {
|
|
384
|
+
errors.push({
|
|
385
|
+
code: "ICU_MISSING_OTHER_PLURAL",
|
|
386
|
+
message: `ICU plural message in "${key}" must include 'other' form`,
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
// Validate select forms
|
|
391
|
+
if (icuInfo.messageType === "select" && icuInfo.selectOptions) {
|
|
392
|
+
if (!icuInfo.selectOptions.includes("other")) {
|
|
393
|
+
warnings.push({
|
|
394
|
+
code: "ICU_MISSING_OTHER_SELECT",
|
|
395
|
+
message: `ICU select message in "${key}" should include 'other' option`,
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
// Check for common ICU syntax errors
|
|
400
|
+
this.validateIcuSyntaxErrors(key, message, errors, warnings);
|
|
401
|
+
}
|
|
402
|
+
validateBracketMatching(message) {
|
|
403
|
+
let depth = 0;
|
|
404
|
+
let inQuotes = false;
|
|
405
|
+
for (let i = 0; i < message.length; i++) {
|
|
406
|
+
const char = message[i];
|
|
407
|
+
const prevChar = i > 0 ? message[i - 1] : "";
|
|
408
|
+
if (char === "'" && prevChar !== "\\") {
|
|
409
|
+
inQuotes = !inQuotes;
|
|
410
|
+
}
|
|
411
|
+
else if (!inQuotes) {
|
|
412
|
+
if (char === "{") {
|
|
413
|
+
depth++;
|
|
414
|
+
}
|
|
415
|
+
else if (char === "}") {
|
|
416
|
+
depth--;
|
|
417
|
+
if (depth < 0) {
|
|
418
|
+
return false; // More closing than opening brackets
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return depth === 0; // All brackets should be matched
|
|
424
|
+
}
|
|
425
|
+
validateIcuSyntaxErrors(key, message, errors, warnings) {
|
|
426
|
+
// Check for common syntax errors
|
|
427
|
+
// Unescaped single quotes in ICU messages
|
|
428
|
+
const unescapedQuotePattern = /(?<!\\)'/g;
|
|
429
|
+
const quotes = message.match(unescapedQuotePattern);
|
|
430
|
+
if (quotes && quotes.length % 2 !== 0) {
|
|
431
|
+
warnings.push({
|
|
432
|
+
code: "ICU_UNMATCHED_QUOTES",
|
|
433
|
+
message: `ICU message in "${key}" may have unmatched single quotes`,
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
// Invalid placeholder syntax
|
|
437
|
+
const invalidPlaceholderPattern = /\{[^}]*[{}][^}]*\}/g;
|
|
438
|
+
if (invalidPlaceholderPattern.test(message)) {
|
|
439
|
+
errors.push({
|
|
440
|
+
code: "ICU_INVALID_PLACEHOLDER",
|
|
441
|
+
message: `ICU message in "${key}" contains invalid placeholder syntax`,
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
// Missing comma in complex placeholders
|
|
445
|
+
const complexPlaceholderPattern = /\{[^,}]+\s+(plural|select)\s*[^,]/g;
|
|
446
|
+
if (complexPlaceholderPattern.test(message)) {
|
|
447
|
+
errors.push({
|
|
448
|
+
code: "ICU_MISSING_COMMA",
|
|
449
|
+
message: `ICU message in "${key}" is missing comma in complex placeholder`,
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
validateResourceMetadataConsistency(data, errors, warnings) {
|
|
454
|
+
for (const [key, value] of Object.entries(data)) {
|
|
455
|
+
// Skip metadata keys
|
|
456
|
+
if (key.startsWith("@")) {
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
if (typeof value === "string") {
|
|
460
|
+
const metadataKey = `@${key}`;
|
|
461
|
+
const metadata = data[metadataKey];
|
|
462
|
+
// Check if resource has ICU syntax but no metadata at all
|
|
463
|
+
const icuInfo = this.analyzeIcuMessage(value);
|
|
464
|
+
if (icuInfo.hasIcuSyntax && !metadata) {
|
|
465
|
+
warnings.push({
|
|
466
|
+
code: "MISSING_PLACEHOLDER_METADATA",
|
|
467
|
+
message: `Resource "${key}" uses ICU placeholders but has no placeholder metadata`,
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
else if (metadata) {
|
|
471
|
+
this.validatePlaceholderConsistency(key, value, metadata, errors, warnings);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
validatePlaceholderConsistency(resourceKey, message, metadata, errors, warnings) {
|
|
477
|
+
const icuInfo = this.analyzeIcuMessage(message);
|
|
478
|
+
if (!icuInfo.hasIcuSyntax) {
|
|
479
|
+
// If no ICU syntax but metadata has placeholders, warn
|
|
480
|
+
if (metadata.placeholders &&
|
|
481
|
+
Object.keys(metadata.placeholders).length > 0) {
|
|
482
|
+
warnings.push({
|
|
483
|
+
code: "METADATA_PLACEHOLDER_MISMATCH",
|
|
484
|
+
message: `Resource "${resourceKey}" has placeholder metadata but no ICU placeholders in message`,
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
// Check if metadata placeholders match ICU placeholders
|
|
490
|
+
if (metadata.placeholders) {
|
|
491
|
+
const metadataPlaceholders = Object.keys(metadata.placeholders);
|
|
492
|
+
const messagePlaceholders = icuInfo.placeholders;
|
|
493
|
+
// Check for missing placeholders in metadata
|
|
494
|
+
for (const placeholder of messagePlaceholders) {
|
|
495
|
+
if (!metadataPlaceholders.includes(placeholder)) {
|
|
496
|
+
warnings.push({
|
|
497
|
+
code: "MISSING_PLACEHOLDER_METADATA",
|
|
498
|
+
message: `Resource "${resourceKey}" uses placeholder "{${placeholder}}" but has no metadata for it`,
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
// Check for extra placeholders in metadata
|
|
503
|
+
for (const placeholder of metadataPlaceholders) {
|
|
504
|
+
if (!messagePlaceholders.includes(placeholder)) {
|
|
505
|
+
warnings.push({
|
|
506
|
+
code: "EXTRA_PLACEHOLDER_METADATA",
|
|
507
|
+
message: `Resource "${resourceKey}" has metadata for placeholder "{${placeholder}}" but doesn't use it`,
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
else if (icuInfo.placeholders.length > 0) {
|
|
513
|
+
// ICU placeholders exist but no metadata
|
|
514
|
+
warnings.push({
|
|
515
|
+
code: "MISSING_PLACEHOLDER_METADATA",
|
|
516
|
+
message: `Resource "${resourceKey}" uses ICU placeholders but has no placeholder metadata`,
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
isValidLocaleCode(locale) {
|
|
521
|
+
// Basic locale code validation (language-country format)
|
|
522
|
+
const localePattern = /^[a-z]{2,3}(_[A-Z]{2})?$/;
|
|
523
|
+
return localePattern.test(locale);
|
|
524
|
+
}
|
|
525
|
+
analyzeIcuMessages(resources) {
|
|
526
|
+
const analysis = {};
|
|
527
|
+
for (const [key, message] of Object.entries(resources)) {
|
|
528
|
+
analysis[key] = this.analyzeIcuMessage(message);
|
|
529
|
+
}
|
|
530
|
+
return analysis;
|
|
531
|
+
}
|
|
532
|
+
analyzeIcuMessage(message) {
|
|
533
|
+
const info = {
|
|
534
|
+
hasIcuSyntax: false,
|
|
535
|
+
placeholders: [],
|
|
536
|
+
messageType: "simple",
|
|
537
|
+
};
|
|
538
|
+
// Check for ICU syntax patterns
|
|
539
|
+
const icuPatterns = [
|
|
540
|
+
/\{[^}]+\}/g, // Basic placeholders
|
|
541
|
+
/\{[^}]+,\s*plural\s*,/g, // Plural forms
|
|
542
|
+
/\{[^}]+,\s*select\s*,/g, // Select forms
|
|
543
|
+
];
|
|
544
|
+
let hasIcuSyntax = false;
|
|
545
|
+
for (const pattern of icuPatterns) {
|
|
546
|
+
if (pattern.test(message)) {
|
|
547
|
+
hasIcuSyntax = true;
|
|
548
|
+
break;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
info.hasIcuSyntax = hasIcuSyntax;
|
|
552
|
+
if (hasIcuSyntax) {
|
|
553
|
+
// Extract placeholders
|
|
554
|
+
info.placeholders = this.extractIcuPlaceholders(message);
|
|
555
|
+
// Determine message type - check for specific patterns
|
|
556
|
+
if (message.includes(", plural,") || message.includes(",plural,")) {
|
|
557
|
+
info.messageType = "plural";
|
|
558
|
+
info.pluralForms = this.extractPluralForms(message);
|
|
559
|
+
}
|
|
560
|
+
else if (message.includes(", select,") ||
|
|
561
|
+
message.includes(",select,")) {
|
|
562
|
+
info.messageType = "select";
|
|
563
|
+
info.selectOptions = this.extractSelectOptions(message);
|
|
564
|
+
}
|
|
565
|
+
else if (info.placeholders.length > 0) {
|
|
566
|
+
info.messageType = "complex";
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
return info;
|
|
570
|
+
}
|
|
571
|
+
extractIcuPlaceholders(message) {
|
|
572
|
+
const placeholders = [];
|
|
573
|
+
// Use a simple approach to find all {variable} patterns
|
|
574
|
+
const placeholderPattern = /\{([a-zA-Z_][a-zA-Z0-9_]*)/g;
|
|
575
|
+
let match;
|
|
576
|
+
while ((match = placeholderPattern.exec(message)) !== null) {
|
|
577
|
+
const variableName = match[1];
|
|
578
|
+
if (!placeholders.includes(variableName)) {
|
|
579
|
+
placeholders.push(variableName);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
return placeholders;
|
|
583
|
+
}
|
|
584
|
+
extractPluralForms(message) {
|
|
585
|
+
const pluralForms = [];
|
|
586
|
+
// Match plural patterns like {count, plural, =0 {no items} =1 {one item} other {# items}}
|
|
587
|
+
const pluralPattern = /\{[^,]+,\s*plural\s*,\s*(.+)\}/g;
|
|
588
|
+
const match = pluralPattern.exec(message);
|
|
589
|
+
if (match) {
|
|
590
|
+
const pluralContent = match[1];
|
|
591
|
+
// Extract plural form keys (=0, =1, other, few, many, etc.)
|
|
592
|
+
// Use a more robust approach to handle nested braces
|
|
593
|
+
const formPattern = /(=\d+|zero|one|two|few|many|other)\s*\{/g;
|
|
594
|
+
let formMatch;
|
|
595
|
+
while ((formMatch = formPattern.exec(pluralContent)) !== null) {
|
|
596
|
+
const form = formMatch[1];
|
|
597
|
+
if (!pluralForms.includes(form)) {
|
|
598
|
+
pluralForms.push(form);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
return pluralForms;
|
|
603
|
+
}
|
|
604
|
+
extractSelectOptions(message) {
|
|
605
|
+
const selectOptions = [];
|
|
606
|
+
// Match select patterns like {gender, select, male {he} female {she} other {they}}
|
|
607
|
+
const selectPattern = /\{[^,]+,\s*select\s*,\s*(.+)\}/g;
|
|
608
|
+
const match = selectPattern.exec(message);
|
|
609
|
+
if (match) {
|
|
610
|
+
const selectContent = match[1];
|
|
611
|
+
// Extract select option keys
|
|
612
|
+
// Use a more robust approach to handle nested braces
|
|
613
|
+
const optionPattern = /(\w+)\s*\{/g;
|
|
614
|
+
let optionMatch;
|
|
615
|
+
while ((optionMatch = optionPattern.exec(selectContent)) !== null) {
|
|
616
|
+
const option = optionMatch[1];
|
|
617
|
+
if (!selectOptions.includes(option)) {
|
|
618
|
+
selectOptions.push(option);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
return selectOptions;
|
|
623
|
+
}
|
|
624
|
+
validateIcuMessageIntegrity(original, translated) {
|
|
625
|
+
const originalInfo = this.analyzeIcuMessage(original);
|
|
626
|
+
const translatedInfo = this.analyzeIcuMessage(translated);
|
|
627
|
+
// Both should have same ICU syntax presence
|
|
628
|
+
if (originalInfo.hasIcuSyntax !== translatedInfo.hasIcuSyntax) {
|
|
629
|
+
return false;
|
|
630
|
+
}
|
|
631
|
+
// If no ICU syntax, no further validation needed
|
|
632
|
+
if (!originalInfo.hasIcuSyntax) {
|
|
633
|
+
return true;
|
|
634
|
+
}
|
|
635
|
+
// Check message types match
|
|
636
|
+
if (originalInfo.messageType !== translatedInfo.messageType) {
|
|
637
|
+
return false;
|
|
638
|
+
}
|
|
639
|
+
// Check placeholders match
|
|
640
|
+
if (originalInfo.placeholders.length !== translatedInfo.placeholders.length) {
|
|
641
|
+
return false;
|
|
642
|
+
}
|
|
643
|
+
for (const placeholder of originalInfo.placeholders) {
|
|
644
|
+
if (!translatedInfo.placeholders.includes(placeholder)) {
|
|
645
|
+
return false;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
// Check plural forms match (if applicable)
|
|
649
|
+
if (originalInfo.pluralForms && translatedInfo.pluralForms) {
|
|
650
|
+
if (originalInfo.pluralForms.length !== translatedInfo.pluralForms.length) {
|
|
651
|
+
return false;
|
|
652
|
+
}
|
|
653
|
+
for (const form of originalInfo.pluralForms) {
|
|
654
|
+
if (!translatedInfo.pluralForms.includes(form)) {
|
|
655
|
+
return false;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
// Check select options match (if applicable)
|
|
660
|
+
if (originalInfo.selectOptions && translatedInfo.selectOptions) {
|
|
661
|
+
if (originalInfo.selectOptions.length !==
|
|
662
|
+
translatedInfo.selectOptions.length) {
|
|
663
|
+
return false;
|
|
664
|
+
}
|
|
665
|
+
for (const option of originalInfo.selectOptions) {
|
|
666
|
+
if (!translatedInfo.selectOptions.includes(option)) {
|
|
667
|
+
return false;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
return true;
|
|
672
|
+
}
|
|
673
|
+
getIndentation(options) {
|
|
674
|
+
if (options?.indentation !== undefined) {
|
|
675
|
+
return options.indentation;
|
|
676
|
+
}
|
|
677
|
+
return 2; // Default indentation
|
|
678
|
+
}
|
|
679
|
+
}
|