mirror of
https://github.com/hacs/documentation.git
synced 2025-08-06 10:16:08 +00:00
Add macro to reuse frontend translation strings
This commit is contained in:
51
source/macros.py
Normal file
51
source/macros.py
Normal file
@ -0,0 +1,51 @@
|
||||
import requests
|
||||
import os
|
||||
from pathlib import Path
|
||||
import json
|
||||
|
||||
TRANSLATIONSFILES = ".cache/translations/"
|
||||
|
||||
def flatten_json(json_obj, parent_key='', sep='.'):
|
||||
items = {}
|
||||
for k, v in json_obj.items():
|
||||
new_key = f"{parent_key}{sep}{k}" if parent_key else k
|
||||
if isinstance(v, dict):
|
||||
items.update(flatten_json(v, new_key, sep=sep))
|
||||
else:
|
||||
items[new_key] = v
|
||||
return items
|
||||
|
||||
def init_translations():
|
||||
if not os.path.exists(TRANSLATIONSFILES):
|
||||
os.makedirs(TRANSLATIONSFILES)
|
||||
|
||||
if not os.path.exists(f"{TRANSLATIONSFILES}/hacs.json"):
|
||||
with open(f"{TRANSLATIONSFILES}/hacs.json", "w") as file:
|
||||
translations = requests.get("https://raw.githubusercontent.com/hacs/frontend/main/src/localize/languages/en.json").json()
|
||||
json.dump(flatten_json(translations), file)
|
||||
|
||||
if not os.path.exists(f"{TRANSLATIONSFILES}/core.json"):
|
||||
with open(f"{TRANSLATIONSFILES}/core.json", "w") as file:
|
||||
translations = requests.get("https://raw.githubusercontent.com/home-assistant/frontend/dev/src/translations/en.json").json()
|
||||
json.dump(flatten_json(translations), file)
|
||||
|
||||
|
||||
def define_env(env):
|
||||
init_translations()
|
||||
translations = {
|
||||
"hacs": json.loads(Path(f"{TRANSLATIONSFILES}/hacs.json").read_text()),
|
||||
"core": json.loads(Path(f"{TRANSLATIONSFILES}/core.json").read_text())
|
||||
}
|
||||
|
||||
@env.macro
|
||||
def hacsui(text: str, placeholders: dict = {}):
|
||||
if (translated := translations["hacs"].get(text)) is None:
|
||||
raise ValueError(f"Translation for '{text}' not found")
|
||||
return translated.format(**placeholders)
|
||||
|
||||
@env.macro
|
||||
def coreui(text: str, placeholders: dict = {}):
|
||||
if (translated := translations["core"].get(text)) is None:
|
||||
raise ValueError(f"Translation for '{text}' not found")
|
||||
return translated.format(**placeholders)
|
||||
|
Reference in New Issue
Block a user