diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index af88ec0..e2538e4 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -21,6 +21,8 @@ en: not_supported: "This language is not supported by the translator." too_long: "This post is too long to be translated by the translator." not_available: "The translator service is currently not available." + amazon: + invalid_credentials: "The provided credentials for AWS translate are invalid." microsoft: missing_token: "The translator was unable to retrieve a valid token." diff --git a/plugin.rb b/plugin.rb index f951584..448a07d 100644 --- a/plugin.rb +++ b/plugin.rb @@ -144,8 +144,10 @@ def execute(args) DistributedMutex.synchronize("detect_translation_#{post.id}") do "DiscourseTranslator::#{SiteSetting.translator}".constantize.detect(post) - post.save_custom_fields unless post.custom_fields_clean? - post.publish_change_to_clients! :revised + if !post.custom_fields_clean? + post.save_custom_fields + post.publish_change_to_clients! :revised + end end end end diff --git a/services/discourse_translator/amazon.rb b/services/discourse_translator/amazon.rb index ac30278..1aae2c6 100644 --- a/services/discourse_translator/amazon.rb +++ b/services/discourse_translator/amazon.rb @@ -105,16 +105,20 @@ def self.detect(topic_or_post) return if text.blank? - detected_lang = - client.translate_text( - { - text: text, - source_language_code: "auto", - target_language_code: SUPPORTED_LANG_MAPPING[I18n.locale], - }, - )&.source_language_code - - assign_lang_custom_field(topic_or_post, detected_lang) + begin + detected_lang = + client.translate_text( + { + text: text, + source_language_code: "auto", + target_language_code: SUPPORTED_LANG_MAPPING[I18n.locale], + }, + )&.source_language_code + + assign_lang_custom_field(topic_or_post, detected_lang) + rescue Aws::Errors::MissingCredentialsError + raise I18n.t("translator.amazon.invalid_credentials") + end end def self.translate(topic_or_post) diff --git a/spec/jobs/detect_translation_spec.rb b/spec/jobs/detect_translation_spec.rb index 1428d3b..7e48363 100644 --- a/spec/jobs/detect_translation_spec.rb +++ b/spec/jobs/detect_translation_spec.rb @@ -37,9 +37,19 @@ it "detects translation" do post = Fabricate(:post, raw: "this is a sample post") - Jobs::DetectTranslation.new.execute(post_id: post.id) + messages = MessageBus.track_publish { Jobs::DetectTranslation.new.execute(post_id: post.id) } expect(post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD]).to eq("en") + expect(messages.size).to eq(1) + end + + it "does not publish change if no change in translation" do + post = Fabricate(:post, raw: "this is a sample post") + post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "en" + post.save_custom_fields + + messages = MessageBus.track_publish { Jobs::DetectTranslation.new.execute(post_id: post.id) } + expect(messages.size).to eq(0) end end end