|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from 'fs/promises'; |
| 4 | +import path from 'path'; |
| 5 | + |
| 6 | +// Using Gemini API (currently used AI model) |
| 7 | +// Will automatically use GEMINI_API_KEY from .env |
| 8 | +const API_KEY = process.env.GEMINI_API_KEY; |
| 9 | + |
| 10 | +if (!API_KEY) { |
| 11 | + console.error('Error: Please provide GEMINI_API_KEY in your .env file to run translation.'); |
| 12 | + console.error('Example: GEMINI_API_KEY=your_key npm run machine-translate'); |
| 13 | + process.exit(1); |
| 14 | +} |
| 15 | + |
| 16 | +const localesDir = path.join(process.cwd(), 'messages'); |
| 17 | + |
| 18 | +async function translateKeys(missingKeys, targetLanguage) { |
| 19 | + const prompt = `You are a professional translator. Translate the following JSON values from English to ${targetLanguage}. |
| 20 | +Keep the exact same JSON keys. Return ONLY valid JSON, no markdown formatting. |
| 21 | +
|
| 22 | +JSON to translate: |
| 23 | +${JSON.stringify(missingKeys, null, 2)}`; |
| 24 | + |
| 25 | + const response = await fetch( |
| 26 | + `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${API_KEY}`, |
| 27 | + { |
| 28 | + method: 'POST', |
| 29 | + headers: { |
| 30 | + 'Content-Type': 'application/json' |
| 31 | + }, |
| 32 | + body: JSON.stringify({ |
| 33 | + systemInstruction: { |
| 34 | + parts: [ |
| 35 | + { |
| 36 | + text: 'Output ONLY valid JSON containing the translated values for the given keys. Do not include markdown blocks like ```json.' |
| 37 | + } |
| 38 | + ] |
| 39 | + }, |
| 40 | + contents: [ |
| 41 | + { |
| 42 | + parts: [{ text: prompt }] |
| 43 | + } |
| 44 | + ], |
| 45 | + generationConfig: { |
| 46 | + responseMimeType: 'application/json' |
| 47 | + } |
| 48 | + }) |
| 49 | + } |
| 50 | + ); |
| 51 | + |
| 52 | + if (!response.ok) { |
| 53 | + const errText = await response.text(); |
| 54 | + throw new Error(`Gemini API error: ${response.status} ${response.statusText} - ${errText}`); |
| 55 | + } |
| 56 | + |
| 57 | + const data = await response.json(); |
| 58 | + |
| 59 | + try { |
| 60 | + const textContent = data.candidates[0].content.parts[0].text; |
| 61 | + return JSON.parse(textContent); |
| 62 | + } catch (e) { |
| 63 | + console.error('Failed to parse Gemini response as JSON. Raw response:'); |
| 64 | + console.error(JSON.stringify(data, null, 2)); |
| 65 | + throw e; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +async function main() { |
| 70 | + try { |
| 71 | + const files = await fs.readdir(localesDir); |
| 72 | + const jsonFiles = files.filter((f) => f.endsWith('.json')); |
| 73 | + |
| 74 | + if (!jsonFiles.includes('en.json')) { |
| 75 | + console.error('en.json not found in messages directory!'); |
| 76 | + process.exit(1); |
| 77 | + } |
| 78 | + |
| 79 | + const enRaw = await fs.readFile(path.join(localesDir, 'en.json'), 'utf-8'); |
| 80 | + const enData = JSON.parse(enRaw); |
| 81 | + const enKeys = Object.keys(enData); |
| 82 | + |
| 83 | + for (const file of jsonFiles) { |
| 84 | + if (file === 'en.json') continue; |
| 85 | + |
| 86 | + const lang = file.replace('.json', ''); |
| 87 | + const targetPath = path.join(localesDir, file); |
| 88 | + const targetRaw = await fs.readFile(targetPath, 'utf-8'); |
| 89 | + const targetData = JSON.parse(targetRaw); |
| 90 | + |
| 91 | + const missingKeys = {}; |
| 92 | + for (const key of enKeys) { |
| 93 | + if (targetData[key] === undefined || targetData[key] === null || targetData[key] === '') { |
| 94 | + missingKeys[key] = enData[key]; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + const missingCount = Object.keys(missingKeys).length; |
| 99 | + if (missingCount > 0) { |
| 100 | + console.log(`\nFound ${missingCount} missing keys for ${lang} (${file}). Translating...`); |
| 101 | + |
| 102 | + const translated = await translateKeys(missingKeys, lang); |
| 103 | + |
| 104 | + // Merge taking care of keeping the en.json sort order |
| 105 | + const finalData = {}; |
| 106 | + for (const key of enKeys) { |
| 107 | + if (targetData[key] !== undefined && targetData[key] !== null && targetData[key] !== '') { |
| 108 | + finalData[key] = targetData[key]; |
| 109 | + } else if (translated[key]) { |
| 110 | + finalData[key] = translated[key]; |
| 111 | + } else { |
| 112 | + // Fallback to english if translation failed for a specific key unexpectedly |
| 113 | + finalData[key] = enData[key]; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + await fs.writeFile(targetPath, JSON.stringify(finalData, null, '\t') + '\n', 'utf-8'); |
| 118 | + console.log(`✅ Successfully updated ${file}`); |
| 119 | + } else { |
| 120 | + console.log(`\n✅ ${lang} (${file}) is up to date.`); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + console.log('\nAll languages checked and updated!'); |
| 125 | + } catch (error) { |
| 126 | + console.error('An error occurred during translation:', error); |
| 127 | + process.exit(1); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +main(); |
0 commit comments