A platform for community discussion. Free, open, simple.
FIX: Topic has localization's inconsistent locale matching (#38365)
Natalie Tay523b64b7228c
diff --git a/app/models/concerns/localizable.rb b/app/models/concerns/localizable.rbindex 06039e0216d..23cb65f34fa 100644--- a/app/models/concerns/localizable.rb+++ b/app/models/concerns/localizable.rb@@ -31,6 +31,10 @@ module Localizableendend+ def has_localization?(locale = I18n.locale)+ get_localization(locale).present?+ end+def in_user_locale?LocaleNormalizer.is_same?(locale, I18n.locale)enddiff --git a/app/models/post.rb b/app/models/post.rbindex e64e3a8c5a5..39f6161cd0e 100644--- a/app/models/post.rb+++ b/app/models/post.rb@@ -1187,10 +1187,6 @@ class Post < ActiveRecord::BasePrettyText.extract_mentions(Nokogiri::HTML5.fragment(cooked))end- def has_localization?(locale = I18n.locale)- get_localization(locale).present?- end-privatedef access_control_post_id_for_uploaddiff --git a/app/models/topic.rb b/app/models/topic.rbindex 6243377822b..8b1b8f19703 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -2188,10 +2188,6 @@ class Topic < ActiveRecord::Basefieldsend- def has_localization?(locale = I18n.locale)- localizations.exists?(locale: locale.to_s.sub("-", "_"))- end-privatedef invite_to_private_message(invited_by, target_user, guardian)diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rbindex 0e2b62c9e3f..cdd74d5d045 100644--- a/spec/models/topic_spec.rb+++ b/spec/models/topic_spec.rb@@ -3816,6 +3816,20 @@ RSpec.describe Topic doexpect(topic.has_localization?("z")).to eq(false)end++ it "returns true for a regional match (ja matches ja_JP)" do+ topic = Fabricate(:topic)+ Fabricate(:topic_localization, topic: topic, locale: "ja_JP")++ expect(topic.has_localization?("ja")).to eq(true)+ end++ it "returns true for a base locale match (pt_BR matches pt)" do+ topic = Fabricate(:topic)+ Fabricate(:topic_localization, topic: topic, locale: "pt")++ expect(topic.has_localization?("pt_BR")).to eq(true)+ endenddescribe "#get_localization" do