I am adding I18N to my rails application by passing the locale using url params. My urls are looking like and (for the english and arabic locales respectively).

In my routes file, I have defined my routes with a :path_prefix option:

map.resources :users, :path_prefix => '/:locale'

And locale is being set using a before_filter defined in ApplicationController

def set_locale
    I18n.locale = params[:locale]

I also defined ApplicationController#default_url_options, to add locale to all urls generated by the application:

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

What I want is to add a link in the layout header (displayed in all pages) that would link to the same page but with the other locale.

For instance, if I am browsing the arabic locale, I want a "English" link in the header, that will redirect me back to my current page, and set the locale to english. Is there a way to do this in rails?

Was it helpful?


Took me a while to find this but here is my solution:

link_to 'English', url_for( :locale => 'en' )
link_to 'Deutch', url_for( :locale => 'de' ) 

From the docs here:

When generating a new URL, missing values may be filled in from the current request‘s parameters. For example, url_for :action => ‘some_action‘ will retain the current controller, as expected. This behavior extends to other parameters, including :controller, :id, and any other parameters that are placed into a Route‘s path.

So using url_for will default to the current request's parameters, just change the one's you want in your code. In this case all I changed was :locale, so everything else stays the same.

Note this also works for "hidden" :parameters. So if you have:

map.my_map ':locale/my_map', :controller => 'home', :action => 'my_map'

using the above url_for in the page /en/my_map will not have 'home' in the url (ie /en/home/my_map). Bonus.


So I found a way to more explicitly do this with out relying on (as much) rails magic.

url_for(params.merge({:your_new_parameter => value}))

This should work in any link_to.

All its doing is taking the current request's parameters and merging your new desired hash into them and then creating a new url for that.

Link to current page with different locales

Tested on Rails 4

Hello all. After some time of research I decide to write my own solution for this.

link_to 'English', url_for( :locale => 'en' )
link_to 'Deutch', url_for( :locale => 'de' ) 

This works perfect, but it allows XSS Vulnerability just passing parameters in your URL like below:


Or worst case:


Check out what URLs you will get after going through this link in your application.

My production solution. Method "change language" redirects to any page with proper locale just using HTTP_REFERER in request object. Please note: URI.path method for get only path, not whole url

Make "change language" method in any controller:

        def change_lang

        if request.referer.nil?
                 refer = root_url
                 uri = URI(request.referer)
                 refer = uri.path
        lang = params[:lang]
        cookies[:locale] = lang
        redirect_to refer



before_action :set_locale

def set_locale

# -- Get lang from cookies or url parameter locale

user_locale = cookies[:locale] || params[:locale]

# -- If present

if user_locale.present? 

    # -- If it is has 2 symbols

    user_locale = user_locale.scan(/[a-zA-Z]{2}/) 

    # -- If no - use default en locale

    user_locale = 'en'

# -- Check, is this locale available for using.
# Please note: this needed for disable invalid locale warning.

if I18n.available_locales.include?(user_locale[0].to_sym)

    I18n.locale =  user_locale[0]
    I18n.locale =   "en"


add this to your layout

<%= link_to 'English', change_lang_path('en') %> <%= link_to 'Russian', change_lang_path('ru') %>


scope "(:locale)", locale: /[a-zA-Z]{2}/ do
get "change_lang/:lang" => "users#change_lang", :as => "change_lang"

There is no need to use params.merge or any monkey-patch solution.

I hope this helps, because I personally spent a lot of time to solve it.

You can parse request_uri, and replace your locale in the path with regular expression

Ok, here is helper example. If I correctly understand the goal

def locale_url(url, locale)
  url.gsub(/\/\w*$/, "/#{locale}")

url = "" # or request.request_uri
locale = "en"
locale_url(url, locale) #=> ""

This is a start point, so you can make some different stuff that you need

Have a look at this, though it may not be DRY and proper one, but works perfectly for me. It reads all the parameters you supplied replacing only the locale EX urls : etc

  def us_link           
        link_to "US", form_locale_url("/us")            

  def jp_link           
    link_to "Japan",form_locale_url("/jp")           

  def form_locale_url(locale)            
    new_url = request.request_uri          
    new_locale_url = new_us_url = new_jp_url = new_url           
    if new_url == "/"          
    elsif (new_url =~/\/us/) == 0        
    elsif (new_url =~/\/jp/) == 0          

A much quicker avenue - and convenient if you have many parameters that change in different places... avoid the clutter with an anchor tag that just merges the new locale param to the existing ones (and actually killing the old locale param).

<%= link_to "ру", request.params.merge( locale: 'ru' ) %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow