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

Enhancement/i18n thread safe #647

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 7 additions & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception

around_action :switch_locale
before_action :mobile_filter_header
before_action :set_locale

def mobile_filter_header
@mobile = true
end

def set_locale
I18n.locale = http_accept_language.language_region_compatible_from(I18n.available_locales)
def switch_locale(&action)
locale = http_accept_language.language_region_compatible_from(I18n.available_locales)
locale ||= I18n.default_locale

I18n.with_locale(locale, &action)
end
end
2 changes: 2 additions & 0 deletions spec/controllers/pages_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'spec_helper'

describe PagesController, type: :controller do
it_behaves_like 'localized request', :index

it "#index" do
get :index
assert_response :success
Expand Down
2 changes: 2 additions & 0 deletions spec/controllers/restrooms_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'spec_helper'

describe RestroomsController, type: :controller do
it_behaves_like 'localized request', :index

it "#index" do
get :index
assert_response :success
Expand Down
9 changes: 9 additions & 0 deletions spec/support/shared_examples/localized_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
shared_examples_for 'localized request' do |action|
it 'calls I18n.with_locale in requests' do
allow(I18n).to receive(:with_locale)

get action

expect(I18n).to have_received(:with_locale).with(:en)
end
end
Comment on lines +1 to +9
Copy link
Contributor

@DeeDeeG DeeDeeG Oct 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brunoocasali would it be very difficult to also test loading a page with another locale? So far our test suite runs strictly against the site in English. I would love to have some test for a locale other than English, even if it was a very basic or simple test.

If you could do that, it would be much appreciated. If it's a lot of work (or really if you prefer not to for any reason), let me know, and I'll still accept this pull request without it.

Thank you, and best regards.

- DeeDeeg

Copy link
Contributor Author

@brunoocasali brunoocasali Oct 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @DeeDeeG, thanks for your review!

Don't you think that way we could start testing some code that are already tested by the framework?

The test in this PR just guarantees that every request will contain some kind of language definition.
When we add a multi-language switcher like a querystring called "lang" for example we could add another spec here, to check:

If the url contains the "lang" querystring all the messages will be translated properly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be fairly easy to set the locale within a spec's code.

But I can see how that would be unrelated to this pull request. Perhaps when I re-add the language switcher feature, I will @mention you to see if you might be interested in adding some tests. No obligation, but certainly it would be appreciated if you wanted to help with that.

Thanks for this, I'm going to merge now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DeeDeeG of course! just mention me and I see what can I do ;)