# Localhost:3000/en/x instead of localhost:3000/x?locale=en

**URL:** <https://www.ruby-forum.com/t/localhost-3000-en-x-instead-of-localhost-3000-x-locale-en/187117>\
**Category:** Rails I18n\
**Created:** [April 17, 2010, 12:35pm UTC](https://www.ruby-forum.com/t/localhost-3000-en-x-instead-of-localhost-3000-x-locale-en/187117 "2010-04-17T12:35:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![taranaki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@taranaki](https://www.ruby-forum.com/u/taranaki)\
**Post date:** [April 17, 2010, 12:35pm UTC](https://www.ruby-forum.com/t/localhost-3000-en-x-instead-of-localhost-3000-x-locale-en/187117/1 "2010-04-17T12:35:57Z")

</div>

Hello everyone

At first I have to say that I am quite new to Ruby on Rails.

Now I have encountered the following problem.

My application (which is something similar to a library system and quite  
big) is able to change between German and English. So far I have  
achieved this be adding the locale at the end of the url. I would very  
much like to change this.

Some of my code:  
Here is the whole file: application\_controller.rb:

class ApplicationController \< ActionController::Base  
include AuthenticatedSystem

before\_filter :login\_required, :set\_locale

helper :all # include all helpers, all the time  
protect\_from\_forgery # See ActionController::RequestForgeryProtection  
for details

def readable\_controller\_name  
return controller\_name.gsub(/\_/, ’ ').gsub(/\b\w/){$&.upcase}  
end

# Scrub sensitive parameters from your log

# filter\_parameter\_logging :password

before\_filter :set\_locale  
def set\_locale  
#changed this line, used to be = ‘de’  
I18n.locale = params[:locale]  
end

#new  
def default\_url\_options(options={})  
logger.debug “default\_url\_options is passed options:  
#{options.inspect}\n”  
{ :locale =\> I18n.locale }  
end  
end

And here is the part where the user can change the language by clicking  
on a link: It is part of the application.html.erb:

```
    <div class="language_options">
        <%= link_to "deutsch", "?locale=de", { :title => "deutsch"}

```

%\> |  
\<%= link\_to “english”, “?locale=en”, { :title =\> “english”}  
%\>

So far so good and everything works

I have read heaps about how I could change the url. But I still cannot  
figure out how to do this in a way that I do not have to change  
everything else (we’re a group of people working on this project).

What I have so far is that I could just insert for EVERY page the  
following code in the routes.rb document:

map.resources :catalog, :path\_prefix =\> ‘/:locale’

When I now access the catalog I get:  
[http://localhost:8081/en/catalog](http://localhost:8081/en/catalog) and the page is in English. The problem

- and I know that that’s completely logical - is that when clicking on  
the button I get:  
[http://localhost:8081/en/catalog?locale=de](http://localhost:8081/en/catalog?locale=de) and the page is still in  
English.

So I have the following questions:  
How can I change the links such that they would change the url in the  
correct way? [http://localhost:8081/en/catalog](http://localhost:8081/en/catalog) will be  
[http://localhost:8081/de/catalog](http://localhost:8081/de/catalog)?

Also, is there another way than to add a new route for every page?

And is there anything else I have to take care of regarding the already  
existing code?

I have found the following article in another post in this forum, would  
I have to use one of the presented frameworks?:  
[http://www.artweb-design.de/2007/5/13/concise-localized-rails-url-helpers-solved-twice](http://www.artweb-design.de/2007/5/13/concise-localized-rails-url-helpers-solved-twice)

Thank you very much for your help.

taranaki

---

<div class="post-metadata">

**Author:** ![taranaki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@taranaki](https://www.ruby-forum.com/u/taranaki)\
**Post date:** [April 17, 2010, 1:20pm UTC](https://www.ruby-forum.com/t/localhost-3000-en-x-instead-of-localhost-3000-x-locale-en/187117/2 "2010-04-17T13:20:25Z")

</div>

You can use this plugin [1]. Is from Sven Fuch. one of the developers of  
i18n (I think)

[1] [GitHub - svenfuchs/routing-filter: routing-filter wraps around the complex beast that the Rails routing system is, allowing for unseen flexibility and power in Rails URL recognition and generation.](http://github.com/svenfuchs/routing-filter)

2010/4/17 Sara K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![taranaki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@taranaki](https://www.ruby-forum.com/u/taranaki)\
**Post date:** [April 18, 2010, 7:25pm UTC](https://www.ruby-forum.com/t/localhost-3000-en-x-instead-of-localhost-3000-x-locale-en/187117/3 "2010-04-18T19:25:52Z")

</div>

2010/4/17 Sara K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> ```
> <div class="language_options">
> <%= link_to "deutsch", "?locale=de", { :title => "deutsch"}
> 
> ```
> 
> %\> |  
> \<%= link\_to “english”, “?locale=en”, { :title =\> “english”}  
> %\>

If you pass paremeters as string, they are not recognized by Rails  
router. You have to pass them as regular hash, e.g.

\<%= link\_to “deutsch”, {:locale =\> :de}, { :title =\> “deutsch”} %\>

And it will solve your problem.

Regards,  
KK
