@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,171 @@
|
|
|
1
|
+
import { countUntranslatedStrings } from '../utils/poFileUtils.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Prepares batches of untranslated strings for translation processing.
|
|
5
|
+
* Extracts strings that need translation and splits them into manageable batches.
|
|
6
|
+
*
|
|
7
|
+
* @since 1.0.0
|
|
8
|
+
*
|
|
9
|
+
* @param {Object} parsedPot - Parsed POT data structure containing translations.
|
|
10
|
+
* @param {number} batchSize - Maximum number of strings per batch.
|
|
11
|
+
* @param {Object} logger - Logger instance for outputting messages.
|
|
12
|
+
* @param {number|null} maxStrings - Maximum number of strings to translate (null for no limit).
|
|
13
|
+
*
|
|
14
|
+
* @return {Array<Array>} Array of batches, each containing translation items.
|
|
15
|
+
*/
|
|
16
|
+
export function prepareBatches(parsedPot, batchSize, logger, maxStrings = null) {
|
|
17
|
+
if (!parsedPot?.translations) {
|
|
18
|
+
logger.error('Invalid .pot data: missing translations object');
|
|
19
|
+
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const stringsToTranslate = _extractUntranslatedStrings(parsedPot);
|
|
24
|
+
const totalUntranslated = countUntranslatedStrings(parsedPot);
|
|
25
|
+
|
|
26
|
+
logger.info(`Found ${totalUntranslated} untranslated strings`);
|
|
27
|
+
|
|
28
|
+
if (stringsToTranslate.length === 0) {
|
|
29
|
+
logger.info('No strings need translation');
|
|
30
|
+
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Apply maxStrings limitation if specified.
|
|
35
|
+
const finalStringsToTranslate = _applyStringLimits(stringsToTranslate, maxStrings, logger);
|
|
36
|
+
|
|
37
|
+
if (finalStringsToTranslate.length === 0) {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Split into batches.
|
|
42
|
+
const batches = _createBatches(finalStringsToTranslate, batchSize);
|
|
43
|
+
|
|
44
|
+
logger.info(`Prepared ${batches.length} batches of max ${batchSize} strings each`);
|
|
45
|
+
|
|
46
|
+
return batches;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Extracts untranslated strings from parsed POT data.
|
|
51
|
+
*
|
|
52
|
+
* @private
|
|
53
|
+
*
|
|
54
|
+
* @since 1.0.0
|
|
55
|
+
*
|
|
56
|
+
* @param {Object} parsedPot - Parsed POT data structure.
|
|
57
|
+
*
|
|
58
|
+
* @return {Array} Array of untranslated string objects.
|
|
59
|
+
*/
|
|
60
|
+
function _extractUntranslatedStrings(parsedPot) {
|
|
61
|
+
const stringsToTranslate = [];
|
|
62
|
+
|
|
63
|
+
for (const [contextKey, context] of Object.entries(parsedPot.translations)) {
|
|
64
|
+
if (!context || typeof context !== 'object') {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
for (const [msgid, entry] of Object.entries(context)) {
|
|
69
|
+
if (msgid === '' || !entry) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const msgstr = entry?.msgstr;
|
|
74
|
+
|
|
75
|
+
// Check if it needs translation (empty or dry run placeholder.).
|
|
76
|
+
let needsTranslation = !msgstr || msgstr.every((str) => !str);
|
|
77
|
+
|
|
78
|
+
// Also consider dry run placeholders as needing translation.
|
|
79
|
+
if (!needsTranslation && msgstr) {
|
|
80
|
+
needsTranslation = msgstr.every((str) => {
|
|
81
|
+
if (!str || str.trim() === '') {
|
|
82
|
+
return true; // Empty strings need translation.
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Dry run placeholders also need translation.
|
|
86
|
+
return str.startsWith('[DRY RUN]');
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (!needsTranslation) {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Convert reference string to array format for consistency.
|
|
95
|
+
const reference = entry?.comments?.reference;
|
|
96
|
+
const references = reference ? (Array.isArray(reference) ? reference : [reference]) : [];
|
|
97
|
+
|
|
98
|
+
// Check if this is a plural form entry.
|
|
99
|
+
const isPlural = entry.msgid_plural && entry.msgid_plural !== '';
|
|
100
|
+
|
|
101
|
+
stringsToTranslate.push({
|
|
102
|
+
msgid,
|
|
103
|
+
msgctxt: contextKey,
|
|
104
|
+
msgid_plural: entry.msgid_plural || null,
|
|
105
|
+
isPlural,
|
|
106
|
+
extractedComments: entry?.comments?.extracted || '',
|
|
107
|
+
comments: entry?.comments || {},
|
|
108
|
+
references,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return stringsToTranslate;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Applies string limits to the translation array.
|
|
118
|
+
*
|
|
119
|
+
* @private
|
|
120
|
+
*
|
|
121
|
+
* @since 1.0.0
|
|
122
|
+
*
|
|
123
|
+
* @param {Array} stringsToTranslate - Array of strings to translate.
|
|
124
|
+
* @param {number|null} maxStrings - Maximum number of strings to translate.
|
|
125
|
+
* @param {Object} logger - Logger instance.
|
|
126
|
+
*
|
|
127
|
+
* @return {Array} Limited array of strings to translate.
|
|
128
|
+
*/
|
|
129
|
+
function _applyStringLimits(stringsToTranslate, maxStrings, logger) {
|
|
130
|
+
if (maxStrings === null || maxStrings < 0) {
|
|
131
|
+
return stringsToTranslate;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (maxStrings === 0) {
|
|
135
|
+
logger.info('maxStrings is 0, returning empty batches');
|
|
136
|
+
|
|
137
|
+
return [];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (stringsToTranslate.length <= maxStrings) {
|
|
141
|
+
return stringsToTranslate;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const limitedStrings = stringsToTranslate.slice(0, maxStrings);
|
|
145
|
+
|
|
146
|
+
logger.info(`Limiting translation to first ${maxStrings} strings (out of ${stringsToTranslate.length} total untranslated)`);
|
|
147
|
+
|
|
148
|
+
return limitedStrings;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Creates batches from the array of strings to translate.
|
|
153
|
+
*
|
|
154
|
+
* @private
|
|
155
|
+
*
|
|
156
|
+
* @since 1.0.0
|
|
157
|
+
*
|
|
158
|
+
* @param {Array} stringsToTranslate - Array of strings to translate.
|
|
159
|
+
* @param {number} batchSize - Maximum number of strings per batch.
|
|
160
|
+
*
|
|
161
|
+
* @return {Array<Array>} Array of batches.
|
|
162
|
+
*/
|
|
163
|
+
function _createBatches(stringsToTranslate, batchSize) {
|
|
164
|
+
const batches = [];
|
|
165
|
+
|
|
166
|
+
for (let i = 0; i < stringsToTranslate.length; i += batchSize) {
|
|
167
|
+
batches.push(stringsToTranslate.slice(i, i + batchSize));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return batches;
|
|
171
|
+
}
|