forked from gramps-project/gramps-web-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate.py
More file actions
258 lines (213 loc) · 8.16 KB
/
Copy pathtranslate.py
File metadata and controls
258 lines (213 loc) · 8.16 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python3
"""
Gramps Web Docs Translation Tool using OpenAI GPT-4o-mini
Translates Markdown documentation while preserving formatting.
"""
import argparse
import os
import sys
import time
from pathlib import Path
from openai import OpenAI
from dotenv import load_dotenv
# Load environment variables from .env file if it exists
load_dotenv()
# Configuration
CONFIG = {
"api": {
"provider": "openai",
"model": "gpt-4o-mini",
"timeout": 180,
"max_retries": 3,
"retry_delay": 5
},
"translation": {
"source_language": "en",
"target_languages": ["de", "fr", "es", "zh", "vi", "tr", "ru", "pt", "ja", "da", "fi", "it", "uk"],
"docs_dir": "docs",
"source_dir": "docs/en"
}
}
# Language names for prompting and display
LANGUAGE_NAMES = {
"de": "German",
"fr": "French",
"es": "Spanish",
"zh": "Simplified Chinese",
"vi": "Vietnamese",
"tr": "Turkish",
"ru": "Russian",
"pt": "Portuguese",
"ja": "Japanese",
"da": "Danish",
"fi": "Finnish",
"it": "Italian",
"uk": "Ukrainian",
"en": "English"
}
# Get API key from environment variable
API_KEY = os.getenv("OPENAI_API_KEY")
if not API_KEY:
raise ValueError(
"OPENAI_API_KEY environment variable not set.\n"
"Set it in .env file or as environment variable."
)
def translate_with_openai(content, target_lang, source_lang="en"):
"""Translate Markdown content using OpenAI GPT-4o-mini"""
api_config = CONFIG["api"]
client = OpenAI(api_key=API_KEY)
model = api_config["model"]
target_lang_name = LANGUAGE_NAMES.get(target_lang, target_lang)
source_lang_name = LANGUAGE_NAMES.get(source_lang, source_lang)
prompt = f"""You are translating technical documentation from {source_lang_name} to {target_lang_name}.
INSTRUCTIONS:
- Translate ALL text content to {target_lang_name}
- Preserve ALL Markdown formatting exactly (tables, links, code blocks, etc.)
- Keep URLs, file paths, and code unchanged
- Maintain the exact structure and line breaks
Document to translate:
{content}
Translated document in {target_lang_name}:"""
max_retries = api_config.get("max_retries", 3)
timeout = api_config.get("timeout", 180)
retry_delay = api_config.get("retry_delay", 5)
for attempt in range(max_retries):
try:
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are a professional translator specialized in technical documentation. You preserve Markdown formatting perfectly."},
{"role": "user", "content": prompt}
],
temperature=0.3, # Lower temperature for more consistent translations
timeout=timeout
)
translated = response.choices[0].message.content
# Remove potential markdown code block wrappers if AI added them
if translated.startswith("```markdown\n"):
translated = translated[len("```markdown\n"):]
if translated.startswith("```\n"):
translated = translated[len("```\n"):]
if translated.endswith("\n```"):
translated = translated[:-len("\n```")]
if translated.endswith("```"):
translated = translated[:-len("```")]
return translated.strip()
except Exception as e:
if attempt < max_retries - 1:
wait_time = retry_delay * (2 ** attempt) # Exponential backoff
print(f" Retry {attempt + 1}/{max_retries} after error: {e}")
time.sleep(wait_time)
else:
raise Exception(f"Translation failed after {max_retries} attempts: {e}")
def translate_file(src_path, target_lang, force=False):
"""Translate a single Markdown file"""
translation_config = CONFIG["translation"]
source_dir = Path(translation_config["source_dir"])
docs_dir = Path(translation_config["docs_dir"])
relative_path = src_path.relative_to(source_dir)
dst_path = docs_dir / target_lang / relative_path
if dst_path.exists() and not force:
print(f" Skipping {dst_path} (already exists)")
return True
try:
with open(src_path, "r", encoding="utf-8") as f:
content = f.read()
print(f" Translating to {target_lang}: {relative_path}")
translated = translate_with_openai(content, target_lang)
dst_path.parent.mkdir(parents=True, exist_ok=True)
with open(dst_path, "w", encoding="utf-8") as f:
f.write(translated)
# Ensure file ends with newline
if not translated.endswith('\n'):
f.write('\n')
print(f" Saved: {dst_path}")
return True
except Exception as e:
print(f" ERROR: Failed to translate {src_path}: {e}")
return False
def main():
parser = argparse.ArgumentParser(
description="Translate Gramps Web documentation using OpenAI GPT-4o-mini"
)
parser.add_argument(
"--file",
type=str,
help="Translate a specific file (relative to docs/en/)"
)
parser.add_argument(
"--lang",
type=str,
choices=["de", "fr", "es", "zh", "vi", "tr", "ru", "pt", "ja", "da", "fi", "it", "uk", "all"],
default="all",
help="Target language(s)"
)
parser.add_argument(
"--test",
action="store_true",
help="Test mode: only translate docs/en/user-guide/first-login.md"
)
parser.add_argument(
"--force",
action="store_true",
help="Force translation even if target file exists"
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Show what would be done without actually translating"
)
args = parser.parse_args()
translation_config = CONFIG["translation"]
source_dir = Path(translation_config["source_dir"])
# Determine target languages
if args.lang == "all":
target_languages = translation_config["target_languages"]
else:
target_languages = [args.lang]
# Determine files to translate
if args.test:
files_to_translate = [source_dir / "user-guide" / "first-login.md"]
elif args.file:
file_path = Path(args.file)
if not file_path.is_absolute():
file_path = source_dir / file_path
files_to_translate = [file_path]
else:
# Find all .md files recursively
files_to_translate = sorted(source_dir.rglob("*.md"))
print("=" * 60)
print("Gramps Web Docs Translation (OpenAI GPT-4o-mini)")
print("=" * 60)
print(f"Source: {source_dir}")
print(f"Target languages: {', '.join(target_languages)}")
print(f"Files to translate: {len(files_to_translate)}")
print("=" * 60)
print()
if args.dry_run:
print("DRY RUN MODE - No files will be translated")
for lang in target_languages:
print(f"\n{lang.upper()}:")
for file_path in files_to_translate:
print(f" Would translate: {file_path.relative_to(source_dir)}")
return
success_count = 0
error_count = 0
for file_path in files_to_translate:
if not file_path.exists():
print(f"WARNING: File not found: {file_path}")
continue
print(f"\n{file_path.relative_to(source_dir)}")
for lang in target_languages:
if translate_file(file_path, lang, force=args.force):
success_count += 1
else:
error_count += 1
print()
print("=" * 60)
print(f" Success: {success_count}")
print(f" Errors: {error_count}")
print("=" * 60)
sys.exit(0 if error_count == 0 else 1)
if __name__ == "__main__":
main()