@@ -14,4 +14,76 @@ defmodule Cldr.AcceptLanguageTest do
1414 after_tags = [ { 2.0 , "fr" } , { 1.0 , "en-us" } , { 1.0 , "en" } , { 1.0 , "es-es" } , { 1.0 , "es" } ]
1515 assert Cldr.AcceptLanguage . sort_by_quality ( before_tags ) == after_tags
1616 end
17+
18+ describe "regression: gettext-derived supported list with no bare language fallback (#263)" do
19+ # Reproduces the user's setup in
20+ # https://github.com/elixir-cldr/cldr/issues/263 — a Gettext backend with
21+ # regional locales only (e.g. "en_US", "en_GB", "en_CA") and no bare
22+ # "en". Pre-2.47.4 the matcher returned `nil` for inputs like "es-US"
23+ # and "zh-Hant" and collapsed "en-US" to bare "en" even though "en"
24+ # was not in the list. Tests use Cldr.Locale.Match.best_match/2
25+ # directly because that is the function Cldr.AcceptLanguage.best_match/2
26+ # uses internally for both cldr_locale_name and gettext_locale_name
27+ # resolution.
28+
29+ @ gettext_locales ~w( ar bg_BG cs_CZ da_DK de_DE el_GR en_CA en_GB en_US
30+ es_ES fi_FI fr_CA fr_FR hr_HR hu_HU id_ID it_IT
31+ ja_JP lt_LT ms_MY nb_NO nl_NL pl_PL pt pt_BR pt_PT
32+ ro_RO ru_RU sl_SI sv_SE th_TH uk_UA vi_VN zh_CN zh_HK)
33+
34+ test "exact regional match wins over language collapse" do
35+ assert { :ok , "en_US" , 0 } =
36+ Cldr.Locale.Match . best_match ( "en-US" ,
37+ supported: @ gettext_locales ,
38+ backend: TestBackend.Cldr
39+ )
40+ end
41+
42+ test "bare language with no exact supported entry resolves to the paradigm regional" do
43+ # "en" maximises to "en-Latn-US"; en_US is therefore the closest match.
44+ assert { :ok , "en_US" , 0 } =
45+ Cldr.Locale.Match . best_match ( "en" ,
46+ supported: @ gettext_locales ,
47+ backend: TestBackend.Cldr
48+ )
49+ end
50+
51+ test "regional locale not in supported list falls back to a configured regional, not nil" do
52+ # "es-US" used to return nil; should now resolve to a Spanish regional.
53+ assert { :ok , "es_ES" , _distance } =
54+ Cldr.Locale.Match . best_match ( "es-US" ,
55+ supported: @ gettext_locales ,
56+ backend: TestBackend.Cldr
57+ )
58+ end
59+
60+ test "regional locale outside paradigm group falls back to the nearest configured regional" do
61+ # "en-AU" used to collapse to bare "en"; per CLDR it should match
62+ # "en_GB" (en-AU shares the en-001 region group with en-GB).
63+ assert { :ok , "en_GB" , _distance } =
64+ Cldr.Locale.Match . best_match ( "en-AU" ,
65+ supported: @ gettext_locales ,
66+ backend: TestBackend.Cldr
67+ )
68+ end
69+
70+ test "script-only tag resolves to a regional with matching script" do
71+ # "zh-Hans" maximises to zh-Hans-CN → matches zh_CN.
72+ assert { :ok , "zh_CN" , 0 } =
73+ Cldr.Locale.Match . best_match ( "zh-Hans" ,
74+ supported: @ gettext_locales ,
75+ backend: TestBackend.Cldr
76+ )
77+ end
78+
79+ test "Traditional Chinese tag falls back to zh_HK rather than nil" do
80+ # "zh-Hant" used to return nil; should now match zh_HK
81+ # (Traditional script region).
82+ assert { :ok , "zh_HK" , _distance } =
83+ Cldr.Locale.Match . best_match ( "zh-Hant" ,
84+ supported: @ gettext_locales ,
85+ backend: TestBackend.Cldr
86+ )
87+ end
88+ end
1789end
0 commit comments