mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
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:
committed by
Paulus Schoutsen
parent
a60712d826
commit
b434ffba2d
108
tests/helpers/test_translation.py
Normal file
108
tests/helpers/test_translation.py
Normal file
@@ -0,0 +1,108 @@
|
||||
"""Test the translation helper."""
|
||||
# pylint: disable=protected-access
|
||||
from os import path
|
||||
|
||||
import homeassistant.helpers.translation as translation
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
|
||||
def test_flatten():
|
||||
"""Test the flatten function."""
|
||||
data = {
|
||||
"parent1": {
|
||||
"child1": "data1",
|
||||
"child2": "data2",
|
||||
},
|
||||
"parent2": "data3",
|
||||
}
|
||||
|
||||
flattened = translation.flatten(data)
|
||||
|
||||
assert flattened == {
|
||||
"parent1.child1": "data1",
|
||||
"parent1.child2": "data2",
|
||||
"parent2": "data3",
|
||||
}
|
||||
|
||||
|
||||
async def test_component_translation_file(hass):
|
||||
"""Test the component translation file function."""
|
||||
assert await async_setup_component(hass, 'switch', {
|
||||
'switch': {'platform': 'test'}
|
||||
})
|
||||
assert await async_setup_component(hass, 'test_standalone', {
|
||||
'test_standalone'
|
||||
})
|
||||
assert await async_setup_component(hass, 'test_package', {
|
||||
'test_package'
|
||||
})
|
||||
|
||||
assert path.normpath(translation.component_translation_file(
|
||||
'switch.test', 'en')) == path.normpath(hass.config.path(
|
||||
'custom_components', 'switch', '.translations', 'test.en.json'))
|
||||
|
||||
assert path.normpath(translation.component_translation_file(
|
||||
'test_standalone', 'en')) == path.normpath(hass.config.path(
|
||||
'custom_components', '.translations', 'test_standalone.en.json'))
|
||||
|
||||
assert path.normpath(translation.component_translation_file(
|
||||
'test_package', 'en')) == path.normpath(hass.config.path(
|
||||
'custom_components', 'test_package', '.translations', 'en.json'))
|
||||
|
||||
|
||||
def test_load_translations_files(hass):
|
||||
"""Test the load translation files function."""
|
||||
# Test one valid and one invalid file
|
||||
file1 = hass.config.path(
|
||||
'custom_components', 'switch', '.translations', 'test.en.json')
|
||||
file2 = hass.config.path(
|
||||
'custom_components', 'switch', '.translations', 'invalid.json')
|
||||
assert translation.load_translations_files({
|
||||
'switch.test': file1,
|
||||
'invalid': file2
|
||||
}) == {
|
||||
'switch.test': {
|
||||
'state': {
|
||||
'string1': 'Value 1',
|
||||
'string2': 'Value 2',
|
||||
}
|
||||
},
|
||||
'invalid': {},
|
||||
}
|
||||
|
||||
|
||||
async def test_get_translations(hass):
|
||||
"""Test the get translations helper."""
|
||||
translations = await translation.async_get_translations(hass, 'en')
|
||||
assert translations == {}
|
||||
|
||||
assert await async_setup_component(hass, 'switch', {
|
||||
'switch': {'platform': 'test'}
|
||||
})
|
||||
|
||||
translations = await translation.async_get_translations(hass, 'en')
|
||||
assert translations == {
|
||||
'component.switch.state.string1': 'Value 1',
|
||||
'component.switch.state.string2': 'Value 2',
|
||||
}
|
||||
|
||||
translations = await translation.async_get_translations(hass, 'de')
|
||||
assert translations == {
|
||||
'component.switch.state.string1': 'German Value 1',
|
||||
'component.switch.state.string2': 'German Value 2',
|
||||
}
|
||||
|
||||
# Test a partial translation
|
||||
translations = await translation.async_get_translations(hass, 'es')
|
||||
assert translations == {
|
||||
'component.switch.state.string1': 'Spanish Value 1',
|
||||
'component.switch.state.string2': 'Value 2',
|
||||
}
|
||||
|
||||
# Test that an untranslated language falls back to English.
|
||||
translations = await translation.async_get_translations(
|
||||
hass, 'invalid-language')
|
||||
assert translations == {
|
||||
'component.switch.state.string1': 'Value 1',
|
||||
'component.switch.state.string2': 'Value 2',
|
||||
}
|
||||
Reference in New Issue
Block a user