Debian Codegraph @

scripts/translation_fixer.py

raw
1 import os
2 from collections.abc import Iterable
3 from pathlib import Path
4 from typing import Annotated
5
6 import typer
7
8 from scripts.doc_parsing_utils import check_translation
9
10 non_translated_sections = (
11 f"reference{os.sep}",
12 "release-notes.md",
13 "fastapi-people.md",
14 "external-links.md",
15 "newsletter.md",
16 "management-tasks.md",
17 "management.md",
18 "contributing.md",
19 "translations.md",
20 )
21
22
23 cli = typer.Typer()
24
25
26 @cli.callback()
27 def callback():
28 pass
29
30
31 def iter_all_lang_paths(lang_path_root: Path) -> Iterable[Path]:
32 """
33 Iterate on the markdown files to translate in order of priority.
34 """
35
36 first_dirs = [
37 lang_path_root / "learn",
38 lang_path_root / "tutorial",
39 lang_path_root / "advanced",
40 lang_path_root / "about",
41 lang_path_root / "how-to",
42 ]
43 first_parent = lang_path_root
44 yield from first_parent.glob("*.md")
45 for dir_path in first_dirs:
46 yield from dir_path.rglob("*.md")
47 first_dirs_str = tuple(str(d) for d in first_dirs)
48 for path in lang_path_root.rglob("*.md"):
49 if str(path).startswith(first_dirs_str):
50 continue
51 if path.parent == first_parent:
52 continue
53 yield path
54
55
56 def get_all_paths(lang: str):
57 res: list[str] = []
58 lang_docs_root = Path("docs") / lang / "docs"
59 for path in iter_all_lang_paths(lang_docs_root):
60 relpath = path.relative_to(lang_docs_root)
61 if not str(relpath).startswith(non_translated_sections):
62 res.append(str(relpath))
63 return res
64
65
66 def process_one_page(path: Path) -> bool:
67 """
68 Fix one translated document by comparing it to the English version.
69
70 Returns True if processed successfully, False otherwise.
71 """
72
73 try:
74 lang_code = path.parts[1]
75 if lang_code == "en":
76 print(f"Skipping English document: {path}")
77 return True
78
79 en_doc_path = Path("docs") / "en" / Path(*path.parts[2:])
80
81 doc_lines = path.read_text(encoding="utf-8").splitlines()
82 en_doc_lines = en_doc_path.read_text(encoding="utf-8").splitlines()
83
84 doc_lines = check_translation(
85 doc_lines=doc_lines,
86 en_doc_lines=en_doc_lines,
87 lang_code=lang_code,
88 auto_fix=True,
89 path=str(path),
90 )
91
92 # Write back the fixed document
93 doc_lines.append("") # Ensure file ends with a newline
94 path.write_text("\n".join(doc_lines), encoding="utf-8")
95
96 except ValueError as e:
97 print(f"Error processing {path}: {e}")
98 return False
99 return True
100
101
102 @cli.command()
103 def fix_all(ctx: typer.Context, language: str):
104 docs = get_all_paths(language)
105
106 all_good = True
107 for page in docs:
108 doc_path = Path("docs") / language / "docs" / page
109 res = process_one_page(doc_path)
110 all_good = all_good and res
111
112 if not all_good:
113 raise typer.Exit(code=1)
114
115
116 @cli.command()
117 def fix_pages(
118 doc_paths: Annotated[
119 list[Path],
120 typer.Argument(help="List of paths to documents."),
121 ],
122 ):
123 all_good = True
124 for path in doc_paths:
125 res = process_one_page(path)
126 all_good = all_good and res
127
128 if not all_good:
129 raise typer.Exit(code=1)
130
131
132 if __name__ == "__main__":
133 cli()