mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Add script to copy backend translations to frontend (#34706)
This commit is contained in:
parent
454c5d824a
commit
c97ce05b09
@ -1,6 +1,5 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Merge all translation sources into a single JSON file."""
|
"""Merge all translation sources into a single JSON file."""
|
||||||
import glob
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
@ -47,16 +46,6 @@ def run_download_docker():
|
|||||||
raise ExitApp("Failed to download translations")
|
raise ExitApp("Failed to download translations")
|
||||||
|
|
||||||
|
|
||||||
def load_json(filename: str) -> Union[List, Dict]:
|
|
||||||
"""Load JSON data from a file and return as dict or list.
|
|
||||||
|
|
||||||
Defaults to returning empty dict if file is not found.
|
|
||||||
"""
|
|
||||||
with open(filename, encoding="utf-8") as fdesc:
|
|
||||||
return json.loads(fdesc.read())
|
|
||||||
return {}
|
|
||||||
|
|
||||||
|
|
||||||
def save_json(filename: str, data: Union[List, Dict]):
|
def save_json(filename: str, data: Union[List, Dict]):
|
||||||
"""Save JSON data to a file.
|
"""Save JSON data to a file.
|
||||||
|
|
||||||
@ -69,11 +58,6 @@ def save_json(filename: str, data: Union[List, Dict]):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def get_language(path):
|
|
||||||
"""Get the language code for the given file path."""
|
|
||||||
return os.path.splitext(os.path.basename(path))[0]
|
|
||||||
|
|
||||||
|
|
||||||
def get_component_path(lang, component):
|
def get_component_path(lang, component):
|
||||||
"""Get the component translation path."""
|
"""Get the component translation path."""
|
||||||
if os.path.isdir(os.path.join("homeassistant", "components", component)):
|
if os.path.isdir(os.path.join("homeassistant", "components", component)):
|
||||||
@ -132,10 +116,9 @@ def save_language_translations(lang, translations):
|
|||||||
|
|
||||||
def write_integration_translations():
|
def write_integration_translations():
|
||||||
"""Write integration translations."""
|
"""Write integration translations."""
|
||||||
paths = glob.iglob("build/translations-download/*.json")
|
for lang_file in DOWNLOAD_DIR.glob("*.json"):
|
||||||
for path in paths:
|
lang = lang_file.stem
|
||||||
lang = get_language(path)
|
translations = json.loads(lang_file.read_text())
|
||||||
translations = load_json(path)
|
|
||||||
save_language_translations(lang, translations)
|
save_language_translations(lang, translations)
|
||||||
|
|
||||||
|
|
||||||
|
46
script/translations/frontend.py
Normal file
46
script/translations/frontend.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
"""Write updated translations to the frontend."""
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
|
||||||
|
from .const import FRONTEND_DIR
|
||||||
|
from .download import DOWNLOAD_DIR, run_download_docker
|
||||||
|
from .util import get_base_arg_parser
|
||||||
|
|
||||||
|
FRONTEND_BACKEND_TRANSLATIONS = FRONTEND_DIR / "translations/backend"
|
||||||
|
|
||||||
|
|
||||||
|
def get_arguments() -> argparse.Namespace:
|
||||||
|
"""Get parsed passed in arguments."""
|
||||||
|
parser = get_base_arg_parser()
|
||||||
|
parser.add_argument(
|
||||||
|
"--skip-download", action="store_true", help="Skip downloading translations."
|
||||||
|
)
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
def run():
|
||||||
|
"""Update frontend translations with backend data.
|
||||||
|
|
||||||
|
We use the downloaded Docker files because it gives us each language in 1 file.
|
||||||
|
"""
|
||||||
|
args = get_arguments()
|
||||||
|
|
||||||
|
if not args.skip_download:
|
||||||
|
run_download_docker()
|
||||||
|
|
||||||
|
for lang_file in DOWNLOAD_DIR.glob("*.json"):
|
||||||
|
translations = json.loads(lang_file.read_text())
|
||||||
|
|
||||||
|
to_write_translations = {"component": {}}
|
||||||
|
|
||||||
|
for domain, domain_translations in translations["component"].items():
|
||||||
|
if "state" not in domain_translations:
|
||||||
|
continue
|
||||||
|
|
||||||
|
to_write_translations["component"][domain] = {
|
||||||
|
"state": domain_translations["state"]
|
||||||
|
}
|
||||||
|
|
||||||
|
(FRONTEND_BACKEND_TRANSLATIONS / lang_file.name).write_text(
|
||||||
|
json.dumps(to_write_translations, indent=2)
|
||||||
|
)
|
@ -327,27 +327,17 @@ def find_frontend_states():
|
|||||||
def run():
|
def run():
|
||||||
"""Migrate translations."""
|
"""Migrate translations."""
|
||||||
# Import new common keys
|
# Import new common keys
|
||||||
# migrate_project_keys_translations(
|
migrate_project_keys_translations(
|
||||||
# FRONTEND_PROJECT_ID,
|
CORE_PROJECT_ID,
|
||||||
# CORE_PROJECT_ID,
|
FRONTEND_PROJECT_ID,
|
||||||
# {
|
{
|
||||||
# "state::default::off": "common::state::off",
|
"common::state::off": "state::default::off",
|
||||||
# "state::default::on": "common::state::on",
|
"common::state::on": "state::default::on",
|
||||||
# "state::cover::open": "common::state::open",
|
},
|
||||||
# "state::cover::closed": "common::state::closed",
|
)
|
||||||
# "state::binary_sensor::connectivity::on": "common::state::connected",
|
|
||||||
# "state::binary_sensor::connectivity::off": "common::state::disconnected",
|
|
||||||
# "state::lock::locked": "common::state::locked",
|
|
||||||
# "state::lock::unlocked": "common::state::unlocked",
|
|
||||||
# "state::timer::active": "common::state::active",
|
|
||||||
# "state::camera::idle": "common::state::idle",
|
|
||||||
# "state::media_player::standby": "common::state::standby",
|
|
||||||
# "state::media_player::paused": "common::state::paused",
|
|
||||||
# "state::device_tracker::home": "common::state::home",
|
|
||||||
# "state::device_tracker::not_home": "common::state::not_home",
|
|
||||||
# },
|
|
||||||
# )
|
|
||||||
|
|
||||||
find_frontend_states()
|
# find_frontend_states()
|
||||||
|
|
||||||
|
# find_different_languages()
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
@ -13,7 +13,7 @@ def get_base_arg_parser() -> argparse.ArgumentParser:
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"action",
|
"action",
|
||||||
type=str,
|
type=str,
|
||||||
choices=["clean", "develop", "download", "migrate", "upload"],
|
choices=["clean", "develop", "download", "frontend", "migrate", "upload"],
|
||||||
)
|
)
|
||||||
parser.add_argument("--debug", action="store_true", help="Enable log output")
|
parser.add_argument("--debug", action="store_true", help="Enable log output")
|
||||||
return parser
|
return parser
|
||||||
|
Loading…
x
Reference in New Issue
Block a user