Skip to content

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

DEV: Add new key for anon cache for localization (#32640)

Natalie Tay413fea80510a
diff --git a/lib/content_localization.rb b/lib/content_localization.rb
new file mode 100644
index 00000000000..0eb173e07c3
--- /dev/null
+++ b/lib/content_localization.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+class ContentLocalization
+ SHOW_ORIGINAL_COOKIE = "content-localization-show-original"
+
+ # @param scope [Object] The serializer scope from which the method is called
+ # @return [Boolean] if the cookie is set, false otherwise
+ def self.show_original?(scope)
+ scope&.request&.cookies&.key?(SHOW_ORIGINAL_COOKIE)
+ end
+
+ # This method returns true when we should try to show the translated post.
+ # @param scope [Object] The serializer scope from which the method is called
+ # @param post [Post] The post object
+ # @return [Boolean]
+ def self.show_translated_post?(post, scope)
+ SiteSetting.experimental_content_localization && post.locale.present? &&
+ !post.in_user_locale? && !show_original?(scope)
+ end
+end
diff --git a/lib/middleware/anonymous_cache.rb b/lib/middleware/anonymous_cache.rb
index ea4868ba191..e4fb6473cf2 100644
--- a/lib/middleware/anonymous_cache.rb
+++ b/lib/middleware/anonymous_cache.rb
@@ -18,6 +18,7 @@ module Middleware
t: "key_cache_theme_ids",
ca: "key_compress_anon",
l: "key_locale",
+ lso: "key_show_original_content",
cm: "key_forced_color_mode",
}
end
@@ -186,6 +187,10 @@ module Middleware
GlobalSetting.compress_anon_cache
end
+ def key_show_original_content
+ @request.cookies.key?(ContentLocalization::SHOW_ORIGINAL_COOKIE)
+ end
+
def theme_ids
ids, _ = @request.cookies["theme_ids"]&.split("|")
id = ids&.split(",")&.map(&:to_i)&.first
diff --git a/spec/lib/content_localization_spec.rb b/spec/lib/content_localization_spec.rb
new file mode 100644
index 00000000000..7b1503e010a
--- /dev/null
+++ b/spec/lib/content_localization_spec.rb
@@ -0,0 +1,70 @@
+# frozen_string_literal: true
+
+describe ContentLocalization do
+ def create_scope(cookie: nil)
+ env = create_request_env.merge("HTTP_COOKIE" => cookie)
+ mock().tap { |m| m.stubs(:request).returns(ActionDispatch::Request.new(env)) }
+ end
+
+ describe ".show_original" do
+ it "returns true when cookie is present" do
+ scope = create_scope(cookie: ContentLocalization::SHOW_ORIGINAL_COOKIE)
+
+ expect(ContentLocalization.show_original?(scope)).to be true
+ end
+
+ it "returns false when cookie is absent" do
+ scope = create_scope
+
+ expect(ContentLocalization.show_original?(scope)).to be false
+ end
+ end
+
+ describe ".show_translated_post?" do
+ fab!(:post)
+
+ it "returns true when criteria met" do
+ SiteSetting.experimental_content_localization = true
+ post.update!(locale: "ja")
+ I18n.locale = "de"
+ scope = create_scope
+
+ expect(ContentLocalization.show_translated_post?(post, scope)).to be true
+ end
+
+ context "when criteria not met" do
+ before do
+ SiteSetting.experimental_content_localization = true
+ post.update!(locale: "ja")
+ I18n.locale = "de"
+ end
+
+ it "returns false when experimental_content_localization is false" do
+ SiteSetting.experimental_content_localization = false
+ scope = create_scope
+
+ expect(ContentLocalization.show_translated_post?(post, scope)).to be false
+ end
+
+ it "returns false when post locale is nil" do
+ post.update!(locale: nil)
+ scope = create_scope
+
+ expect(ContentLocalization.show_translated_post?(post, scope)).to be false
+ end
+
+ it "returns false when post is in user locale" do
+ post.update!(locale: I18n.locale)
+ scope = create_scope
+
+ expect(ContentLocalization.show_translated_post?(post, scope)).to be false
+ end
+
+ it "returns false when show_original? is true" do
+ scope = create_scope(cookie: ContentLocalization::SHOW_ORIGINAL_COOKIE)
+
+ expect(ContentLocalization.show_translated_post?(post, scope)).to be false
+ end
+ end
+ end
+end
diff --git a/spec/lib/middleware/anonymous_cache_spec.rb b/spec/lib/middleware/anonymous_cache_spec.rb
index be8522065c4..3758b976522 100644
--- a/spec/lib/middleware/anonymous_cache_spec.rb
+++ b/spec/lib/middleware/anonymous_cache_spec.rb
@@ -124,6 +124,14 @@ RSpec.describe Middleware::AnonymousCache do
}.not_to raise_error
end
+ it "handles showing original content" do
+ show_orig_key =
+ new_helper("HTTP_COOKIE" => ContentLocalization::SHOW_ORIGINAL_COOKIE).cache_key
+ regular_key = new_helper.cache_key
+
+ expect(show_orig_key).not_to eq(regular_key)
+ end
+
context "when cached" do
let!(:helper) { new_helper("ANON_CACHE_DURATION" => 10) }