Skip to content

Commit c044b6f

Browse files
committed
core/desktopentry: fix locale matching to ignore mismatched modifiers/territories
1 parent 84c4146 commit c044b6f

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/core/desktopentry.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,19 @@ struct Locale {
6161

6262
[[nodiscard]] int matchScore(const Locale& other) const {
6363
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;
6664

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;
7077

7178
return score;
7279
}
@@ -178,11 +185,21 @@ ParsedDesktopEntryData DesktopEntry::parseText(const QString& id, const QString&
178185

179186
if (entries.contains(key)) {
180187
const auto& old = entries.value(key);
188+
const int oldScore = system.matchScore(old.first);
189+
const int newScore = system.matchScore(locale);
181190

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+
}
184201

185-
if (newScore > oldScore || (oldScore == 0 && !locale.isValid())) {
202+
if (shouldReplace) {
186203
entries.insert(key, qMakePair(locale, value));
187204
}
188205
} else {

0 commit comments

Comments
 (0)