From 4982b7d4e01d7bcae2afdf03cc2f262c27206297 Mon Sep 17 00:00:00 2001 From: Bruno Casali Date: Wed, 11 Nov 2020 01:04:49 -0300 Subject: [PATCH 1/2] Create a LocaleService to centralize locale logic --- app/services/locale_service.rb | 17 +++++++++++ spec/services/locale_service_spec.rb | 43 ++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 app/services/locale_service.rb create mode 100644 spec/services/locale_service_spec.rb diff --git a/app/services/locale_service.rb b/app/services/locale_service.rb new file mode 100644 index 00000000..3163050f --- /dev/null +++ b/app/services/locale_service.rb @@ -0,0 +1,17 @@ +class LocaleService + def self.call(param_locale, browser_locale) + new(param_locale, browser_locale).perform + end + + def initialize(param_locale, browser_locale) + @param_locale = param_locale + @browser_locale = browser_locale + @default = I18n.default_locale + end + + def perform + return @param_locale if @param_locale.present? + + @browser_locale || @default + end +end diff --git a/spec/services/locale_service_spec.rb b/spec/services/locale_service_spec.rb new file mode 100644 index 00000000..343bf571 --- /dev/null +++ b/spec/services/locale_service_spec.rb @@ -0,0 +1,43 @@ +require 'spec_helper' + +RSpec.describe LocaleService do + before { allow(I18n).to receive(:default_locale).and_return('fr') } + + describe '.call' do + subject(:service) { described_class.call(param_locale, http_locale) } + + let(:param_locale) { nil } + let(:http_locale) { nil } + + context 'when @param_locale is valid' do + let(:param_locale) { 'pt-BR' } + + it 'responds with @locale' do + expect(service).to eq('pt-BR') + end + end + + context 'when @param_locale is nil' do + let(:http_locale) { 'es' } + + it 'responds with @http_locale' do + expect(service).to eq('es') + end + end + + context 'when @param_locale is blank' do + let(:param_locale) { ' ' } + let(:http_locale) { 'en' } + + it 'responds with @http_locale' do + expect(service).to eq('en') + end + end + + context 'when both @param_locale and @http_locale is nil' do + it 'responds with I18n.default_locale' do + expect(service).to eq('fr') + end + end + end +end From 2fe1b2f430842e5a248055007a7d5d6b0b738612 Mon Sep 17 00:00:00 2001 From: Bruno Casali Date: Wed, 11 Nov 2020 01:18:47 -0300 Subject: [PATCH 2/2] 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