Fix loading translations for embedded platforms (#20122)

* Fix loading translations for embedded platforms

* Update doc string

* Lint
This commit is contained in:
Paulus Schoutsen 2019-01-15 16:06:04 -08:00 committed by GitHub
parent cd6679eb5b
commit 9fd21d20ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 16 deletions

View File

@ -1,10 +1,10 @@
"""Translation string lookup helpers.""" """Translation string lookup helpers."""
import logging import logging
from os import path import pathlib
from typing import Any, Dict, Iterable from typing import Any, Dict, Iterable
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.loader import get_component, bind_hass from homeassistant.loader import get_component, get_platform, bind_hass
from homeassistant.util.json import load_json from homeassistant.util.json import load_json
from .typing import HomeAssistantType from .typing import HomeAssistantType
@ -32,24 +32,51 @@ def flatten(data: Dict) -> Dict[str, Any]:
def component_translation_file(hass: HomeAssistantType, component: str, def component_translation_file(hass: HomeAssistantType, component: str,
language: str) -> str: language: str) -> str:
"""Return the translation json file location for a component.""" """Return the translation json file location for a component.
if '.' in component:
name = component.split('.', 1)[1]
else:
name = component
module = get_component(hass, component) For component one of:
- components/light/.translations/nl.json
- components/.translations/group.nl.json
For platform one of:
- components/light/.translations/hue.nl.json
- components/hue/.translations/light.nl.json
"""
is_platform = '.' in component
if not is_platform:
module = get_component(hass, component)
assert module is not None
module_path = pathlib.Path(module.__file__)
if module.__name__ == module.__package__:
# light/__init__.py
filename = '{}.json'.format(language)
else:
# group.py
filename = '{}.{}.json'.format(component, language)
return str(module_path.parent / '.translations' / filename)
# It's a platform
parts = component.split('.', 1)
module = get_platform(hass, *parts)
assert module is not None assert module is not None
component_path = path.dirname(module.__file__)
# If loading translations for the package root, (__init__.py), the # Either within HA or custom_components
# prefix should be skipped. # Either light/hue.py or hue/light.py
if module.__name__ == module.__package__: module_path = pathlib.Path(module.__file__)
filename = '{}.json'.format(language)
# Compare to parent so we don't have to strip off `.py`
if module_path.parent.name == parts[0]:
# this is light/hue.py
filename = "{}.{}.json".format(parts[1], language)
else: else:
filename = '{}.{}.json'.format(name, language) # this is hue/light.py
filename = "{}.{}.json".format(parts[0], language)
return path.join(component_path, '.translations', filename) return str(module_path.parent / '.translations' / filename)
def load_translations_files(translation_files: Dict[str, str]) \ def load_translations_files(translation_files: Dict[str, str]) \

View File

@ -40,7 +40,10 @@ def test_flatten():
async def test_component_translation_file(hass): async def test_component_translation_file(hass):
"""Test the component translation file function.""" """Test the component translation file function."""
assert await async_setup_component(hass, 'switch', { assert await async_setup_component(hass, 'switch', {
'switch': {'platform': 'test'} 'switch': [
{'platform': 'test'},
{'platform': 'test_embedded'}
]
}) })
assert await async_setup_component(hass, 'test_standalone', { assert await async_setup_component(hass, 'test_standalone', {
'test_standalone' 'test_standalone'
@ -53,6 +56,11 @@ async def test_component_translation_file(hass):
hass, 'switch.test', 'en')) == path.normpath(hass.config.path( hass, 'switch.test', 'en')) == path.normpath(hass.config.path(
'custom_components', 'switch', '.translations', 'test.en.json')) 'custom_components', 'switch', '.translations', 'test.en.json'))
assert path.normpath(translation.component_translation_file(
hass, 'switch.test_embedded', 'en')) == path.normpath(hass.config.path(
'custom_components', 'test_embedded', '.translations',
'switch.en.json'))
assert path.normpath(translation.component_translation_file( assert path.normpath(translation.component_translation_file(
hass, 'test_standalone', 'en')) == path.normpath(hass.config.path( hass, 'test_standalone', 'en')) == path.normpath(hass.config.path(
'custom_components', '.translations', 'test_standalone.en.json')) 'custom_components', '.translations', 'test_standalone.en.json'))

View File

@ -0,0 +1,6 @@
"""Component with embedded platforms."""
async def async_setup(hass, config):
"""Mock config."""
return True

View File

@ -0,0 +1,7 @@
"""Switch platform for the embedded component."""
async def async_setup_platform(hass, config, async_add_entities_callback,
discovery_info=None):
"""Find and return test switches."""
pass