2011-03-22 42 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 
相关问题