translate_status_service.rb 729 B

123456789101112131415161718192021222324252627
  1. # frozen_string_literal: true
  2. class TranslateStatusService < BaseService
  3. CACHE_TTL = 1.day.freeze
  4. include FormattingHelper
  5. def call(status, target_language)
  6. raise Mastodon::NotPermittedError unless status.public_visibility? || status.unlisted_visibility?
  7. @status = status
  8. @content = status_content_format(@status)
  9. @target_language = target_language
  10. Rails.cache.fetch("translations/#{@status.language}/#{@target_language}/#{content_hash}", expires_in: CACHE_TTL) { translation_backend.translate(@content, @status.language, @target_language) }
  11. end
  12. private
  13. def translation_backend
  14. TranslationService.configured
  15. end
  16. def content_hash
  17. Digest::SHA256.base64digest(@content)
  18. end
  19. end