Add user content translations with configurable backends (#19218)

This commit is contained in:
Eugen Rochko
2022-09-23 23:00:12 +02:00
committed by GitHub
parent d2f7e30a28
commit 0d6b878808
16 changed files with 306 additions and 11 deletions

View File

@ -0,0 +1,24 @@
# frozen_string_literal: true
class TranslateStatusService < BaseService
CACHE_TTL = 1.day.freeze
def call(status, target_language)
raise Mastodon::NotPermittedError unless status.public_visibility? || status.unlisted_visibility?
@status = status
@target_language = target_language
Rails.cache.fetch("translations/#{@status.language}/#{@target_language}/#{content_hash}", expires_in: CACHE_TTL) { translation_backend.translate(@status.text, @status.language, @target_language) }
end
private
def translation_backend
TranslationService.configured
end
def content_hash
Digest::SHA256.base64digest(@status.text)
end
end