-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate_critical_files.py
More file actions
162 lines (129 loc) · 5 KB
/
translate_critical_files.py
File metadata and controls
162 lines (129 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""
Translate critical files for Urdu demo
- Part intros (5 files)
- First lesson of each chapter (~10 files)
Total: ~15 files for quick demo
"""
import os
import google.generativeai as genai
import time
# Configure Gemini API
import sys
GEMINI_API_KEY = sys.argv[1] if len(sys.argv) > 1 else os.getenv("GEMINI_API_KEY")
if not GEMINI_API_KEY:
print("ERROR: GEMINI_API_KEY not provided!")
print("Run: python translate_critical_files.py YOUR_API_KEY")
exit(1)
genai.configure(api_key=GEMINI_API_KEY)
model = genai.GenerativeModel('gemini-2.5-flash') # Updated to latest stable model
# Critical files to translate (in order of importance)
CRITICAL_FILES = [
# Part intros
"textbook/docs/part1/intro.md",
"textbook/docs/part2/intro.md",
"textbook/docs/part3/intro.md",
"textbook/docs/part4/intro.md",
"textbook/docs/part5/intro.md",
# Part 1 - First lessons of each chapter
"textbook/docs/part1/chapter1-what-is-physical-ai/lesson1-defining-physical-ai.md",
"textbook/docs/part1/chapter2-foundations-of-robotics/lesson1-mechanical-systems.md",
"textbook/docs/part1/chapter3-ai-fundamentals/lesson1-machine-learning-basics.md",
# Part 2 - First lessons
"textbook/docs/part2/chapter1-humanoid-design/lesson1-biped-mechanics.md",
"textbook/docs/part2/chapter2-locomotion/lesson1-walking-gaits.md",
# Part 3 - First lesson
"textbook/docs/part3/chapter1-perception-ai/lesson1.md",
# Part 4 - First lesson
"textbook/docs/part4/chapter1-industrial-applications/lesson1.md",
# Part 5 - First lesson
"textbook/docs/part5/chapter1-humanoids-in-everyday-life/lesson1-humanoids-in-the-smart-home.md",
]
def translate_to_urdu(english_content):
"""Translate English markdown content to Urdu using Gemini"""
prompt = f"""Translate the following markdown content to Urdu (اردو).
IMPORTANT RULES:
1. Translate YAML frontmatter fields (title, description, sidebar_label) to Urdu
2. Translate ALL headings, paragraphs, bullet points, and text to Urdu
3. Keep code blocks in ENGLISH (do not translate code)
4. Keep image paths unchanged: 
5. Keep markdown syntax (##, -, *, etc.)
6. Use proper Urdu script and RTL formatting
7. Technical terms can use transliteration (روبوٹ, سینسر, etc.)
English Content:
{english_content}
Urdu Translation:"""
try:
response = model.generate_content(prompt)
return response.text
except Exception as e:
print(f"Translation error: {e}")
return None
def get_urdu_path(english_path):
"""Convert English path to Urdu i18n path"""
return english_path.replace(
"textbook/docs/",
"textbook/i18n/ur/docusaurus-plugin-content-docs/current/"
)
def translate_file(english_path):
"""Translate a single file"""
urdu_path = get_urdu_path(english_path)
# Check if already translated
if os.path.exists(urdu_path):
with open(urdu_path, 'r', encoding='utf-8') as f:
content = f.read()
# Check if it has Urdu script
if any(ord(char) >= 0x0600 and ord(char) <= 0x06FF for char in content[:500]):
print(f"[OK] SKIP: {english_path} (already translated)")
return True
# Read English file
if not os.path.exists(english_path):
print(f"[ERROR] File not found: {english_path}")
return False
with open(english_path, 'r', encoding='utf-8') as f:
english_content = f.read()
print(f"[...] Translating: {english_path}")
# Translate
urdu_content = translate_to_urdu(english_content)
if not urdu_content:
print(f"[FAIL] Translation failed for {english_path}")
return False
# Create directory if needed
urdu_dir = os.path.dirname(urdu_path)
os.makedirs(urdu_dir, exist_ok=True)
# Write Urdu file
with open(urdu_path, 'w', encoding='utf-8') as f:
f.write(urdu_content)
print(f"[OK] SUCCESS: {urdu_path}")
return True
def main():
print("=" * 60)
print("CRITICAL FILES TRANSLATION FOR URDU DEMO")
print("=" * 60)
print(f"Total files to translate: {len(CRITICAL_FILES)}")
print()
success_count = 0
failed_files = []
for i, file_path in enumerate(CRITICAL_FILES, 1):
print(f"\n[{i}/{len(CRITICAL_FILES)}] {file_path}")
if translate_file(file_path):
success_count += 1
else:
failed_files.append(file_path)
# Rate limiting - wait 2 seconds between requests
if i < len(CRITICAL_FILES):
time.sleep(2)
print("\n" + "=" * 60)
print("TRANSLATION COMPLETE!")
print("=" * 60)
print(f"[OK] Successful: {success_count}/{len(CRITICAL_FILES)}")
if failed_files:
print(f"[FAIL] Failed: {len(failed_files)}")
for f in failed_files:
print(f" - {f}")
print()
print("Next steps:")
print("1. Run: cd textbook && npm run build")
print("2. Test Urdu pages at /ur/...")
print("3. Implement per-chapter translation button")
if __name__ == "__main__":
main()