From 4a3f34144452c652ffcb2dab8644bd2cbb3efc2d Mon Sep 17 00:00:00 2001 From: leranp Date: Wed, 31 May 2023 05:27:32 +0300 Subject: [PATCH] Language codes for Hebrew (#93681) * Language codes for Hebrew There is 2 optional code for Hebrew: he-IL is the new code iw-IL is the old code , the google cloud STT for example is using the old code (iw) * Update language.py * Update test_language.py * Update test_language.py * Update test_language.py * Simplify duplicate language check --------- Co-authored-by: Paulus Schoutsen --- homeassistant/util/language.py | 12 +++++++++--- tests/util/test_language.py | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/homeassistant/util/language.py b/homeassistant/util/language.py index 615024e059c..4ec8c74ffa9 100644 --- a/homeassistant/util/language.py +++ b/homeassistant/util/language.py @@ -10,6 +10,14 @@ import re from homeassistant.const import MATCH_ALL SEPARATOR_RE = re.compile(r"[-_]") +SAME_LANGUAGES = ( + # no = spoken Norwegian + # nb = written Norwegian (Bokmål) + ("nb", "no"), + # he = Hebrew new code + # iw = Hebrew old code + ("he", "iw"), +) def preferred_regions( @@ -60,9 +68,7 @@ def is_language_match(lang_1: str, lang_2: str) -> bool: # Exact match return True - if {lang_1, lang_2} == {"no", "nb"}: - # no = spoken Norwegian - # nb = written Norwegian (Bokmål) + if tuple(sorted([lang_1, lang_2])) in SAME_LANGUAGES: return True return False diff --git a/tests/util/test_language.py b/tests/util/test_language.py index 41f3ef4b301..cc4e5d66c31 100644 --- a/tests/util/test_language.py +++ b/tests/util/test_language.py @@ -226,3 +226,39 @@ def test_no_nb_prefer_exact_regions() -> None: "no-AA", ["en-US", "en-GB", "no-AA", "nb-AA"], ) == ["no-AA", "nb-AA"] + + +def test_he_iw_same() -> None: + """Test that the he/iw are interchangeable.""" + assert language.matches( + "he", + ["en-US", "en-GB", "iw"], + ) == ["iw"] + assert language.matches( + "iw", + ["en-US", "en-GB", "he"], + ) == ["he"] + + +def test_he_iw_prefer_exact() -> None: + """Test that the exact language is preferred even if an interchangeable language is available.""" + assert language.matches( + "he", + ["en-US", "en-GB", "iw", "he"], + ) == ["he", "iw"] + assert language.matches( + "he", + ["en-US", "en-GB", "he", "iw"], + ) == ["he", "iw"] + + +def test_he_iw_prefer_exact_regions() -> None: + """Test that the exact language/region is preferred.""" + assert language.matches( + "he-IL", + ["en-US", "en-GB", "iw-IL", "he-IL"], + ) == ["he-IL", "iw-IL"] + assert language.matches( + "he-IL", + ["en-US", "en-GB", "he-IL", "iw-IL"], + ) == ["he-IL", "iw-IL"]