Prefer country over language family + MATCH_ALL (#91753)
* Prefer country over language family * More test fixespull/91767/head
parent
672fb44041
commit
9fdc794b36
|
@ -6,6 +6,8 @@ from dataclasses import dataclass
|
|||
import operator
|
||||
import re
|
||||
|
||||
from homeassistant.const import MATCH_ALL
|
||||
|
||||
SEPARATOR_RE = re.compile(r"[-_]")
|
||||
|
||||
|
||||
|
@ -95,7 +97,7 @@ class Dialect:
|
|||
other_regions = pref_regions if dialect.region is None else {dialect.region}
|
||||
|
||||
# Better match if there is overlap in regions
|
||||
return 1 if regions.intersection(other_regions) else 0
|
||||
return 2 if regions.intersection(other_regions) else 0
|
||||
|
||||
@staticmethod
|
||||
def parse(tag: str) -> Dialect:
|
||||
|
@ -125,6 +127,9 @@ def matches(
|
|||
target: str, supported: Iterable[str], country: str | None = None
|
||||
) -> list[str]:
|
||||
"""Return a sorted list of matching language tags based on a target tag and country hint."""
|
||||
if target == MATCH_ALL:
|
||||
return list(supported)
|
||||
|
||||
target_dialect = Dialect.parse(target)
|
||||
|
||||
# Higher score is better
|
||||
|
|
|
@ -172,8 +172,8 @@
|
|||
'id': 'homeassistant',
|
||||
'name': 'Home Assistant',
|
||||
'supported_languages': list([
|
||||
'de',
|
||||
'de-CH',
|
||||
'de',
|
||||
]),
|
||||
}),
|
||||
dict({
|
||||
|
|
|
@ -443,5 +443,5 @@ async def test_ws_list_engines(
|
|||
assert msg["type"] == "result"
|
||||
assert msg["success"]
|
||||
assert msg["result"] == {
|
||||
"providers": [{"engine_id": engine_id, "supported_languages": ["de", "de-CH"]}]
|
||||
"providers": [{"engine_id": engine_id, "supported_languages": ["de-CH", "de"]}]
|
||||
}
|
||||
|
|
|
@ -1,9 +1,19 @@
|
|||
"""Test Home Assistant language util methods."""
|
||||
from __future__ import annotations
|
||||
|
||||
from homeassistant.const import MATCH_ALL
|
||||
from homeassistant.util import language
|
||||
|
||||
|
||||
def test_match_all() -> None:
|
||||
"""Test MATCH_ALL."""
|
||||
assert language.matches(MATCH_ALL, ["fr-Fr", "en-US", "en-GB"]) == [
|
||||
"fr-Fr",
|
||||
"en-US",
|
||||
"en-GB",
|
||||
]
|
||||
|
||||
|
||||
def test_region_match() -> None:
|
||||
"""Test that an exact language/region match is preferred."""
|
||||
assert language.matches("en-GB", ["fr-Fr", "en-US", "en-GB"]) == [
|
||||
|
@ -53,6 +63,26 @@ def test_country_preferred() -> None:
|
|||
]
|
||||
|
||||
|
||||
def test_country_preferred_over_family() -> None:
|
||||
"""Test that country hint is preferred over language family."""
|
||||
assert (
|
||||
language.matches(
|
||||
"de",
|
||||
["de", "de-CH", "de-DE"],
|
||||
country="CH",
|
||||
)[0]
|
||||
== "de-CH"
|
||||
)
|
||||
assert (
|
||||
language.matches(
|
||||
"de",
|
||||
["de", "de-CH", "de-DE"],
|
||||
country="DE",
|
||||
)[0]
|
||||
== "de-DE"
|
||||
)
|
||||
|
||||
|
||||
def test_language_as_region() -> None:
|
||||
"""Test that the language itself can be interpreted as a region."""
|
||||
assert language.matches(
|
||||
|
|
Loading…
Reference in New Issue