@@ -61,12 +61,19 @@ struct Locale {
61
61
62
62
[[nodiscard]] int matchScore (const Locale& other) const {
63
63
if (this ->language != other.language ) return 0 ;
64
- auto territoryMatches = !this ->territory .isEmpty () && this ->territory == other.territory ;
65
- auto modifierMatches = !this ->modifier .isEmpty () && this ->modifier == other.modifier ;
66
64
67
- auto score = 1 ;
68
- if (territoryMatches) score += 2 ;
69
- if (modifierMatches) score += 1 ;
65
+ // If candidate specifies a modifier, it must match
66
+ if (!other.modifier .isEmpty () && this ->modifier != other.modifier ) return 0 ;
67
+
68
+ // If candidate specifies a territory, it must match
69
+ if (!other.territory .isEmpty () && this ->territory != other.territory ) return 0 ;
70
+
71
+ // Base language match
72
+ int score = 1 ;
73
+
74
+ // Reward specificity when it matches
75
+ if (!other.territory .isEmpty ()) score += 2 ;
76
+ if (!other.modifier .isEmpty ()) score += 1 ;
70
77
71
78
return score;
72
79
}
@@ -178,11 +185,21 @@ ParsedDesktopEntryData DesktopEntry::parseText(const QString& id, const QString&
178
185
179
186
if (entries.contains (key)) {
180
187
const auto & old = entries.value (key);
188
+ const int oldScore = system.matchScore (old.first );
189
+ const int newScore = system.matchScore (locale);
181
190
182
- auto oldScore = system.matchScore (old.first );
183
- auto newScore = system.matchScore (locale);
191
+ bool shouldReplace = false ;
192
+
193
+ if (newScore > oldScore) {
194
+ shouldReplace = true ;
195
+ } else if (newScore == oldScore) {
196
+ // Prefer unlocalized (invalid locale) when both scores tie (usually 0)
197
+ if (!locale.isValid () && old.first .isValid ()) {
198
+ shouldReplace = true ;
199
+ }
200
+ }
184
201
185
- if (newScore > oldScore || (oldScore == 0 && !locale. isValid ()) ) {
202
+ if (shouldReplace ) {
186
203
entries.insert (key, qMakePair (locale, value));
187
204
}
188
205
} else {
0 commit comments