Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 759495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:36:13+00:00 2026-05-14T15:36:13+00:00

I am adding I18N to my rails application by passing the locale using url

  • 0

I am adding I18N to my rails application by passing the locale using url params. My urls are looking like http://example.com/en/users and http://example.com/ar/users (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]
end

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}
end

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:36:13+00:00Added an answer on May 14, 2026 at 3:36 pm

    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: http://api.rubyonrails.org/classes/ActionController/Base.html#M000649

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm adding i18n to an existing project (web application). This involves replacing every bit
When adding internationalisation capabilities to an Oracle web application (build on mod_plsql), I'd like
I'm looking at adding scripting functionality to an existing codebase and am weighing up
I have a monolingual GWT application (English) and I would like to use localized
Adding an element to the head of an alist (Associative list) is simple enough:
Adding source files more than one directory away (e.g. ../../source.cpp or ../../../somewhere_else/source.cpp, vs. just
When adding new properties to classes, I find myself typing the same things over
When adding a DLL as a reference to an ASP.Net project, VS2008 adds several
When adding an EditItemTemplate of some complexity (mulitple fields in one template), and then
When adding a reference to a project I sometimes see the message... The Target

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.