MCPcopy Create free account
hub / github.com/MCSManager/MCSManager / processFile

Function processFile

scripts/auto-translate.mjs:126–178  ·  view source on GitHub ↗
(file)

Source from the content-addressed store, hash-verified

124
125 // Process each language file concurrently
126 const processFile = async (file) => {
127 const systemPrompt = SYSTEM_PROMPT.replace(/{target}/g, LANGUAGE_MAP[file]);
128 const chatAiSession = new AiChatSession(apiKey, systemPrompt);
129
130 const filePath = path.join(languagesPath, file);
131 const content = await readFile(filePath, "utf8");
132 const json = JSON.parse(content);
133
134 // Find missing keys
135 const missingKeys = standardKeys.filter((key) => !json.hasOwnProperty(key));
136
137 if (missingKeys.length === 0) {
138 console.log(`✅ ${file} has no missing key-value pairs`);
139 return;
140 }
141
142 console.log(`🔍 ${file} is missing ${missingKeys.length} key-value pairs. Translating...`);
143
144 // Prepare translation data
145 const textsToTranslate = missingKeys.map((key) => ({
146 key: key,
147 text: standardJson[key]
148 }));
149
150 // Get target language
151 const targetLanguage = LANGUAGE_MAP[file];
152 if (!targetLanguage) {
153 console.warn(`⚠️ No language mapping found for ${file}. Skipping.`);
154 return;
155 }
156
157 try {
158 // Call translation function
159 console.log(`🌍 Translating to ${targetLanguage}...`);
160 const translatedTexts = await translateText(chatAiSession, textsToTranslate, targetLanguage);
161
162 // Merge translation results into current language JSON
163 for (const translatedItem of translatedTexts) {
164 json[translatedItem.key] = translatedItem.text;
165 }
166
167 // Write updated content back to file
168 const updatedContent = JSON.stringify(json, null, 2);
169 await writeFile(filePath, updatedContent, "utf8");
170
171 console.log(`✅ ${file} filled ${missingKeys.length} missing key-value pairs`);
172
173 // Clear conversation history to avoid long context
174 chatAiSession.clearHistory();
175 } catch (error) {
176 console.error(`\n\n❌ Error translating ${file}: ${error.message} \n`);
177 }
178 };
179
180 await Promise.all(targetFiles.map((file) => processFile(file)));
181

Callers 1

checkAndFillMissingKeysFunction · 0.85

Calls 4

clearHistoryMethod · 0.95
translateTextFunction · 0.85
logMethod · 0.80
errorMethod · 0.80

Tested by

no test coverage detected