diff --git a/app/controllers/concerns/localize.rb b/app/controllers/concerns/localize.rb index f3b558c1b..2c5a19646 100644 --- a/app/controllers/concerns/localize.rb +++ b/app/controllers/concerns/localize.rb @@ -8,10 +8,16 @@ module Localize private def switch_locale(&action) - locale = Current.family.try(:locale) || I18n.default_locale + locale = locale_from_param || Current.family.try(:locale) || I18n.default_locale I18n.with_locale(locale, &action) end + def locale_from_param + return unless params[:locale].is_a?(String) && params[:locale].present? + locale = params[:locale].to_sym + locale if I18n.available_locales.include?(locale) + end + def switch_timezone(&action) timezone = Current.family.try(:timezone) || Time.zone Time.use_zone(timezone, &action) diff --git a/test/controllers/concerns/localize_test.rb b/test/controllers/concerns/localize_test.rb new file mode 100644 index 000000000..29610d6b4 --- /dev/null +++ b/test/controllers/concerns/localize_test.rb @@ -0,0 +1,25 @@ +require "test_helper" + +class LocalizeTest < ActionDispatch::IntegrationTest + setup do + sign_in users(:family_admin) + end + + test "uses family locale by default" do + get preferences_onboarding_url + assert_response :success + assert_select "h1", text: /Configure your preferences/i + end + + test "switches locale when locale param is provided" do + get preferences_onboarding_url(locale: "fr") + assert_response :success + assert_select "h1", text: /Configurez vos préférences/i + end + + test "ignores invalid locale param and uses family locale" do + get preferences_onboarding_url(locale: "invalid_locale") + assert_response :success + assert_select "h1", text: /Configure your preferences/i + end +end diff --git a/test/system/onboardings_test.rb b/test/system/onboardings_test.rb index 9258c2c13..8b06f1174 100644 --- a/test/system/onboardings_test.rb +++ b/test/system/onboardings_test.rb @@ -115,7 +115,8 @@ class OnboardingsTest < ApplicationSystemTestCase select "DD/MM/YYYY", from: "user_family_attributes_date_format" select "Dark", from: "user_theme" - click_button "Complete" + # Button text is in Spanish due to locale preview + click_button I18n.t("onboardings.preferences.submit", locale: :es) # Wait for redirect to goals page to ensure form was submitted assert_current_path goals_onboarding_path