From 2fe1b2f430842e5a248055007a7d5d6b0b738612 Mon Sep 17 00:00:00 2001 From: Bruno Casali Date: Wed, 11 Nov 2020 01:18:47 -0300 Subject: [PATCH] Create a way to keep track of current_locale even between actions --- app/controllers/application_controller.rb | 17 +++++++--- app/views/layouts/_search.html.haml | 1 + spec/features/restrooms_spec.rb | 40 +++++++++++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 47c4d8c1..55aeeb66 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -11,13 +11,22 @@ def mobile_filter_header end def switch_locale(&action) - locale = params[:locale] || http_accept_language.language_region_compatible_from(I18n.available_locales) - locale ||= I18n.default_locale - - I18n.with_locale(locale, &action) + I18n.with_locale(current_locale, &action) end def default_url_options { locale: I18n.locale } end + + def current_locale + LocaleService.call(params[:locale], http_accept_locale) + end + + helper_method :current_locale + + private + + def http_accept_locale + http_accept_language.language_region_compatible_from(I18n.available_locales) + end end diff --git a/app/views/layouts/_search.html.haml b/app/views/layouts/_search.html.haml index 95de084d..4f5b3aaa 100644 --- a/app/views/layouts/_search.html.haml +++ b/app/views/layouts/_search.html.haml @@ -4,6 +4,7 @@ = form_tag restrooms_path, class: "search-restrooms-form", method: :get do = hidden_field_tag :lat, "" = hidden_field_tag :long, "" + = hidden_field_tag :locale, current_locale .input-group = text_field_tag :search, params[:search], class: "form-control search-bar", aria: {label: t("search_bar.enter_location")} .input-group-btn diff --git a/spec/features/restrooms_spec.rb b/spec/features/restrooms_spec.rb index 6298be5c..e79d4fd8 100644 --- a/spec/features/restrooms_spec.rb +++ b/spec/features/restrooms_spec.rb @@ -86,6 +86,46 @@ # expect(page).to have_css('#mapArea.loaded') # expect(page).to have_css('#mapArea .numberCircleText') end + + context 'with browser accept language header' do + before { page.driver.headers = { 'ACCEPT-LANGUAGE' => 'es' } } + + it 'sets browser locale to the url after search' do + create(:restroom, :geocoded, name: 'Mission Creek Cafe') + + visit root_path + expect(page.current_url).not_to match(/locale=es/) + + fill_in 'search', with: 'San Francisco' + find('.submit-search-button').click + + expect(page).to have_content 'Mission Creek Cafe' + expect(page.current_url).to match(/locale=es/) + end + + it "translates page based on url not browser's" do + create(:restroom, :geocoded, name: 'Mission Creek Cafe') + + visit root_path(locale: 'pt-BR') + + expect(find('.submit-search-button').value).to eq('Buscar') + expect(page.current_url).to match(/locale=pt-BR/) + end + end + + context 'with locale in url' do + it 'sets new locale to the url after search' do + create(:restroom, :geocoded, name: 'Mission Creek Cafe') + + visit root_path(locale: :es) + + fill_in 'search', with: 'San Francisco' + find('.submit-search-button').click + + expect(page).to have_content 'Mission Creek Cafe' + expect(page.current_url).to match(/locale=es/) + end + end end describe 'preview' do