Skip to content

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

DEV: Move locale check to unified concern (#35207)

Natalie Tay273c21997ab3
diff --git a/app/models/concerns/localizable.rb b/app/models/concerns/localizable.rb
index dabcc13ca68..726392ba49a 100644
--- a/app/models/concerns/localizable.rb
+++ b/app/models/concerns/localizable.rb
@@ -19,4 +19,8 @@ module Localizable
localizations.find { |l| LocaleNormalizer.is_same?(l.locale, locale_str) }
end
+
+ def in_user_locale?
+ LocaleNormalizer.is_same?(locale, I18n.locale)
+ end
end
diff --git a/app/models/post.rb b/app/models/post.rb
index c911f841253..2c3148b7385 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -1338,10 +1338,6 @@ class Post < ActiveRecord::Base
get_localization(locale).present?
end
- def in_user_locale?
- LocaleNormalizer.is_same?(locale, I18n.locale)
- end
-
private
def parse_quote_into_arguments(quote)
diff --git a/app/models/topic.rb b/app/models/topic.rb
index 1e6297408c9..f0239cb295f 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -2182,10 +2182,6 @@ class Topic < ActiveRecord::Base
localizations.exists?(locale: locale.to_s.sub("-", "_"))
end
- def in_user_locale?
- LocaleNormalizer.is_same?(locale, I18n.locale)
- end
-
private
def invite_to_private_message(invited_by, target_user, guardian)
diff --git a/lib/content_localization.rb b/lib/content_localization.rb
index 22077a0bfe4..bfb1372ab46 100644
--- a/lib/content_localization.rb
+++ b/lib/content_localization.rb
@@ -26,4 +26,12 @@ class ContentLocalization
SiteSetting.content_localization_enabled && topic.locale.present? && !topic.in_user_locale? &&
!show_original?(scope)
end
+
+ # This method returns true when we should try to show the translated category.
+ # @param scope [Object] The serializer scope from which the method is called
+ # @param category [Category] The category record
+ # @return [Boolean]
+ def self.show_translated_category?(category, scope)
+ SiteSetting.content_localization_enabled && !category.in_user_locale? && !show_original?(scope)
+ end
end
diff --git a/spec/lib/content_localization_spec.rb b/spec/lib/content_localization_spec.rb
index fe12a0ae123..26a6582110e 100644
--- a/spec/lib/content_localization_spec.rb
+++ b/spec/lib/content_localization_spec.rb
@@ -122,4 +122,35 @@ describe ContentLocalization do
end
end
end
+
+ describe ".show_translated_category?" do
+ fab!(:category)
+
+ it "returns false when setting is disabled" do
+ SiteSetting.content_localization_enabled = false
+ category.update!(locale: "ja")
+ I18n.locale = "de"
+ scope = create_scope
+
+ expect(ContentLocalization.show_translated_category?(category, scope)).to be false
+ end
+
+ it "returns true when category locale does not match user locale" do
+ SiteSetting.content_localization_enabled = true
+ category.update!(locale: "ja")
+ I18n.locale = "de"
+ scope = create_scope
+
+ expect(ContentLocalization.show_translated_category?(category, scope)).to be true
+ end
+
+ it "returns false when category locale does not match user locale but cookie set to show original" do
+ SiteSetting.content_localization_enabled = true
+ category.update!(locale: "ja")
+ I18n.locale = "de"
+ scope = create_scope(cookie: ContentLocalization::SHOW_ORIGINAL_COOKIE)
+
+ expect(ContentLocalization.show_translated_category?(category, scope)).to be false
+ end
+ end
end