mirror of
https://ghfast.top/https://github.com/discourse/discourse-translator.git
synced 2026-07-16 11:46:33 +08:00
The criteria to allow a post to be translated is if the user's locale is different from the site's default locale. For each translator (e.g. Google, Amazon, Azure), we maintain a list of mappings `SUPPORTED_LANG_MAPPING` for each locale. On top of that, this mapping is also used to define what language the post should be translated to. When using most translators available in the plugin, "en_GB" forums will translate text to "en" due to these mappings. This is not a problem for Discourse AI provider, since there are no restrictions on language. This PR normalizes locales within Discourse AI translator, so that it sees "ar" to be the same as "ar_SA".
38 lines
1.3 KiB
Ruby
38 lines
1.3 KiB
Ruby
# frozen_string_literal: true
|
|
module DiscourseTranslator::GuardianExtension
|
|
def user_group_allow_translate?
|
|
return false if !current_user
|
|
current_user.in_any_groups?(SiteSetting.restrict_translation_by_group_map)
|
|
end
|
|
|
|
def poster_group_allow_translate?(post)
|
|
return false if !current_user
|
|
return true if SiteSetting.restrict_translation_by_poster_group_map.empty?
|
|
return false if post.user.nil?
|
|
post.user.in_any_groups?(SiteSetting.restrict_translation_by_poster_group_map)
|
|
end
|
|
|
|
def can_detect_language?(post)
|
|
(
|
|
SiteSetting.restrict_translation_by_poster_group_map.empty? ||
|
|
post&.user&.in_any_groups?(SiteSetting.restrict_translation_by_poster_group_map)
|
|
) && post.raw.present? && post.post_type != Post.types[:small_action]
|
|
end
|
|
|
|
def can_translate?(post)
|
|
return false if !user_group_allow_translate?
|
|
|
|
# we will deal with regionalized_strings (not syms) when comparing locales
|
|
# e.g. "en_GB"
|
|
# not "en-GB"
|
|
# nor :en_GB (I18n.locale)
|
|
detected_lang =
|
|
post.custom_fields[::DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD].to_s.sub("-", "_")
|
|
return false if detected_lang.blank?
|
|
|
|
detected_lang != I18n.locale.to_s &&
|
|
"DiscourseTranslator::#{SiteSetting.translator}".constantize.language_supported?(
|
|
detected_lang,
|
|
)
|
|
end
|
|
end
|