Create a new method to change the lang of the app

- The old method (link to `?locale=#{l.to_s}`) was not functional for path that already has query params in current path (such as `/user/spree_user/password/edit?reset_password_token=`) ;
 - This seems that the best way to handle this, is to create a new route, new method that effectively set the lang (not the locale actually...) and then redirect_back (ie. using the HTTP_REFERER, with a fallback to `/`)

s
This commit is contained in:
Jean-Baptiste Bellet
2021-06-28 11:34:04 +02:00
parent 0a6d959742
commit aa982fe8df
4 changed files with 13 additions and 4 deletions

View File

@@ -0,0 +1,8 @@
# frozen_string_literal: true
class LocalesController < BaseController
def show
UserLocaleSetter.new(spree_current_user, params[:id], cookies).set_locale
redirect_back fallback_location: main_app.root_url
end
end

View File

@@ -5,4 +5,4 @@
%ul.dropdown
- OpenFoodNetwork::I18nConfig.selectable_locales.each do |l|
%li
%a{href: "?locale=#{l.to_s}" }= t('language_name', locale: l)
= link_to t('language_name', locale: l), main_app.locale_path(l.to_s)

View File

@@ -20,6 +20,7 @@ Openfoodnetwork::Application.routes.draw do
get "/register", to: "registration#index", as: :registration
get "/register/auth", to: "registration#authenticate", as: :registration_auth
post "/user/registered_email", to: "spree/users#registered_email"
resources :locales, only: [:show]
# Redirects to global website
get "/connect", to: redirect("https://openfoodnetwork.org/#{ENV['DEFAULT_COUNTRY_CODE'].andand.downcase}/connect/")

View File

@@ -127,11 +127,11 @@ feature 'Multilingual', js: true do
find('.language-switcher').click
within '.language-switcher .dropdown' do
expect(page).to have_link I18n.t('language_name', locale: :en), href: '?locale=en'
expect(page).to have_link I18n.t('language_name', locale: :en), href: '/locales/en'
expect(page).to have_link I18n.t('language_name', locale: :es, default: 'Language Name'),
href: '?locale=es'
href: '/locales/es'
find('li a[href="?locale=es"]').click
find('li a[href="/locales/es"]').click
end
expect_menu_and_cookie_in_es