Skip to content

A platform for community discussion. Free, open, simple.

FIX: Show localization for regionless locale if they exist (#33702)

Natalie Tay83670fb01e8b
diff --git a/app/models/post.rb b/app/models/post.rb
index 56af7a437d8..cacc94c5011 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -1330,12 +1330,18 @@ class Post < ActiveRecord::Base
end
def in_user_locale?
- locale == I18n.locale.to_s
+ LocaleNormalizer.is_same?(locale, I18n.locale)
end
def get_localization(locale = I18n.locale)
- locale_string = locale.to_s.sub("-", "_")
- post_localizations.find { |tl| tl.locale == locale_string }
+ locale_str = locale.to_s.sub("-", "_")
+
+ # prioritise exact match
+ if match = post_localizations.find { |l| l.locale == locale_str }
+ return match
+ end
+
+ post_localizations.find { |l| LocaleNormalizer.is_same?(l.locale, locale_str) }
end
private
diff --git a/app/models/topic.rb b/app/models/topic.rb
index 59a1e2c334a..348f136bd6b 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -2143,8 +2143,14 @@ class Topic < ActiveRecord::Base
end
def get_localization(locale = I18n.locale)
- locale_string = locale.to_s.sub("-", "_")
- topic_localizations.find { |tl| tl.locale == locale_string }
+ locale_str = locale.to_s.sub("-", "_")
+
+ # prioritise exact match
+ if match = topic_localizations.find { |l| l.locale == locale_str }
+ return match
+ end
+
+ topic_localizations.find { |l| LocaleNormalizer.is_same?(l.locale, locale_str) }
end
private
diff --git a/app/services/locale_normalizer.rb b/app/services/locale_normalizer.rb
new file mode 100644
index 00000000000..737b7b5c412
--- /dev/null
+++ b/app/services/locale_normalizer.rb
@@ -0,0 +1,53 @@
+# frozen_string_literal: true
+
+class LocaleNormalizer
+ # Normalizes locale string, matching the list of I18n.locales where possible
+ # @param locale [String,Symbol] the locale to normalize
+ # @return [String] the normalized locale
+ def self.normalize_to_i18n(locale)
+ return nil if locale.blank?
+ locale = locale.to_s.gsub("-", "_")
+
+ i18n_pairs.each { |downcased, value| return value if locale.downcase == downcased }
+
+ locale
+ end
+
+ # Checks if two locales are the same based on exact match and normalized match
+ # - is_same?("a_b", "a-b") == true
+ # - is_same?("a_b", "a") == true
+ # @param locale1 [String,Symbol] the first locale to compare
+ # @param locale2 [String,Symbol] the second locale to compare
+ def self.is_same?(locale1, locale2)
+ locale1 = locale1.to_s
+ locale2 = locale2.to_s
+ return true if locale1 == locale2
+ locale1 = locale1.gsub("-", "_").downcase
+ locale2 = locale2.gsub("-", "_").downcase
+ locale1.split("_").first == locale2.split("_").first
+ end
+
+ private
+
+ def self.i18n_pairs
+ # they should look like this for the input to match against:
+ # {
+ # "lowercased" => "actual",
+ # "en" => "en",
+ # "zh_cn" => "zh_CN",
+ # "zh" => "zh_CN",
+ # }
+ @locale_map ||=
+ I18n
+ .available_locales
+ .reduce({}) do |output, sym|
+ locale = sym.to_s
+ output[locale.downcase] = locale
+ if locale.include?("_")
+ short = locale.split("_").first
+ output[short] = locale if output[short].blank?
+ end
+ output
+ end
+ end
+end
diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
index 8ebc5fe415b..31c3383d552 100644
--- a/spec/models/post_spec.rb
+++ b/spec/models/post_spec.rb
@@ -2389,6 +2389,7 @@ RSpec.describe Post do
I18n.locale = "ja"
post = Fabricate(:post)
zh_localization = Fabricate(:post_localization, post: post, locale: "zh_CN")
+ Fabricate(:post_localization, post: post, locale: "ja_JP")
ja_localization = Fabricate(:post_localization, post: post, locale: "ja")
expect(post.get_localization(:zh_CN)).to eq(zh_localization)
@@ -2396,6 +2397,22 @@ RSpec.describe Post do
expect(post.get_localization("xx")).to eq(nil)
expect(post.get_localization).to eq(ja_localization)
end
+
+ it "returns a regional localization (ja_JP) when the user's locale (ja) is not available" do
+ I18n.locale = "ja"
+ post = Fabricate(:post)
+ ja_jp_localization = Fabricate(:post_localization, post: post, locale: "ja_JP")
+
+ expect(post.get_localization).to eq(ja_jp_localization)
+ end
+
+ it "returns a normalized localization (pt) if the user's locale (pt_BR) is not available" do
+ I18n.locale = "pt_BR"
+ post = Fabricate(:post)
+ pt_localization = Fabricate(:post_localization, post: post, locale: "pt")
+
+ expect(post.get_localization).to eq(pt_localization)
+ end
end
describe "#in_user_locale?" do
@@ -2403,6 +2420,8 @@ RSpec.describe Post do
I18n.locale = "ja"
post = Fabricate(:post, locale: "ja")
+ expect(post.in_user_locale?).to eq(true)
+ post.update!(locale: "ja_JP")
expect(post.in_user_locale?).to eq(true)
post.update!(locale: "es")
diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb
index fa20009110d..49ea9e3479f 100644
--- a/spec/models/topic_spec.rb
+++ b/spec/models/topic_spec.rb
@@ -3640,14 +3640,30 @@ describe Topic do
it "returns the localization with the specified locale" do
I18n.locale = "ja"
topic = Fabricate(:topic)
- zh_localization = Fabricate(:topic_localization, topic: topic, locale: "zh_CN")
- ja_localization = Fabricate(:topic_localization, topic: topic, locale: "ja")
+ zh_localization = Fabricate(:topic_localization, topic:, locale: "zh_CN")
+ ja_localization = Fabricate(:topic_localization, topic:, locale: "ja")
expect(topic.get_localization(:zh_CN)).to eq(zh_localization)
expect(topic.get_localization("zh-CN")).to eq(zh_localization)
expect(topic.get_localization("xx")).to eq(nil)
expect(topic.get_localization).to eq(ja_localization)
end
+
+ it "returns a regional localization (ja_JP) when the user's locale (ja) is not available" do
+ I18n.locale = "ja"
+ topic = Fabricate(:topic)
+ ja_jp_localization = Fabricate(:topic_localization, topic:, locale: "ja_JP")
+
+ expect(topic.get_localization).to eq(ja_jp_localization)
+ end
+
+ it "returns a normalized localization (pt) if the user's locale (pt_BR) is not available" do
+ I18n.locale = "pt_BR"
+ topic = Fabricate(:topic)
+ pt_localization = Fabricate(:topic_localization, topic:, locale: "pt")
+
+ expect(topic.get_localization).to eq(pt_localization)
+ end
end
describe "#in_user_locale?" do
diff --git a/spec/services/locale_normalizer_spec.rb b/spec/services/locale_normalizer_spec.rb
new file mode 100644
index 00000000000..d1f87fd29fe
--- /dev/null
+++ b/spec/services/locale_normalizer_spec.rb
@@ -0,0 +1,42 @@
+# frozen_string_literal: true
+
+describe LocaleNormalizer do
+ describe ".normalize_to_i18n" do
+ it "matches input locales to i18n locales" do
+ expect(described_class.normalize_to_i18n("en-GB")).to eq("en_GB")
+ expect(described_class.normalize_to_i18n("en")).to eq("en")
+ expect(described_class.normalize_to_i18n("zh")).to eq("zh_CN")
+ expect(described_class.normalize_to_i18n("tr")).to eq("tr_TR")
+ end
+
+ it "converts dashes to underscores" do
+ expect(described_class.normalize_to_i18n("a-b")).to eq("a_b")
+ end
+ end
+
+ describe "#is_same?" do
+ it "returns true for the same locale" do
+ expect(described_class.is_same?("en", :en)).to be true
+ end
+
+ it "returns true for locales with different cases" do
+ expect(described_class.is_same?("en", "EN")).to be true
+ end
+
+ it "returns true for locales with different separators" do
+ expect(described_class.is_same?("en-US", "en_US")).to be true
+ end
+
+ it "returns false for different locales" do
+ expect(described_class.is_same?("en", "ja")).to be false
+ end
+
+ it "returns true for locales with the same base language" do
+ expect(described_class.is_same?("zh-CN", "zh_TW")).to be true
+ end
+
+ it "returns false for completely different locales" do
+ expect(described_class.is_same?("en", "ja")).to be false
+ end
+ end
+end
diff --git a/spec/system/content_localization_spec.rb b/spec/system/content_localization_spec.rb
index 949769c6175..456ed0f4426 100644
--- a/spec/system/content_localization_spec.rb
+++ b/spec/system/content_localization_spec.rb
@@ -32,10 +32,8 @@ describe "Content Localization" do
let(:topic_list) { PageObjects::Components::TopicList.new }
let(:composer) { PageObjects::Components::Composer.new }
let(:post_1_obj) { PageObjects::Components::Post.new(1) }
+ let(:post_3_obj) { PageObjects::Components::Post.new(3) }
let(:post_4_obj) { PageObjects::Components::Post.new(4) }
- let(:post_history_modal) { PageObjects::Modals::PostHistory.new }
- let(:edit_localized_post_dialog) { PageObjects::Components::Dialog.new }
- let(:fast_editor) { PageObjects::Components::FastEditor.new }
before do
Fabricate(:topic_localization, topic:, locale: "ja", fancy_title: "孫子兵法からの人生戦略")
@@ -56,9 +54,9 @@ describe "Content Localization" do
SiteSetting.content_localization_supported_locales = "en|ja"
end
- it "shows the correct language based on the selected language and login status" do
+ it "shows the user's language based on their user locale" do
sign_in(japanese_user)
- visit("/")
+
visit("/t/#{topic.id}")
expect(topic_page.has_topic_title?("孫子兵法からの人生戦略")).to eq(true)
end
@@ -110,40 +108,57 @@ describe "Content Localization" do
expect(post_4_obj.post_language).to have_content("日本語")
end
- it "allows editing original content when post is localized" do
- sign_in(admin)
+ it "shows 'en' posts for 'en_GB' users" do
+ brit_user = Fabricate(:user, locale: "en_GB")
- topic_page.visit_topic(topic)
- topic_page.expand_post_actions(post_3)
- topic_page.click_post_action_button(post_3, :edit)
- expect(edit_localized_post_dialog).to be_open
- edit_localized_post_dialog.click_yes
- expect(composer).to have_content(post_3.raw)
+ sign_in(brit_user)
+ visit("/")
+
+ topic_list.visit_topic_with_title("Life strategies from The Art of War")
+ expect(post_3_obj.post).to have_content("A general is one who ..")
end
- it "allows editing translated content when post is localized" do
- sign_in(admin)
+ context "when editing" do
+ let(:edit_localized_post_dialog) { PageObjects::Components::Dialog.new }
+ let(:fast_editor) { PageObjects::Components::FastEditor.new }
- topic_page.visit_topic(topic)
- topic_page.expand_post_actions(post_3)
- topic_page.click_post_action_button(post_3, :edit)
- expect(edit_localized_post_dialog).to be_open
- edit_localized_post_dialog.click_no
- expect(page).to have_css(".action-title", text: I18n.t("js.composer.translations.title"))
- end
+ it "allows editing original content when post is localized" do
+ sign_in(admin)
- it "does not open the fast editor for localized posts" do
- sign_in(admin)
+ topic_page.visit_topic(topic)
+ topic_page.expand_post_actions(post_3)
+ topic_page.click_post_action_button(post_3, :edit)
+ expect(edit_localized_post_dialog).to be_open
+ edit_localized_post_dialog.click_yes
+ expect(composer).to have_content(post_3.raw)
+ end
- topic_page.visit_topic(topic)
- select_text_range("#{topic_page.post_by_number_selector(post_3.post_number)} .cooked", 0, 5)
- expect(topic_page.fast_edit_button).to be_visible
- topic_page.click_fast_edit_button
- expect(page).to have_no_css("#fast-edit-input")
- expect(edit_localized_post_dialog).to be_open
+ it "allows editing translated content when post is localized" do
+ sign_in(admin)
+
+ topic_page.visit_topic(topic)
+ topic_page.expand_post_actions(post_3)
+ topic_page.click_post_action_button(post_3, :edit)
+ expect(edit_localized_post_dialog).to be_open
+ edit_localized_post_dialog.click_no
+ expect(page).to have_css(".action-title", text: I18n.t("js.composer.translations.title"))
+ end
+
+ it "does not open the fast editor for localized posts" do
+ sign_in(admin)
+
+ topic_page.visit_topic(topic)
+ select_text_range("#{topic_page.post_by_number_selector(post_3.post_number)} .cooked", 0, 5)
+ expect(topic_page.fast_edit_button).to be_visible
+ topic_page.click_fast_edit_button
+ expect(page).to have_no_css("#fast-edit-input")
+ expect(edit_localized_post_dialog).to be_open
+ end
end
context "for post edit histories" do
+ let(:post_history_modal) { PageObjects::Modals::PostHistory.new }
+
before do
SiteSetting.editing_grace_period = 0
PostRevisor.new(post_1).revise!(Discourse.system_user, { raw: post_1.raw, locale: "" })