Skip to content

Commit 2631ce6

Browse files
authored
FIX: Microsoft Azure language support (#306)
We were checking if the detected language was in the key and value which would fail if the keys and values are not identical. This improves the check so "zh-Hans" (value returned by Azure) to "en" (I18n.locale) is true instead of false.
1 parent 21de0e5 commit 2631ce6

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

app/services/discourse_translator/provider/microsoft.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,12 @@ def self.translate_text!(text, target_locale_sym = I18n.locale)
180180
response_body.first["translations"].first["text"]
181181
end
182182

183-
def self.translate_supported?(detected_lang, target_lang)
184-
SUPPORTED_LANG_MAPPING.keys.include?(detected_lang.to_sym) &&
185-
SUPPORTED_LANG_MAPPING.values.include?(detected_lang.to_s)
183+
def self.translate_supported?(detected, target)
184+
s = SUPPORTED_LANG_MAPPING
185+
186+
# Azure language support indicates to and from language codes
187+
(s.keys.include?(target.to_sym) || s.values.include?(target.to_s)) &&
188+
(s.keys.include?(detected.to_sym) || s.values.include?(detected.to_s))
186189
end
187190

188191
private

spec/services/microsoft_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,16 @@ def detect_endpoint
188188
expect(described_class.translate_text!(text)).to eq(translated_text)
189189
end
190190
end
191+
192+
describe ".translate_supported?" do
193+
it "allows translation of topics in supported languages" do
194+
expect(described_class.translate_supported?(:en, "zh-Hans")).to eq(true)
195+
expect(described_class.translate_supported?("en", :zh_CN)).to eq(true)
196+
expect(described_class.translate_supported?("zh-Hans", :en)).to eq(true)
197+
expect(described_class.translate_supported?(:zh_CN, "en")).to eq(true)
198+
199+
expect(described_class.translate_supported?(:tr, "en")).to eq(true)
200+
expect(described_class.translate_supported?("tr", "en")).to eq(true)
201+
end
202+
end
191203
end

0 commit comments

Comments
 (0)