Manage the remote translation button display
- Add remote_translation_button partial to layout - Only display button when we have remote_translations and if current locale is include on available locales from remote translations service. - Recover available locales from remote translations service. Use daily_cache to detect every day if remote translation service has added new available locale. Co-Authored-By: alessandro <agileontheweb@gmail.com>
This commit is contained in:
6
app/helpers/remote_translations_helper.rb
Normal file
6
app/helpers/remote_translations_helper.rb
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
module RemoteTranslationsHelper
|
||||||
|
|
||||||
|
def display_remote_translation_info?(remote_translations, locale)
|
||||||
|
remote_translations.present? && RemoteTranslations::Microsoft::AvailableLocales.include_locale?(locale)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
<header>
|
<header>
|
||||||
|
<% if display_remote_translation_info?(@remote_translations, I18n.locale) %>
|
||||||
|
<%= render "shared/remote_translations_button", remote_translations: @remote_translations %>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
<div class="top-links">
|
<div class="top-links">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
|
|||||||
@@ -5,9 +5,11 @@ require "json"
|
|||||||
|
|
||||||
module RemoteTranslations::Microsoft::AvailableLocales
|
module RemoteTranslations::Microsoft::AvailableLocales
|
||||||
|
|
||||||
def load_remote_locales
|
def available_locales
|
||||||
|
daily_cache("locales") do
|
||||||
remote_available_locales.map { |locale| locale.first }
|
remote_available_locales.map { |locale| locale.first }
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def parse_locale(locale)
|
def parse_locale(locale)
|
||||||
case locale
|
case locale
|
||||||
@@ -22,6 +24,10 @@ module RemoteTranslations::Microsoft::AvailableLocales
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def include_locale?(locale)
|
||||||
|
available_locales.include?(parse_locale(locale).to_s)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def remote_available_locales
|
def remote_available_locales
|
||||||
@@ -42,4 +48,8 @@ module RemoteTranslations::Microsoft::AvailableLocales
|
|||||||
JSON.parse(result)["translation"]
|
JSON.parse(result)["translation"]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def daily_cache(key, &block)
|
||||||
|
Rails.cache.fetch("remote_available_locales/#{Time.current.strftime('%Y-%m-%d')}/#{key}", &block)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
83
spec/features/remote_translations_spec.rb
Normal file
83
spec/features/remote_translations_spec.rb
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
require "rails_helper"
|
||||||
|
include RemoteTranslations::Microsoft::AvailableLocales
|
||||||
|
|
||||||
|
describe "Remote Translations" do
|
||||||
|
|
||||||
|
before do
|
||||||
|
Setting["feature.remote_translations"] = true
|
||||||
|
create(:proposal)
|
||||||
|
available_locales_response = ["ar", "de", "en", "es", "fa", "fr", "he", "it", "nl", "pl",
|
||||||
|
"pt", "sv", "zh-Hans", "zh-Hant"]
|
||||||
|
expect_any_instance_of(RemoteTranslations::Microsoft::AvailableLocales).to receive(:available_locales).
|
||||||
|
and_return(available_locales_response)
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
allow(I18n).to receive(:available_locales).and_call_original
|
||||||
|
allow(I18n.fallbacks).to receive(:[]).and_call_original
|
||||||
|
Globalize.set_fallbacks_to_all_available_locales
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "Display remote translation button when locale is included in microsoft translate client" do
|
||||||
|
|
||||||
|
context "with locale that has :en fallback" do
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(I18n.fallbacks).to receive(:[]).and_return([:en])
|
||||||
|
Globalize.set_fallbacks_to_all_available_locales
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario "should display text in English" do
|
||||||
|
available_locales_with_fallback_en = [:ar, :de, :fa, :he, :nl, :pl, :sv]
|
||||||
|
|
||||||
|
visit root_path(locale: available_locales_with_fallback_en.sample)
|
||||||
|
|
||||||
|
expect(page).to have_css ".remote-translations-button"
|
||||||
|
expect(page).to have_content "The content of this page is not available in your language"
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario "should display text in English after parse key" do
|
||||||
|
available_locales_with_fallback_en = [:"zh-CN", :"zh-TW"]
|
||||||
|
|
||||||
|
visit root_path(locale: available_locales_with_fallback_en.sample)
|
||||||
|
|
||||||
|
expect(page).to have_css ".remote-translations-button"
|
||||||
|
expect(page).to have_content "The content of this page is not available in your language"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with locale that has :en fallback" do
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(I18n.fallbacks).to receive(:[]).and_return([:es])
|
||||||
|
Globalize.set_fallbacks_to_all_available_locales
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario "with locale that has :es fallback" do
|
||||||
|
available_locales_with_fallback_es = [:es, :fr, :it]
|
||||||
|
|
||||||
|
visit root_path(locale: available_locales_with_fallback_es.sample)
|
||||||
|
|
||||||
|
expect(page).to have_css ".remote-translations-button"
|
||||||
|
expect(page).to have_content "El contenido de esta página no está disponible en tu idioma"
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario "with locale that has :es fallback and need parse key" do
|
||||||
|
visit root_path(locale: :"pt-BR")
|
||||||
|
|
||||||
|
expect(page).to have_css ".remote-translations-button"
|
||||||
|
expect(page).to have_content "El contenido de esta página no está disponible en tu idioma"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario "Not display remote translation button when locale is not included in microsoft translate client" do
|
||||||
|
not_available_locales = [:val, :gl, :sq]
|
||||||
|
|
||||||
|
visit root_path(locale: not_available_locales.sample)
|
||||||
|
|
||||||
|
expect(page).not_to have_css ".remote-translations-button"
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user