Skip to content

Commit

Permalink
re: checking system, add md decompiler and md builder
Browse files Browse the repository at this point in the history
  • Loading branch information
mrf0rtuna4 committed May 5, 2024
1 parent ffe65c2 commit 86ebbea
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions core/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,42 @@
import re


def read_readme():
async def decompile_readme():
with open("README.md", "r", encoding="utf-8") as file:
return file.read()
readme_content = file.read()

code_blocks = re.findall(r"```[\s\S]*?```", readme_content)
non_code_blocks = re.sub(r"```[\s\S]*?```", "CODE_BLOCK", readme_content)
links = re.findall(r"\[([^]]+)]\(([^)]+)\)", non_code_blocks)
non_code_blocks = re.sub(r"\[([^]]+)]\(([^)]+)\)", "LINK", non_code_blocks)
html_tags = re.findall(r"<.*?>", non_code_blocks)
non_code_blocks = re.sub(r"<.*?>", "HTML_TAG", non_code_blocks)

def update_localizations():
readme_content = read_readme()
selected_langs = os.getenv("LANGS")
chunk_size = 5000
chunks = [non_code_blocks[i:i + chunk_size]
for i in range(0, len(non_code_blocks), chunk_size)]

no_html_content = re.sub(r"<.*?>", "", readme_content)
code_blocks = re.findall(r"```[\s\S]*?```", no_html_content)
no_code_content = re.sub(r"```[\s\S]*?```", "CODE_BLOCK", no_html_content)
return chunks, {"code_blocks": code_blocks, "links": links, "html_tags": html_tags}

chunk_size = 5000
chunks = [no_code_content[i:i + chunk_size] for i in range(0, len(no_code_content), chunk_size)]

async def build_readme(translated_chunks, data):
translated_content = " ".join(translated_chunks)

for i, code_block in enumerate(data["code_blocks"]):
translated_content = translated_content.replace(f"CODE_BLOCK", code_block, 1)

for i, link in enumerate(data["links"]):
translated_content = translated_content.replace(f"LINK", f"[{link[0]}]({link[1]})", 1)

for i, html_tag in enumerate(data["html_tags"]):
translated_content = translated_content.replace(f"HTML_TAG", html_tag, 1)

return translated_content


async def update_localizations():
chunks, data = await decompile_readme()
selected_langs = os.getenv("LANGS")

languages = [lang.strip() for lang in selected_langs.split(",")]
files = []
Expand All @@ -27,12 +48,12 @@ def update_localizations():

for lang in languages:
try:
translated_chunks = [GoogleTranslator(source='auto', target=lang).translate(text=chunk) for chunk in chunks]
translated_content = " ".join(translated_chunks)
translated_chunks = []
for chunk in chunks:
translated_chunk = GoogleTranslator(source='auto', target=lang).translate(text=chunk)
translated_chunks.append(translated_chunk)

# Add code blocks back
for i, block in enumerate(code_blocks):
translated_content = translated_content.replace(f"CODE_BLOCK", block, 1)
translated_content = await build_readme(translated_chunks, data)

with open(f"dist/{lang}.md", "w", encoding="utf-8") as file:
file.write(translated_content)
Expand Down

0 comments on commit 86ebbea

Please sign in to comment.