Last active
January 23, 2017 14:17
-
-
Save dimmg/a837d49749ea01c2ba3401214c6033c9 to your computer and use it in GitHub Desktop.
rails localization setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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