2011-03-22 41 views
3

我將i18n添加到我的網頁(針對不同語言的不同內容)。我的網址看看魔神這樣的:Rails3:所有url_for URLS的前綴

scope "/:language/", :language => /de|en/ do 
    match "news/:news_id(/:title)" => "news#show_entry", :constraints => { :news_id => /[0-9]+/ } 
    ... 
end 

我:

http://host.tld/de/news/15 
http://host.tld/en/news/15 
... 

所有應用程序中的我的網址是由link_to/url_for方法這樣

url_for("/news/#{news.id}/#{urlify(news.title)}") 
url_for("/news/#{@news.section}") 
... 

我的路由看起來是這樣的設置添加到我的ApplicationController:

def default_url_options(options={}) 
    {:language => I18n.locale} 
end 

現在,我想將語言前綴添加到所有網址,而無需更改所有url_for() - 調用。有沒有解決方案(參數/配置選項或其他)來添加此前綴?它也應該與相對路徑一起工作。

回答

0

如果您正在尋找不改變所有的URL進行通話,你可以在application_helper.rb文件添加一個方法來重寫現有的方法,在語言添加

def url_for(options={}) 
    if options[:language] 
    '/options[:language]' + super 
    else 
    super 
    end 
end 

def link_tolink_to(*args, &block) 
    options = args[1] || {} 
    if options[:language] 
    '/options[:language]' + super 
    else 
    super 
    end 
end 
相關問題