Rails : Generate local specific version of current page

If you are about to make your application locale specific then, you must come across with a problem of how you can get locale specific version of the currently hit page; so that your visitor can switch to different language versions of the same page.

There is a helper method called url_for. This method is used by every URLHelper in Rails internally [FYI].

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

For generating a new URL, the missing values may be filled in from the current request‘s parameters. Like, 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, in our case, you are only modifying/setting locale so, the other attributes are default to current request’s attributes.

But, problem with this approach is, I am getting the non-RESTful versions of URL. like

# but I am expecting

Solution: I did not do anything; formerly the problem was there, but not the problem is gone. May be the application was needing some time to reload the updated code. So, its better to restart the application.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s