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

Set locale based on Accept-Language header #420

Merged
merged 5 commits into from
Jan 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ gem 'high_voltage', '~> 3.0.0'
gem 'bugsnag'
gem 'rack-cors', :require => 'rack/cors'
gem 'rack-jsonp'
gem 'http_accept_language'

# Upgraded to 1.0.0 for Rails 5.1.4
gem 'activeadmin', '~> 1.0.0'
Expand Down
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ GEM
activesupport (>= 4.1, < 5.2)
hashie (3.5.6)
high_voltage (3.0.0)
http_accept_language (2.1.1)
i18n (0.8.6)
i18n_data (0.8.0)
ice_nine (0.11.2)
Expand Down Expand Up @@ -407,6 +408,7 @@ DEPENDENCIES
grape-swagger (~> 0.7.2)
haml
high_voltage (~> 3.0.0)
http_accept_language
jbuilder (~> 2.5)
jquery-rails
kaminari (~> 0.17.0)
Expand Down Expand Up @@ -436,4 +438,4 @@ RUBY VERSION
ruby 2.3.1p112

BUNDLED WITH
1.14.6
1.15.3
12 changes: 1 addition & 11 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,7 @@ def mobile_filter_header
end

def set_locale
I18n.locale = extract_locale_from_accept_language_header
I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales)
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using http_accept_language.language_region_compatible_from(I18n.available_locales)?

According to this README:

language_region_compatible_from(languages): Returns the first of the user preferred languages that is also found in available languages. Finds best fit by matching on primary language first and secondarily on region. If no matching region is found, return the first language in the group matching that primary language.
vs:
compatible_language_from(languages): Returns the first of the user_preferred_languages that is compatible with the available locales. Ignores region.


private
def extract_locale_from_accept_language_header
accept_language = request.env['HTTP_ACCEPT_LANGUAGE']
if accept_language.nil?
"en"
else
request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
end
end

end
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Application < Rails::Application

# I18n stuff
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
config.i18n.available_locales = [:en]

end
end