translate_status_service.rb 745 B

1234567891011121314151617181920212223242526272829
  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:v2/#{@status.language}/#{@target_language}/#{content_hash}", expires_in: CACHE_TTL) do
  11. translation_backend.translate(@content, @status.language, @target_language)
  12. end
  13. end
  14. private
  15. def translation_backend
  16. TranslationService.configured
  17. end
  18. def content_hash
  19. Digest::SHA256.base64digest(@content)
  20. end
  21. end