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)
skip_scope = options[:skip_scope]
locales = locales.dup
locales.delete skip_scope
scope("/:locale", :constraints => {:locale => /#{locales.join('|')}/}) do
yield
end
yield if skip_scope
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