MCPcopy Create free account
hub / github.com/ronreiter/interactive-tutorials / translate_tutorial

Function translate_tutorial

scripts/translation/translation.py:88–165  ·  view source on GitHub ↗
(base_file_path: str, output_file_path: str, language_code: str)

Source from the content-addressed store, hash-verified

86
87
88async def translate_tutorial(base_file_path: str, output_file_path: str, language_code: str) -> Optional[str]:
89 index_json = load_index_json(language_code)
90 english_title = Path(base_file_path).stem
91
92 # Look for translated title
93 translated_title = None
94 for section_titles in index_json.values():
95 if english_title in section_titles:
96 translated_title = section_titles[english_title]
97 break
98
99 if not translated_title:
100 logger.error(f"Title '{english_title}' not found in `index.json` for {language_code}. Skipping translation.")
101 return None
102
103 # Read the tutorial content
104 try:
105 with open(base_file_path, "r", encoding="utf-8") as f:
106 content = f.read()
107 except FileNotFoundError:
108 logger.error(f"Tutorial file not found: {base_file_path}")
109 return None
110
111 # Extract content without "Tutorial" header and up to "Tutorial Code"
112 content = re.sub(r"^Tutorial\n[-=]+\n", "", content, flags=re.MULTILINE)
113 content = re.search(r"(.*?)\nTutorial Code\n[-=]+", content, re.DOTALL)
114
115 if content:
116 content = content.group(1).strip()
117
118 # Prepare the translation prompt
119 prompt = f"""
120 Translate the following tutorial content into {language_code}. Follow these rules:
121
122 - **Markdown Headers and Formatting**:
123 -While the format must remain consistent, ensure that the header (e.g. ### Header) is translated into {language_code}.
124 Keep all Markdown elements such as headings (#, ##, ###, etc.) and lists (-) unchanged.
125 Do not modify the structure or formatting of the Markdown content.
126 Translate only the textual content within these elements where appropriate.
127 - Keep "Exercise--------" with the same structure but translated to {language_code}.
128 - Other headers such as "Tutorial--------" must stay exactly the same in the original language.
129
130 - **Preserve Code Blocks and Formatting**:
131 - Leave code examples and indentation unchanged.
132 - Do not add backticks (` ``` `) or translate code snippets.
133
134 - **Markdown Structure**:
135 - Maintain lists, headings, and links.
136 - Do not change URLs or Markdown links.
137
138 - **Translation Scope**:
139 - Translate only the explanatory sections.
140 - Leave all code and inline comments unchanged.
141
142 Content:
143 {content}
144 """
145

Callers 1

mainFunction · 0.85

Calls 3

load_index_jsonFunction · 0.85
valuesMethod · 0.80
createMethod · 0.45

Tested by

no test coverage detected