我将 i18n 添加到我的网页(不同语言的不同内容)。我的 URL 看起来像这样:
http://host.tld/de/news/15
http://host.tld/en/news/15
...
我在应用程序中的所有 URL 都是由 link_to
/url_for
方法设置的,如下所示
url_for("/news/#{news.id}/#{urlify(news.title)}")
url_for("/news/#{@news.section}")
...
我的路线是这样的:
scope "/:language/", :language => /de|en/ do
match "news/:news_id(/:title)" => "news#show_entry", :constraints => { :news_id => /[0-9]+/ }
...
end
我将其添加到我的 ApplicationController 中:
def default_url_options(options={})
{:language => I18n.locale}
end
现在我想在不更改所有 url_for() 调用的情况下将语言前缀添加到所有 URL。是否有添加此前缀的解决方案(参数/配置选项或其他)?它也应该适用于相对路径。
最佳答案
如果您不想更改所有调用的 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
关于ruby-on-rails-3 - Rails3 : Prefix to all url_for URLS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5393795/