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 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
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
9 changes: 9 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ class ApplicationController < ActionController::Base
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :mobile_filter_header
before_action :set_locale

def mobile_filter_header
@mobile = true
end

def mobile_filter_header
@mobile = true
end

def set_locale
I18n.locale = http_accept_language.language_region_compatible_from(I18n.available_locales)
end

end
5 changes: 5 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@ class Application < Rails::Application
resource "/api/*", headers: :any, methods: [:get, :post, :options]
end
end

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

end
end