i18n
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Joakim Svensson 2025-07-18 19:58:16 +00:00
parent 6ed58ac405
commit c52d5ad275
1 changed files with 19 additions and 33 deletions

View File

@ -1,11 +1,9 @@
import os
import re
import shutil
import yaml
SOURCE_DIR = "docs"
ENGLISH_DIR = os.path.join(SOURCE_DIR, "en")
MKDOCS_YML = "mkdocs.yml"
# Kopiera alla filer från docs/ till docs/en/
if os.path.exists(ENGLISH_DIR):
@ -16,48 +14,36 @@ def clean_file(path, keep_lang):
with open(path, "r", encoding="utf-8") as f:
content = f.read()
# Ta bort block som inte ska vara kvar
# Ta bort språkblock som inte ska behållas
if keep_lang == "sv":
content = re.sub(r":en\n.*?\n:::", "", content, flags=re.DOTALL)
content = re.sub(r":::\s*en\s*\n(.*?)\n:::", "", content, flags=re.DOTALL)
elif keep_lang == "en":
content = re.sub(r":sv\n.*?\n:::", "", content, flags=re.DOTALL)
content = re.sub(r":::\s*sv\s*\n(.*?)\n:::", "", content, flags=re.DOTALL)
# Ta bort block-taggar för behållna block
content = re.sub(r":(sv|en)\n", "", content)
content = re.sub(r"\n:::", "", content)
# Ta bort språkblock-taggar för det språk vi behåller
content = re.sub(r":::\s*(sv|en)", "", content)
content = re.sub(r":::", "", content)
# Hantera inline-taggar
# Hantera inline-översättningar: [:sv]Text[:], [:en]Text[:]
def inline_replacer(match):
lang, text = match.groups()
lang = match.group(1)
text = match.group(2)
return text if lang == keep_lang else ""
content = re.sub(r"\[:(sv|en)](.*?)(?=(\[:|$))", inline_replacer, content)
# Använd re.DOTALL för att hantera radbrytningar i inline-taggar
content = re.sub(r"\[:(sv|en)](.*?)\[:\]", inline_replacer, content, flags=re.DOTALL)
with open(path, "w", encoding="utf-8") as f:
f.write(content.strip() + "\n")
# Rensa svenska versioner
# Rensa svenska filer i docs/
for filename in os.listdir(SOURCE_DIR):
full_path = os.path.join(SOURCE_DIR, filename)
if filename.endswith(".md") and os.path.isfile(full_path):
clean_file(full_path, keep_lang="sv")
path = os.path.join(SOURCE_DIR, filename)
if os.path.isfile(path) and filename.endswith(".md"):
clean_file(path, keep_lang="sv")
# Rensa engelska versioner
# Rensa engelska filer i docs/en/
for filename in os.listdir(ENGLISH_DIR):
full_path = os.path.join(ENGLISH_DIR, filename)
if filename.endswith(".md") and os.path.isfile(full_path):
clean_file(full_path, keep_lang="en")
# Skapa .pages för engelska versionen utifrån mkdocs.yml
def extract_nav_from_mkdocs_yml(path):
with open(path, "r", encoding="utf-8") as f:
data = yaml.safe_load(f)
return {
"title": data.get("site_name", "SA6ANW"),
"nav": data.get("nav", [])
}
english_pages = extract_nav_from_mkdocs_yml(MKDOCS_YML)
with open(os.path.join(ENGLISH_DIR, ".pages"), "w", encoding="utf-8") as f:
yaml.dump(english_pages, f, allow_unicode=True)
path = os.path.join(ENGLISH_DIR, filename)
if os.path.isfile(path) and filename.endswith(".md"):
clean_file(path, keep_lang="en")