Skip to content

Commit

Permalink
разбери, потом собери
Browse files Browse the repository at this point in the history
  • Loading branch information
mrf0rtuna4 committed May 5, 2024
1 parent 8d5b2f6 commit ffe65c2
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions core/translator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import re

from deep_translator import GoogleTranslator
import re


def read_readme():
Expand All @@ -13,12 +12,12 @@ def update_localizations():
readme_content = read_readme()
selected_langs = os.getenv("LANGS")

no_html_content = re.sub(r"<[^>]+>", "", readme_content)
no_html_content = re.sub(r"!\[([^]]+)]\([^)]+\)", r"\1", no_html_content)
no_links_content = re.sub(r"\[([^]]+)]\(([^)]+)\)", r"\1", no_html_content)
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)

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

languages = [lang.strip() for lang in selected_langs.split(",")]
files = []
Expand All @@ -31,6 +30,10 @@ def update_localizations():
translated_chunks = [GoogleTranslator(source='auto', target=lang).translate(text=chunk) for chunk in chunks]
translated_content = " ".join(translated_chunks)

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

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

0 comments on commit ffe65c2

Please sign in to comment.