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"}
%>
</div>
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
- and I know that that's completely logical - is that when clicking on
the button I get:
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 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-loca...
Thank you very much for your help.
taranaki
on 2010-04-17 12:35
on 2010-04-17 13:20
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 Kilcher <lists@ruby-forum.com>
on 2010-04-18 19:25
2010/4/17 Sara Kilcher <lists@ruby-forum.com> > <div class="language_options"> > <%= link_to "deutsch", "?locale=de", { :title => "deutsch"} > %> | > <%= link_to "english", "?locale=en", { :title => "english"} > %> > </div> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.