prefixing paths

32 views
Skip to first unread message

Steven Garcia

unread,
Jun 26, 2010, 1:08:35 PM6/26/10
to I18n Routing
nice gem!

what if i want a url prefix though?

/es/proyecto
/en/project
/de/projekt

Is there any way to plug that in? If not I could see it fitting here:

map.localized(I18n.available_locales, :verbose => true, :namespace =>
i18n.locale)

Steven Garcia

unread,
Jun 27, 2010, 6:44:00 AM6/27/10
to I18n Routing
Got it!

map.localized(I18n.available_locales, :verbose => true) do
map.with_options(:path_prefix => ":locale") do |m|
m.welcome 'welcome', :controller => 'pages', :action => 'welcome'
end
end

Kwi

unread,
Jun 29, 2010, 9:17:31 PM6/29/10
to I18n Routing
Hi Steven,

Sorry I'm very late on the answers :/ But you finally find the
solution yourself ;) Congrats !
And your solution sounds just good :)

José

unread,
Aug 4, 2010, 12:04:54 PM8/4/10
to I18n Routing
You should use: scope '(:locale)' do [...] end, as path_prefix is
deprecated (http://www.ruby-forum.com/topic/205411)

However, these solutions also enable the following routes:

/es/proyecto
/es/project
/es/proyekt
/en/project
/en/proyecto
/en/projekt
/de/projekt
/de/project
/de/proyecto

Problems may arise if a word is shared by two locales but with
different routes. I can't think of an example now, but I don't think
this is good. Is there a way to avoid this?

hydrozen

unread,
Oct 1, 2010, 11:52:02 PM10/1/10
to I18n Routing
I tried this on a test app on my side and it does not really seem to
work. Are we sure the gem supports the scope construct?
I just created a little test application and I does not seem to work.

My routes.rb looks like this:

localized do
scope (":locale") do
resource :home
end
end

and rake routes returns this:

en_home POST /:locale/home(.:format)
{:i18n_locale=>"en", :action=>"create", :controller=>"homes"}
new_en_home GET /:locale/home/new(.:format)
{:i18n_locale=>"en", :action=>"new", :controller=>"homes"}
edit_en_home GET /:locale/home/edit(.:format)
{:i18n_locale=>"en", :action=>"edit", :controller=>"homes"}
en_home GET /:locale/home(.:format)
{:i18n_locale=>"en", :action=>"show", :controller=>"homes"}
en_home PUT /:locale/home(.:format)
{:i18n_locale=>"en", :action=>"update", :controller=>"homes"}
en_home DELETE /:locale/home(.:format)
{:i18n_locale=>"en", :action=>"destroy", :controller=>"homes"}
fr_home POST /:locale/accueil(.:format)
{:i18n_locale=>"fr", :action=>"create", :controller=>"homes"}
new_fr_home GET /:locale/accueil/new(.:format)
{:i18n_locale=>"fr", :action=>"new", :controller=>"homes"}
edit_fr_home GET /:locale/accueil/edit(.:format)
{:i18n_locale=>"fr", :action=>"edit", :controller=>"homes"}
fr_home GET /:locale/accueil(.:format)
{:i18n_locale=>"fr", :action=>"show", :controller=>"homes"}
fr_home PUT /:locale/accueil(.:format)
{:i18n_locale=>"fr", :action=>"update", :controller=>"homes"}
fr_home DELETE /:locale/accueil(.:format)
{:i18n_locale=>"fr", :action=>"destroy", :controller=>"homes"}
home POST /:locale/home(.:format)
{:action=>"create", :controller=>"homes"}
new_home GET /:locale/home/new(.:format)
{:action=>"new", :controller=>"homes"}
edit_home GET /:locale/home/edit(.:format)
{:action=>"edit", :controller=>"homes"}
home GET /:locale/home(.:format)
{:action=>"show", :controller=>"homes"}
home PUT /:locale/home(.:format)
{:action=>"update", :controller=>"homes"}
home DELETE /:locale/home(.:format)
{:action=>"destroy", :controller=>"homes"}

and yet if I try to access /fr/accueil on my server, I get his:

Routing Error
No route matches "/fr/accueil"

any ideas what I might be doing wrong?

Kwi

unread,
Oct 5, 2010, 1:13:01 PM10/5/10
to I18n Routing
Hi,

Sorry to answer late, but this is kind of strange cause everything
looks good on rake routes, can tou try ty specify the languages in
your localized do, like

localized(['fr', 'en']) do

hydrozen

unread,
Oct 8, 2010, 6:32:18 PM10/8/10
to I18n Routing
So passing in localized([:en, :fr]) seems to work... but passing in
localized(I18n.available_locales) doesn't.
Also, why does it generate 3 sets of routes? one for en, one for fr,
and one without locale. Shouldn't it just generate the 2 routes with
locales?

here's the output of rake routes

en_home POST /:locale/blah(.:format)
{:action=>"create", :controller=>"homes", :i18n_locale=>"en"}
new_en_home GET /:locale/blah/new(.:format)
{:action=>"new", :controller=>"homes", :i18n_locale=>"en"}
edit_en_home GET /:locale/blah/edit(.:format)
{:action=>"edit", :controller=>"homes", :i18n_locale=>"en"}
en_home GET /:locale/blah(.:format)
{:action=>"show", :controller=>"homes", :i18n_locale=>"en"}
en_home PUT /:locale/blah(.:format)
{:action=>"update", :controller=>"homes", :i18n_locale=>"en"}
en_home DELETE /:locale/blah(.:format)
{:action=>"destroy", :controller=>"homes", :i18n_locale=>"en"}
fr_home POST /:locale/accueil(.:format)
{:action=>"create", :controller=>"homes", :i18n_locale=>"fr"}
new_fr_home GET /:locale/accueil/new(.:format)
{:action=>"new", :controller=>"homes", :i18n_locale=>"fr"}
edit_fr_home GET /:locale/accueil/edit(.:format)
{:action=>"edit", :controller=>"homes", :i18n_locale=>"fr"}
fr_home GET /:locale/accueil(.:format)
{:action=>"show", :controller=>"homes", :i18n_locale=>"fr"}
fr_home PUT /:locale/accueil(.:format)
{:action=>"update", :controller=>"homes", :i18n_locale=>"fr"}
fr_home DELETE /:locale/accueil(.:format)
{:action=>"destroy", :controller=>"homes", :i18n_locale=>"fr"}
home POST /:locale/home(.:format)
{:action=>"create", :controller=>"homes"}
new_home GET /:locale/home/new(.:format)
{:action=>"new", :controller=>"homes"}
edit_home GET /:locale/home/edit(.:format)
{:action=>"edit", :controller=>"homes"}
home GET /:locale/home(.:format)
{:action=>"show", :controller=>"homes"}
home PUT /:locale/home(.:format)
{:action=>"update", :controller=>"homes"}
home DELETE /:locale/home(.:format)
{:action=>"destroy", :controller=>"homes"}



Guillaume Luccisano

unread,
Oct 11, 2010, 12:25:48 PM10/11/10
to i18n-r...@googlegroups.com
Hi Hydrozen

Glad to know you finally get everything working, if your I18n.available_locales does not work, it's because i18n is not loaded at that time, try to require it first.
Then for the 3 routes instead of 2, yes this is normal, just to allow you to still use old url, or yet untranslated urls.

Cheers,
Guillaume

2010/10/8 hydrozen <hydr...@gmail.com>
Reply all
Reply to author
Forward
0 new messages