Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to keep locale querystring when click on search #657

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 17 additions & 0 deletions app/services/locale_service.rb
Original file line number Diff line number Diff line change
@@ -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
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
43 changes: 43 additions & 0 deletions spec/services/locale_service_spec.rb
Original file line number Diff line number Diff line change
@@ -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