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
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
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
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
def feature_enabled?(feature_name)
@ -309,14 +320,25 @@ class ApplicationController < ActionController::Base
end
def switch_locale(&action)
locale = nil
if cookies[:locale]
locale = cookies[:locale]
elsif ENV.fetch('LOCALIZATION_ENABLED', 'false') == 'true'
locale = http_accept_language.compatible_language_from(I18n.available_locales)
else
locale = I18n.default_locale
end
locale = extract_locale_from_query_params ||
extract_locale_from_cookie ||
extract_locale_from_accept_language_header ||
I18n.default_locale
I18n.with_locale(locale, &action)
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

View file

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

View file

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

View file

@ -1,11 +1,11 @@
.dropdown.locale-dropdown
%button.button.dropdown-button.icon-only.header-menu-button{ title: "Translate", 'aria-expanded' => 'false', 'aria-controls' => 'locale_menu' }
.dropdown.locale-dropdown.header-menu-opener
%button.button.dropdown-button.icon-only.header-menu-button{ title: "Translate", aria: { expanded: 'false', controls: 'locale_menu' } }
.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
%li
= link_to save_locale_path(locale: :en), method: :post, class: "menu-item menu-link" do
EN - English
= link_to save_locale_path(locale: :en), method: :post, class: "menu-item menu-link" do
EN English
%li
= link_to save_locale_path(locale: :fr), method: :post, class: "menu-item menu-link" do
FR - français
= link_to save_locale_path(locale: :fr), method: :post, class: "menu-item menu-link" do
FR français

View file

@ -8,7 +8,7 @@ feature 'Accessing the website in different languages:' do
expect(page).to have_text('Connectez-vous')
click_on 'Translate'
click_on 'EN - English'
click_on 'EN English'
# The page is now in English
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(:instructeur_signed_in?).and_return((profile == :instructeur))
allow(view).to receive(:current_instructeur).and_return(current_instructeur)
allow(view).to receive(:localization_enabled?).and_return(false)
if user
sign_in user

View file

@ -5,6 +5,7 @@ describe 'layouts/procedure_context.html.haml', type: :view do
before do
allow(view).to receive(:instructeur_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
subject do