0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-05 13:59:03 +08:00
discourse/lib/content_localization.rb
Natalie Tay cff8f6ea4d
FEATURE: Allow selection of multiple content languages when viewing topics and posts (#42128)
Related:
https://meta.discourse.org/t/multi-language-preferences-for-displaying-localized-content/381116

Allow anon and logged in users to set more than one language that they
understand, when content localization is enabled.


### User preferences

In user preferences, we now include a Content Languages section when the
site has Content Localization enabled.

### Topic menu

In topics where there are any languages that's not the user's language,
the option will appear to set their preferred content languages. This
appears for anon and logged in users, but anon users will be asked to
logged in to set the languages.
__________

These preferences apply only to topic titles and posts. They do not
affect navigational features like categories, tags, or sidebar content.
The per-post option to show the original remains independent of the
user's preferences.

This PR temporarily retains `show_original_content` as a compatibility
field computed from `automatically_translate`, keeping cached clients
and rolling deployments safe. A follow-up PR will remove the legacy API
and frontend compatibility code, then retire the old database column
through the staged column-removal process.
2026-07-30 12:19:44 +08:00

86 lines
3.3 KiB
Ruby
Vendored

# 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
def self.show_original?(scope)
!automatically_translate?(scope)
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
# 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
# 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
end