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

  • SEARCH
  • Home
  • 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 am trying to translate my Rails 3 application, read the primer at http://guides.rubyonrails.org/i18n.html#adding-date-time-formats
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
By adding a custom handler to the SiteMapResolve event, I can update sitemap url's
I have a monolingual GWT application (English) and I would like to use localized
Adding contents to listview is a simple proceess like ListViewItem item = new ListViewItem();
Adding files to a specific directory using rackspace cloud files, c# bindings api more
Adding expected types parameter changes response result in unexpected way. Request 1: https://maps.googleapis.com/maps/api/place/search/json?location=38.4551,-122.672045&radius=100&sensor=false&key= Request
I have an engine, with this routes file: Rails.application.routes.draw do resources :comments, :controller =>
Adding 'index_file' => FALSE to the Kohana::init successfully removes the index.php part of URLs,

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.