Skip to content

Commit 094ffeb

Browse files
authored
DEV: Indicate source and target languages in error prompt (#185)
* DEV: Indicate source and target languages in error prompt * Technically... it's not 'your language'
1 parent f07446c commit 094ffeb

File tree

9 files changed

+66
-27
lines changed

9 files changed

+66
-27
lines changed

app/services/discourse_translator/amazon.rb

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,42 +109,46 @@ def self.access_token_key
109109

110110
def self.detect(topic_or_post)
111111
text = truncate text_for_detection(topic_or_post)
112-
113112
return if text.blank?
114113

115-
begin
116-
detected_lang =
114+
topic_or_post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] ||= (
115+
begin
117116
client.translate_text(
118117
{
119118
text: text,
120119
source_language_code: "auto",
121120
target_language_code: SUPPORTED_LANG_MAPPING[I18n.locale],
122121
},
123122
)&.source_language_code
124-
125-
assign_lang_custom_field(topic_or_post, detected_lang)
126-
rescue Aws::Errors::MissingCredentialsError
127-
raise I18n.t("translator.amazon.invalid_credentials")
128-
end
123+
rescue Aws::Errors::MissingCredentialsError
124+
raise I18n.t("translator.amazon.invalid_credentials")
125+
end
126+
)
129127
end
130128

131129
def self.translate(topic_or_post)
132-
from_custom_fields(topic_or_post) do
133-
result =
134-
client.translate_text(
135-
{
136-
text: truncate(text_for_translation(topic_or_post)),
137-
source_language_code: "auto",
138-
target_language_code: SUPPORTED_LANG_MAPPING[I18n.locale],
139-
},
140-
)
130+
detected_lang = detect(topic_or_post)
141131

142-
detected_lang = assign_lang_custom_field(topic_or_post, result.source_language_code)
132+
from_custom_fields(topic_or_post) do
133+
begin
134+
result =
135+
client.translate_text(
136+
{
137+
text: truncate(text_for_translation(topic_or_post)),
138+
source_language_code: "auto",
139+
target_language_code: SUPPORTED_LANG_MAPPING[I18n.locale],
140+
},
141+
)
142+
rescue Aws::Translate::Errors::UnsupportedLanguagePairException
143+
raise I18n.t(
144+
"translator.failed",
145+
source_locale: detected_lang,
146+
target_locale: I18n.locale,
147+
)
148+
end
143149

144150
[detected_lang, result.translated_text]
145151
end
146-
rescue Aws::Translate::Errors::UnsupportedLanguagePairException
147-
raise I18n.t("translator.failed")
148152
end
149153

150154
def self.client

app/services/discourse_translator/google.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ def self.translate(topic_or_post)
105105
# here we handle that situation by returning the original string if the source and target lang are the same.
106106
return detected_lang, get_text(topic_or_post) if (detected_lang&.to_s.eql? I18n.locale.to_s)
107107

108-
raise I18n.t("translator.failed") unless translate_supported?(detected_lang, I18n.locale)
108+
unless translate_supported?(detected_lang, I18n.locale)
109+
raise I18n.t("translator.failed", source_locale: detected_lang, target_locale: I18n.locale)
110+
end
109111

110112
translated_text =
111113
from_custom_fields(topic_or_post) do

app/services/discourse_translator/libre_translate.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ def self.translate_supported?(source, target)
103103
def self.translate(topic_or_post)
104104
detected_lang = detect(topic_or_post)
105105

106-
raise I18n.t("translator.failed") unless translate_supported?(detected_lang, I18n.locale)
106+
unless translate_supported?(detected_lang, I18n.locale)
107+
raise I18n.t("translator.failed", source_locale: detected_lang, target_locale: I18n.locale)
108+
end
107109

108110
translated_text =
109111
from_custom_fields(topic_or_post) do

app/services/discourse_translator/microsoft.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,13 @@ def self.translate(topic_or_post)
112112

113113
if !SUPPORTED_LANG_MAPPING.keys.include?(detected_lang.to_sym) &&
114114
!SUPPORTED_LANG_MAPPING.values.include?(detected_lang.to_s)
115-
raise TranslatorError.new(I18n.t("translator.failed"))
115+
raise TranslatorError.new(
116+
I18n.t(
117+
"translator.failed",
118+
source_locale: detected_lang,
119+
target_locale: I18n.locale,
120+
),
121+
)
116122
end
117123

118124
if get_text(topic_or_post).length > LENGTH_LIMIT

app/services/discourse_translator/yandex.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,13 @@ def self.translate(topic_or_post)
141141

142142
if !SUPPORTED_LANG_MAPPING.keys.include?(detected_lang.to_sym) &&
143143
!SUPPORTED_LANG_MAPPING.values.include?(detected_lang.to_s)
144-
raise TranslatorError.new(I18n.t("translator.failed"))
144+
raise TranslatorError.new(
145+
I18n.t(
146+
"translator.failed",
147+
source_locale: detected_lang,
148+
target_locale: I18n.locale,
149+
),
150+
)
145151
end
146152

147153
translated_text =

config/locales/server.en.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ en:
1818
restrict_translation_by_group: "Only allowed groups can translate"
1919
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."
2020
translator:
21-
failed: "The translator is unable to translate this language."
21+
failed: "The translator is unable to translate this content (%{source_locale}) to the default language of this site (%{target_locale})."
2222
not_supported: "This language is not supported by the translator."
2323
too_long: "This post is too long to be translated by the translator."
2424
not_available: "The translator service is currently not available."

spec/services/amazon_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,15 @@
6565
},
6666
)
6767
described_class.stubs(:client).returns(client)
68+
post.custom_fields[::DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "en"
69+
post.save_custom_fields
70+
I18n.stubs(:locale).returns(:es)
6871
end
6972

7073
it "raises an error when trying to translate an unsupported language" do
71-
expect { described_class.translate(post) }.to raise_error(I18n.t("translator.failed"))
74+
expect { described_class.translate(post) }.to raise_error(
75+
I18n.t("translator.failed", source_locale: "en", target_locale: "es"),
76+
)
7277
end
7378
end
7479
end

spec/services/google_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,20 @@
167167
expect { described_class.translate(post) }.to raise_error DiscourseTranslator::TranslatorError
168168
end
169169

170+
it "returns error with source and target locale when translation is not supported" do
171+
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "cat"
172+
post.save_custom_fields
173+
I18n.stubs(:locale).returns(:dog)
174+
175+
Excon.expects(:post).returns(
176+
mock_response.new(200, %{ { "data": { "languages": [ { "language": "kit" }] } } }),
177+
)
178+
179+
expect { described_class.translate(post) }.to raise_error(
180+
I18n.t("translator.failed", source_locale: "cat", target_locale: "dog"),
181+
)
182+
end
183+
170184
it "truncates text for translation to max_characters_per_translation setting" do
171185
SiteSetting.max_characters_per_translation = 50
172186
post.cooked = "a" * 100

spec/services/microsoft_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def translate_endpoint
179179

180180
expect { described_class.translate(post) }.to raise_error(
181181
DiscourseTranslator::TranslatorError,
182-
I18n.t("translator.failed"),
182+
I18n.t("translator.failed", source_locale: "donkey", target_locale: I18n.locale),
183183
)
184184
end
185185

0 commit comments

Comments
 (0)