mirror of https://github.com/tiangolo/fastapi.git
🔨 Refactor translate script with extra feedback (prints) (#13932)
This commit is contained in:
parent
4f0aae9d23
commit
adc328e258
|
|
@ -106,7 +106,9 @@ def translate_page(*, lang: str, path: Path) -> None:
|
||||||
original_content = path.read_text()
|
original_content = path.read_text()
|
||||||
old_translation: str | None = None
|
old_translation: str | None = None
|
||||||
if out_path.exists():
|
if out_path.exists():
|
||||||
|
print(f"Found existing translation: {out_path}")
|
||||||
old_translation = out_path.read_text()
|
old_translation = out_path.read_text()
|
||||||
|
print(f"Translating {path} to {lang} ({language})")
|
||||||
agent = Agent("openai:gpt-4o")
|
agent = Agent("openai:gpt-4o")
|
||||||
|
|
||||||
prompt_segments = [
|
prompt_segments = [
|
||||||
|
|
@ -131,13 +133,14 @@ def translate_page(*, lang: str, path: Path) -> None:
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
prompt = "\n\n".join(prompt_segments)
|
prompt = "\n\n".join(prompt_segments)
|
||||||
|
print(f"Running agent for {out_path}")
|
||||||
result = agent.run_sync(prompt)
|
result = agent.run_sync(prompt)
|
||||||
out_content = f"{result.data.strip()}\n"
|
out_content = f"{result.data.strip()}\n"
|
||||||
|
print(f"Saving translation to {out_path}")
|
||||||
out_path.write_text(out_content)
|
out_path.write_text(out_content)
|
||||||
|
|
||||||
|
|
||||||
def iter_paths_to_translate() -> Iterable[Path]:
|
def iter_all_en_paths() -> Iterable[Path]:
|
||||||
"""
|
"""
|
||||||
Iterate on the markdown files to translate in order of priority.
|
Iterate on the markdown files to translate in order of priority.
|
||||||
"""
|
"""
|
||||||
|
|
@ -161,13 +164,16 @@ def iter_paths_to_translate() -> Iterable[Path]:
|
||||||
yield path
|
yield path
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
def iter_en_paths_to_translate() -> Iterable[Path]:
|
||||||
def translate_all(lang: str) -> None:
|
for path in iter_all_en_paths():
|
||||||
paths_to_process: list[Path] = []
|
|
||||||
for path in iter_paths_to_translate():
|
|
||||||
if str(path).replace("docs/en/docs/", "").startswith(non_translated_sections):
|
if str(path).replace("docs/en/docs/", "").startswith(non_translated_sections):
|
||||||
continue
|
continue
|
||||||
paths_to_process.append(path)
|
yield path
|
||||||
|
|
||||||
|
|
||||||
|
@app.command()
|
||||||
|
def translate_all(lang: str) -> None:
|
||||||
|
paths_to_process = list(iter_en_paths_to_translate())
|
||||||
print("Original paths:")
|
print("Original paths:")
|
||||||
for p in paths_to_process:
|
for p in paths_to_process:
|
||||||
print(f" - {p}")
|
print(f" - {p}")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue