Devise and i18n internationalization and Routes issue

249 views
Skip to first unread message

Alvaro

unread,
Mar 21, 2011, 12:58:44 PM3/21/11
to Devise
Hi guys. I am developing a web page in rails 3 with devise
authentication and internationalization with i18n gem.

I am having some problems mixing all together. The first thing I have
is my application layout where I render a header partial. In this
header I have two links to translate my page in this way (I use Haml
for the views)

= link_to image_tag('spanish.png', :height => "30px", :width =>
"30px", :class => "flag", :alt => "Español")

= link_to image_tag('english.png', :height => "30px", :width =>
"30px", :class => "flag", :alt => "English")

In my application_controller I have this

before_filter :set_locale

def set_locale

I18n.locale = request.compatible_language_from LANGUAGE_CODES

end

compatible_language_from is an action from http_accept_language gem
and LANGUAGE_CODES is a constant with the available language codes
This is working perfectly. Depending on my browser
HTTP_ACCEPT_LANGUAGE header the root page is translated in english or
spanish.

Now, I have in my routes.rb the following code

#Authentication routes definitions

devise_for :users

#Choosing custom paths for user sign_in

devise_scope :user do

get "/", :to => "devise/sessions#new"
end

root :to => "welcome#index"

Doing this "get "/" in the scope of devise I get browsing to
http://localhost:3000 with the application layout and its header and
devise stuff with no need to browse to users/sign_in etc.

I added this to the link_to tag

, :action => "update_locale"

so that when I click on "Spanish or English" the "update_locale"
action fires to update the locale but I am getting this error:

ActionController::RoutingError in Devise/sessions#new

Showing /Users/alvaro/webApps/myFirstApp/app/views/shared/
_header.html.haml where line #15 raised:

No route matches {:controller=>"devise/
sessions", :action=>"update_locale"}

Has anyone an idea on how to fix this? Maybe what is wrong is
routes.rb? My brain is getting boiled!!

Thank you very much

José Valim

unread,
Mar 21, 2011, 1:16:34 PM3/21/11
to Devise
Try using a named route or explicitly passing the controller to
link_to instead of just :action => "update_locale". Make sure
your :controller starts with "/", otherwise it will use a controller
relative to the current namespace (which is Devise).
> Doing this "get "/" in the scope of devise I get browsing tohttp://localhost:3000with the application layout and its header and

Alvaro

unread,
Mar 21, 2011, 3:00:30 PM3/21/11
to Devise
Thank you very much for your quick answer
> > Doing this "get "/" in the scope of devise I get browsing tohttp://localhost:3000withthe application layout and its header and

José Valim

unread,
Mar 21, 2011, 5:26:24 PM3/21/11
to Devise
Did it work? :D
> > > Doing this "get "/" in the scope of devise I get browsing tohttp://localhost:3000withtheapplication layout and its header and

Alvaro

unread,
Mar 22, 2011, 1:53:53 PM3/22/11
to Devise
With the slash did work but I am trying to find out a way of passing
de locale not as a parameter. I mean, I don't want my urls look like
http://localhost/?locale=es. I'd like to have in the the link_to the
info of the language I am selecting and then refresh the page and set
I18n.locale to the desired language so that all the devise links,
labels and links be translated. Maybe a cookie is a good solution?
Maybe Javascript? AJAX?

I don't know if I am explaining well.
Thank you very much again
> > > > Doing this "get "/" in the scope of devise I get browsing tohttp://localhost:3000withtheapplicationlayout and its header and

byrnejb

unread,
Mar 23, 2011, 4:01:22 PM3/23/11
to Devise


On Mar 22, 1:53 pm, Alvaro <alvaro.igna...@gmail.com> wrote:
> With the slash did work but I am trying to find out a way of passing
> de locale not as a parameter. I mean, I don't want my urls look likehttp://localhost/?locale=es.  I'd like to have in the the link_to the
> info of the language I am selecting and then refresh the page and set
> I18n.locale to the desired language so that all the devise links,
> labels and links be translated. Maybe a cookie is a good solution?
> Maybe Javascript? AJAX?

How about doing it the Rails way? Take a look at http://guides.rubyonrails.org/i18n.html.

José Valim

unread,
Mar 23, 2011, 4:08:29 PM3/23/11
to Devise
Don't use cookies. The locale should be in the url. You can scope all
your urls to a locale like this:

scope "(/:locale)" do

end

Then, you could have a filter in your controller that checks if a
locale was given in the url, and, if not, redirect to the proper one
detected.

On Mar 22, 6:53 pm, Alvaro <alvaro.igna...@gmail.com> wrote:
> With the slash did work but I am trying to find out a way of passing
> de locale not as a parameter. I mean, I don't want my urls look likehttp://localhost/?locale=es.  I'd like to have in the the link_to the
> > > > > Doing this "get "/" in the scope of devise I get browsing tohttp://localhost:3000withtheapplicationlayoutand its header and

Alvaro

unread,
Mar 24, 2011, 12:38:58 PM3/24/11
to Devise
Thanks for all yor answers.

Finally I decided to take locale from subdomain. I wrote the code so
that an user could take www.myapp.com with locale from
HTTP_ACCEPT_LANGUAGE header or en.myapp.com, es.myapp.com etc.

I followed your advise of not using cookies.
The problem comes now with this code in my routes.rb

devise_for :users do
get "/", :to => "devise/sessions#new"
end

root :to => "welcome#index"

As you can see, I want devise fields and links appear when the user is
in the root page, for example www.myapp.com with no need to browse to
www.myapp.com/users/sign_in

But when the user does log in I get this error from firefox:

The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for
this address in a way that will never complete.

* This problem can sometimes be caused by disabling or refusing
to accept
cookies.

Am I doing well this?
devise_for :users do
get "/", :to => "devise/sessions#new"
end

maybe I should recode this and use a scope or something like that

Thank you very much once again
> > > > > > Doing this "get "/" in the scope of devise I get browsing tohttp://localhost:3000withtheapplicationlayoutandits header and
Reply all
Reply to author
Forward
0 new messages