Skip to content

Instantly share code, notes, and snippets.

@tokland
Created September 20, 2011 13:52
Show Gist options
  • Save tokland/1229107 to your computer and use it in GitHub Desktop.
Save tokland/1229107 to your computer and use it in GitHub Desktop.
How to add locale scope to i18n_routing
class ActionDispatch::Routing::Mapper
def localize_and_scope_for(locales, options = {}, &block)
scoped_locales = locales - Array(options[:skip_scope])
localized(locales) do
locale_regexp = Regexp.new(scoped_locales.join('|'))
scope("/:i18n_locale", :constraints => {:i18n_locale => locale_regexp}) do
yield
end
yield if options[:skip_scope]
end
end
end
MyApp::Application.routes.draw do
localize_and_scope_for(I18n.available_locales, :skip_scope => I18n.default_locale) do
# Your routes here.
# Example:
#
# resources :pages
#
# You will have no scope for the default locale, i.e. '/pages/new'
# but all other locales will create a scope, i.e. '/es/paginas/nueva'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment