feat(i18n): enable localization by query param

Providing a query param ("locale") will enable localization. A language picker will be shown once
localization is activated. Locale is stored in a cookie "locale".
This commit is contained in:
Paul Chavard 2021-08-24 11:39:15 +02:00
parent 3b93336c20
commit 3b6528decf
7 changed files with 44 additions and 20 deletions

View file

@ -21,7 +21,7 @@ class ApplicationController < ActionController::Base
around_action :switch_locale around_action :switch_locale
helper_method :multiple_devise_profile_connect?, :instructeur_signed_in?, :current_instructeur, :current_expert, :expert_signed_in?, helper_method :multiple_devise_profile_connect?, :instructeur_signed_in?, :current_instructeur, :current_expert, :expert_signed_in?,
:administrateur_signed_in?, :current_administrateur, :current_account :administrateur_signed_in?, :current_administrateur, :current_account, :localization_enabled?, :set_locale
def staging_authenticate def staging_authenticate
if StagingAuthService.enabled? && !authenticate_with_http_basic { |username, password| StagingAuthService.authenticate(username, password) } if StagingAuthService.enabled? && !authenticate_with_http_basic { |username, password| StagingAuthService.authenticate(username, password) }
@ -71,6 +71,17 @@ class ApplicationController < ActionController::Base
alias_method :pundit_user, :current_account alias_method :pundit_user, :current_account
def localization_enabled?
ENV.fetch('LOCALIZATION_ENABLED', 'false') == 'true' || cookies[:locale].present?
end
def set_locale(locale)
if locale && locale.to_sym.in?(I18n.available_locales)
cookies[:locale] = locale
locale
end
end
protected protected
def feature_enabled?(feature_name) def feature_enabled?(feature_name)
@ -309,14 +320,25 @@ class ApplicationController < ActionController::Base
end end
def switch_locale(&action) def switch_locale(&action)
locale = nil locale = extract_locale_from_query_params ||
if cookies[:locale] extract_locale_from_cookie ||
locale = cookies[:locale] extract_locale_from_accept_language_header ||
elsif ENV.fetch('LOCALIZATION_ENABLED', 'false') == 'true' I18n.default_locale
locale = http_accept_language.compatible_language_from(I18n.available_locales)
else
locale = I18n.default_locale
end
I18n.with_locale(locale, &action) I18n.with_locale(locale, &action)
end end
def extract_locale_from_query_params
set_locale(request.query_parameters[:locale])
end
def extract_locale_from_cookie
cookies[:locale]
end
def extract_locale_from_accept_language_header
if localization_enabled?
http_accept_language.compatible_language_from(I18n.available_locales)
end
end
end end

View file

@ -89,7 +89,7 @@ class RootController < ApplicationController
end end
def save_locale def save_locale
cookies[:locale] = params[:locale] set_locale(params[:locale])
redirect_to request.referer redirect_back(fallback_location: root_path)
end end
end end

View file

@ -82,6 +82,6 @@
- else - else
= render partial: 'shared/help/help_button' = render partial: 'shared/help/help_button'
- if ENV.fetch('LOCALIZATION_ENABLED', 'false') == 'true' - if localization_enabled?
%li %li
= render partial: 'layouts/locale_dropdown' = render partial: 'layouts/locale_dropdown'

View file

@ -1,11 +1,11 @@
.dropdown.locale-dropdown .dropdown.locale-dropdown.header-menu-opener
%button.button.dropdown-button.icon-only.header-menu-button{ title: "Translate", 'aria-expanded' => 'false', 'aria-controls' => 'locale_menu' } %button.button.dropdown-button.icon-only.header-menu-button{ title: "Translate", aria: { expanded: 'false', controls: 'locale_menu' } }
.hidden Translate .hidden Translate
= image_tag "icons/translate-icon.svg", alt: 'Translate', 'aria-hidden':'true' = image_tag "icons/translate-icon.svg", alt: 'Translate', width: 24, height: 24, lazy: true, aria: { hidden: true }
%ul.header-menu.dropdown-content %ul.header-menu.dropdown-content
%li %li
= link_to save_locale_path(locale: :en), method: :post, class: "menu-item menu-link" do = link_to save_locale_path(locale: :en), method: :post, class: "menu-item menu-link" do
EN - English EN English
%li %li
= link_to save_locale_path(locale: :fr), method: :post, class: "menu-item menu-link" do = link_to save_locale_path(locale: :fr), method: :post, class: "menu-item menu-link" do
FR - français FR français

View file

@ -8,7 +8,7 @@ feature 'Accessing the website in different languages:' do
expect(page).to have_text('Connectez-vous') expect(page).to have_text('Connectez-vous')
click_on 'Translate' click_on 'Translate'
click_on 'EN - English' click_on 'EN English'
# The page is now in English # The page is now in English
expect(page).to have_text('Sign in') expect(page).to have_text('Sign in')

View file

@ -5,6 +5,7 @@ describe 'layouts/_header.html.haml', type: :view do
allow(view).to receive(:multiple_devise_profile_connect?).and_return(false) allow(view).to receive(:multiple_devise_profile_connect?).and_return(false)
allow(view).to receive(:instructeur_signed_in?).and_return((profile == :instructeur)) allow(view).to receive(:instructeur_signed_in?).and_return((profile == :instructeur))
allow(view).to receive(:current_instructeur).and_return(current_instructeur) allow(view).to receive(:current_instructeur).and_return(current_instructeur)
allow(view).to receive(:localization_enabled?).and_return(false)
if user if user
sign_in user sign_in user

View file

@ -5,6 +5,7 @@ describe 'layouts/procedure_context.html.haml', type: :view do
before do before do
allow(view).to receive(:instructeur_signed_in?).and_return(false) allow(view).to receive(:instructeur_signed_in?).and_return(false)
allow(view).to receive(:administrateur_signed_in?).and_return(false) allow(view).to receive(:administrateur_signed_in?).and_return(false)
allow(view).to receive(:localization_enabled?).and_return(false)
end end
subject do subject do