2010-12-18 39 views
1

我有一些國際化,我把它設置地方通過URL參數,像這樣的Rails應用程序:在Ruby on Rails中,如何從url中省略默認語言環境?

/EN /後/顯示/ 4的英語
/JA /後/顯示/ 4日本

我的路線:

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", :requirements => {:id => /[-\d]+/}, :path_prefix => '/:locale' 
    map.connect ":controller/:action.:format", :path_prefix => '/:locale' 
    map.connect ":controller/:action", :path_prefix => '/:locale' 
end 

我想是有它因此省略URL的區域一部分時,它會使用默認的語言環境。所以這個:

/後/顯示/ 4是一樣的
/EN /後/顯示/ 4

,將區域設置爲英語。

什麼是一個很好的方法來實現這一目標?謝謝!

回答

1

從URL中找出當前語言並設置I18n.locale。例如:=

另請參閱一些great documentation

+0

我所試圖做的是使其工作時,有沒有從URL發送語言。 – 2010-12-19 07:03:58

1

我打算在我的應用中實現相同的方法,但我還沒有嘗試過。我將使用可選的線路段,像

match '(:locale/)post/show/:id' => 'posts#show' 

雖然我認爲它只能在Rails的3

+0

這看起來不錯,我在2.3.5上,我不認爲我的客戶熱衷於升級。我會看看是否有類似的選擇。 – 2010-12-19 17:11:27

相關問題