mirror of
https://github.com/langgenius/dify-docs.git
synced 2026-03-27 13:28:32 +07:00
When a PR is force-pushed, github.event.before points to an orphaned commit that isn't fetched by actions/checkout. This caused the workflow to fail with "Error: command not found" (exit code 127) because: 1. pr_analyzer.py's git diff failed on the inaccessible commit 2. Error messages went to stdout, breaking the source command This fix: - Validates COMPARE_BASE accessibility before use via git cat-file - Falls back to merge-base strategy if commit is orphaned - Sends error messages to stderr for defensive error handling 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Automatic Document Translation
Multi-language document auto-translation system based on GitHub Actions and Dify AI, supporting English, Chinese, and Japanese.
How It Works
Workflow Triggers
-
Execute Workflow (New PRs):
- Triggers when PR is opened with
.md/.mdxchanges inen/directory - Creates translation PR with fresh translations for all changed files
- Translation PR tracks the source PR
- Triggers when PR is opened with
-
Update Workflow (Incremental Changes):
- Triggers on new commits to source PR
- Updates existing translation PR with incremental changes
- Context-aware translation: Uses existing translation + git diff for modified files
- Surgical reconciliation: Detects and applies move/rename operations
Translation Operations
- ✅ New files: Fresh translation to all target languages
- ✅ Modified files: Context-aware update using existing translation + git diff
- ✅ Deleted files: Removed from all language sections + physical files
- ✅ Moved files: Detected via
group_pathchanges, applied with index-based navigation - ✅ Renamed files: Detected when deleted+added in same location, preserves file extensions
Surgical Reconciliation
Automatically detects structural changes in docs.json:
- Move detection: Same file, different
group_path→ moves zh/ja files to same nested location using index-based navigation - Rename detection: File deleted+added in same location → renames zh/ja files with extension preserved
- Index-based navigation: Groups matched by position, not name (works across translations: "Nodes" ≠ "节点")
System Features
- 🌐 Multi-language Support: Configuration-based language mapping (
config.json) - 📚 Terminology Consistency: Built-in professional terminology database (
termbase_i18n.md) - 🔄 Incremental Updates: Context-aware translation using git diff for modified files
- 🎯 Surgical Reconciliation: Automatic detection and application of move/rename operations
- 🛡️ Fault Tolerance: Retry mechanism with exponential backoff
- ⚡ Efficient Processing: Only processes changed files since last commit
Language Directories
- Latest docs:
en/(source) →zh/,ja/(targets) - Versioned docs:
versions/{version}/en-us/→versions/{version}/zh-zh/,versions/{version}/ja/
Configuration in tools/translate/config.json.
Usage
For Document Writers
- Create branch from main
- Add/modify/delete files in
en/directory - Update
docs.jsonif adding/removing/moving/renaming files - Push to branch → workflow creates translation PR automatically
- Make additional changes → workflow updates translation PR incrementally
- Review and merge translation PR
Testing Moves & Renames
Move: Edit docs.json to move file between groups (e.g., Getting Started → Nodes)
// Before: en/test-file in "Getting Started" group
// After: en/test-file in "Nodes" group
Rename: Rename file + update docs.json entry
git mv en/old-name.md en/new-name.md
# Update docs.json: "en/old-name" → "en/new-name"
Logs will show:
INFO: Detected 1 moves, 0 renames, 0 adds, 0 deletes
INFO: Moving en/test-file from 'Dropdown > GroupA' to 'Dropdown > GroupB'
SUCCESS: Moved zh/test-file to new location
SUCCESS: Moved ja/test-file to new location
Configuration
Language Settings
Edit tools/translate/config.json:
{
"source_language": "en",
"target_languages": ["zh", "ja"],
"languages": {
"en": {"code": "en", "name": "English", "directory": "en"},
"zh": {
"code": "zh",
"name": "Chinese",
"directory": "zh",
"translation_notice": "<Note>⚠️ AI translation...</Note>"
}
}
}
Terminology Database
Edit tools/translate/termbase_i18n.md to update professional terminology translations.
Translation Model
Configure in Dify Studio - adjust prompts or change base models.
Local Development
Setup
# Create virtual environment
python -m venv venv
source venv/bin/activate # macOS/Linux
# venv\Scripts\activate # Windows
# Install dependencies
pip install -r tools/translate/requirements.txt
# Configure API key
echo "DIFY_API_KEY=your_key" > tools/translate/.env
Run Translation
# Interactive mode
python tools/translate/main.py
# Specify file
python tools/translate/main.py path/to/file.mdx
Test Surgical Reconciliation
# Test locally with git refs
cd tools/translate
python -c "
from sync_and_translate import DocsSynchronizer
import asyncio
import os
api_key = os.getenv('DIFY_API_KEY')
sync = DocsSynchronizer(api_key)
# Test with specific commits
logs = sync.reconcile_docs_json_structural_changes('base_sha', 'head_sha')
for log in logs:
print(log)
"
Troubleshooting
Translation Issues
- HTTP 504: Verify
response_mode: "streaming"inmain.py - Missing output: Check Dify workflow has output variable
output1 - Failed workflow: Review Dify workflow logs for node errors
Move/Rename Issues
- Not detected: Check logs for "INFO: Detected X moves, Y renames" - verify
group_pathchanged - Wrong location: Structure mismatch between languages - verify group indices align
- File not found: Ensure file has .md or .mdx extension
Key Files
config.json- Language configuration (single source of truth)termbase_i18n.md- Translation terminology databasesync_and_translate.py- Core translation + surgical reconciliation logicmain.py- Local translation tool with Dify API integrationtranslate_pr.py- PR workflow orchestration.github/workflows/sync_docs_execute.yml- Execute workflow (new PRs).github/workflows/sync_docs_update.yml- Update workflow (incremental changes)
Technical Details
- Concurrent translation limited to 2 tasks for API stability
- Supports
.mdand.mdxfile formats - Based on Dify API streaming mode
- Index-based navigation for language-independent group matching
- Extension detection and preservation for rename operations