Allow translate locales that need to be mapping

It has been detected that for the :pt-BR, :zh-CN and :zh-TW locales,
the translate button was being displayed, but when requesting the
translation, the remote translation validation failed due to:

'''
validates :locale, inclusion: { in: ->(_) {
     RemoteTranslations::Microsoft::AvailableLocales.available_locales }}
'''

That available_locales method did not contemplate these 3 languages
in the format used by the application.

To solve this problem the api response is mapped to return all
locales in the format expected by the application.

Add remote translation model test to ensure that a remote translation
is valid when its locale is pt-BR.

Co-Authored-By: Javi Martín <35156+javierm@users.noreply.github.com>
This commit is contained in:
taitus
2023-03-09 09:00:41 +01:00
parent c64b49b128
commit 306e7356c3
5 changed files with 42 additions and 16 deletions

View File

@@ -6,29 +6,32 @@ require "json"
class RemoteTranslations::Microsoft::AvailableLocales
def self.available_locales
daily_cache("locales") do
remote_available_locales.map(&:first)
remote_available_locales.map { |locale| remote_locale_to_app_locale(locale) }
end
end
def self.parse_locale(locale)
case locale
when :"pt-BR"
:pt
when :"zh-CN"
:"zh-Hans"
when :"zh-TW"
:"zh-Hant"
else
locale
end
def self.app_locale_to_remote_locale(locale)
app_locale_to_remote_locale_map[locale] || locale
end
def self.include_locale?(locale)
available_locales.include?(parse_locale(locale).to_s)
available_locales.include?(locale.to_s)
end
private
def self.remote_locale_to_app_locale(locale)
app_locale_to_remote_locale_map.invert[locale] || locale
end
def self.app_locale_to_remote_locale_map
{
"pt-BR" => "pt",
"zh-CN" => "zh-Hans",
"zh-TW" => "zh-Hant"
}
end
def self.remote_available_locales
host = "https://api.cognitive.microsofttranslator.com"
path = "/languages?api-version=3.0"
@@ -44,7 +47,7 @@ class RemoteTranslations::Microsoft::AvailableLocales
result = response.body.force_encoding("utf-8")
JSON.parse(result)["translation"]
JSON.parse(result)["translation"].map(&:first)
end
def self.daily_cache(key, &block)

View File

@@ -5,7 +5,7 @@ class RemoteTranslations::Microsoft::Client
def call(fields_values, locale)
texts = prepare_texts(fields_values)
valid_locale = RemoteTranslations::Microsoft::AvailableLocales.parse_locale(locale)
valid_locale = RemoteTranslations::Microsoft::AvailableLocales.app_locale_to_remote_locale(locale)
request_translation(texts, valid_locale)
end

View File

@@ -5,7 +5,7 @@ describe Layout::RemoteTranslationsButtonComponent do
let(:component) { Layout::RemoteTranslationsButtonComponent.new(translations) }
before do
allow(RemoteTranslations::Microsoft::AvailableLocales).to receive(:available_locales)
allow(RemoteTranslations::Microsoft::AvailableLocales).to receive(:remote_available_locales)
.and_return(%w[de en es fr pt zh-Hans])
end

View File

@@ -0,0 +1,13 @@
require "rails_helper"
describe RemoteTranslations::Microsoft::AvailableLocales do
describe ".available_locales" do
it "includes locales with the same format as I18n.available_locales" do
allow(RemoteTranslations::Microsoft::AvailableLocales).to receive(:remote_available_locales)
.and_return(%w[de en es fr pt zh-Hans])
available_locales = RemoteTranslations::Microsoft::AvailableLocales.available_locales
expect(available_locales).to eq %w[de en es fr pt-BR zh-CN]
end
end
end

View File

@@ -49,6 +49,16 @@ describe RemoteTranslation, :remote_translations do
expect(remote_translation).to be_valid
end
it "is valid with a locale that uses a different name in the remote service" do
allow(RemoteTranslations::Microsoft::AvailableLocales).to receive(:available_locales).and_call_original
allow(RemoteTranslations::Microsoft::AvailableLocales).to receive(:remote_available_locales)
.and_return(["pt"])
remote_translation.locale = :"pt-BR"
expect(remote_translation).to be_valid
end
describe "#enqueue_remote_translation", :delay_jobs do
it "after create enqueue Delayed Job" do
expect { remote_translation.save }.to change { Delayed::Job.count }.by(1)