From 42153d1d37a1897abf52367bf0cf7572ae12c607 Mon Sep 17 00:00:00 2001 From: lydiasan <> Date: Wed, 12 May 2021 16:30:35 +0200 Subject: [PATCH 1/5] ajout du selecteur de langue --- app/assets/images/icons/translate-icon.svg | 1 + app/controllers/application_controller.rb | 15 +++++++++++---- app/controllers/root_controller.rb | 5 +++++ app/views/layouts/_header.haml | 3 +++ app/views/layouts/_locale_dropdown.html.haml | 11 +++++++++++ config/routes.rb | 1 + 6 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 app/assets/images/icons/translate-icon.svg create mode 100644 app/views/layouts/_locale_dropdown.html.haml diff --git a/app/assets/images/icons/translate-icon.svg b/app/assets/images/icons/translate-icon.svg new file mode 100644 index 000000000..2c34cd92b --- /dev/null +++ b/app/assets/images/icons/translate-icon.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 35b393a6b..c23773bd8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -18,7 +18,8 @@ class ApplicationController < ActionController::Base before_action :set_active_storage_host before_action :setup_javascript_settings before_action :setup_tracking - before_action :set_locale + + 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 @@ -308,9 +309,15 @@ class ApplicationController < ActionController::Base current_user&.email end - def set_locale - if ENV.fetch('LOCALIZATION_ENABLED', 'false') == 'true' - I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales) + 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 + I18n.with_locale(locale, &action) end end diff --git a/app/controllers/root_controller.rb b/app/controllers/root_controller.rb index a61a15282..1a1c7e910 100644 --- a/app/controllers/root_controller.rb +++ b/app/controllers/root_controller.rb @@ -87,4 +87,9 @@ class RootController < ApplicationController format.js { render js: helpers.remove_element('#outdated-browser-banner') } end end + + def save_locale + cookies[:locale] = params[:locale] + redirect_to request.referer + end end diff --git a/app/views/layouts/_header.haml b/app/views/layouts/_header.haml index 88e2c7266..0ab8116d8 100644 --- a/app/views/layouts/_header.haml +++ b/app/views/layouts/_header.haml @@ -81,3 +81,6 @@ - else = render partial: 'shared/help/help_button' + + %li + = render partial: 'layouts/locale_dropdown' diff --git a/app/views/layouts/_locale_dropdown.html.haml b/app/views/layouts/_locale_dropdown.html.haml new file mode 100644 index 000000000..d05429745 --- /dev/null +++ b/app/views/layouts/_locale_dropdown.html.haml @@ -0,0 +1,11 @@ +.dropdown.locale-dropdown + %button.button.dropdown-button.icon-only.header-menu-button{ title: "Translate", 'aria-expanded' => 'false', 'aria-controls' => 'locale_menu' } + .hidden Translate + = image_tag "icons/translate-icon.svg", alt: 'Translate', 'aria-hidden':'true' + %ul.header-menu.dropdown-content#mon_compte_menu + %li + = 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 diff --git a/config/routes.rb b/config/routes.rb index acdac88ea..f9714db2f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -151,6 +151,7 @@ Rails.application.routes.draw do get "patron" => "root#patron" get "suivi" => "root#suivi" post "dismiss_outdated_browser" => "root#dismiss_outdated_browser" + post "save_locale" => "root#save_locale" get "contact", to: "support#index" post "contact", to: "support#create" From 71069c0e1b4603a05e48beaede0b5fc3b4151919 Mon Sep 17 00:00:00 2001 From: lydiasan <> Date: Tue, 25 May 2021 10:05:52 +0200 Subject: [PATCH 2/5] correction de la variable d'environnement 'LOCALIZATION_ENABLED' --- app/controllers/application_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c23773bd8..6ce5dc733 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -313,7 +313,7 @@ class ApplicationController < ActionController::Base locale = nil if cookies[:locale] locale = cookies[:locale] - elsif ENV.fetch('LOCALIZATION_ENABLED', 'false') == 'true' + elsif ENV['LOCALIZATION_ENABLED'] == 'enabled' locale = http_accept_language.compatible_language_from(I18n.available_locales) else locale = I18n.default_locale From e85dcf4c58b008e2c6a2da3bb68a24bc14144955 Mon Sep 17 00:00:00 2001 From: lydiasan <56531846+lydiasan@users.noreply.github.com> Date: Tue, 25 May 2021 13:32:26 +0200 Subject: [PATCH 3/5] Update app/controllers/application_controller.rb Co-authored-by: Pierre de La Morinerie --- app/controllers/application_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6ce5dc733..c23773bd8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -313,7 +313,7 @@ class ApplicationController < ActionController::Base locale = nil if cookies[:locale] locale = cookies[:locale] - elsif ENV['LOCALIZATION_ENABLED'] == 'enabled' + elsif ENV.fetch('LOCALIZATION_ENABLED', 'false') == 'true' locale = http_accept_language.compatible_language_from(I18n.available_locales) else locale = I18n.default_locale From 355616e9a76e92792ea89cad8905656ec39bd2c4 Mon Sep 17 00:00:00 2001 From: lydiasan <56531846+lydiasan@users.noreply.github.com> Date: Tue, 25 May 2021 13:36:32 +0200 Subject: [PATCH 4/5] Update app/views/layouts/_header.haml Co-authored-by: Pierre de La Morinerie --- app/views/layouts/_header.haml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/views/layouts/_header.haml b/app/views/layouts/_header.haml index 0ab8116d8..9ac9fe22d 100644 --- a/app/views/layouts/_header.haml +++ b/app/views/layouts/_header.haml @@ -82,5 +82,6 @@ - else = render partial: 'shared/help/help_button' - %li - = render partial: 'layouts/locale_dropdown' + - if ENV.fetch('LOCALIZATION_ENABLED', 'false') == 'true' + %li + = render partial: 'layouts/locale_dropdown' From 884e52326e49e5b997b5ed6e36231412b49921ea Mon Sep 17 00:00:00 2001 From: lydiasan <56531846+lydiasan@users.noreply.github.com> Date: Tue, 25 May 2021 13:36:50 +0200 Subject: [PATCH 5/5] Update app/views/layouts/_locale_dropdown.html.haml Co-authored-by: Pierre de La Morinerie --- app/views/layouts/_locale_dropdown.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/layouts/_locale_dropdown.html.haml b/app/views/layouts/_locale_dropdown.html.haml index d05429745..b9a12432f 100644 --- a/app/views/layouts/_locale_dropdown.html.haml +++ b/app/views/layouts/_locale_dropdown.html.haml @@ -2,7 +2,7 @@ %button.button.dropdown-button.icon-only.header-menu-button{ title: "Translate", 'aria-expanded' => 'false', 'aria-controls' => 'locale_menu' } .hidden Translate = image_tag "icons/translate-icon.svg", alt: 'Translate', 'aria-hidden':'true' - %ul.header-menu.dropdown-content#mon_compte_menu + %ul.header-menu.dropdown-content %li = link_to save_locale_path(locale: :en), method: :post, class: "menu-item menu-link" do EN - English