diff --git a/lib/remote_translations/microsoft/available_locales.rb b/lib/remote_translations/microsoft/available_locales.rb index 21ebf05cd..2bc39d2e3 100644 --- a/lib/remote_translations/microsoft/available_locales.rb +++ b/lib/remote_translations/microsoft/available_locales.rb @@ -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) diff --git a/lib/remote_translations/microsoft/client.rb b/lib/remote_translations/microsoft/client.rb index cbe856de3..ad17adccd 100644 --- a/lib/remote_translations/microsoft/client.rb +++ b/lib/remote_translations/microsoft/client.rb @@ -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 diff --git a/spec/components/layout/remote_translations_button_component_spec.rb b/spec/components/layout/remote_translations_button_component_spec.rb index a427b4a64..541a57835 100644 --- a/spec/components/layout/remote_translations_button_component_spec.rb +++ b/spec/components/layout/remote_translations_button_component_spec.rb @@ -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 diff --git a/spec/lib/remote_translations/microsoft/available_locales_spec.rb b/spec/lib/remote_translations/microsoft/available_locales_spec.rb new file mode 100644 index 000000000..421ef606a --- /dev/null +++ b/spec/lib/remote_translations/microsoft/available_locales_spec.rb @@ -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 diff --git a/spec/models/remote_translation_spec.rb b/spec/models/remote_translation_spec.rb index ce8508898..8ce808ce5 100644 --- a/spec/models/remote_translation_spec.rb +++ b/spec/models/remote_translation_spec.rb @@ -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)