Rails3: prefix for all url_for URLs

I am adding i18n to my web page (different content for different languages). My urls are as follows:

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

All my URLs in the application are set using the link_to/ method url_forlike this

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

My routing is as follows:

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

I add this to my ApplicationController:

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

Now I want to add a language prefix to all URLs without changing all url_for () calls. Is there a solution (/ config-option option or something else) to add this prefix? It should also work with relative paths.

+3
source share
1 answer

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
0

All Articles