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

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 and the page is in English. The problem

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 will be
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

Thank you very much for your help.

taranaki

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

[1] http://github.com/svenfuchs/routing-filter

2010/4/17 Sara K. [email protected]

2010/4/17 Sara K. [email protected]

   <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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs