|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const { execSync } = require('child_process'); |
| 4 | +const { OpenAI } = require('openai'); |
| 5 | + |
| 6 | +const openai = new OpenAI({ |
| 7 | + apiKey: process.env.OPENAI_API_KEY |
| 8 | +}); |
| 9 | + |
| 10 | +const SOURCE_DIR = 'docs'; |
| 11 | +const TARGET_LANGUAGES = ['de', 'fr', 'es', 'ar', 'pt', 'th', 'pl', 'ja']; |
| 12 | + |
| 13 | +let totalPromptTokens = 0; |
| 14 | +let totalCompletionTokens = 0; |
| 15 | +let totalTokens = 0; |
| 16 | + |
| 17 | +function buildGlossary(targetLang) { |
| 18 | + const glossaryRaw = process.env.TRANSLATION_GLOSSARY; |
| 19 | + if (!glossaryRaw) return ""; |
| 20 | + |
| 21 | + try { |
| 22 | + const glossaryObj = JSON.parse(glossaryRaw); |
| 23 | + if (!glossaryObj[targetLang]) return ""; |
| 24 | + |
| 25 | + const entries = Object.entries(glossaryObj[targetLang]) |
| 26 | + .map(([src, tgt]) => `- Translate "${src}" as "${tgt}"`) |
| 27 | + .join("\n"); |
| 28 | + |
| 29 | + return `\n\nGlossary rules for ${targetLang}:\n${entries}`; |
| 30 | + } catch (err) { |
| 31 | + console.error("Glossary parsing failed:", err); |
| 32 | + return ""; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function buildSystemPrompt(targetLang) { |
| 37 | + const fromSecret = process.env.TRANSLATION_PROMPT; |
| 38 | + |
| 39 | + if (!fromSecret || !fromSecret.trim()) { |
| 40 | + throw new Error("TRANSLATION_PROMPT secret is missing or empty!"); |
| 41 | + } |
| 42 | + return fromSecret.replaceAll("${targetLang}", targetLang) + buildGlossary(targetLang); |
| 43 | +} |
| 44 | + |
| 45 | +function getChangedMarkdownFiles() { |
| 46 | + try { |
| 47 | + const output = execSync('git diff --name-only HEAD~1 HEAD', { encoding: 'utf8' }); |
| 48 | + return output |
| 49 | + .split('\n') |
| 50 | + .filter(file => file.startsWith(SOURCE_DIR) && file.endsWith('.md') && fs.existsSync(file)); |
| 51 | + } catch (error) { |
| 52 | + return getAllMarkdownFiles(); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +function getAllMarkdownFiles() { |
| 57 | + const output = execSync( |
| 58 | + `find ${SOURCE_DIR} -name "*.md" -not -path "./node_modules/*" -not -path "./.git/*"`, |
| 59 | + { encoding: 'utf8' } |
| 60 | + ); |
| 61 | + return output.split('\n').filter(file => file.trim() !== ''); |
| 62 | +} |
| 63 | + |
| 64 | +async function translateContent(content, targetLang) { |
| 65 | + const systemPrompt = buildSystemPrompt(targetLang); |
| 66 | + |
| 67 | + // console.log(`\n--- DEBUG: Prompt for ${targetLang} ---`); |
| 68 | + // console.log(systemPrompt); |
| 69 | + |
| 70 | + const response = await openai.chat.completions.create({ |
| 71 | + model: 'gpt-4.1-mini', |
| 72 | + messages: [ |
| 73 | + { |
| 74 | + role: 'system', |
| 75 | + content: systemPrompt |
| 76 | + }, |
| 77 | + { |
| 78 | + role: 'user', |
| 79 | + content: content |
| 80 | + } |
| 81 | + ], |
| 82 | + temperature: 0.1 |
| 83 | + }); |
| 84 | + |
| 85 | + if (response.usage) { |
| 86 | + totalPromptTokens += response.usage.prompt_tokens || 0; |
| 87 | + totalCompletionTokens += response.usage.completion_tokens || 0; |
| 88 | + totalTokens += response.usage.total_tokens || 0; |
| 89 | + } |
| 90 | + |
| 91 | + return response.choices[0].message.content; |
| 92 | +} |
| 93 | + |
| 94 | +async function main() { |
| 95 | + const changedFiles = getChangedMarkdownFiles(); |
| 96 | + console.log(`Found ${changedFiles.length} changed markdown files`); |
| 97 | + |
| 98 | + for (const file of changedFiles) { |
| 99 | + const content = fs.readFileSync(file, 'utf8'); |
| 100 | + const relPath = path.relative(SOURCE_DIR, file); |
| 101 | + |
| 102 | + for (const lang of TARGET_LANGUAGES) { |
| 103 | + console.log(`Translating ${file} to ${lang}...`); |
| 104 | + |
| 105 | + try { |
| 106 | + const translatedContent = await translateContent(content, lang); |
| 107 | + |
| 108 | + const outputFile = path.join( |
| 109 | + 'i18n', |
| 110 | + lang, |
| 111 | + 'docusaurus-plugin-content-docs/current', |
| 112 | + relPath |
| 113 | + ); |
| 114 | + |
| 115 | + fs.mkdirSync(path.dirname(outputFile), { recursive: true }); |
| 116 | + fs.writeFileSync(outputFile, translatedContent); |
| 117 | + |
| 118 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 119 | + } catch (error) { |
| 120 | + console.error(`Error translating ${file} to ${lang}:`, error); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + console.log('\n--- Translation Token Usage ---'); |
| 126 | + console.log(`Prompt tokens: ${totalPromptTokens}`); |
| 127 | + console.log(`Completion tokens: ${totalCompletionTokens}`); |
| 128 | + console.log(`Total tokens: ${totalTokens}`); |
| 129 | +} |
| 130 | + |
| 131 | +main().catch(console.error); |
0 commit comments