Skip to content

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

5.7 KB
Raw Blame
# frozen_string_literal: true

class ContentLocalization
  AUTOMATICALLY_TRANSLATE_COOKIE = "automatically_translate"

  # @param scope [Object] The serializer scope from which the method is called
  # @return [Boolean]
  def self.automatically_translate?(scope)
    return scope.user.user_option.automatically_translate if scope&.user

    automatically_translate_anonymously?(scope&.request&.cookies)
  end

  # @param cookies [ActionDispatch::Cookies::CookieJar, Hash]
  # @return [Boolean]
  def self.automatically_translate_anonymously?(cookies)
    return true if cookies.blank?

    cookies[AUTOMATICALLY_TRANSLATE_COOKIE].to_s != "false"
  end

  # @param locale [String] The source locale of a topic or post
  # @param scope [Object] The serializer scope from which the method is called
  # @return [Boolean]
  def self.understands?(locale, scope)
    return false if locale.blank? || !scope&.user

    scope.user.user_option.understood_languages.any? do |understood_locale|
      LocaleNormalizer.is_same?(locale, understood_locale)
    end
  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? && 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? &&
      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

  # Expects I18n.locale to be the recipient's locale.
  # @param topic [Topic, nil]
  # @param post [Post, nil]
  # @param scope [Guardian] The recipient's guardian
  # @return [Hash] translated `:topic_title` and `:excerpt`, for whichever are available
  def self.translated_notification_fields(topic:, post:, scope:)
    fields = {}

    if (title = translated_topic_title(topic, scope))
      fields[:topic_title] = title
    end

    if show_translated_post?(post, scope)
      localization = post.get_localization
      # Stale translations would describe an earlier revision of the post.
      if localization&.post_version == post.version && localization.cooked.present?
        fields[:excerpt] = Post.excerpt(
          localization.cooked,
          400,
          text_entities: true,
          strip_links: true,
          remap_emoji: true,
          plain_hashtags: true,
        )
      end
    end

    fields
  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

  def self.show_translated_sidebar_section?(section, scope)
    SiteSetting.content_localization_enabled && section.public? && section.custom_section? &&
      section.locale.present? && !section.in_user_locale?
  end

  def self.show_translated_sidebar_url?(sidebar_url, scope)
    SiteSetting.content_localization_enabled && sidebar_url.locale.present? &&
      !sidebar_url.in_user_locale?
  end

  def self.crawler_locale_param_enabled?
    SiteSetting.content_localization_enabled && SiteSetting.content_localization_crawler_param &&
      SiteSetting.set_locale_from_param
  end

  PLAIN_URL_SERVES_DEFAULT_LOCALE_ENV = "discourse.plain_url_serves_default_locale"

  # Whether an anonymous client gets the default locale at the plain URL, so that URL can stand in
  # for the default locale's locale param URL. A locale cookie written from the locale param can
  # change what the plain URL serves between requests, so it is never guaranteed then.
  # @param request [Rack::Request]
  # @return [Boolean]
  def self.plain_url_serves_default_locale?(request)
    return false if SiteSetting.set_locale_from_cookie

    Discourse.anonymous_locale(request, include_param: false) == SiteSetting.default_locale
  end

  # These are the same conditions the header renders the switcher on. Keeping the two set-equal
  # means a locale cookie is only ever honoured while there is UI to change it back.
  # @return [Boolean]
  def self.language_switcher_enabled?
    SiteSetting.content_localization_enabled && SiteSetting.allow_user_locale &&
      SiteSetting.content_localization_language_switcher != "none" &&
      SiteSetting.content_localization_supported_locales.present?
  end
end