|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import OpenAI from 'openai'; |
| 4 | +import { getAllMdxFiles, resolveTranslatedFilePath, translateContent } from '../utils/translate.mjs'; |
| 5 | + |
| 6 | +export function registerTranslateCommand(cli) { |
| 7 | + cli |
| 8 | + .command('translate [files...]', 'Translate documentation files') |
| 9 | + .option('--all', 'Translate all files in content/docs') |
| 10 | + .option('--model <model>', 'OpenAI model to use', { default: 'gpt-4o' }) |
| 11 | + .action(async (files, options) => { |
| 12 | + const OPENAI_API_KEY = process.env.OPENAI_API_KEY; |
| 13 | + const OPENAI_BASE_URL = process.env.OPENAI_BASE_URL; |
| 14 | + |
| 15 | + if (!OPENAI_API_KEY) { |
| 16 | + console.error('Error: Missing OPENAI_API_KEY environment variable.'); |
| 17 | + process.exit(1); |
| 18 | + } |
| 19 | + |
| 20 | + const openai = new OpenAI({ |
| 21 | + apiKey: OPENAI_API_KEY, |
| 22 | + baseURL: OPENAI_BASE_URL, |
| 23 | + }); |
| 24 | + |
| 25 | + let targetFiles = []; |
| 26 | + |
| 27 | + if (options.all) { |
| 28 | + console.log('Scanning for all .mdx files in content/docs...'); |
| 29 | + targetFiles = getAllMdxFiles('content/docs'); |
| 30 | + } else if (files && files.length > 0) { |
| 31 | + targetFiles = files; |
| 32 | + } else if (process.env.CHANGED_FILES) { |
| 33 | + targetFiles = process.env.CHANGED_FILES.split(','); |
| 34 | + } |
| 35 | + |
| 36 | + if (targetFiles.length === 0) { |
| 37 | + console.log('No files to translate.'); |
| 38 | + console.log('Usage:'); |
| 39 | + console.log(' docs-cli translate content/docs/file.mdx'); |
| 40 | + console.log(' docs-cli translate --all'); |
| 41 | + console.log(' (CI): Set CHANGED_FILES environment variable'); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + console.log(`Processing ${targetFiles.length} files...`); |
| 46 | + |
| 47 | + for (const file of targetFiles) { |
| 48 | + const enFilePath = path.resolve(process.cwd(), file); |
| 49 | + |
| 50 | + if (!fs.existsSync(enFilePath)) { |
| 51 | + console.log(`File skipped (not found): ${file}`); |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + const zhFilePath = resolveTranslatedFilePath(enFilePath); |
| 56 | + |
| 57 | + if (zhFilePath === enFilePath) { |
| 58 | + console.log(`Skipping: Source and destination are the same for ${file}`); |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + console.log(`Translating: ${file} -> ${path.relative(process.cwd(), zhFilePath)}`); |
| 63 | + |
| 64 | + try { |
| 65 | + const content = fs.readFileSync(enFilePath, 'utf-8'); |
| 66 | + const translatedContent = await translateContent(content, openai, options.model); |
| 67 | + |
| 68 | + const dir = path.dirname(zhFilePath); |
| 69 | + if (!fs.existsSync(dir)) { |
| 70 | + fs.mkdirSync(dir, { recursive: true }); |
| 71 | + } |
| 72 | + |
| 73 | + fs.writeFileSync(zhFilePath, translatedContent); |
| 74 | + console.log(`✓ Automatically translated: ${zhFilePath}`); |
| 75 | + } catch (error) { |
| 76 | + console.error(`✗ Failed to translate ${file}:`, error); |
| 77 | + } |
| 78 | + } |
| 79 | + }); |
| 80 | +} |
0 commit comments