discourse-translator/app/services/discourse_translator/discourse_ai.rb
Natalie Tay 7d411e458b
FEATURE: Translates every post to automatic_translation_target_languages (#207)
Introduces two site settings which will automatically translate posts to the language:

automatic_translation_target_languages
automatic_translation_backfill_maximum_posts_per_hour (hidden)
2025-02-14 11:46:53 +08:00

39 lines
1.1 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
save_detected_locale(topic_or_post) do
::DiscourseAi::LanguageDetector.new(text_for_detection(topic_or_post)).detect
end
end
def self.translate!(translatable, target_locale_sym = I18n.locale)
return unless required_settings_enabled
save_translation(translatable, target_locale_sym) do
::DiscourseAi::Translator.new(
text_for_translation(translatable),
target_locale_sym,
).translate
end
end
private
def self.required_settings_enabled
SiteSetting.translator_enabled && SiteSetting.translator == "DiscourseAi" &&
SiteSetting.discourse_ai_enabled && SiteSetting.ai_helper_enabled
end
end
end