diff --git a/components/brave_news/browser/publishers_controller.cc b/components/brave_news/browser/publishers_controller.cc index 6ab682e83c17..632cd69e73f9 100644 --- a/components/brave_news/browser/publishers_controller.cc +++ b/components/brave_news/browser/publishers_controller.cc @@ -262,6 +262,33 @@ void PublishersController::EnsurePublishersIsUpdating( .timeout = GetDefaultRequestTimeout()}); } +std::string PublishersController::GetFallbackLocale(const std::string& requested_locale, const base::flat_set& available_locales) { + + // Exact match + if (available_locales.find(requested_locale) != available_locales.end()) { + return requested_locale; + } + + // Regional fallback + std::string country_code = requested_locale.substr(requested_locale.find('_') + 1); + for (const auto& locale : available_locales) { + if (locale.substr(locale.find('_') + 1) == country_code) { + return locale; + } + } + + // Language fallback + std::string language_code = requested_locale.substr(0, requested_locale.find('_')); + for (const auto& locale : available_locales) { + if (locale.substr(0, locale.find('_')) == language_code) { + return locale; + } + } + + // Default fallback + return "en_US"; +} + void PublishersController::UpdateDefaultLocale() { auto available_locales = GetPublisherLocales(publishers_); @@ -271,11 +298,7 @@ void PublishersController::UpdateDefaultLocale() { base::StrCat({brave_l10n::GetDefaultISOLanguageCodeString(), "_", brave_l10n::GetDefaultISOCountryCodeString()}); - // Fallback to en_US, if we can't match anything else. - // TODO(fallaciousreasoning): Implement more complicated fallback - default_locale_ = base::Contains(available_locales, brave_news_locale) - ? brave_news_locale - : "en_US"; + default_locale_ = GetFallbackLocale(brave_news_locale, available_locales); } void PublishersController::ClearCache() { diff --git a/components/brave_news/browser/publishers_controller.h b/components/brave_news/browser/publishers_controller.h index 455cbc06696f..af6312bb0319 100644 --- a/components/brave_news/browser/publishers_controller.h +++ b/components/brave_news/browser/publishers_controller.h @@ -49,6 +49,8 @@ class PublishersController { void GetOrFetchPublishers(const BraveNewsSubscriptions& subscriptions, base::OnceClosure callback, bool wait_for_current_update); + + std::string GetFallbackLocale(const std::string& requested_locale, const base::flat_set& available_locales); void UpdateDefaultLocale(); raw_ptr api_request_helper_;