Commit afd386e098c46691cd1766097c1c5f89f40b4eef
Committed by
Rafael Manzo
1 parent
5a1ca97e
Exists in
colab
and in
4 other branches
Implement language detection from browser information
Signed-off-by: Rafael Reggiani Manzo <rr.manzo@gmail.com>
Showing
4 changed files
with
35 additions
and
6 deletions
Show diff stats
Gemfile
| ... | ... | @@ -63,6 +63,9 @@ gem 'exception_notification', '~> 4.0.1' |
| 63 | 63 | # Google Analytics |
| 64 | 64 | gem 'google-analytics-rails', '~> 0.0.6' |
| 65 | 65 | |
| 66 | +# Browser language detection | |
| 67 | +gem 'http_accept_language' | |
| 68 | + | |
| 66 | 69 | group :test do |
| 67 | 70 | # Easier test writing |
| 68 | 71 | gem "shoulda-matchers", '~> 2.8.0' | ... | ... |
Gemfile.lock
| ... | ... | @@ -138,6 +138,7 @@ GEM |
| 138 | 138 | activesupport (>= 4.1.0) |
| 139 | 139 | google-analytics-rails (0.0.6) |
| 140 | 140 | hike (1.2.3) |
| 141 | + http_accept_language (2.0.5) | |
| 141 | 142 | i18n (0.7.0) |
| 142 | 143 | jbuilder (2.2.12) |
| 143 | 144 | activesupport (>= 3.0.0, < 5) |
| ... | ... | @@ -318,6 +319,7 @@ DEPENDENCIES |
| 318 | 319 | exception_notification (~> 4.0.1) |
| 319 | 320 | factory_girl_rails (~> 4.5.0) |
| 320 | 321 | google-analytics-rails (~> 0.0.6) |
| 322 | + http_accept_language | |
| 321 | 323 | jbuilder (~> 2.2.2) |
| 322 | 324 | jquery-rails |
| 323 | 325 | jquery-ui-rails (~> 5.0.0) | ... | ... |
app/controllers/application_controller.rb
| 1 | +require 'http_accept_language' | |
| 2 | + | |
| 1 | 3 | class ApplicationController < ActionController::Base |
| 2 | 4 | # Prevent CSRF attacks by raising an exception. |
| 3 | 5 | # For APIs, you may want to use :null_session instead. |
| ... | ... | @@ -17,10 +19,9 @@ class ApplicationController < ActionController::Base |
| 17 | 19 | end |
| 18 | 20 | |
| 19 | 21 | def set_locale |
| 20 | - if (params[:locale]) | |
| 21 | - I18n.locale = params[:locale] | |
| 22 | - else | |
| 23 | - I18n.locale = I18n.default_locale | |
| 22 | + compatible_locale = http_accept_language.compatible_language_from(I18n.available_locales) | |
| 23 | + unless compatible_locale.nil? | |
| 24 | + I18n.locale = compatible_locale | |
| 24 | 25 | end |
| 25 | 26 | end |
| 26 | 27 | end | ... | ... |
spec/controllers/home_controller_spec.rb
| ... | ... | @@ -5,11 +5,34 @@ describe HomeController, :type => :controller do |
| 5 | 5 | context '#index' do |
| 6 | 6 | before :each do |
| 7 | 7 | Project.expects(:latest).with(5).returns([]) |
| 8 | + end | |
| 8 | 9 | |
| 9 | - get :index | |
| 10 | + describe 'Rendering' do | |
| 11 | + before :each do | |
| 12 | + get :index | |
| 13 | + end | |
| 14 | + it {is_expected.to render_template(:index)} | |
| 10 | 15 | end |
| 11 | 16 | |
| 12 | - it {is_expected.to render_template(:index)} | |
| 17 | + describe 'Language auto-detection' do | |
| 18 | + it 'should automatically use the language specified in the request headers' do | |
| 19 | + request.env['HTTP_ACCEPT_LANGUAGE'] = 'pt-BR' | |
| 20 | + get :index | |
| 21 | + expect(I18n.locale).to eq(:pt) | |
| 22 | + end | |
| 23 | + | |
| 24 | + it 'should use a different region if still the best match' do | |
| 25 | + request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-GB' | |
| 26 | + get :index | |
| 27 | + expect(I18n.locale).to eq(:en) | |
| 28 | + end | |
| 29 | + | |
| 30 | + it 'should use the default language if no available language matches the requested one' do | |
| 31 | + request.env['HTTP_ACCEPT_LANGUAGE'] = 'de' | |
| 32 | + get :index | |
| 33 | + expect(I18n.locale).to eq(:en) | |
| 34 | + end | |
| 35 | + end | |
| 13 | 36 | end |
| 14 | 37 | end |
| 15 | 38 | end | ... | ... |