Skip to content

Instantly share code, notes, and snippets.

@brunto
Created July 11, 2012 10:11
Show Gist options
  • Save brunto/3089435 to your computer and use it in GitHub Desktop.
Save brunto/3089435 to your computer and use it in GitHub Desktop.
Rails3 locale
In my application.rb :
config.i18n.available_locales = [:fr, :en, :nl, :es]
My routes :
scope "(:locale)" do
root :to => "home#index"
devise_for :users, :controllers => {:registrations => 'registrations'}
get '/home/test' => 'home#test', :as => 'home_test'
end
My app controller :
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :set_locale
private
def set_locale
param_locale = params[:locale]
if param_locale.nil?
request.user_preferred_languages
I18n.locale = request.compatible_language_from(I18n.available_locales)
elsif !param_locale.nil? && I18n.available_locales.include?(:"#{param_locale}")
I18n.locale = param_locale
elsif !param_locale.nil? && !I18n.available_locales.include?(:"#{param_locale}")
redirect_to main_app.root_path, :alert => 'Locale not found'
else
I18n.locale = I18n.default_locale
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment