Browse Source

Undo unrelated changes in translate.py

Let's handle these changes in another PR.

Except the modifications in the prompt, this translate.py is now in sync with the translate.py in update-translations-prompt (#13968).
pull/14015/head
Nils Lindemann 11 months ago
parent
commit
d67ed51f19
  1. 93
      scripts/translate.py

93
scripts/translate.py

@ -700,17 +700,8 @@ def generate_en_path(*, lang: str, path: Path) -> Path:
@app.command()
def translate_page(
*,
language: Annotated[
str,
typer.Option(envvar="LANGUAGE", help="Target language, e.g. `es`, `fr`, `de`"),
],
en_path: Annotated[
Path,
typer.Option(
envvar="EN_PATH",
help="Path to the English source, relative to the FastAPI root directory. For example, `docs/en/docs/index.md`.",
),
],
language: Annotated[str, typer.Option(envvar="LANGUAGE")],
en_path: Annotated[Path, typer.Option(envvar="EN_PATH")],
) -> None:
assert language != "en", (
"`en` is the source language, choose another language as translation target"
@ -809,64 +800,32 @@ def iter_en_paths_to_translate() -> Iterable[Path]:
@app.command()
def translate_lang(
language: Annotated[
str,
typer.Option(envvar="LANGUAGE", help="Target language, e.g. `es`, `fr`, `de`"),
],
mode: Annotated[
str,
typer.Option(
help="Which files of the target language to translate, one of: `missing`, `existing`, `all`"
),
] = "missing",
verbose: Annotated[bool, typer.Option(help="Print all paths")] = False,
preview: Annotated[
bool, typer.Option(help="Show what will be done, but do not translate")
] = False,
) -> None:
allowed_modes = ["missing", "existing", "all"]
assert mode in allowed_modes, (
f"`mode` parameter must be one of {', '.join(f'`{mode}`' for mode in allowed_modes)}"
)
translatable_paths = list(iter_en_paths_to_translate())
def translate_lang(language: Annotated[str, typer.Option(envvar="LANGUAGE")]) -> None:
paths_to_process = list(iter_en_paths_to_translate())
print("Original paths:")
for p in paths_to_process:
print(f" - {p}")
print(f"Total original paths: {len(paths_to_process)}")
missing_paths: list[Path] = []
existing_paths: list[Path] = []
for p in translatable_paths:
skipped_paths: list[Path] = []
for p in paths_to_process:
lang_path = generate_lang_path(lang=language, path=p)
(existing_paths if lang_path.exists() else missing_paths).append(p)
def print_pathinfo(title: str, paths: list[Path], verbose: bool = verbose):
print(f"{len(paths)} {title}", end="")
if verbose and paths:
print(":")
for p in paths:
print(f" - {p}")
else:
print()
print_pathinfo("translatable paths", translatable_paths)
print_pathinfo("paths with a translation", existing_paths)
print_pathinfo("paths with no translation", missing_paths)
print(f"Mode: translate {mode}")
if mode == "missing" or (mode == "all" and len(existing_paths) == 0):
tbd_paths = missing_paths
action = "translate"
elif mode == "existing" or (mode == "all" and len(missing_paths) == 0):
tbd_paths = existing_paths
action = "update"
else:
tbd_paths = translatable_paths
action = "translate/update"
print(f"{len(tbd_paths)} paths to {action}")
if not preview:
for c, p in enumerate(tbd_paths):
print(f"({c + 1}/{len(tbd_paths)}) Translating: {p}")
translate_page(language=language, en_path=p)
print(f"Done translating: {p}")
if lang_path.exists():
skipped_paths.append(p)
continue
missing_paths.append(p)
print("Paths to skip:")
for p in skipped_paths:
print(f" - {p}")
print(f"Total paths to skip: {len(skipped_paths)}")
print("Paths to process:")
for p in missing_paths:
print(f" - {p}")
print(f"Total paths to process: {len(missing_paths)}")
for p in missing_paths:
print(f"Translating: {p}")
translate_page(language="es", en_path=p)
print(f"Done translating: {p}")
@app.command()

Loading…
Cancel
Save