Fix zh Hant/Hans (#91390)

* Fix zh Hant/Hans

* Fix comments
pull/91947/head
Michael Hansen 2023-04-24 09:55:08 -05:00 committed by GitHub
parent 1ae7610622
commit 5075281add
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 8 deletions

View File

@ -16,7 +16,11 @@ def preferred_regions(
country: str | None = None,
code: str | None = None,
) -> Iterable[str | None]:
"""Yield preferred regions for a language based on country/code hints."""
"""Yield an ordered list of regions for a language based on country/code hints.
Regions should be checked for support in the returned order if no other
information is available.
"""
if country is not None:
yield country.upper()
@ -27,10 +31,8 @@ def preferred_regions(
elif language == "zh":
if code == "Hant":
yield "HK"
elif code == "Hans":
yield "TW"
else:
# Prefer China if no matching code
yield "CN"
# fr -> fr-FR

View File

@ -95,31 +95,40 @@ def test_language_as_region() -> None:
def test_zh_hant() -> None:
"""Test that the zh-Hant defaults to HK."""
"""Test that the zh-Hant matches HK or TW first."""
assert language.matches(
"zh-Hant",
["en-US", "en-GB", "zh-CN", "zh-HK", "zh-TW"],
) == [
"zh-HK",
"zh-CN",
"zh-TW",
"zh-CN",
]
assert language.matches(
"zh-Hant",
["en-US", "en-GB", "zh-CN", "zh-TW", "zh-HK"],
) == [
"zh-TW",
"zh-HK",
"zh-CN",
]
def test_zh_hans() -> None:
"""Test that the zh-Hans defaults to TW."""
"""Test that the zh-Hans matches CN first."""
assert language.matches(
"zh-Hans",
["en-US", "en-GB", "zh-CN", "zh-HK", "zh-TW"],
) == [
"zh-TW",
"zh-CN",
"zh-HK",
"zh-TW",
]
def test_zh_no_code() -> None:
"""Test that the zh defaults to CN."""
"""Test that the zh defaults to CN first."""
assert language.matches(
"zh",
["en-US", "en-GB", "zh-CN", "zh-HK", "zh-TW"],