Skip to content

Commit 5d3555d

Browse files
committed
core/desktopentry: fix locale matching
1 parent 84c4146 commit 5d3555d

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/core/desktopentry.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ 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;
64+
65+
if (!other.modifier.isEmpty() && this->modifier != other.modifier) return 0;
66+
if (!other.territory.isEmpty() && this->territory != other.territory) return 0;
6667

6768
auto score = 1;
68-
if (territoryMatches) score += 2;
69-
if (modifierMatches) score += 1;
69+
70+
if (!other.territory.isEmpty()) score += 2;
71+
if (!other.modifier.isEmpty()) score += 1;
7072

7173
return score;
7274
}
@@ -178,11 +180,21 @@ ParsedDesktopEntryData DesktopEntry::parseText(const QString& id, const QString&
178180

179181
if (entries.contains(key)) {
180182
const auto& old = entries.value(key);
183+
const int oldScore = system.matchScore(old.first);
184+
const int newScore = system.matchScore(locale);
185+
186+
bool shouldReplace = false;
181187

182-
auto oldScore = system.matchScore(old.first);
183-
auto newScore = system.matchScore(locale);
188+
if (newScore > oldScore) {
189+
shouldReplace = true;
190+
} else if (newScore == oldScore) {
191+
// Prefer unlocalized (invalid locale) when both scores tie (usually 0)
192+
if (!locale.isValid() && old.first.isValid()) {
193+
shouldReplace = true;
194+
}
195+
}
184196

185-
if (newScore > oldScore || (oldScore == 0 && !locale.isValid())) {
197+
if (shouldReplace) {
186198
entries.insert(key, qMakePair(locale, value));
187199
}
188200
} else {

0 commit comments

Comments
 (0)