Created
September 20, 2011 13:52
-
-
Save tokland/1229107 to your computer and use it in GitHub Desktop.
How to add locale scope to i18n_routing
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 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 |
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
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