A platform for community discussion. Free, open, simple.
DEV: Add value helpers to `ContentLocalization`
Régis Hanol067a071526b2
diff --git a/app/serializers/basic_post_serializer.rb b/app/serializers/basic_post_serializer.rbindex 93cd17efc39..b0a7e4e2a6f 100644--- a/app/serializers/basic_post_serializer.rb+++ b/app/serializers/basic_post_serializer.rb@@ -34,12 +34,8 @@ class BasicPostSerializer < ApplicationSerializerI18n.t("flagging.user_must_edit")endelse- cooked = object.filter_quotes(@parent_post)-- translated_cooked =- object.get_localization&.cooked if ContentLocalization.show_translated_post?(object, scope)-- translated_cooked || cooked+ ContentLocalization.translated_post_cooked(object, scope) ||+ object.filter_quotes(@parent_post)endenddiff --git a/app/serializers/concerns/localized_fancy_topic_title_mixin.rb b/app/serializers/concerns/localized_fancy_topic_title_mixin.rbindex 7254203e104..16d0b38dfea 100644--- a/app/serializers/concerns/localized_fancy_topic_title_mixin.rb+++ b/app/serializers/concerns/localized_fancy_topic_title_mixin.rb@@ -45,7 +45,6 @@ module LocalizedFancyTopicTitleMixinenddef translated_title- ContentLocalization.show_translated_topic?(_topic, scope) &&- _topic.get_localization&.fancy_title.presence+ ContentLocalization.translated_topic_fancy_title(_topic, scope)endenddiff --git a/lib/content_localization.rb b/lib/content_localization.rbindex 83ea383c9fe..57c3971a2b4 100644--- a/lib/content_localization.rb+++ b/lib/content_localization.rb@@ -11,10 +11,6 @@ class ContentLocalizationautomatically_translate_anonymously?(scope&.request&.cookies)end- def self.show_original?(scope)- !automatically_translate?(scope)- end-# @param cookies [ActionDispatch::Cookies::CookieJar, Hash]# @return [Boolean]def self.automatically_translate_anonymously?(cookies)@@ -39,19 +35,37 @@ class ContentLocalization# @param post [Post] The post object# @return [Boolean]def self.show_translated_post?(post, scope)- SiteSetting.content_localization_enabled && post.raw.present? && post.locale.present? &&+ SiteSetting.content_localization_enabled && post&.raw.present? && post.locale.present? &&!post.in_user_locale? && automatically_translate?(scope) && !understands?(post.locale, scope)end+ def self.translated_post_cooked(post, scope)+ return if !show_translated_post?(post, scope)++ post.get_localization&.cooked.presence+ end+# This method returns true when we should try to show the translated topic.# @param scope [Object] The serializer scope from which the method is called# @param topic [Topic] The topic record# @return [Boolean]def self.show_translated_topic?(topic, scope)- SiteSetting.content_localization_enabled && topic.locale.present? && !topic.in_user_locale? &&+ SiteSetting.content_localization_enabled && topic&.locale.present? && !topic.in_user_locale? &&automatically_translate?(scope) && !understands?(topic.locale, scope)end+ def self.translated_topic_title(topic, scope)+ return if !show_translated_topic?(topic, scope)++ topic.get_localization&.title.presence+ end++ def self.translated_topic_fancy_title(topic, scope)+ return if !show_translated_topic?(topic, scope)++ topic.get_localization&.fancy_title.presence+ end+# This method returns true when we should try to show the translated category.# @param category [Category] The category record# @param scope [Object] The serializer scope from which the method is calleddiff --git a/spec/lib/content_localization_spec.rb b/spec/lib/content_localization_spec.rbindex 3e1c860558b..92bcc59c2e2 100644--- a/spec/lib/content_localization_spec.rb+++ b/spec/lib/content_localization_spec.rb@@ -170,6 +170,42 @@ describe ContentLocalization doendend+ describe "translated value helpers" do+ fab!(:post)++ before do+ SiteSetting.content_localization_enabled = true+ post.topic.update!(locale: "ja")+ post.update!(locale: "ja")+ I18n.locale = "de"+ end++ it "returns the localization, or nil when there is none or no record" do+ scope = create_scope++ expect(ContentLocalization.translated_post_cooked(post, scope)).to be_nil+ expect(ContentLocalization.translated_topic_title(post.topic, scope)).to be_nil+ expect(ContentLocalization.translated_post_cooked(nil, scope)).to be_nil+ expect(ContentLocalization.translated_topic_title(nil, scope)).to be_nil++ Fabricate(:post_localization, post: post, locale: "de", cooked: "<p>uebersetzt</p>")+ Fabricate(+ :topic_localization,+ topic: post.topic,+ locale: "de",+ title: "Titel",+ fancy_title: "Fancy Titel",+ )+ post.reload.topic.reload++ expect(ContentLocalization.translated_post_cooked(post, scope)).to eq("<p>uebersetzt</p>")+ expect(ContentLocalization.translated_topic_title(post.topic, scope)).to eq("Titel")+ expect(ContentLocalization.translated_topic_fancy_title(post.topic, scope)).to eq(+ "Fancy Titel",+ )+ end+ end+describe ".show_translated_category?" dofab!(:category)