mirror of
https://github.com/lunaisnotaboy/mastodon.git
synced 2024-09-11 20:57:44 +00:00
28 lines
693 B
Ruby
28 lines
693 B
Ruby
# frozen_string_literal: true
|
|
|
|
class TranslateStatusService < BaseService
|
|
CACHE_TTL = 1.day.freeze
|
|
|
|
include FormattingHelper
|
|
|
|
def call(status, target_language)
|
|
raise Mastodon::NotPermittedError unless status.translatable?
|
|
|
|
@status = status
|
|
@content = status_content_format(@status)
|
|
@target_language = target_language
|
|
|
|
Rails.cache.fetch("translations/#{@status.language}/#{@target_language}/#{content_hash}", expires_in: CACHE_TTL) { translation_backend.translate(@content, @status.language, @target_language) }
|
|
end
|
|
|
|
private
|
|
|
|
def translation_backend
|
|
TranslationService.configured
|
|
end
|
|
|
|
def content_hash
|
|
Digest::SHA256.base64digest(@content)
|
|
end
|
|
end
|