Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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