mirror of
https://github.com/hacs/documentation.git
synced 2025-08-12 23:53:35 +00:00
Add macro to reuse frontend translation strings
This commit is contained in:
@ -70,6 +70,8 @@ plugins:
|
||||
- privacy
|
||||
- macros:
|
||||
on_undefined: strict
|
||||
include_dir: source/includes
|
||||
module_name: source/macros
|
||||
- search:
|
||||
lang: en
|
||||
- social:
|
||||
|
@ -22,7 +22,7 @@ All downloaded repositories will have a quick action button (this is the 3 dots
|
||||
|
||||
With this you can quickly manage the repository and get to its issue tracker.
|
||||
|
||||
## Search
|
||||
## {{coreui("ui.components.data-table.search")}}
|
||||
|
||||
Above the list of repositories there is a search field.
|
||||
In this field you can search for both downloaded and available repositories you can manage with HACS.
|
||||
@ -60,7 +60,7 @@ With this filter you can select one of the available [types](/docs/use/type/inde
|
||||
|
||||
Once enabled it will show repositories in that category.
|
||||
|
||||
## Columns
|
||||
## {{hacsui("dialog_overview.columns")}}
|
||||
|
||||
!!! note
|
||||
This is **not** available for mobile devices.
|
||||
|
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