@@ -104,20 +104,30 @@ def score(
104104 return (- 1 , 0 )
105105
106106 is_exact_language = self .language == dialect .language
107+ is_exact_language_and_code = is_exact_language and (self .code == dialect .code )
107108
108109 if (self .region is None ) and (dialect .region is None ):
109110 # Weak match with no region constraint
110111 # Prefer exact language match
111- return (2 if is_exact_language else 1 , 0 )
112+ if is_exact_language_and_code :
113+ return (3 , 0 )
114+
115+ if is_exact_language :
116+ return (2 , 0 )
117+
118+ return (1 , 0 )
112119
113120 if (self .region is not None ) and (dialect .region is not None ):
114121 if self .region == dialect .region :
115122 # Same language + region match
116123 # Prefer exact language match
117- return (
118- math .inf ,
119- 1 if is_exact_language else 0 ,
120- )
124+ if is_exact_language_and_code :
125+ return (math .inf , 2 )
126+
127+ if is_exact_language :
128+ return (math .inf , 1 )
129+
130+ return (math .inf , 0 )
121131
122132 # Regions are both set, but don't match
123133 return (0 , 0 )
@@ -139,8 +149,8 @@ def score(
139149 region_idx = pref_regions .index (dialect .region )
140150
141151 # More preferred regions are at the front.
142- # Add 1 to boost above a weak match where no regions are set.
143- return (1 + (len (pref_regions ) - region_idx ), 0 )
152+ # Add 2 to boost above a weak match where no regions are set.
153+ return (2 + (len (pref_regions ) - region_idx ), 0 )
144154 except ValueError :
145155 # Region was not in preferred list
146156 pass
0 commit comments