Posts

Showing posts with the label url routes

A Simple Way to Provide Optional Locale Scope in Url

Putting the locale key in the url is a common method to pass in the I18n locale,  see Railscast: http://railscasts.com/episodes/138-i18n-revised But how do you make the default locale not show up in the url? i.e. Instead of http://www.example.com/en/some_path (for default :en locale) I want: http://www.example.com/some_path (for default :en locale)         & http://www.example.com/cn/some_path (for :cn locale) ---- Without using gems, there is a simple way to do this, simply by changing a few lines in the application controller and routes file. (The following method assumes you followed Railscast's method of setting up the locale scope) In the routes.rb file, make the locale scope optional: 1 2 3 4 5 6 7 Rails . application . routes . draw do scope "(:locale)" , locale : /en|cn/ do #your paths here end match '*path' , to : redirect( "/ #{ I18n . default_locale } /%{path}" ), constraints : lambda { | req | ! re...