Add Chinese translation for README and implement README translation script

- Created README.zh.md for Chinese localization of the project.
- Developed a README translation script to support multiple languages using Claude Agent SDK.
- Implemented CLI and programmatic usage for the translation tool.
- Added examples for integrating the translation tool into build scripts and CI/CD pipelines.
- Enhanced error handling and logging for translation processes.
- Included support for various languages and output customization options.
This commit is contained in:
Alex Newman
2025-12-12 00:58:36 -05:00
parent f154e32145
commit 25684ea8f7
9 changed files with 2220 additions and 2054 deletions
+235
View File
@@ -0,0 +1,235 @@
# README Translator
Translate README.md files to multiple languages using the Claude Agent SDK. Perfect for build scripts and CI/CD pipelines.
## Installation
```bash
npm install readme-translator
# or
npm install -g readme-translator # for CLI usage
```
## Requirements
- Node.js 18+
- **Authentication** (one of the following):
- Claude Code installed and authenticated (Pro/Max subscription) - **no API key needed**
- `ANTHROPIC_API_KEY` environment variable set (for API-based usage)
- AWS Bedrock (`CLAUDE_CODE_USE_BEDROCK=1` + AWS credentials)
- Google Vertex AI (`CLAUDE_CODE_USE_VERTEX=1` + GCP credentials)
If you have Claude Code installed and logged in with your Pro/Max subscription, the SDK will automatically use that authentication.
## CLI Usage
```bash
# Basic usage
translate-readme README.md es fr de
# With options
translate-readme -v -o ./i18n --pattern docs.{lang}.md README.md es fr de ja zh
# List supported languages
translate-readme --list-languages
```
### CLI Options
| Option | Description |
|--------|-------------|
| `-o, --output <dir>` | Output directory (default: same as source) |
| `-p, --pattern <pat>` | Output filename pattern (default: `README.{lang}.md`) |
| `--no-preserve-code` | Translate code blocks too (not recommended) |
| `-m, --model <model>` | Claude model to use (default: `sonnet`) |
| `--max-budget <usd>` | Maximum budget in USD |
| `-v, --verbose` | Show detailed progress |
| `-h, --help` | Show help message |
| `--list-languages` | List all supported language codes |
## Programmatic Usage
```typescript
import { translateReadme } from "readme-translator";
const result = await translateReadme({
source: "./README.md",
languages: ["es", "fr", "de", "ja", "zh"],
verbose: true,
});
console.log(`Translated ${result.successful} files`);
console.log(`Total cost: $${result.totalCostUsd.toFixed(4)}`);
```
### API Options
```typescript
interface TranslationOptions {
/** Source README file path */
source: string;
/** Target language codes */
languages: string[];
/** Output directory (defaults to same directory as source) */
outputDir?: string;
/** Output filename pattern (use {lang} placeholder) */
pattern?: string; // default: "README.{lang}.md"
/** Preserve code blocks without translation */
preserveCode?: boolean; // default: true
/** Claude model to use */
model?: string; // default: "sonnet"
/** Maximum budget in USD */
maxBudgetUsd?: number;
/** Verbose output */
verbose?: boolean;
}
```
### Return Value
```typescript
interface TranslationJobResult {
results: TranslationResult[];
totalCostUsd: number;
successful: number;
failed: number;
}
interface TranslationResult {
language: string;
outputPath: string;
success: boolean;
error?: string;
costUsd?: number;
}
```
## Build Script Integration
### package.json
```json
{
"scripts": {
"translate": "translate-readme README.md es fr de ja zh",
"translate:all": "translate-readme -v -o ./i18n README.md es fr de it pt ja ko zh ru ar",
"prebuild": "npm run translate"
}
}
```
### GitHub Actions
Note: CI/CD environments require an API key since Claude Code won't be authenticated there.
```yaml
name: Translate README
on:
push:
branches: [main]
paths: [README.md]
jobs:
translate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm install -g readme-translator
- name: Translate README
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
translate-readme -v -o ./i18n README.md es fr de ja zh
- name: Commit translations
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add i18n/
git diff --staged --quiet || git commit -m "chore: update README translations"
git push
```
### Programmatic Build Script
```typescript
// scripts/translate.ts
import { translateReadme } from "readme-translator";
async function main() {
const result = await translateReadme({
source: "./README.md",
languages: (process.env.TRANSLATE_LANGS || "es,fr,de").split(","),
outputDir: "./docs/i18n",
maxBudgetUsd: 5.0,
verbose: !process.env.CI,
});
if (result.failed > 0) {
console.error("Some translations failed");
process.exit(1);
}
}
main();
```
## Supported Languages
| Code | Language | Code | Language |
|------|----------|------|----------|
| `ar` | Arabic | `ko` | Korean |
| `bg` | Bulgarian | `lt` | Lithuanian |
| `cs` | Czech | `lv` | Latvian |
| `da` | Danish | `nl` | Dutch |
| `de` | German | `no` | Norwegian |
| `el` | Greek | `pl` | Polish |
| `es` | Spanish | `pt` | Portuguese |
| `et` | Estonian | `pt-br` | Brazilian Portuguese |
| `fi` | Finnish | `ro` | Romanian |
| `fr` | French | `ru` | Russian |
| `he` | Hebrew | `sk` | Slovak |
| `hi` | Hindi | `sl` | Slovenian |
| `hu` | Hungarian | `sv` | Swedish |
| `id` | Indonesian | `th` | Thai |
| `it` | Italian | `tr` | Turkish |
| `ja` | Japanese | `uk` | Ukrainian |
| | | `vi` | Vietnamese |
| | | `zh` | Chinese (Simplified) |
| | | `zh-tw` | Chinese (Traditional) |
## Best Practices
1. **Preserve Code Blocks**: Keep `preserveCode: true` (default) to avoid breaking code examples
2. **Set Budget Limits**: Use `maxBudgetUsd` to prevent runaway costs
3. **Run on Releases Only**: In CI/CD, trigger translations only on main branch or releases
4. **Review Translations**: Automated translations are good but not perfect - consider human review for critical docs
5. **Cache Results**: Don't re-translate unchanged content - check if README changed before running
## Cost Estimation
Typical costs per language (varies by README length):
- Short README (~500 words): ~$0.01-0.02
- Medium README (~2000 words): ~$0.05-0.10
- Long README (~5000 words): ~$0.15-0.25
## License
MIT
+233
View File
@@ -0,0 +1,233 @@
#!/usr/bin/env npx tsx
import { translateReadme, SUPPORTED_LANGUAGES } from "./index.ts";
interface CliArgs {
source: string;
languages: string[];
outputDir?: string;
pattern?: string;
preserveCode: boolean;
model?: string;
maxBudget?: number;
verbose: boolean;
help: boolean;
listLanguages: boolean;
}
function printHelp(): void {
console.log(`
readme-translator - Translate README.md files using Claude Agent SDK
AUTHENTICATION:
If Claude Code is installed and authenticated (Pro/Max subscription),
no API key is needed. Otherwise, set ANTHROPIC_API_KEY environment variable.
USAGE:
translate-readme [options] <source> <languages...>
translate-readme --help
translate-readme --list-languages
ARGUMENTS:
source Path to the source README.md file
languages Target language codes (e.g., es fr de ja zh)
OPTIONS:
-o, --output <dir> Output directory (default: same as source)
-p, --pattern <pat> Output filename pattern (default: README.{lang}.md)
--no-preserve-code Translate code blocks too (not recommended)
-m, --model <model> Claude model to use (default: sonnet)
--max-budget <usd> Maximum budget in USD
-v, --verbose Show detailed progress
-h, --help Show this help message
--list-languages List all supported language codes
EXAMPLES:
# Translate to Spanish and French
translate-readme README.md es fr
# Translate to multiple languages with custom output
translate-readme -v -o ./i18n --pattern docs.{lang}.md README.md de ja ko zh
# Use in npm scripts
# package.json: "translate": "translate-readme README.md es fr de"
SUPPORTED LANGUAGES:
Run with --list-languages to see all supported language codes
`);
}
function printLanguages(): void {
const LANGUAGE_NAMES: Record<string, string> = {
ar: "Arabic",
bg: "Bulgarian",
cs: "Czech",
da: "Danish",
de: "German",
el: "Greek",
es: "Spanish",
et: "Estonian",
fi: "Finnish",
fr: "French",
he: "Hebrew",
hi: "Hindi",
hu: "Hungarian",
id: "Indonesian",
it: "Italian",
ja: "Japanese",
ko: "Korean",
lt: "Lithuanian",
lv: "Latvian",
nl: "Dutch",
no: "Norwegian",
pl: "Polish",
pt: "Portuguese",
"pt-br": "Brazilian Portuguese",
ro: "Romanian",
ru: "Russian",
sk: "Slovak",
sl: "Slovenian",
sv: "Swedish",
th: "Thai",
tr: "Turkish",
uk: "Ukrainian",
vi: "Vietnamese",
zh: "Chinese (Simplified)",
"zh-tw": "Chinese (Traditional)",
};
console.log("\nSupported Language Codes:\n");
const sorted = Object.entries(LANGUAGE_NAMES).sort((a, b) =>
a[1].localeCompare(b[1])
);
for (const [code, name] of sorted) {
console.log(` ${code.padEnd(8)} ${name}`);
}
console.log("");
}
function parseArgs(argv: string[]): CliArgs {
const args: CliArgs = {
source: "",
languages: [],
preserveCode: true,
verbose: false,
help: false,
listLanguages: false,
};
const positional: string[] = [];
let i = 2; // Skip node and script path
while (i < argv.length) {
const arg = argv[i];
switch (arg) {
case "-h":
case "--help":
args.help = true;
break;
case "--list-languages":
args.listLanguages = true;
break;
case "-v":
case "--verbose":
args.verbose = true;
break;
case "--no-preserve-code":
args.preserveCode = false;
break;
case "-o":
case "--output":
args.outputDir = argv[++i];
break;
case "-p":
case "--pattern":
args.pattern = argv[++i];
break;
case "-m":
case "--model":
args.model = argv[++i];
break;
case "--max-budget":
args.maxBudget = parseFloat(argv[++i]);
break;
default:
if (arg.startsWith("-")) {
console.error(`Unknown option: ${arg}`);
process.exit(1);
}
positional.push(arg);
}
i++;
}
if (positional.length > 0) {
args.source = positional[0];
args.languages = positional.slice(1);
}
return args;
}
async function main(): Promise<void> {
const args = parseArgs(process.argv);
if (args.help) {
printHelp();
process.exit(0);
}
if (args.listLanguages) {
printLanguages();
process.exit(0);
}
if (!args.source) {
console.error("Error: No source file specified");
console.error("Run with --help for usage information");
process.exit(1);
}
if (args.languages.length === 0) {
console.error("Error: No target languages specified");
console.error("Run with --help for usage information");
process.exit(1);
}
// Validate language codes
const invalidLangs = args.languages.filter(
(lang) => !SUPPORTED_LANGUAGES.includes(lang.toLowerCase())
);
if (invalidLangs.length > 0) {
console.error(`Error: Unknown language codes: ${invalidLangs.join(", ")}`);
console.error("Run with --list-languages to see supported codes");
process.exit(1);
}
try {
const result = await translateReadme({
source: args.source,
languages: args.languages,
outputDir: args.outputDir,
pattern: args.pattern,
preserveCode: args.preserveCode,
model: args.model,
maxBudgetUsd: args.maxBudget,
verbose: args.verbose,
});
// Exit with error code if any translations failed
if (result.failed > 0) {
process.exit(1);
}
} catch (error) {
console.error(
"Translation failed:",
error instanceof Error ? error.message : error
);
process.exit(1);
}
}
main();
+147
View File
@@ -0,0 +1,147 @@
/**
* Example: Using readme-translator in build scripts
*
* These examples show how to integrate the translator into your build pipeline.
*/
import { translateReadme, TranslationJobResult, SUPPORTED_LANGUAGES } from "./index.js";
// Example 1: Simple usage - translate to a few common languages
async function translateToCommonLanguages(): Promise<void> {
const result = await translateReadme({
source: "./README.md",
languages: ["es", "fr", "de", "ja", "zh"],
verbose: true,
});
console.log(`Translated to ${result.successful} languages`);
}
// Example 2: Full i18n setup with custom output directory
async function fullI18nSetup(): Promise<void> {
const result = await translateReadme({
source: "./README.md",
languages: ["es", "fr", "de", "it", "pt", "ja", "ko", "zh", "ru", "ar"],
outputDir: "./docs/i18n",
pattern: "README.{lang}.md",
preserveCode: true,
model: "sonnet",
maxBudgetUsd: 5.0, // Cap spending at $5
verbose: true,
});
// Handle results programmatically
for (const r of result.results) {
if (!r.success) {
console.error(`Failed to translate to ${r.language}: ${r.error}`);
}
}
}
// Example 3: Build script integration with error handling
// Note: If Claude Code is authenticated, no API key needed locally.
// CI/CD environments will need ANTHROPIC_API_KEY set.
async function buildScriptIntegration(): Promise<number> {
try {
const result = await translateReadme({
source: process.env.README_PATH || "./README.md",
languages: (process.env.TRANSLATE_LANGS || "es,fr,de").split(","),
outputDir: process.env.I18N_OUTPUT || "./i18n",
verbose: process.env.CI !== "true", // Quiet in CI
});
// Return exit code for build scripts
return result.failed > 0 ? 1 : 0;
} catch (error) {
console.error("Translation failed:", error);
return 1;
}
}
// Example 4: Batch translation of multiple READMEs
async function batchTranslation(): Promise<void> {
const readmes = [
"./README.md",
"./packages/core/README.md",
"./packages/cli/README.md",
];
const languages = ["es", "fr", "de"];
for (const readme of readmes) {
console.log(`\nProcessing: ${readme}`);
await translateReadme({
source: readme,
languages,
verbose: true,
});
}
}
// Example 5: Custom output pattern for docs sites
async function docsiteSetup(): Promise<void> {
// For docusaurus/vitepress style: docs/README.es.md
await translateReadme({
source: "./README.md",
languages: ["es", "fr", "de", "ja", "zh"],
outputDir: "./docs",
pattern: "README.{lang}.md",
verbose: true,
});
}
// Example 6: Conditional translation in CI/CD
async function cicdTranslation(): Promise<void> {
// Only translate on main branch releases
const isRelease = process.env.GITHUB_REF === "refs/heads/main";
const isManualTrigger = process.env.GITHUB_EVENT_NAME === "workflow_dispatch";
if (!isRelease && !isManualTrigger) {
console.log("Skipping translation - not a release build");
return;
}
const result = await translateReadme({
source: "./README.md",
languages: ["es", "fr", "de", "ja", "ko", "zh", "pt-br"],
outputDir: "./dist/i18n",
maxBudgetUsd: 10.0,
verbose: true,
});
// Write summary for GitHub Actions
if (process.env.GITHUB_STEP_SUMMARY) {
const summary = `
## Translation Summary
- ✅ Successful: ${result.successful}
- ❌ Failed: ${result.failed}
- 💰 Cost: $${result.totalCostUsd.toFixed(4)}
`;
// In real usage, write to GITHUB_STEP_SUMMARY
console.log(summary);
}
}
// Run an example
const example = process.argv[2];
switch (example) {
case "simple":
translateToCommonLanguages();
break;
case "full":
fullI18nSetup();
break;
case "batch":
batchTranslation();
break;
case "docs":
docsiteSetup();
break;
case "ci":
cicdTranslation();
break;
default:
console.log("Available examples: simple, full, batch, docs, ci");
console.log("\nSupported languages:", SUPPORTED_LANGUAGES.join(", "));
}
+292
View File
@@ -0,0 +1,292 @@
import { query, type SDKMessage, type SDKResultMessage } from "@anthropic-ai/claude-agent-sdk";
import * as fs from "fs/promises";
import * as path from "path";
export interface TranslationOptions {
/** Source README file path */
source: string;
/** Target languages (e.g., ['es', 'fr', 'de', 'ja', 'zh']) */
languages: string[];
/** Output directory (defaults to same directory as source) */
outputDir?: string;
/** Output filename pattern (use {lang} placeholder, defaults to 'README.{lang}.md') */
pattern?: string;
/** Preserve code blocks without translation */
preserveCode?: boolean;
/** Model to use (defaults to 'sonnet') */
model?: string;
/** Maximum budget in USD for the entire translation job */
maxBudgetUsd?: number;
/** Verbose output */
verbose?: boolean;
}
export interface TranslationResult {
language: string;
outputPath: string;
success: boolean;
error?: string;
costUsd?: number;
}
export interface TranslationJobResult {
results: TranslationResult[];
totalCostUsd: number;
successful: number;
failed: number;
}
const LANGUAGE_NAMES: Record<string, string> = {
ar: "Arabic",
bg: "Bulgarian",
cs: "Czech",
da: "Danish",
de: "German",
el: "Greek",
es: "Spanish",
et: "Estonian",
fi: "Finnish",
fr: "French",
he: "Hebrew",
hi: "Hindi",
hu: "Hungarian",
id: "Indonesian",
it: "Italian",
ja: "Japanese",
ko: "Korean",
lt: "Lithuanian",
lv: "Latvian",
nl: "Dutch",
no: "Norwegian",
pl: "Polish",
pt: "Portuguese",
"pt-br": "Brazilian Portuguese",
ro: "Romanian",
ru: "Russian",
sk: "Slovak",
sl: "Slovenian",
sv: "Swedish",
th: "Thai",
tr: "Turkish",
uk: "Ukrainian",
vi: "Vietnamese",
zh: "Chinese (Simplified)",
"zh-tw": "Chinese (Traditional)",
};
function getLanguageName(code: string): string {
return LANGUAGE_NAMES[code.toLowerCase()] || code;
}
async function translateToLanguage(
content: string,
targetLang: string,
options: Pick<TranslationOptions, "preserveCode" | "model" | "verbose">
): Promise<{ translation: string; costUsd: number }> {
const languageName = getLanguageName(targetLang);
const preserveCodeInstructions = options.preserveCode
? `
IMPORTANT: Preserve all code blocks exactly as they are. Do NOT translate:
- Code inside \`\`\` blocks
- Inline code inside \` backticks
- Command examples
- File paths
- Variable names, function names, and technical identifiers
- URLs and links
`
: "";
const prompt = `Translate the following README.md content from English to ${languageName} (${targetLang}).
${preserveCodeInstructions}
Guidelines:
- Maintain all Markdown formatting (headers, lists, links, etc.)
- Keep the same document structure
- Translate headings, descriptions, and explanatory text naturally
- Preserve technical accuracy
- Use appropriate technical terminology for ${languageName}
- Keep proper nouns (product names, company names) unchanged unless they have official translations
Here is the README content to translate:
---
${content}
---
Output ONLY the translated README content, nothing else. Do not include any preamble or explanation.`;
let translation = "";
let costUsd = 0;
let charCount = 0;
const startTime = Date.now();
const stream = query({
prompt,
options: {
model: options.model || "sonnet",
systemPrompt: `You are an expert technical translator specializing in software documentation.
You translate README files while preserving Markdown formatting and technical accuracy.
Always output only the translated content without any surrounding explanation.`,
permissionMode: "bypassPermissions",
allowDangerouslySkipPermissions: true,
includePartialMessages: true, // Enable streaming events
},
});
// Progress spinner frames
const spinnerFrames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
let spinnerIdx = 0;
for await (const message of stream) {
// Handle streaming text deltas
if (message.type === "stream_event") {
const event = message.event as { type: string; delta?: { type: string; text?: string } };
if (event.type === "content_block_delta" && event.delta?.type === "text_delta" && event.delta.text) {
translation += event.delta.text;
charCount += event.delta.text.length;
if (options.verbose) {
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
const spinner = spinnerFrames[spinnerIdx++ % spinnerFrames.length];
process.stdout.write(`\r ${spinner} Translating... ${charCount} chars (${elapsed}s)`);
}
}
}
// Handle full assistant messages (fallback)
if (message.type === "assistant") {
for (const block of message.message.content) {
if (block.type === "text" && !translation) {
translation = block.text;
charCount = translation.length;
}
}
}
if (message.type === "result") {
const result = message as SDKResultMessage;
if (result.subtype === "success") {
costUsd = result.total_cost_usd;
// Use the result text if we didn't get it from streaming
if (!translation && result.result) {
translation = result.result;
charCount = translation.length;
}
}
}
}
// Clear the progress line
if (options.verbose) {
process.stdout.write("\r" + " ".repeat(60) + "\r");
}
return { translation: translation.trim(), costUsd };
}
export async function translateReadme(
options: TranslationOptions
): Promise<TranslationJobResult> {
const {
source,
languages,
outputDir,
pattern = "README.{lang}.md",
preserveCode = true,
model,
maxBudgetUsd,
verbose = false,
} = options;
// Read source file
const sourcePath = path.resolve(source);
const content = await fs.readFile(sourcePath, "utf-8");
// Determine output directory
const outDir = outputDir ? path.resolve(outputDir) : path.dirname(sourcePath);
await fs.mkdir(outDir, { recursive: true });
const results: TranslationResult[] = [];
let totalCostUsd = 0;
if (verbose) {
console.log(`📖 Source: ${sourcePath}`);
console.log(`📂 Output: ${outDir}`);
console.log(`🌍 Languages: ${languages.join(", ")}`);
console.log("");
}
for (const lang of languages) {
// Check budget
if (maxBudgetUsd && totalCostUsd >= maxBudgetUsd) {
results.push({
language: lang,
outputPath: "",
success: false,
error: "Budget exceeded",
});
continue;
}
const outputFilename = pattern.replace("{lang}", lang);
const outputPath = path.join(outDir, outputFilename);
if (verbose) {
console.log(`🔄 Translating to ${getLanguageName(lang)} (${lang})...`);
}
try {
const { translation, costUsd } = await translateToLanguage(content, lang, {
preserveCode,
model,
verbose,
});
await fs.writeFile(outputPath, translation, "utf-8");
totalCostUsd += costUsd;
results.push({
language: lang,
outputPath,
success: true,
costUsd,
});
if (verbose) {
console.log(` ✅ Saved to ${outputFilename} ($${costUsd.toFixed(4)})`);
}
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
results.push({
language: lang,
outputPath,
success: false,
error: errorMessage,
});
if (verbose) {
console.log(` ❌ Failed: ${errorMessage}`);
}
}
}
const successful = results.filter((r) => r.success).length;
const failed = results.filter((r) => !r.success).length;
if (verbose) {
console.log("");
console.log(`📊 Summary: ${successful} succeeded, ${failed} failed`);
console.log(`💰 Total cost: $${totalCostUsd.toFixed(4)}`);
}
return {
results,
totalCostUsd,
successful,
failed,
};
}
// Export language codes for convenience
export const SUPPORTED_LANGUAGES = Object.keys(LANGUAGE_NAMES);