* Use en_US.json as the standard to fill missing keys in other language files
()
| 105 | * Use en_US.json as the standard to fill missing keys in other language files |
| 106 | */ |
| 107 | async function checkAndFillMissingKeys() { |
| 108 | const apiKey = await getApiKey(); |
| 109 | const languagesPath = path.join(import.meta.dirname, "../languages"); |
| 110 | |
| 111 | // Read standard file en_US.json |
| 112 | const standardFilePath = path.join(languagesPath, "en_US.json"); |
| 113 | const standardContent = await readFile(standardFilePath, "utf8"); |
| 114 | const standardJson = JSON.parse(standardContent); |
| 115 | const standardKeys = Object.keys(standardJson); |
| 116 | |
| 117 | console.log(`Standard file en_US.json contains ${standardKeys.length} key-value pairs`); |
| 118 | |
| 119 | // Get all language files |
| 120 | const languageFiles = await readdir(languagesPath); |
| 121 | const targetFiles = languageFiles.filter( |
| 122 | (file) => file.endsWith(".json") && file !== "en_US.json" |
| 123 | ); |
| 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; |
no test coverage detected