discourse/lib/topic_list_responder.rb
Natalie Tay a44f0d15a2
FIX: Use resolved locale for localizations, instead of param+site default fallback (#39395)
Our `TopicListResponder` determined content locale using only the `?tl=`
URL param, falling back to `SiteSetting.default_locale`. This ignored
the locale cookie set by the language switcher or persisted from a
previous `?tl=` visit. The fix is to simply use `I18n.locale` which is
already resolved from all sources (?tl= param, cookie, Accept-Language
header, site default).

This only affects initial page loads and browser refreshes. Ember
internal navigation (format.json) is unaffected since it doesn't call
`localize_topic_list_content`.

Scenarios affected and fixed (per related urls):
1. Anonymous user switches to German via language switcher →
German-origin topics show English titles on page load
2. User arrives via ?tl=fi from Google → first page works, but
subsequent navigations (without ?tl= in URL) fall back to English
despite the locale cookie being set


Related issue: 
-
https://meta.discourse.org/t/localisation-bug-for-anonymous-users-with-tl-lang/400090
-
https://meta.discourse.org/t/topic-list-is-shown-in-a-language-de-but-has-one-de-topic-not-translated/400209
2026-04-21 15:52:35 +08:00

34 lines
917 B
Ruby

# frozen_string_literal: true
module TopicListResponder
def respond_with_list(list)
discourse_expires_in 1.minute
respond_to do |format|
format.html do
@list = list
localize_topic_list_content(list)
store_preloaded(
list.preload_key,
MultiJson.dump(TopicListSerializer.new(list, scope: guardian)),
)
render "list/list"
end
format.json { render_serialized(list, TopicListSerializer) }
end
end
private
def localize_topic_list_content(list)
return if list.topics.blank? || !SiteSetting.content_localization_enabled
return if cookies.key?(ContentLocalization::SHOW_ORIGINAL_COOKIE)
return if current_user&.user_option&.show_original_content
crawl_locale = I18n.locale
list.topics.each do |topic|
LocalizationAttributesReplacer.replace_topic_attributes(topic, crawl_locale)
end
end
end