A platform for community discussion. Free, open, simple.
FEATURE: Serve localized content in the site's default locale when user's language is unsupported (#36160)
Natalie Tay6cc063b84510
diff --git a/app/models/concerns/localizable.rb b/app/models/concerns/localizable.rbindex 726392ba49a..06039e0216d 100644--- a/app/models/concerns/localizable.rb+++ b/app/models/concerns/localizable.rb@@ -5,9 +5,13 @@ module Localizableincluded { has_many :localizations, class_name: "#{model_name}Localization", dependent: :destroy }- # Returns the localization for the given locale, or the best match if an exact match is not found.+ # Returns the localization for (in order of priority):+ # - the given locale,+ # - or the best match if an exact match is not found+ # - or the site default locale if `content_localization_use_default_locale_when_unsupported` enabled+ ## The query used to find the localization is optimized for performance, and assumes- # that localizations are indexed by locale, and have been preloaded where necessary.+ # that localizations are indexed by locale, and have been preloaded.# @return [Localization, nil] the localization object for the given locale, or nil if no match is found.def get_localization(locale = I18n.locale)locale_str = locale.to_s.sub("-", "_")@@ -17,7 +21,14 @@ module Localizablereturn matchend- localizations.find { |l| LocaleNormalizer.is_same?(l.locale, locale_str) }+ if match = localizations.find { |l| LocaleNormalizer.is_same?(l.locale, locale_str) }+ return match+ end++ if SiteSetting.content_localization_use_default_locale_when_unsupported+ default_locale = SiteSetting.default_locale.to_s.sub("-", "_")+ localizations.find { |l| LocaleNormalizer.is_same?(l.locale, default_locale) }+ endenddef in_user_locale?diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 97de5b8b683..ddf285b71e3 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -2773,6 +2773,7 @@ en:content_localization_allowed_groups: "Groups allowed to update localized content. Requires 'content localization enabled'."content_localization_language_switcher: "Show a language switcher in the header, allowing visitors to switch between translated versions of Discourse and user-contributed content. Uses languages defined in 'content localization supported locales'"content_localization_crawler_param: "Serve localized content to web crawlers when 'content localization enabled' and 'set locale from param' is enabled. The list of supported locales is defined in 'content localization supported locales'."+ content_localization_use_default_locale_when_unsupported: "Serve localized content in the site's default locale to users whose preferred language is not listed in 'content localization supported locales'."enable_upcoming_changes: "Enable upcoming changes"fake_upcoming_change: "This is a fake upcoming change for testing purposes. No need to translate this string."diff --git a/config/site_settings.yml b/config/site_settings.ymlindex 410cc683f77..022aee80155 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -1745,6 +1745,9 @@ content_localization:content_localization_crawler_param:default: falsearea: "localization"+ content_localization_use_default_locale_when_unsupported:+ default: true+ area: "localization"email:email_time_window_mins:diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rbindex 49b3afd0e51..4cfb4f88d64 100644--- a/spec/models/tag_spec.rb+++ b/spec/models/tag_spec.rb@@ -432,7 +432,7 @@ RSpec.describe Tag doendit "returns nil when no localization exists" do- Fabricate(:tag_localization, tag:, locale: "en")+ Fabricate(:tag_localization, tag:, locale: "el")expect(tag.get_localization("es")).to be_nilenddiff --git a/spec/serializers/basic_post_serializer_spec.rb b/spec/serializers/basic_post_serializer_spec.rbindex f958f97fb2b..79a50c24fb2 100644--- a/spec/serializers/basic_post_serializer_spec.rb+++ b/spec/serializers/basic_post_serializer_spec.rb@@ -22,13 +22,27 @@ RSpec.describe BasicPostSerializer doexpect(json[:cooked]).to eq(post.cooked)end- it "returns the localized cooked" do- SiteSetting.content_localization_enabled = true- Fabricate(:post_localization, post: post, cooked: "X", locale: "ja")- I18n.locale = "ja"- post.update!(locale: "en")+ describe "localizations" do+ it "returns the localized cooked" do+ SiteSetting.content_localization_enabled = true+ Fabricate(:post_localization, post: post, cooked: "X", locale: "ja")+ I18n.locale = "ja"+ post.update!(locale: "en")- expect(json[:cooked]).to eq("X")+ expect(json[:cooked]).to eq("X")+ end++ it "returns the site default locale cooked when no exact match found and `content_localization_use_default_locale_when_unsupported` is true" do+ SiteSetting.content_localization_enabled = true+ SiteSetting.content_localization_use_default_locale_when_unsupported = true+ SiteSetting.default_locale = "el"++ Fabricate(:post_localization, post:, cooked: "site default cooked", locale: "el")+ I18n.locale = "ja"+ post.update!(locale: "en")++ expect(json[:cooked]).to eq("site default cooked")+ endendendenddiff --git a/spec/serializers/basic_topic_serializer_spec.rb b/spec/serializers/basic_topic_serializer_spec.rbindex 33d40933655..9cfe4cf7d32 100644--- a/spec/serializers/basic_topic_serializer_spec.rb+++ b/spec/serializers/basic_topic_serializer_spec.rb@@ -10,15 +10,36 @@ describe BasicTopicSerializer doexpect(json[:basic_topic][:fancy_title]).to eq(topic.title)end- it "returns the fancy title with a modifier" do- SiteSetting.content_localization_enabled = true- Fabricate(:topic_localization, topic:, fancy_title: "X", locale: "ja")- I18n.locale = "ja"- topic.update!(locale: "en")+ describe "localizations" do+ it "returns the fancy title with a modifier" do+ SiteSetting.content_localization_enabled = true+ Fabricate(:topic_localization, topic:, fancy_title: "X", locale: "ja")+ I18n.locale = "ja"+ topic.update!(locale: "en")- json = BasicTopicSerializer.new(topic).as_json+ json = BasicTopicSerializer.new(topic).as_json++ expect(json[:basic_topic][:fancy_title]).to eq("X")+ end++ it "returns the site default locale fancy title when no exact match found and `content_localization_use_default_locale_when_unsupported` is true" do+ SiteSetting.content_localization_enabled = true+ SiteSetting.content_localization_use_default_locale_when_unsupported = true+ SiteSetting.default_locale = "el"++ Fabricate(+ :topic_localization,+ topic:,+ fancy_title: "site default fancy title",+ locale: "el",+ )+ I18n.locale = "ja"+ topic.update!(locale: "en")++ json = BasicTopicSerializer.new(topic).as_json- expect(json[:basic_topic][:fancy_title]).to eq("X")+ expect(json[:basic_topic][:fancy_title]).to eq("site default fancy title")+ endendendenddiff --git a/spec/system/content_localization_spec.rb b/spec/system/content_localization_spec.rbindex 3153080f61b..6231b98e728 100644--- a/spec/system/content_localization_spec.rb+++ b/spec/system/content_localization_spec.rb@@ -29,7 +29,11 @@ describe "Content Localization" do)endfab!(:post_3) { Fabricate(:post, topic:, locale: "ja", raw: "将とは、智・信・仁・勇・厳なり。") }+ fab!(:topic_ja_localization) do+ Fabricate(:topic_localization, topic:, locale: "ja", fancy_title: "孫子兵法からの人生戦略")+ end+ # page objectslet(:topic_page) { PageObjects::Pages::Topic.new }let(:topic_list) { PageObjects::Components::TopicList.new }let(:composer) { PageObjects::Components::Composer.new }@@ -38,10 +42,6 @@ describe "Content Localization" dolet(:post_3_obj) { PageObjects::Components::Post.new(3) }let(:post_4_obj) { PageObjects::Components::Post.new(4) }- fab!(:topic_ja_localization) do- Fabricate(:topic_localization, topic:, locale: "ja", fancy_title: "孫子兵法からの人生戦略")- end-def scroll_to_post(post_number)5.times dobreak if page.has_css?("#post_#{post_number} .cooked", visible: :all, wait: 0)@@ -371,6 +371,82 @@ describe "Content Localization" doexpect(page).to have_title(shady_topic.title)endend++ context "for a Greek user in an English forum with Japanese users" do+ fab!(:greek_user) { Fabricate(:user, locale: "el") }++ fab!(:jap_post) { Fabricate(:post, locale: "ja", cooked: "皆さんは「ジョジョの奇妙な冒険」をご存知ですか?") }+ fab!(:jap_topic) do+ jap_post.topic.tap { |t| t.update(locale: "ja", fancy_title: "ジョジョの奇妙な冒険") }+ end+ fab!(:en_loc_jap_post) do+ Fabricate(+ :post_localization,+ locale: "en",+ post: jap_post,+ cooked: "Do you know “JoJo’s Bizarre Adventure”?",+ )+ end+ fab!(:en_loc_jap_topic) do+ Fabricate(+ :topic_localization,+ locale: "en",+ topic: jap_topic,+ fancy_title: "JoJo's Bizarre Adventure",+ )+ end++ before do+ SiteSetting.default_locale = "en" # explicit+ SiteSetting.content_localization_use_default_locale_when_unsupported = true+ end++ context "for a topic / post with no locale" do+ it "shows content as-is" do+ jap_post.update(locale: nil)+ jap_topic.update(locale: nil)++ sign_in(greek_user)++ topic_page.visit_topic(jap_topic)+ expect(topic_page).to have_topic_title(jap_topic.fancy_title)+ expect(post_1_obj).to have_cooked_content(jap_post.cooked)++ SiteSetting.content_localization_enabled = false++ page.refresh+ expect(topic_page).to have_topic_title(jap_topic.fancy_title)+ expect(post_1_obj).to have_cooked_content(jap_post.cooked)+ end+ end++ context "for a topic / post written in Site default language (en)" do+ it "shows Site default language (en) translation to Greek user" do+ sign_in(greek_user)++ topic_page.visit_topic(jap_topic)+ expect(topic_page).to have_topic_title(en_loc_jap_topic.fancy_title)+ expect(post_1_obj).to have_cooked_content(en_loc_jap_post.cooked)++ SiteSetting.content_localization_use_default_locale_when_unsupported = false++ page.refresh+ expect(topic_page).to have_topic_title(jap_topic.fancy_title)+ expect(post_1_obj).to have_cooked_content(jap_post.cooked)+ end+ end++ it "shows content as-is when no localization exists" do+ en_loc_jap_topic.destroy+ en_loc_jap_post.destroy++ sign_in(greek_user)++ topic_page.visit_topic(jap_topic)+ expect(topic_page).to have_topic_title(jap_topic.fancy_title)+ expect(post_1_obj).to have_cooked_content(jap_post.cooked)+ end+ endendcontext "for site settings" dodiff --git a/spec/system/page_objects/components/post.rb b/spec/system/page_objects/components/post.rbindex d3e6b4c13de..3617841daa6 100644--- a/spec/system/page_objects/components/post.rb+++ b/spec/system/page_objects/components/post.rb@@ -28,8 +28,12 @@ module PageObjectsfind("#embedded-posts__bottom--#{@post_number} .load-more-replies").clickend+ def cooked_content+ post.find(".contents > .cooked")+ end+def has_cooked_content?(value)- post.find(".contents > .cooked").has_content?(value)+ cooked_content.has_content?(value)enddef has_replies?(count: nil)