Skip to content

Commit

Permalink
Create a way to keep track of current_locale even between actions
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoocasali committed Nov 11, 2020
1 parent 4982b7d commit 2fe1b2f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
17 changes: 13 additions & 4 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions app/views/layouts/_search.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions spec/features/restrooms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2fe1b2f

Please sign in to comment.