Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions docs/scripts/translate_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
---
"""


# Define the source and target directories
source_dir = "docs"
languages = {
Expand Down Expand Up @@ -177,7 +176,6 @@ def built_instructions(target_language: str, lang_code: str) -> str:
6. Once the final output is ready, return **only** the translated markdown text. No extra commentary.
"""


# Function to translate and save files
def translate_file(file_path: str, target_path: str, lang_code: str) -> None:
print(f"Translating {file_path} into a different language: {lang_code}")
Expand Down Expand Up @@ -221,21 +219,18 @@ def translate_file(file_path: str, target_path: str, lang_code: str) -> None:
translated_content: list[str] = []
for chunk in chunks:
instructions = built_instructions(languages[lang_code], lang_code)
if OPENAI_MODEL.startswith("o"):
response = openai_client.responses.create(
model=OPENAI_MODEL,
instructions=instructions,
input=chunk,
)
translated_content.append(response.output_text)
else:
response = openai_client.responses.create(
model=OPENAI_MODEL,
instructions=instructions,
input=chunk,
temperature=0.0,
)
translated_content.append(response.output_text)
# Use Chat Completions for translation (typed in openai v1.96+)
chat = openai_client.chat.completions.create(
model=OPENAI_MODEL,
messages=[
{"role": "system", "content": instructions},
{"role": "user", "content": chunk},
],
temperature=0.0,
)
# Ensure content is a string before appending
content_str = chat.choices[0].message.content or ""
translated_content.append(content_str)

translated_text = "\n".join(translated_content)
for idx, code_block in enumerate(code_blocks):
Expand Down Expand Up @@ -275,7 +270,6 @@ def main():
# Translate a single file
# Handle both "foo.md" and "docs/foo.md" formats
if args.file.startswith("docs/"):
# Remove "docs/" prefix if present
relative_file = args.file[5:]
else:
relative_file = args.file
Expand All @@ -293,7 +287,6 @@ def main():
# Skip the target directories
if any(lang in root for lang in languages):
continue
# Increasing this will make the translation faster; you can decide considering the model's capacity
concurrency = 6
with ThreadPoolExecutor(max_workers=concurrency) as executor:
futures = []
Expand All @@ -309,5 +302,4 @@ def main():


if __name__ == "__main__":
# translate_single_source_file("docs/index.md")
main()