mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
Fix loading translations for embedded platforms (#20122)
* Fix loading translations for embedded platforms * Update doc string * Lint
This commit is contained in:
parent
cd6679eb5b
commit
9fd21d20ae
@ -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
|
|
||||||
|
|
||||||
|
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)
|
module = get_component(hass, component)
|
||||||
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
|
module_path = pathlib.Path(module.__file__)
|
||||||
# prefix should be skipped.
|
|
||||||
if module.__name__ == module.__package__:
|
if module.__name__ == module.__package__:
|
||||||
|
# light/__init__.py
|
||||||
filename = '{}.json'.format(language)
|
filename = '{}.json'.format(language)
|
||||||
else:
|
else:
|
||||||
filename = '{}.{}.json'.format(name, language)
|
# group.py
|
||||||
|
filename = '{}.{}.json'.format(component, language)
|
||||||
|
|
||||||
return path.join(component_path, '.translations', filename)
|
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
|
||||||
|
|
||||||
|
# Either within HA or custom_components
|
||||||
|
# Either light/hue.py or hue/light.py
|
||||||
|
module_path = pathlib.Path(module.__file__)
|
||||||
|
|
||||||
|
# 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:
|
||||||
|
# this is hue/light.py
|
||||||
|
filename = "{}.{}.json".format(parts[0], language)
|
||||||
|
|
||||||
|
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]) \
|
||||||
|
@ -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'))
|
||||||
|
@ -0,0 +1,6 @@
|
|||||||
|
"""Component with embedded platforms."""
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup(hass, config):
|
||||||
|
"""Mock config."""
|
||||||
|
return True
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user