From afd386e098c46691cd1766097c1c5f89f40b4eef Mon Sep 17 00:00:00 2001 From: Daniel Miranda Date: Mon, 23 Mar 2015 14:46:28 -0300 Subject: [PATCH] Implement language detection from browser information --- Gemfile | 3 +++ Gemfile.lock | 2 ++ app/controllers/application_controller.rb | 9 +++++---- spec/controllers/home_controller_spec.rb | 27 +++++++++++++++++++++++++-- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Gemfile b/Gemfile index 66456e7..583a468 100644 --- a/Gemfile +++ b/Gemfile @@ -63,6 +63,9 @@ gem 'exception_notification', '~> 4.0.1' # Google Analytics gem 'google-analytics-rails', '~> 0.0.6' +# Browser language detection +gem 'http_accept_language' + group :test do # Easier test writing gem "shoulda-matchers", '~> 2.8.0' diff --git a/Gemfile.lock b/Gemfile.lock index 979db6b..9c2f107 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -138,6 +138,7 @@ GEM activesupport (>= 4.1.0) google-analytics-rails (0.0.6) hike (1.2.3) + http_accept_language (2.0.5) i18n (0.7.0) jbuilder (2.2.12) activesupport (>= 3.0.0, < 5) @@ -318,6 +319,7 @@ DEPENDENCIES exception_notification (~> 4.0.1) factory_girl_rails (~> 4.5.0) google-analytics-rails (~> 0.0.6) + http_accept_language jbuilder (~> 2.2.2) jquery-rails jquery-ui-rails (~> 5.0.0) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4160e67..d53225a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,5 @@ +require 'http_accept_language' + class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. @@ -17,10 +19,9 @@ class ApplicationController < ActionController::Base end def set_locale - if (params[:locale]) - I18n.locale = params[:locale] - else - I18n.locale = I18n.default_locale + compatible_locale = http_accept_language.compatible_language_from(I18n.available_locales) + unless compatible_locale.nil? + I18n.locale = compatible_locale end end end diff --git a/spec/controllers/home_controller_spec.rb b/spec/controllers/home_controller_spec.rb index 46904cb..1447db4 100644 --- a/spec/controllers/home_controller_spec.rb +++ b/spec/controllers/home_controller_spec.rb @@ -5,11 +5,34 @@ describe HomeController, :type => :controller do context '#index' do before :each do Project.expects(:latest).with(5).returns([]) + end - get :index + describe 'Rendering' do + before :each do + get :index + end + it {is_expected.to render_template(:index)} end - it {is_expected.to render_template(:index)} + describe 'Language auto-detection' do + it 'should automatically use the language specified in the request headers' do + request.env['HTTP_ACCEPT_LANGUAGE'] = 'pt-BR' + get :index + expect(I18n.locale).to eq(:pt) + end + + it 'should use a different region if still the best match' do + request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-GB' + get :index + expect(I18n.locale).to eq(:en) + end + + it 'should use the default language if no available language matches the requested one' do + request.env['HTTP_ACCEPT_LANGUAGE'] = 'de' + get :index + expect(I18n.locale).to eq(:en) + end + end end end end -- libgit2 0.21.2