()
| 5 | const LANGUAGES_DIR = path.join(import.meta.dirname, "../languages"); |
| 6 | |
| 7 | async function fillMissingKeys() { |
| 8 | try { |
| 9 | // Read the base file (en_US.json) |
| 10 | const baseFile = path.join(LANGUAGES_DIR, "en_US.json"); |
| 11 | const baseContent = await fs.readFile(baseFile, "utf8"); |
| 12 | const baseJson = JSON.parse(baseContent); |
| 13 | |
| 14 | // Get all language files in the directory |
| 15 | const files = await fs.readdir(LANGUAGES_DIR); |
| 16 | |
| 17 | for (const file of files) { |
| 18 | // Skip the base file and non-json files |
| 19 | if (file === "en_US.json" || !file.endsWith(".json")) continue; |
| 20 | |
| 21 | const filePath = path.join(LANGUAGES_DIR, file); |
| 22 | const fileContent = await fs.readFile(filePath, "utf8"); |
| 23 | const fileJson = JSON.parse(fileContent); |
| 24 | |
| 25 | let hasChanges = false; |
| 26 | |
| 27 | // Iterate through all keys in the base file |
| 28 | for (const key in baseJson) { |
| 29 | // If the target file is missing this key, fill it with English value |
| 30 | if (!fileJson.hasOwnProperty(key)) { |
| 31 | fileJson[key] = baseJson[key]; |
| 32 | hasChanges = true; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // If there are changes, write back to the file |
| 37 | if (hasChanges) { |
| 38 | await fs.writeFile(filePath, JSON.stringify(fileJson, null, 2) + "\n", "utf8"); |
| 39 | console.log(`Updated missing keys in ${file}`); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | console.log("All language files have been processed."); |
| 44 | } catch (error) { |
| 45 | console.error("Error:", error); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | fillMissingKeys(); |
no test coverage detected