Skip to content

Commit

Permalink
Merge pull request #2 from aviscasillas/detect-client-language
Browse files Browse the repository at this point in the history
Detect client language
  • Loading branch information
aviscasillas committed Nov 9, 2013
2 parents 36075e6 + e354d69 commit 315116a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ gem 'mongoid', git: 'git://github.com/mongoid/mongoid.git'

# Use slim template creation instead ERB
gem 'slim-rails'

# Client language
gem 'http_accept_language'
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ GEM
erubis (2.7.0)
execjs (2.0.2)
hike (1.2.3)
http_accept_language (2.0.0)
i18n (0.6.5)
jbuilder (1.5.2)
activesupport (>= 3.0.0)
Expand Down Expand Up @@ -145,6 +146,7 @@ PLATFORMS

DEPENDENCIES
coffee-rails (~> 4.0.0)
http_accept_language
jbuilder (~> 1.2)
jquery-rails
mongoid!
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception

before_filter :set_locale

private

def set_locale
I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales)
end
end
2 changes: 1 addition & 1 deletion app/controllers/excuses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ def index
end

def rnd
@msg = Excuse.find_rnd.msg['es']
@msg = Excuse.find_rnd.msg[I18n.locale.to_s]
end
end
2 changes: 2 additions & 0 deletions config/locales/ca.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ca:
hello: "Hola Mon"
2 changes: 2 additions & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
es:
hello: "Hola Mundo"
15 changes: 12 additions & 3 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Excuse.all.each do |e|
e.remove
end
Excuse.create(msg: { es: 'Hoy hace mucha humedad' } )
Excuse.create(msg: { es: 'Esta roca se desace mucho' } )
Excuse.create(msg: { es: 'Si fuera un poco mas alto...' } )
Excuse.create(msg: {
en: 'Today is too wet',
es: 'Hoy hace mucha humedad'
})
Excuse.create(msg: {
en: 'This rock is too soft',
es: 'Esta roca se desace mucho'
})
Excuse.create(msg: {
en: 'If I would be higher...',
es: 'Si fuera un poco mas alto...'
})

0 comments on commit 315116a

Please sign in to comment.