diff --git a/script/translations/download.py b/script/translations/download.py index 8f17e05708001dc6b7abb5193155ecc70d0950a8..7fc4c3365ccac9ea7272dc48f29f15251496e407 100755 --- a/script/translations/download.py +++ b/script/translations/download.py @@ -7,7 +7,7 @@ import re import subprocess from typing import Dict, List, Union -from .const import CLI_2_DOCKER_IMAGE, CORE_PROJECT_ID +from .const import CLI_2_DOCKER_IMAGE, CORE_PROJECT_ID, INTEGRATIONS_DIR from .error import ExitApp from .util import get_lokalise_token @@ -74,23 +74,13 @@ def get_component_path(lang, component): def get_platform_path(lang, component, platform): """Get the platform translation path.""" - if os.path.isdir(os.path.join("homeassistant", "components", component, platform)): - return os.path.join( - "homeassistant", - "components", - component, - platform, - "translations", - f"{lang}.json", - ) - else: - return os.path.join( - "homeassistant", - "components", - component, - "translations", - f"{platform}.{lang}.json", - ) + return os.path.join( + "homeassistant", + "components", + component, + "translations", + f"{platform}.{lang}.json", + ) def get_component_translations(translations): @@ -111,9 +101,12 @@ def save_language_translations(lang, translations): os.makedirs(os.path.dirname(path), exist_ok=True) save_json(path, base_translations) - for platform, platform_translations in component_translations.get( - "platform", {} - ).items(): + if "platform" not in component_translations: + continue + + for platform, platform_translations in component_translations[ + "platform" + ].items(): path = get_platform_path(lang, component, platform) os.makedirs(os.path.dirname(path), exist_ok=True) save_json(path, platform_translations) @@ -127,12 +120,20 @@ def write_integration_translations(): save_language_translations(lang, translations) +def delete_old_translations(): + """Delete old translations.""" + for fil in INTEGRATIONS_DIR.glob("*/translations/*"): + fil.unlink() + + def run(): """Run the script.""" DOWNLOAD_DIR.mkdir(parents=True, exist_ok=True) run_download_docker() + delete_old_translations() + write_integration_translations() return 0