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:
parent
a60712d826
commit
b434ffba2d
19 changed files with 575 additions and 6 deletions
81
script/translations_upload_merge.py
Executable file
81
script/translations_upload_merge.py
Executable file
|
@ -0,0 +1,81 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Merge all translation sources into a single JSON file."""
|
||||
import glob
|
||||
import itertools
|
||||
import os
|
||||
import re
|
||||
|
||||
from homeassistant.util import json as json_util
|
||||
|
||||
FILENAME_FORMAT = re.compile(r'strings\.(?P<suffix>\w+)\.json')
|
||||
|
||||
|
||||
def find_strings_files():
|
||||
"""Return the paths of the strings source files."""
|
||||
return itertools.chain(
|
||||
glob.iglob("strings*.json"),
|
||||
glob.iglob("*{}strings*.json".format(os.sep)),
|
||||
)
|
||||
|
||||
|
||||
def get_component_platform(path):
|
||||
"""Get the component and platform name from the path."""
|
||||
directory, filename = os.path.split(path)
|
||||
match = FILENAME_FORMAT.search(filename)
|
||||
suffix = match.group('suffix') if match else None
|
||||
if directory:
|
||||
return directory, suffix
|
||||
else:
|
||||
return suffix, None
|
||||
|
||||
|
||||
def get_translation_dict(translations, component, platform):
|
||||
"""Return the dict to hold component translations."""
|
||||
if not component:
|
||||
return translations['component']
|
||||
|
||||
if component not in translations:
|
||||
translations['component'][component] = {}
|
||||
|
||||
if not platform:
|
||||
return translations['component'][component]
|
||||
|
||||
if 'platform' not in translations['component'][component]:
|
||||
translations['component'][component]['platform'] = {}
|
||||
|
||||
if platform not in translations['component'][component]['platform']:
|
||||
translations['component'][component]['platform'][platform] = {}
|
||||
|
||||
return translations['component'][component]['platform'][platform]
|
||||
|
||||
|
||||
def main():
|
||||
"""Main section of the script."""
|
||||
if not os.path.isfile("requirements_all.txt"):
|
||||
print("Run this from HA root dir")
|
||||
return
|
||||
|
||||
root = os.getcwd()
|
||||
os.chdir(os.path.join("homeassistant", "components"))
|
||||
|
||||
translations = {
|
||||
'component': {}
|
||||
}
|
||||
|
||||
paths = find_strings_files()
|
||||
for path in paths:
|
||||
component, platform = get_component_platform(path)
|
||||
parent = get_translation_dict(translations, component, platform)
|
||||
strings = json_util.load_json(path)
|
||||
parent.update(strings)
|
||||
|
||||
os.chdir(root)
|
||||
|
||||
os.makedirs("build", exist_ok=True)
|
||||
|
||||
json_util.save_json(
|
||||
os.path.join("build", "translations-upload.json"), translations)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue