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,75 @@
|
|
|
1
|
+
import type { FormatOptions, IFormatHandler, ValidationResult } from "../format.interface.js";
|
|
2
|
+
import type { TranslationFile } from "../translate.interface.js";
|
|
3
|
+
/**
|
|
4
|
+
* Base class for format handlers that provides common validation functionality
|
|
5
|
+
*/
|
|
6
|
+
export declare abstract class BaseFormatHandler implements IFormatHandler {
|
|
7
|
+
abstract canHandle(filePath: string, content?: string): boolean;
|
|
8
|
+
abstract parse(content: string): TranslationFile;
|
|
9
|
+
abstract serialize(data: TranslationFile, options?: FormatOptions): string;
|
|
10
|
+
abstract getFileExtension(): string;
|
|
11
|
+
abstract validateStructure(data: TranslationFile): ValidationResult;
|
|
12
|
+
/**
|
|
13
|
+
* Get the format name for this handler
|
|
14
|
+
*/
|
|
15
|
+
protected abstract getFormatName(): string;
|
|
16
|
+
/**
|
|
17
|
+
* Enhanced validation using the centralized validation system
|
|
18
|
+
*/
|
|
19
|
+
validateWithRules(data: TranslationFile, filePath?: string, originalContent?: string): ValidationResult;
|
|
20
|
+
/**
|
|
21
|
+
* Comprehensive validation with error recovery
|
|
22
|
+
*/
|
|
23
|
+
validateWithRecovery(content: string, filePath?: string, options?: {
|
|
24
|
+
attemptRecovery?: boolean;
|
|
25
|
+
includeGuidance?: boolean;
|
|
26
|
+
strictMode?: boolean;
|
|
27
|
+
}): Promise<import("../validation/validation-service.js").ValidationServiceResult>;
|
|
28
|
+
/**
|
|
29
|
+
* Merge multiple validation results
|
|
30
|
+
*/
|
|
31
|
+
protected mergeValidationResults(...results: ValidationResult[]): ValidationResult;
|
|
32
|
+
/**
|
|
33
|
+
* Remove duplicate validation issues
|
|
34
|
+
*/
|
|
35
|
+
private removeDuplicateIssues;
|
|
36
|
+
/**
|
|
37
|
+
* Create a validation error
|
|
38
|
+
*/
|
|
39
|
+
protected createValidationError(code: string, message: string, line?: number, column?: number): {
|
|
40
|
+
code: string;
|
|
41
|
+
message: string;
|
|
42
|
+
line: number | undefined;
|
|
43
|
+
column: number | undefined;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Create a validation warning
|
|
47
|
+
*/
|
|
48
|
+
protected createValidationWarning(code: string, message: string, line?: number, column?: number): {
|
|
49
|
+
code: string;
|
|
50
|
+
message: string;
|
|
51
|
+
line: number | undefined;
|
|
52
|
+
column: number | undefined;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Validate that a string contains valid content for translation
|
|
56
|
+
*/
|
|
57
|
+
protected validateTranslationString(value: string, path: string): {
|
|
58
|
+
errors: any[];
|
|
59
|
+
warnings: any[];
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Validate object structure recursively
|
|
63
|
+
*/
|
|
64
|
+
protected validateObjectStructure(obj: any, path?: string, visited?: Set<unknown>): {
|
|
65
|
+
errors: any[];
|
|
66
|
+
warnings: any[];
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Check if content appears to be valid for this format
|
|
70
|
+
*/
|
|
71
|
+
protected validateContentFormat(content: string): {
|
|
72
|
+
errors: any[];
|
|
73
|
+
warnings: any[];
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { FormatValidator, ValidationService, } from "../validation/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Base class for format handlers that provides common validation functionality
|
|
4
|
+
*/
|
|
5
|
+
export class BaseFormatHandler {
|
|
6
|
+
/**
|
|
7
|
+
* Enhanced validation using the centralized validation system
|
|
8
|
+
*/
|
|
9
|
+
validateWithRules(data, filePath, originalContent) {
|
|
10
|
+
const context = {
|
|
11
|
+
format: this.getFormatName(),
|
|
12
|
+
filePath,
|
|
13
|
+
originalContent,
|
|
14
|
+
handler: this,
|
|
15
|
+
metadata: data._metadata,
|
|
16
|
+
};
|
|
17
|
+
// Use the centralized validation system
|
|
18
|
+
const centralizedResult = FormatValidator.validate(data, context);
|
|
19
|
+
// Also run format-specific validation from the handler
|
|
20
|
+
const handlerResult = this.validateStructure(data);
|
|
21
|
+
// Merge results
|
|
22
|
+
return this.mergeValidationResults(centralizedResult, handlerResult);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Comprehensive validation with error recovery
|
|
26
|
+
*/
|
|
27
|
+
async validateWithRecovery(content, filePath, options = {}) {
|
|
28
|
+
return ValidationService.validateFile(content, filePath || "unknown", this, options);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Merge multiple validation results
|
|
32
|
+
*/
|
|
33
|
+
mergeValidationResults(...results) {
|
|
34
|
+
const allErrors = results.flatMap((result) => result.errors);
|
|
35
|
+
const allWarnings = results.flatMap((result) => result.warnings);
|
|
36
|
+
// Remove duplicates based on code and message
|
|
37
|
+
const uniqueErrors = this.removeDuplicateIssues(allErrors);
|
|
38
|
+
const uniqueWarnings = this.removeDuplicateIssues(allWarnings);
|
|
39
|
+
return {
|
|
40
|
+
isValid: uniqueErrors.length === 0,
|
|
41
|
+
errors: uniqueErrors,
|
|
42
|
+
warnings: uniqueWarnings,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Remove duplicate validation issues
|
|
47
|
+
*/
|
|
48
|
+
removeDuplicateIssues(issues) {
|
|
49
|
+
const seen = new Set();
|
|
50
|
+
return issues.filter((issue) => {
|
|
51
|
+
const key = `${issue.code}:${issue.message}`;
|
|
52
|
+
if (seen.has(key)) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
seen.add(key);
|
|
56
|
+
return true;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Create a validation error
|
|
61
|
+
*/
|
|
62
|
+
createValidationError(code, message, line, column) {
|
|
63
|
+
return { code, message, line, column };
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Create a validation warning
|
|
67
|
+
*/
|
|
68
|
+
createValidationWarning(code, message, line, column) {
|
|
69
|
+
return { code, message, line, column };
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Validate that a string contains valid content for translation
|
|
73
|
+
*/
|
|
74
|
+
validateTranslationString(value, path) {
|
|
75
|
+
const errors = [];
|
|
76
|
+
const warnings = [];
|
|
77
|
+
// Check for empty strings
|
|
78
|
+
if (value.trim().length === 0) {
|
|
79
|
+
warnings.push(this.createValidationWarning("EMPTY_TRANSLATION_STRING", `Empty or whitespace-only string at ${path}`));
|
|
80
|
+
}
|
|
81
|
+
// Check for very long strings
|
|
82
|
+
if (value.length > 10000) {
|
|
83
|
+
warnings.push(this.createValidationWarning("VERY_LONG_STRING", `String at ${path} is very long (${value.length} characters) - may indicate data issue`));
|
|
84
|
+
}
|
|
85
|
+
// Check for potential control characters
|
|
86
|
+
if (/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/.test(value)) {
|
|
87
|
+
warnings.push(this.createValidationWarning("CONTROL_CHARACTERS", `String at ${path} contains control characters`));
|
|
88
|
+
}
|
|
89
|
+
return { errors, warnings };
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Validate object structure recursively
|
|
93
|
+
*/
|
|
94
|
+
validateObjectStructure(obj, path = "", visited = new Set()) {
|
|
95
|
+
const errors = [];
|
|
96
|
+
const warnings = [];
|
|
97
|
+
// Prevent infinite recursion
|
|
98
|
+
if (visited.has(obj)) {
|
|
99
|
+
return { errors, warnings };
|
|
100
|
+
}
|
|
101
|
+
if (typeof obj === "object" && obj !== null) {
|
|
102
|
+
visited.add(obj);
|
|
103
|
+
}
|
|
104
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
105
|
+
// Skip metadata keys
|
|
106
|
+
if (key.startsWith("_")) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
const currentPath = path ? `${path}.${key}` : key;
|
|
110
|
+
if (typeof value === "string") {
|
|
111
|
+
const stringValidation = this.validateTranslationString(value, currentPath);
|
|
112
|
+
errors.push(...stringValidation.errors);
|
|
113
|
+
warnings.push(...stringValidation.warnings);
|
|
114
|
+
}
|
|
115
|
+
else if (typeof value === "object" &&
|
|
116
|
+
value !== null &&
|
|
117
|
+
!Array.isArray(value)) {
|
|
118
|
+
const nestedValidation = this.validateObjectStructure(value, currentPath, visited);
|
|
119
|
+
errors.push(...nestedValidation.errors);
|
|
120
|
+
warnings.push(...nestedValidation.warnings);
|
|
121
|
+
}
|
|
122
|
+
else if (Array.isArray(value)) {
|
|
123
|
+
value.forEach((item, index) => {
|
|
124
|
+
const arrayPath = `${currentPath}[${index}]`;
|
|
125
|
+
if (typeof item === "string") {
|
|
126
|
+
const stringValidation = this.validateTranslationString(item, arrayPath);
|
|
127
|
+
errors.push(...stringValidation.errors);
|
|
128
|
+
warnings.push(...stringValidation.warnings);
|
|
129
|
+
}
|
|
130
|
+
else if (typeof item === "object" && item !== null) {
|
|
131
|
+
const nestedValidation = this.validateObjectStructure(item, arrayPath, visited);
|
|
132
|
+
errors.push(...nestedValidation.errors);
|
|
133
|
+
warnings.push(...nestedValidation.warnings);
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
warnings.push(this.createValidationWarning("NON_TRANSLATABLE_ARRAY_ITEM", `Array item at ${arrayPath} is not translatable (${typeof item})`));
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
warnings.push(this.createValidationWarning("NON_TRANSLATABLE_VALUE", `Value at ${currentPath} is not translatable (${typeof value})`));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (typeof obj === "object" && obj !== null) {
|
|
145
|
+
visited.delete(obj);
|
|
146
|
+
}
|
|
147
|
+
return { errors, warnings };
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Check if content appears to be valid for this format
|
|
151
|
+
*/
|
|
152
|
+
validateContentFormat(content) {
|
|
153
|
+
const errors = [];
|
|
154
|
+
const warnings = [];
|
|
155
|
+
// Basic content validation
|
|
156
|
+
if (!content || content.trim().length === 0) {
|
|
157
|
+
errors.push(this.createValidationError("EMPTY_CONTENT", "File content is empty"));
|
|
158
|
+
}
|
|
159
|
+
// Check for very large files
|
|
160
|
+
if (content.length > 50 * 1024 * 1024) {
|
|
161
|
+
// 50MB
|
|
162
|
+
warnings.push(this.createValidationWarning("VERY_LARGE_FILE", `File is very large (${Math.round(content.length / 1024 / 1024)}MB) - may cause performance issues`));
|
|
163
|
+
}
|
|
164
|
+
return { errors, warnings };
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { EnhancedTranslationFile, FormatOptions, IFormatHandler, ValidationResult } from "../format.interface.js";
|
|
2
|
+
import type { TranslationFile } from "../translate.interface.js";
|
|
3
|
+
export interface CsvOptions extends FormatOptions {
|
|
4
|
+
delimiter?: string;
|
|
5
|
+
keyColumn?: string;
|
|
6
|
+
valueColumn?: string;
|
|
7
|
+
hasHeaders?: boolean;
|
|
8
|
+
columns?: string[];
|
|
9
|
+
multiLanguageColumns?: Record<string, string>;
|
|
10
|
+
quote?: string;
|
|
11
|
+
escape?: string;
|
|
12
|
+
lineTerminator?: string;
|
|
13
|
+
dialect?: "excel" | "unix" | "rfc4180" | "custom";
|
|
14
|
+
skipEmptyLines?: boolean;
|
|
15
|
+
trimFields?: boolean;
|
|
16
|
+
}
|
|
17
|
+
export declare class CsvHandler implements IFormatHandler {
|
|
18
|
+
canHandle(filePath: string, content?: string): boolean;
|
|
19
|
+
parse(content: string, options?: CsvOptions): EnhancedTranslationFile;
|
|
20
|
+
serialize(data: EnhancedTranslationFile, options?: CsvOptions): string;
|
|
21
|
+
getFileExtension(): string;
|
|
22
|
+
validateStructure(data: TranslationFile): ValidationResult;
|
|
23
|
+
private getDialectOptions;
|
|
24
|
+
private isValidCsvContent;
|
|
25
|
+
private detectDelimiter;
|
|
26
|
+
private parseCSVContent;
|
|
27
|
+
private parseCSVLine;
|
|
28
|
+
private detectHeaders;
|
|
29
|
+
private looksLikeTranslatableText;
|
|
30
|
+
private validateDelimiterConsistency;
|
|
31
|
+
private processRows;
|
|
32
|
+
private detectKeyColumn;
|
|
33
|
+
private detectMultiLanguageColumns;
|
|
34
|
+
private detectValueColumn;
|
|
35
|
+
private determineColumns;
|
|
36
|
+
private createRecords;
|
|
37
|
+
private escapeField;
|
|
38
|
+
}
|