Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV: Indicate source and target languages in error prompt #185

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions app/services/discourse_translator/amazon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,42 +109,46 @@ 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,
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
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
Expand Down
4 changes: 3 additions & 1 deletion app/services/discourse_translator/google.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion app/services/discourse_translator/libre_translate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion app/services/discourse_translator/microsoft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion app/services/discourse_translator/yandex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
7 changes: 6 additions & 1 deletion spec/services/amazon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions spec/services/google_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/services/microsoft_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down