Skip to content

Instantly share code, notes, and snippets.

@dimmg
Last active January 23, 2017 14:17
Show Gist options
  • Save dimmg/a837d49749ea01c2ba3401214c6033c9 to your computer and use it in GitHub Desktop.
Save dimmg/a837d49749ea01c2ba3401214c6033c9 to your computer and use it in GitHub Desktop.
rails localization setup
class ApplicationController < ActionController::Base
before_filter :set_locale
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
UNLOCALIZED_NAMESPACES = %w(admin)
def set_locale
I18n.locale = params[:locale]
end
def default_url_options(options={})
if is_url_localized?
_locale = I18n.locale
else
_locale = nil
end
{locale: _locale}
end
def is_url_localized?
!UNLOCALIZED_NAMESPACES.any? {
|namespace| params[:controller].include? namespace
}
end
end
Rails.application.routes.draw do
namespace :admin do
resources :controller
end
# localized routes
scope ':locale', locale: /#{I18n.available_locales.join('|')}/ do
resources :controller
get 'url', to: 'controller#action', as: action
end
get '', to: redirect("/#{I18n.default_locale}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment