diff --git a/app/services/discourse_translator/amazon.rb b/app/services/discourse_translator/amazon.rb index 9e123109..c5752486 100644 --- a/app/services/discourse_translator/amazon.rb +++ b/app/services/discourse_translator/amazon.rb @@ -109,11 +109,10 @@ def self.access_token_key def self.detect(topic_or_post) text = truncate text_for_detection(topic_or_post) - return if text.blank? - begin - detected_lang = + topic_or_post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] ||= ( + begin client.translate_text( { text: text, @@ -121,30 +120,35 @@ def self.detect(topic_or_post) 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 + rescue Aws::Errors::MissingCredentialsError + raise I18n.t("translator.amazon.invalid_credentials") + end + ) end def self.translate(topic_or_post) - from_custom_fields(topic_or_post) do - result = - client.translate_text( - { - text: truncate(text_for_translation(topic_or_post)), - source_language_code: "auto", - target_language_code: SUPPORTED_LANG_MAPPING[I18n.locale], - }, - ) + detected_lang = detect(topic_or_post) - detected_lang = assign_lang_custom_field(topic_or_post, result.source_language_code) + from_custom_fields(topic_or_post) do + begin + result = + client.translate_text( + { + text: truncate(text_for_translation(topic_or_post)), + source_language_code: "auto", + target_language_code: SUPPORTED_LANG_MAPPING[I18n.locale], + }, + ) + rescue Aws::Translate::Errors::UnsupportedLanguagePairException + raise I18n.t( + "translator.failed", + source_locale: detected_lang, + target_locale: I18n.locale, + ) + end [detected_lang, result.translated_text] end - rescue Aws::Translate::Errors::UnsupportedLanguagePairException - raise I18n.t("translator.failed") end def self.client diff --git a/app/services/discourse_translator/google.rb b/app/services/discourse_translator/google.rb index 7002f80a..753affa4 100644 --- a/app/services/discourse_translator/google.rb +++ b/app/services/discourse_translator/google.rb @@ -105,7 +105,9 @@ def self.translate(topic_or_post) # here we handle that situation by returning the original string if the source and target lang are the same. return detected_lang, get_text(topic_or_post) if (detected_lang&.to_s.eql? I18n.locale.to_s) - raise I18n.t("translator.failed") unless translate_supported?(detected_lang, I18n.locale) + unless translate_supported?(detected_lang, I18n.locale) + raise I18n.t("translator.failed", source_locale: detected_lang, target_locale: I18n.locale) + end translated_text = from_custom_fields(topic_or_post) do diff --git a/app/services/discourse_translator/libre_translate.rb b/app/services/discourse_translator/libre_translate.rb index 751c8e7d..0996a809 100644 --- a/app/services/discourse_translator/libre_translate.rb +++ b/app/services/discourse_translator/libre_translate.rb @@ -103,7 +103,9 @@ def self.translate_supported?(source, target) def self.translate(topic_or_post) detected_lang = detect(topic_or_post) - raise I18n.t("translator.failed") unless translate_supported?(detected_lang, I18n.locale) + unless translate_supported?(detected_lang, I18n.locale) + raise I18n.t("translator.failed", source_locale: detected_lang, target_locale: I18n.locale) + end translated_text = from_custom_fields(topic_or_post) do diff --git a/app/services/discourse_translator/microsoft.rb b/app/services/discourse_translator/microsoft.rb index ca0fc6a2..67da6726 100644 --- a/app/services/discourse_translator/microsoft.rb +++ b/app/services/discourse_translator/microsoft.rb @@ -112,7 +112,13 @@ def self.translate(topic_or_post) if !SUPPORTED_LANG_MAPPING.keys.include?(detected_lang.to_sym) && !SUPPORTED_LANG_MAPPING.values.include?(detected_lang.to_s) - raise TranslatorError.new(I18n.t("translator.failed")) + raise TranslatorError.new( + I18n.t( + "translator.failed", + source_locale: detected_lang, + target_locale: I18n.locale, + ), + ) end if get_text(topic_or_post).length > LENGTH_LIMIT diff --git a/app/services/discourse_translator/yandex.rb b/app/services/discourse_translator/yandex.rb index 73144031..2b8af875 100644 --- a/app/services/discourse_translator/yandex.rb +++ b/app/services/discourse_translator/yandex.rb @@ -141,7 +141,13 @@ def self.translate(topic_or_post) if !SUPPORTED_LANG_MAPPING.keys.include?(detected_lang.to_sym) && !SUPPORTED_LANG_MAPPING.values.include?(detected_lang.to_s) - raise TranslatorError.new(I18n.t("translator.failed")) + raise TranslatorError.new( + I18n.t( + "translator.failed", + source_locale: detected_lang, + target_locale: I18n.locale, + ), + ) end translated_text = diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 135e457d..a571de87 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -18,7 +18,7 @@ en: restrict_translation_by_group: "Only allowed groups can translate" restrict_translation_by_poster_group: "Only allow translation of posts made by users in allowed groups. If empty, allow translations of posts from all users." translator: - failed: "The translator is unable to translate this language." + failed: "The translator is unable to translate this content (%{source_locale}) to the default language of this site (%{target_locale})." 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." diff --git a/spec/services/amazon_spec.rb b/spec/services/amazon_spec.rb index cba7ff0d..87c26b18 100644 --- a/spec/services/amazon_spec.rb +++ b/spec/services/amazon_spec.rb @@ -65,10 +65,15 @@ }, ) described_class.stubs(:client).returns(client) + post.custom_fields[::DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "en" + post.save_custom_fields + I18n.stubs(:locale).returns(:es) end it "raises an error when trying to translate an unsupported language" do - expect { described_class.translate(post) }.to raise_error(I18n.t("translator.failed")) + expect { described_class.translate(post) }.to raise_error( + I18n.t("translator.failed", source_locale: "en", target_locale: "es"), + ) end end end diff --git a/spec/services/google_spec.rb b/spec/services/google_spec.rb index 06073af2..746ff66a 100644 --- a/spec/services/google_spec.rb +++ b/spec/services/google_spec.rb @@ -167,6 +167,20 @@ expect { described_class.translate(post) }.to raise_error DiscourseTranslator::TranslatorError end + it "returns error with source and target locale when translation is not supported" do + post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "cat" + post.save_custom_fields + I18n.stubs(:locale).returns(:dog) + + Excon.expects(:post).returns( + mock_response.new(200, %{ { "data": { "languages": [ { "language": "kit" }] } } }), + ) + + expect { described_class.translate(post) }.to raise_error( + I18n.t("translator.failed", source_locale: "cat", target_locale: "dog"), + ) + end + it "truncates text for translation to max_characters_per_translation setting" do SiteSetting.max_characters_per_translation = 50 post.cooked = "a" * 100 diff --git a/spec/services/microsoft_spec.rb b/spec/services/microsoft_spec.rb index d5b78d99..53dcecf3 100644 --- a/spec/services/microsoft_spec.rb +++ b/spec/services/microsoft_spec.rb @@ -179,7 +179,7 @@ def translate_endpoint expect { described_class.translate(post) }.to raise_error( DiscourseTranslator::TranslatorError, - I18n.t("translator.failed"), + I18n.t("translator.failed", source_locale: "donkey", target_locale: I18n.locale), ) end