Support serving of backend translations (#12453)

* Add view to support backend translation fetching

* Load backend translations from component json

* Translations for season sensor

* Scripts to merge and unpack Lokalise translations

* Fix copy paste error

* Serve post-lokalise translations to frontend

* Linting

* Auto-deploy translations with Travis

* Commit post-lokalise translation files

* Split logic into more helper functions

* Fall back to English for missing keys

* Move local translation copies to `.translations`

* Linting

* Initial tests

* Remove unnecessary file check

* Convert translation helper to async/await

* Convert translation helper tests to async/await

* Use set subtraction to find missing_components

* load_translation_files use component->file mapping

* Remove duplicated resources fetching

Get to take advantage of the slick Python 3.5 dict merging here.

* Switch to live project ID
This commit is contained in:
Adam Mills 2018-02-28 22:31:38 -05:00 committed by Paulus Schoutsen
parent a60712d826
commit b434ffba2d
19 changed files with 575 additions and 6 deletions

View file

@ -0,0 +1,81 @@
#!/usr/bin/env python3
"""Merge all translation sources into a single JSON file."""
import glob
import os
import re
from homeassistant.util import json as json_util
FILENAME_FORMAT = re.compile(r'strings\.(?P<suffix>\w+)\.json')
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):
"""Get the component translation path."""
if os.path.isdir(os.path.join("homeassistant", "components", component)):
return os.path.join(
"homeassistant", "components", component, ".translations",
"{}.json".format(lang))
else:
return os.path.join(
"homeassistant", "components", ".translations",
"{}.{}.json".format(component, lang))
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", "{}.json".format(lang))
else:
return os.path.join(
"homeassistant", "components", component, ".translations",
"{}.{}.json".format(platform, lang))
def get_component_translations(translations):
"""Get the component level translations."""
translations = translations.copy()
translations.pop('platform', None)
return translations
def save_language_translations(lang, translations):
"""Distribute the translations for this language."""
components = translations.get('component', {})
for component, component_translations in components.items():
base_translations = get_component_translations(component_translations)
if base_translations:
path = get_component_path(lang, component)
os.makedirs(os.path.dirname(path), exist_ok=True)
json_util.save_json(path, base_translations)
for platform, platform_translations in component_translations.get(
'platform', {}).items():
path = get_platform_path(lang, component, platform)
os.makedirs(os.path.dirname(path), exist_ok=True)
json_util.save_json(path, platform_translations)
def main():
"""Main section of the script."""
if not os.path.isfile("requirements_all.txt"):
print("Run this from HA root dir")
return
paths = glob.iglob("build/translations-download/*.json")
for path in paths:
lang = get_language(path)
translations = json_util.load_json(path)
save_language_translations(lang, translations)
if __name__ == '__main__':
main()