mirror of
https://ghfast.top/https://github.com/discourse/discourse-translator.git
synced 2026-07-17 11:56:50 +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".
45 lines
1.3 KiB
Ruby
45 lines
1.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require_relative "base"
|
|
require "json"
|
|
|
|
module DiscourseTranslator
|
|
class DiscourseAi < Base
|
|
MAX_DETECT_LOCALE_TEXT_LENGTH = 1000
|
|
def self.language_supported?(detected_lang)
|
|
locale_without_region = I18n.locale.to_s.split("_").first
|
|
detected_lang != locale_without_region
|
|
end
|
|
|
|
def self.detect(topic_or_post)
|
|
return unless required_settings_enabled
|
|
|
|
topic_or_post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] ||= begin
|
|
::DiscourseAi::LanguageDetector.new(text_for_detection(topic_or_post)).detect
|
|
end
|
|
rescue => e
|
|
Rails.logger.warn(
|
|
"#{::DiscourseTranslator::PLUGIN_NAME}: Failed to detect language for #{topic_or_post.class.name} #{topic_or_post.id}: #{e}",
|
|
)
|
|
end
|
|
|
|
def self.translate(topic_or_post)
|
|
return unless required_settings_enabled
|
|
|
|
detected_lang = detect(topic_or_post)
|
|
translated_text =
|
|
from_custom_fields(topic_or_post) do
|
|
::DiscourseAi::Translator.new(text_for_translation(topic_or_post), I18n.locale).translate
|
|
end
|
|
|
|
[detected_lang, translated_text]
|
|
end
|
|
|
|
private
|
|
|
|
def self.required_settings_enabled
|
|
SiteSetting.translator_enabled && SiteSetting.translator == "DiscourseAi" &&
|
|
SiteSetting.discourse_ai_enabled && SiteSetting.ai_helper_enabled
|
|
end
|
|
end
|
|
end
|