Commit afd386e098c46691cd1766097c1c5f89f40b4eef

Authored by Daniel
Committed by Rafael Manzo
1 parent 5a1ca97e

Implement language detection from browser information

Signed-off-by: Rafael Reggiani Manzo <rr.manzo@gmail.com>
@@ -63,6 +63,9 @@ gem &#39;exception_notification&#39;, &#39;~&gt; 4.0.1&#39; @@ -63,6 +63,9 @@ gem &#39;exception_notification&#39;, &#39;~&gt; 4.0.1&#39;
63 # Google Analytics 63 # Google Analytics
64 gem 'google-analytics-rails', '~> 0.0.6' 64 gem 'google-analytics-rails', '~> 0.0.6'
65 65
  66 +# Browser language detection
  67 +gem 'http_accept_language'
  68 +
66 group :test do 69 group :test do
67 # Easier test writing 70 # Easier test writing
68 gem "shoulda-matchers", '~> 2.8.0' 71 gem "shoulda-matchers", '~> 2.8.0'
@@ -138,6 +138,7 @@ GEM @@ -138,6 +138,7 @@ GEM
138 activesupport (>= 4.1.0) 138 activesupport (>= 4.1.0)
139 google-analytics-rails (0.0.6) 139 google-analytics-rails (0.0.6)
140 hike (1.2.3) 140 hike (1.2.3)
  141 + http_accept_language (2.0.5)
141 i18n (0.7.0) 142 i18n (0.7.0)
142 jbuilder (2.2.12) 143 jbuilder (2.2.12)
143 activesupport (>= 3.0.0, < 5) 144 activesupport (>= 3.0.0, < 5)
@@ -318,6 +319,7 @@ DEPENDENCIES @@ -318,6 +319,7 @@ DEPENDENCIES
318 exception_notification (~> 4.0.1) 319 exception_notification (~> 4.0.1)
319 factory_girl_rails (~> 4.5.0) 320 factory_girl_rails (~> 4.5.0)
320 google-analytics-rails (~> 0.0.6) 321 google-analytics-rails (~> 0.0.6)
  322 + http_accept_language
321 jbuilder (~> 2.2.2) 323 jbuilder (~> 2.2.2)
322 jquery-rails 324 jquery-rails
323 jquery-ui-rails (~> 5.0.0) 325 jquery-ui-rails (~> 5.0.0)
app/controllers/application_controller.rb
  1 +require 'http_accept_language'
  2 +
1 class ApplicationController < ActionController::Base 3 class ApplicationController < ActionController::Base
2 # Prevent CSRF attacks by raising an exception. 4 # Prevent CSRF attacks by raising an exception.
3 # For APIs, you may want to use :null_session instead. 5 # For APIs, you may want to use :null_session instead.
@@ -17,10 +19,9 @@ class ApplicationController &lt; ActionController::Base @@ -17,10 +19,9 @@ class ApplicationController &lt; ActionController::Base
17 end 19 end
18 20
19 def set_locale 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 end 25 end
25 end 26 end
26 end 27 end
spec/controllers/home_controller_spec.rb
@@ -5,11 +5,34 @@ describe HomeController, :type =&gt; :controller do @@ -5,11 +5,34 @@ describe HomeController, :type =&gt; :controller do
5 context '#index' do 5 context '#index' do
6 before :each do 6 before :each do
7 Project.expects(:latest).with(5).returns([]) 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 end 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 end 36 end
14 end 37 end
15 end 38 end