[core] Fix local language not being used in some cases

Signed-off-by: Yannik Bloscheck <git@yannikbloscheck.com>
This commit is contained in:
Yannik Bloscheck
2025-08-05 17:44:13 +02:00
parent f09b372590
commit 185ae66101
4 changed files with 50 additions and 27 deletions

View File

@@ -128,34 +128,50 @@ bool IsNativeLang(feature::RegionData const & regionData, int8_t deviceLang)
return false; return false;
} }
vector<int8_t> MakeLanguagesPriorityList(int8_t deviceLang, bool preferDefault) int8_t DefaultLanguage(feature::RegionData const & regionData, vector<int8_t> langs)
{ {
vector<int8_t> langPriority = {deviceLang}; for (auto const lang : langs)
for (auto const & lang : languages::GetPreferredLangIndexes())
{ {
if (find(langPriority.begin(), langPriority.end(), lang) == langPriority.end()) if (regionData.HasLanguage(lang))
return lang;
for (auto const similiarLang : GetSimilarLanguages(lang))
{ {
langPriority.push_back(lang); if (regionData.HasLanguage(similiarLang))
return similiarLang;
} }
} }
if (preferDefault) return StrUtf8::kDefaultCode;
langPriority.push_back(StrUtf8::kDefaultCode); }
/// @DebugNote vector<int8_t> PrioritizedLanguages(vector<int8_t> const & langs, int8_t defaultLang)
// Add ru lang for descriptions/rendering tests. {
//langPriority.push_back(StrUtf8::GetLangIndex("ru")); vector<int8_t> prioritizedLangs = {};
auto const similarLangs = GetSimilarLanguages(deviceLang); for (auto const lang : langs)
langPriority.insert(langPriority.cend(), similarLangs.cbegin(), similarLangs.cend()); {
langPriority.insert(langPriority.cend(), {StrUtf8::kInternationalCode, StrUtf8::kEnglishCode}); if (find(prioritizedLangs.begin(), prioritizedLangs.end(), lang) == prioritizedLangs.end())
prioritizedLangs.push_back(lang);
return langPriority; if (defaultLang != StrUtf8::kUnsupportedLanguageCode && defaultLang == lang)
prioritizedLangs.push_back(StrUtf8::kDefaultCode);
auto const similarLangs = GetSimilarLanguages(lang);
prioritizedLangs.insert(prioritizedLangs.cend(), similarLangs.cbegin(), similarLangs.cend());
}
prioritizedLangs.push_back(StrUtf8::kInternationalCode);
prioritizedLangs.push_back(StrUtf8::kEnglishCode);
prioritizedLangs.push_back(StrUtf8::kDefaultCode);
return prioritizedLangs;
} }
void GetReadableNameImpl(NameParamsIn const & in, bool preferDefault, NameParamsOut & out) void GetReadableNameImpl(NameParamsIn const & in, bool preferDefault, NameParamsOut & out)
{ {
auto const langPriority = MakeLanguagesPriorityList(in.deviceLang, preferDefault); auto const preferredLangs = languages::GetPreferredLangIndexes();
auto const langPriority = PrioritizedLanguages(preferredLangs, DefaultLanguage(in.regionData, preferredLangs));
if (GetBestName(in.src, langPriority, out.primary)) if (GetBestName(in.src, langPriority, out.primary))
return; return;
@@ -361,7 +377,8 @@ void GetPreferredNames(NameParamsIn const & in, NameParamsOut & out)
if (in.IsNativeOrSimilarLang()) if (in.IsNativeOrSimilarLang())
return GetReadableNameImpl(in, true /* preferDefault */, out); return GetReadableNameImpl(in, true /* preferDefault */, out);
auto const primaryCodes = MakeLanguagesPriorityList(in.deviceLang, false /* preferDefault */); auto const preferredLangs = languages::GetPreferredLangIndexes();
auto const primaryCodes = PrioritizedLanguages(preferredLangs, DefaultLanguage(in.regionData, preferredLangs));
if (!GetBestName(in.src, primaryCodes, out.primary) && in.allowTranslit) if (!GetBestName(in.src, primaryCodes, out.primary) && in.allowTranslit)
GetTransliteratedName(in.regionData, in.src, out.transliterated); GetTransliteratedName(in.regionData, in.src, out.transliterated);
@@ -426,14 +443,15 @@ int8_t GetNameForSearchOnBooking(RegionData const & regionData, StringUtf8Multil
bool GetPreferredName(StringUtf8Multilang const & src, int8_t deviceLang, string_view & out) bool GetPreferredName(StringUtf8Multilang const & src, int8_t deviceLang, string_view & out)
{ {
auto const priorityList = MakeLanguagesPriorityList(deviceLang, true /* preferDefault */); auto const preferredLangs = languages::GetPreferredLangIndexes();
auto const priorityList = PrioritizedLanguages(preferredLangs, StrUtf8::kUnsupportedLanguageCode);
return GetBestName(src, priorityList, out); return GetBestName(src, priorityList, out);
} }
vector<int8_t> GetDescriptionLangPriority(RegionData const & regionData, int8_t const deviceLang) vector<int8_t> GetDescriptionLangPriority(RegionData const & regionData)
{ {
bool const preferDefault = IsNativeLang(regionData, deviceLang); auto const preferredLangs = languages::GetPreferredLangIndexes();
return MakeLanguagesPriorityList(deviceLang, preferDefault); return PrioritizedLanguages(preferredLangs, DefaultLanguage(regionData, preferredLangs));
} }
vector<string> GetCuisines(TypesHolder const & types) vector<string> GetCuisines(TypesHolder const & types)

View File

@@ -136,12 +136,13 @@ namespace feature
/// Returns priority list of language codes for feature description, /// Returns priority list of language codes for feature description,
/// the priority is the following: /// the priority is the following:
/// - device language code; /// - device language codes in order of preference;
/// - default language code if MWM contains user's language (or similar to device languages if provided); /// - including default language code, if MWM contains the language (or similar to device language if provided);
/// - languages that we know are similar to device language; /// - including languages that we know are similar to device language;
/// - international language code; /// - international language code;
/// - english language code; /// - english language code;
std::vector<int8_t> GetDescriptionLangPriority(RegionData const & regionData, int8_t const deviceLang); /// - default language code;
std::vector<int8_t> GetDescriptionLangPriority(RegionData const & regionData);
// Returns vector of cuisines readable names from classificator. // Returns vector of cuisines readable names from classificator.
std::vector<std::string> GetCuisines(TypesHolder const & types); std::vector<std::string> GetCuisines(TypesHolder const & types);

View File

@@ -3339,8 +3339,7 @@ void Framework::FillDescription(FeatureType & ft, place_page::Info & info) const
if (!ft.GetID().m_mwmId.IsAlive()) if (!ft.GetID().m_mwmId.IsAlive())
return; return;
auto const & regionData = ft.GetID().m_mwmId.GetInfo()->GetRegionData(); auto const & regionData = ft.GetID().m_mwmId.GetInfo()->GetRegionData();
auto const deviceLang = StringUtf8Multilang::GetLangIndex(languages::GetCurrentMapLanguage()); auto const langPriority = feature::GetDescriptionLangPriority(regionData);
auto const langPriority = feature::GetDescriptionLangPriority(regionData, deviceLang);
std::string wikiDescription = m_descriptionsLoader->GetWikiDescription(ft.GetID(), langPriority); std::string wikiDescription = m_descriptionsLoader->GetWikiDescription(ft.GetID(), langPriority);
if (!wikiDescription.empty()) if (!wikiDescription.empty())

View File

@@ -191,6 +191,11 @@ std::string GetCurrentMapLanguage()
std::vector<int8_t> GetPreferredLangIndexes() std::vector<int8_t> GetPreferredLangIndexes()
{ {
std::vector<int8_t> langs = {}; std::vector<int8_t> langs = {};
auto const mapLang = StringUtf8Multilang::GetLangIndex(languages::GetCurrentMapLanguage());
if (mapLang != StringUtf8Multilang::kUnsupportedLanguageCode)
langs.push_back(mapLang);
for (auto const & systemLanguage : GetSystemPreferred()) for (auto const & systemLanguage : GetSystemPreferred())
{ {
auto normalizedLang = Normalize(systemLanguage); auto normalizedLang = Normalize(systemLanguage);