A platform for community discussion. Free, open, simple.
FIX: Make twitter card meta data translatable (#39150)
Régis HANOL54fc63b4ad68
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rbindex e90e0d2eff1..5d4f3ee5579 100644--- a/app/helpers/application_helper.rb+++ b/app/helpers/application_helper.rb@@ -369,9 +369,17 @@ module ApplicationHelperif opts[:read_time] && opts[:read_time] > 0 && opts[:like_count] && opts[:like_count] > 0result << tag(:meta, name: "twitter:label1", value: I18n.t("reading_time"))- result << tag(:meta, name: "twitter:data1", value: "#{opts[:read_time]} mins 🕑")+ result << tag(+ :meta,+ name: "twitter:data1",+ value: I18n.t("reading_time_minutes", count: opts[:read_time]),+ )result << tag(:meta, name: "twitter:label2", value: I18n.t("likes"))- result << tag(:meta, name: "twitter:data2", value: "#{opts[:like_count]} ❤")+ result << tag(+ :meta,+ name: "twitter:data2",+ value: I18n.t("likes_count", count: opts[:like_count]),+ )endif opts[:published_time]diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 59b0679b603..46ee7432cb4 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -481,7 +481,13 @@ en:delete_topic_failed: "There was an error deleting that topic. Please contact the site's administrator."reading_time: "Reading time"+ reading_time_minutes:+ one: "%{count} min 🕑"+ other: "%{count} mins 🕑"likes: "Likes"+ likes_count:+ one: "%{count} ❤"+ other: "%{count} ❤"action_already_performed: "Oops! You already performed this action. Can you try refreshing the page?"diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rbindex 1188bc3ce9a..9bac7413aca 100644--- a/spec/helpers/application_helper_spec.rb+++ b/spec/helpers/application_helper_spec.rb@@ -780,6 +780,38 @@ RSpec.describe ApplicationHelper do)endend++ context "with reading time and likes" do+ it "uses translated strings for reading time and likes" do+ result = helper.crawlable_meta_data(read_time: 5, like_count: 10)++ expect(result).to include(+ "<meta name=\"twitter:label1\" value=\"#{I18n.t("reading_time")}\" />",+ )+ expect(result).to include(+ "<meta name=\"twitter:data1\" value=\"#{I18n.t("reading_time_minutes", count: 5)}\" />",+ )+ expect(result).to include("<meta name=\"twitter:label2\" value=\"#{I18n.t("likes")}\" />")+ expect(result).to include(+ "<meta name=\"twitter:data2\" value=\"#{I18n.t("likes_count", count: 10)}\" />",+ )+ end++ it "handles singular reading time correctly" do+ result = helper.crawlable_meta_data(read_time: 1, like_count: 1)++ expect(result).to include("1 min 🕑")+ expect(result).not_to include("1 mins")+ end++ it "does not include twitter card labels when read_time or like_count is missing" do+ result = helper.crawlable_meta_data(read_time: 5)+ expect(result).not_to include("twitter:label1")++ result = helper.crawlable_meta_data(like_count: 10)+ expect(result).not_to include("twitter:label1")+ end+ endenddescribe "#title_content" do