@publishpress/potomatic 0.0.0-stage → 1.4.5
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/CHANGELOG.md +127 -0
- package/LICENSE.md +20 -0
- package/README.md +470 -2
- package/config/openai-pricing.json +112 -0
- package/config/po-header.json +10 -0
- package/config/prompt.md +23 -0
- package/package.json +84 -5
- package/potomatic +103 -0
- package/src/config/index.js +497 -0
- package/src/logging/index.js +362 -0
- package/src/orchestrator/index.js +1031 -0
- package/src/processors/batchProcessor.js +171 -0
- package/src/processors/languageProcessor.js +1464 -0
- package/src/providers/ProviderFactory.js +124 -0
- package/src/providers/base/Provider.js +198 -0
- package/src/providers/openai/OpenAIProvider.js +805 -0
- package/src/providers/openai/modelCapabilities.js +256 -0
- package/src/reporters/ConsoleReporter.js +256 -0
- package/src/reporters/JsonReporter.js +254 -0
- package/src/reporters/ReporterFactory.js +218 -0
- package/src/reporters/base/BaseReporter.js +124 -0
- package/src/utils/colorUtils.js +93 -0
- package/src/utils/costCalculations.js +245 -0
- package/src/utils/costTracker.js +277 -0
- package/src/utils/dictionaryUtils.js +211 -0
- package/src/utils/languageMapping.js +1113 -0
- package/src/utils/pluralization.js +39 -0
- package/src/utils/poFileUtils.js +599 -0
- package/src/utils/promptLoader.js +115 -0
- package/src/utils/reportingUtils.js +417 -0
- package/src/utils/validationStats.js +110 -0
- package/src/utils/xmlTranslation.js +532 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dictionary utilities for consistent term translation.
|
|
3
|
+
*
|
|
4
|
+
* Provides functionality to load user-defined translation dictionaries
|
|
5
|
+
* and find matching terms in translation batches for AI context.
|
|
6
|
+
*
|
|
7
|
+
* @since 1.0.0
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import fs from 'fs';
|
|
11
|
+
import path from 'path';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Loads dictionary for target language with fallback support.
|
|
15
|
+
* Attempts to load language-specific dictionary first, then falls back to default.
|
|
16
|
+
* Normalizes dictionary keys to lowercase for case-insensitive matching.
|
|
17
|
+
*
|
|
18
|
+
* @since 1.0.0
|
|
19
|
+
*
|
|
20
|
+
* @param {string} dictionaryDir - Directory containing dictionary files.
|
|
21
|
+
* @param {string} targetLanguage - Target language code (e.g., 'fr_FR', 'es').
|
|
22
|
+
* @param {Object} logger - Logger instance.
|
|
23
|
+
*
|
|
24
|
+
* @return {Object|null} Dictionary object with normalized lowercase keys or null if not found.
|
|
25
|
+
*/
|
|
26
|
+
export function loadDictionary(dictionaryDir, targetLanguage, logger) {
|
|
27
|
+
if (!dictionaryDir || !fs.existsSync(dictionaryDir)) {
|
|
28
|
+
logger.debug(`Dictionary directory not found: ${dictionaryDir}`);
|
|
29
|
+
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Normalize language code for file naming (fr_FR -> fr-fr, fr -> fr.)
|
|
34
|
+
const langCode = targetLanguage.toLowerCase().replace(/[_]/g, '-');
|
|
35
|
+
|
|
36
|
+
// Try specific dictionary first (.e.g., dictionary-fr-fr.json)
|
|
37
|
+
const specificDict = path.join(dictionaryDir, `dictionary-${langCode}.json`);
|
|
38
|
+
|
|
39
|
+
// Try base language if full locale fails (.e.g., dictionary-fr.json for fr_FR)
|
|
40
|
+
const baseLangCode = langCode.split('-')[0];
|
|
41
|
+
const baseDict = path.join(dictionaryDir, `dictionary-${baseLangCode}.json`);
|
|
42
|
+
|
|
43
|
+
// Fallback to default dictionary.
|
|
44
|
+
const fallbackDict = path.join(dictionaryDir, 'dictionary.json');
|
|
45
|
+
|
|
46
|
+
const candidatePaths = [specificDict, baseDict, fallbackDict].filter((filePath, index, arr) => {
|
|
47
|
+
// Remove duplicates (when langCode === baseLangCode.)
|
|
48
|
+
return arr.indexOf(filePath) === index;
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
for (const dictPath of candidatePaths) {
|
|
52
|
+
try {
|
|
53
|
+
if (fs.existsSync(dictPath)) {
|
|
54
|
+
logger.debug(`Loading dictionary: ${dictPath}`);
|
|
55
|
+
|
|
56
|
+
const content = fs.readFileSync(dictPath, 'utf-8');
|
|
57
|
+
const dictionary = JSON.parse(content);
|
|
58
|
+
|
|
59
|
+
if (typeof dictionary !== 'object' || dictionary === null || Array.isArray(dictionary)) {
|
|
60
|
+
logger.warn(`Invalid dictionary format in ${dictPath}: expected object, got ${Array.isArray(dictionary) ? 'array' : typeof dictionary}`);
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Normalize dictionary keys to lowercase for case-insensitive matching.
|
|
65
|
+
const normalizedDictionary = {};
|
|
66
|
+
|
|
67
|
+
for (const [source, target] of Object.entries(dictionary)) {
|
|
68
|
+
if (typeof source === 'string' && typeof target === 'string') {
|
|
69
|
+
const normalizedKey = source.toLowerCase();
|
|
70
|
+
|
|
71
|
+
// Warn about potential conflicts when normalizing.
|
|
72
|
+
if (normalizedDictionary[normalizedKey] && normalizedDictionary[normalizedKey] !== target) {
|
|
73
|
+
logger.warn(`Dictionary key conflict: "${source}" and existing entry both normalize to "${normalizedKey}". Using: "${target}"`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
normalizedDictionary[normalizedKey] = target;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
logger.debug(`Successfully loaded dictionary with ${Object.keys(normalizedDictionary).length} entries (normalized to lowercase)`);
|
|
81
|
+
|
|
82
|
+
return normalizedDictionary;
|
|
83
|
+
}
|
|
84
|
+
} catch (error) {
|
|
85
|
+
logger.warn(`Failed to load dictionary from ${dictPath}: ${error.message}`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Finds dictionary terms that appear in the current translation batch.
|
|
94
|
+
* Uses case-insensitive word boundary matching with normalized lowercase dictionary keys.
|
|
95
|
+
*
|
|
96
|
+
* @since 1.0.0
|
|
97
|
+
*
|
|
98
|
+
* @param {Array} batch - Current translation batch containing `msgid` strings.
|
|
99
|
+
* @param {Object|null} dictionary - Dictionary object with normalized lowercase keys and target mappings.
|
|
100
|
+
*
|
|
101
|
+
* @return {Array} Array of {source, target} dictionary matches with normalized lowercase keys.
|
|
102
|
+
*/
|
|
103
|
+
export function findDictionaryMatches(batch, dictionary) {
|
|
104
|
+
if (!dictionary || !batch || batch.length === 0) {
|
|
105
|
+
return [];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const matches = [];
|
|
109
|
+
|
|
110
|
+
// Combine all msgid text for efficient searching.
|
|
111
|
+
const allText = batch
|
|
112
|
+
.map((item) => item.msgid || '')
|
|
113
|
+
.filter((text) => text.length > 0)
|
|
114
|
+
.join(' ');
|
|
115
|
+
|
|
116
|
+
if (!allText) {
|
|
117
|
+
return [];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Convert to lowercase for matching since dictionary keys are normalized.
|
|
121
|
+
const allTextLower = allText.toLowerCase();
|
|
122
|
+
|
|
123
|
+
for (const [normalizedSource, target] of Object.entries(dictionary)) {
|
|
124
|
+
if (!normalizedSource || !target || typeof normalizedSource !== 'string' || typeof target !== 'string') {
|
|
125
|
+
continue; // Skip invalid entries.
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Use word boundary regex for case-insensitive matching.
|
|
129
|
+
// Since dictionary keys are already lowercase, we can match directly.
|
|
130
|
+
const escapedSource = normalizedSource.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
131
|
+
const wordBoundaryRegex = new RegExp(`\\b${escapedSource}\\b`);
|
|
132
|
+
|
|
133
|
+
// Check if the term appears as a whole word in the lowercase text.
|
|
134
|
+
if (wordBoundaryRegex.test(allTextLower)) {
|
|
135
|
+
// Return the normalized dictionary key for consistent display.
|
|
136
|
+
matches.push({ source: normalizedSource, target });
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return matches;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Validates dictionary file structure and content.
|
|
145
|
+
*
|
|
146
|
+
* @since 1.0.0
|
|
147
|
+
*
|
|
148
|
+
* @param {string} dictionaryPath - Path to dictionary file.
|
|
149
|
+
*
|
|
150
|
+
* @return {Object} Validation result with isValid flag and errors array.
|
|
151
|
+
*/
|
|
152
|
+
export function validateDictionary(dictionaryPath) {
|
|
153
|
+
const result = {
|
|
154
|
+
isValid: true,
|
|
155
|
+
errors: [],
|
|
156
|
+
warnings: [],
|
|
157
|
+
entryCount: 0,
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
try {
|
|
161
|
+
if (!fs.existsSync(dictionaryPath)) {
|
|
162
|
+
result.isValid = false;
|
|
163
|
+
result.errors.push(`Dictionary file not found: ${dictionaryPath}`);
|
|
164
|
+
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const content = fs.readFileSync(dictionaryPath, 'utf-8');
|
|
169
|
+
let dictionary;
|
|
170
|
+
|
|
171
|
+
try {
|
|
172
|
+
dictionary = JSON.parse(content);
|
|
173
|
+
} catch (parseError) {
|
|
174
|
+
result.isValid = false;
|
|
175
|
+
result.errors.push(`Invalid JSON format: ${parseError.message}`);
|
|
176
|
+
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (typeof dictionary !== 'object' || dictionary === null || Array.isArray(dictionary)) {
|
|
181
|
+
result.isValid = false;
|
|
182
|
+
result.errors.push('Dictionary must be a JSON object with string key-value pairs');
|
|
183
|
+
|
|
184
|
+
return result;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Validate entries
|
|
188
|
+
let validEntryCount = 0;
|
|
189
|
+
|
|
190
|
+
for (const [source, target] of Object.entries(dictionary)) {
|
|
191
|
+
if (typeof source !== 'string' || typeof target !== 'string' || source.trim() === '' || target.trim() === '') {
|
|
192
|
+
result.warnings.push(`Invalid entry: "${source}" -> "${target}" (both must be non-empty strings)`);
|
|
193
|
+
} else {
|
|
194
|
+
validEntryCount++;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
result.entryCount = validEntryCount;
|
|
199
|
+
|
|
200
|
+
if (validEntryCount === 0) {
|
|
201
|
+
result.warnings.push('Dictionary contains no valid entries');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return result;
|
|
205
|
+
} catch (error) {
|
|
206
|
+
result.isValid = false;
|
|
207
|
+
result.errors.push(`An error occurred: ${error.message}`);
|
|
208
|
+
|
|
209
|
+
return result;
|
|
210
|
+
}
|
|
211
|
+
}
|