| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1e72cfe commit b7d2eb5
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,6 +6,8 @@ The original content is written in Markdown, write the translation in Markdown a | |||
| 6 | 6 | ||
| 7 | 7 | The original content will be surrounded by triple percentage signs (%%%). Do not include the triple percentage signs in the translation. | |
| 8 | 8 | ||
| 9 | + [placeholder_for_additional_instructions] | ||
| 10 | + | ||
| 9 | 11 | ### Technical terms in English | |
| 10 | 12 | ||
| 11 | 13 | For technical terms in English that don't have a common translation term, use the original term in English. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -57,39 +57,19 @@ def generate_en_path(*, lang: str, path: Path) -> Path: | |||
| 57 | 57 | return out_path | |
| 58 | 58 | ||
| 59 | 59 | ||
| 60 | - @app.command() | ||
| 61 | - def translate_page( | ||
| 62 | - *, | ||
| 63 | - language: Annotated[str, typer.Option(envvar="LANGUAGE")], | ||
| 64 | - en_path: Annotated[Path, typer.Option(envvar="EN_PATH")], | ||
| 65 | - ) -> None: | ||
| 66 | - assert language != "en", ( | ||
| 67 | - "`en` is the source language, choose another language as translation target" | ||
| 60 | + def get_prompt( | ||
| 61 | + lang_prompt_content: str, | ||
| 62 | + old_translation: str | None, | ||
| 63 | + language: str, | ||
| 64 | + language_name: str, | ||
| 65 | + original_content: str, | ||
| 66 | + additional_instructions: str, | ||
| 67 | + ) -> str: | ||
| 68 | + general_prompt_with_additional_instructions = general_prompt.replace( | ||
| 69 | + "[placeholder_for_additional_instructions]", additional_instructions | ||
| 68 | 70 | ) | |
| 69 | - langs = get_langs() | ||
| 70 | - language_name = langs[language] | ||
| 71 | - lang_path = Path(f"docs/{language}") | ||
| 72 | - lang_path.mkdir(exist_ok=True) | ||
| 73 | - lang_prompt_path = lang_path / "llm-prompt.md" | ||
| 74 | - assert lang_prompt_path.exists(), f"Prompt file not found: {lang_prompt_path}" | ||
| 75 | - lang_prompt_content = lang_prompt_path.read_text(encoding="utf-8") | ||
| 76 | - | ||
| 77 | - en_docs_path = Path("docs/en/docs") | ||
| 78 | - assert str(en_path).startswith(str(en_docs_path)), ( | ||
| 79 | - f"Path must be inside {en_docs_path}" | ||
| 80 | - ) | ||
| 81 | - out_path = generate_lang_path(lang=language, path=en_path) | ||
| 82 | - out_path.parent.mkdir(parents=True, exist_ok=True) | ||
| 83 | - original_content = en_path.read_text(encoding="utf-8") | ||
| 84 | - old_translation: str | None = None | ||
| 85 | - if out_path.exists(): | ||
| 86 | - print(f"Found existing translation: {out_path}") | ||
| 87 | - old_translation = out_path.read_text(encoding="utf-8") | ||
| 88 | - print(f"Translating {en_path} to {language} ({language_name})") | ||
| 89 | - agent = Agent("openai:gpt-5") | ||
| 90 | - | ||
| 91 | 71 | prompt_segments = [ | |
| 92 | - general_prompt, | ||
| 72 | + general_prompt_with_additional_instructions, | ||
| 93 | 73 | lang_prompt_content, | |
| 94 | 74 | ] | |
| 95 | 75 | if old_translation: | |
@@ -119,12 +99,57 @@ def translate_page( | |||
| 119 | 99 | f"%%%\n{original_content}%%%", | |
| 120 | 100 | ] | |
| 121 | 101 | ) | |
| 122 | - prompt = "\n\n".join(prompt_segments) | ||
| 102 | + return "\n\n".join(prompt_segments) | ||
| 103 | + | ||
| 104 | + | ||
| 105 | + @app.command() | ||
| 106 | + def translate_page( | ||
| 107 | + *, | ||
| 108 | + language: Annotated[str, typer.Option(envvar="LANGUAGE")], | ||
| 109 | + en_path: Annotated[Path, typer.Option(envvar="EN_PATH")], | ||
| 110 | + ) -> None: | ||
| 111 | + assert language != "en", ( | ||
| 112 | + "`en` is the source language, choose another language as translation target" | ||
| 113 | + ) | ||
| 114 | + langs = get_langs() | ||
| 115 | + language_name = langs[language] | ||
| 116 | + lang_path = Path(f"docs/{language}") | ||
| 117 | + lang_path.mkdir(exist_ok=True) | ||
| 118 | + lang_prompt_path = lang_path / "llm-prompt.md" | ||
| 119 | + assert lang_prompt_path.exists(), f"Prompt file not found: {lang_prompt_path}" | ||
| 120 | + lang_prompt_content = lang_prompt_path.read_text(encoding="utf-8") | ||
| 121 | + | ||
| 122 | + en_docs_path = Path("docs/en/docs") | ||
| 123 | + assert str(en_path).startswith(str(en_docs_path)), ( | ||
| 124 | + f"Path must be inside {en_docs_path}" | ||
| 125 | + ) | ||
| 126 | + out_path = generate_lang_path(lang=language, path=en_path) | ||
| 127 | + out_path.parent.mkdir(parents=True, exist_ok=True) | ||
| 128 | + original_content = en_path.read_text(encoding="utf-8") | ||
| 129 | + old_translation: str | None = None | ||
| 130 | + if out_path.exists(): | ||
| 131 | + print(f"Found existing translation: {out_path}") | ||
| 132 | + old_translation = out_path.read_text(encoding="utf-8") | ||
| 133 | + print(f"Translating {en_path} to {language} ({language_name})") | ||
| 134 | + agent = Agent("openai:gpt-5") | ||
| 123 | 135 | ||
| 124 | 136 | MAX_ATTEMPTS = 3 | |
| 137 | + additional_instructions = "" | ||
| 125 | 138 | for attempt_no in range(1, MAX_ATTEMPTS + 1): | |
| 126 | 139 | print(f"Running agent for {out_path} (attempt {attempt_no}/{MAX_ATTEMPTS})") | |
| 127 | - result = agent.run_sync(prompt) | ||
| 140 | + prompt = get_prompt( | ||
| 141 | + lang_prompt_content=lang_prompt_content, | ||
| 142 | + old_translation=old_translation, | ||
| 143 | + language=language, | ||
| 144 | + language_name=language_name, | ||
| 145 | + original_content=original_content, | ||
| 146 | + additional_instructions=additional_instructions, | ||
| 147 | + ) | ||
| 148 | + result = agent.run_sync( | ||
| 149 | + prompt.replace( | ||
| 150 | + "[placeholder_for_additional_instructions]", additional_instructions | ||
| 151 | + ) | ||
| 152 | + ) | ||
| 128 | 153 | out_content = f"{result.output.strip()}\n" | |
| 129 | 154 | try: | |
| 130 | 155 | check_translation( | |
@@ -139,6 +164,11 @@ def translate_page( | |||
| 139 | 164 | print( | |
| 140 | 165 | f"Translation check failed on attempt {attempt_no}/{MAX_ATTEMPTS}: {e}" | |
| 141 | 166 | ) | |
| 167 | + additional_instructions = ( | ||
| 168 | + f"Current translation fails validation checks ({str(e)}). " | ||
| 169 | + "Please, pay special attention to it." | ||
| 170 | + ) | ||
| 171 | + old_translation = out_content | ||
| 142 | 172 | continue # Retry if not reached max attempts | |
| 143 | 173 | else: # Max retry attempts reached | |
| 144 | 174 | print(f"Translation failed for {out_path} after {MAX_ATTEMPTS} attempts") | |
| Back | FazBrowse Home | New Git URL |
0 commit comments