discourse-translator/services/discourse_translator/base.rb
2019-05-13 11:08:27 +08:00

49 lines
1.1 KiB
Ruby

# frozen_string_literal: true
module DiscourseTranslator
extend ActiveSupport::Concern
class TranslatorError < ::StandardError; end
class Base
def self.key_prefix
"#{PLUGIN_NAME}:".freeze
end
def self.access_token_key
raise "Not Implemented"
end
def self.cache_key
"#{key_prefix}#{access_token_key}"
end
def self.translate(post)
raise "Not Implemented"
end
def self.detect(post)
raise "Not Implemented"
end
def self.access_token
raise "Not Implemented"
end
def self.from_custom_fields(post)
post_translated_custom_field = post.custom_fields[DiscourseTranslator::TRANSLATED_CUSTOM_FIELD] ||= {}
post_translated_custom_field = post_translated_custom_field.with_indifferent_access
if !(translated_text = post_translated_custom_field[I18n.locale])
translated_text = yield
post.custom_fields[DiscourseTranslator::TRANSLATED_CUSTOM_FIELD] =
post_translated_custom_field.merge(I18n.locale => translated_text)
post.save!
end
translated_text
end
end
end