I have a Rails app with some internationalization, I have it setting the locale through a url parameter, like this:
/en/post/show/4 for english
/ja/post/show/4 for japaneseMy routes:
ActionController::Routing::Routes.draw do |map|
map.connect "", :controller => "post", :action => "index"
map.connect "/:locale", :controller => "post", :action => "index"
map.connect "post/show/:id/:tag_title", :controller => "post", :action => "show", :requirements => {:id => /\d+/}, :path_prefix => '/:locale'
map.connect ":controller/:action/:id.:format", :requirements => {:id => /[-\d]+/}, :path_prefix => '/:locale'
map.connect ":controller/:action/:id"开发者_Python百科, :requirements => {:id => /[-\d]+/}, :path_prefix => '/:locale'
map.connect ":controller/:action.:format", :path_prefix => '/:locale'
map.connect ":controller/:action", :path_prefix => '/:locale'
end
What I want is to have it so when the locale part of the url is omitted it will use the default locale. So this:
/post/show/4 is the same as
/en/post/show/4and will set the locale to english.
What would be a good way to accomplish this? Thank you!
Find out the current language from the URL and set the I18n.locale
. For example: = I18n.locale = 'en'
See also some great documentation.
I plan to implement the same approach in my app, though I have not tried it yet. I'm going to use optional route segments, like
match '(:locale/)post/show/:id' => 'posts#show'
though I think it only works in Rails 3.
精彩评论