discourse/lib/content_localization.rb
Natalie Tay 736f23173e
FIX: User options take precedence over show-original anon cookie (#39520)
Repro:
  1. anonymous user browsing
  2. sets content language switcher to "Japanese"
  3. sees japanese
  4. clicks on "show original" in the post
  5. sees original
  6. logs in to their account
7. (their account should see Japanese - UNCHECKED user pref "Show
original content instead of translated content.")
  8. BUG: but they see original because of the anon cookie
  
This PR fixes the bug by only using the user option if they're logged
in.
2026-04-25 00:44:20 +08:00

48 lines
2 KiB
Ruby

# 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)
return true if scope&.user&.user_option&.show_original_content
return false if scope&.user
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.content_localization_enabled && post.raw.present? && post.locale.present? &&
!post.in_user_locale? && !show_original?(scope)
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? &&
!show_original?(scope)
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 called
# @return [Boolean]
def self.show_translated_category?(category, scope)
SiteSetting.content_localization_enabled && category.locale.present? &&
!category.in_user_locale?
end
# This method returns true when we should try to show the translated tag.
# @param tag [Tag] The tag record
# @param scope [Object] The serializer scope from which the method is called
# @return [Boolean]
def self.show_translated_tag?(tag, scope)
SiteSetting.content_localization_enabled && tag.locale.present? && !tag.in_user_locale?
end
end