Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

url_for and generate

30 views
Skip to first unread message

richard

unread,
Nov 29, 2010, 6:36:52 PM11/29/10
to I18n Routing
Hello,

I was able to get the plugin to work for named paths but does this
plugin work for url_for and generate methods? When I call those
methods, I do not get the translated urls I would expect; I get the
original untranslated urls.

Thank you,
Richard

Guillaume Luccisano

unread,
Nov 29, 2010, 8:45:34 PM11/29/10
to i18n-r...@googlegroups.com
Hi Richard,

Yes, it should work, that's the purpose of the gem :)
Which version of Rails are you using ?
You can print your routes with rake routes to see if you routes are correctly translated.

Thanks

2010/11/29 richard <ran13...@gmail.com>

richard

unread,
Dec 1, 2010, 1:57:20 AM12/1/10
to I18n Routing
Hi Guillaume,

Thanks for your quick reply, I was travelling back from Thanksgiving
and am just getting back to emails.

I am using Rails 2.3.2 and I installed the plugin (as opposed to the
gem) to avoid the I18n gem issue.

rake routes shows the translated routes
zh_cn_view_product /:productname/網
店/:id
{:controller=>"products", :action=>"view_product"}
view_product /:productname/
stores/:id
{:controller=>"products", :action=>"view_product"}

And console data shows how the named path is working but url_for is
not

ruby-1.8.6-p420 > symbolhash = {}
=> {}
ruby-1.8.6-p420 > {"action"=>"view_product", "id"=>"7",
"controller"=>"products", "productname"=>"panduit-10-14-ring-terminal-
rohs"}.each { |k,v| symbolhash[k.intern] = v}
=> {"action"=>"view_product", "id"=>"7", "controller"=>"products",
"productname"=>"panduit-10-14-ring-terminal-rohs"}
ruby-1.8.6-p420 > app.url_for(symbolhash)
=> "http://www.example.com/panduit-10-14-ring-terminal-rohs/stores/
7"
ruby-1.8.6-p420 > app.view_product_path(symbolhash)
=> "/panduit-10-14-ring-terminal-rohs/stores/7"
ruby-1.8.6-p420 > I18n.locale
=> :en
ruby-1.8.6-p420 > I18n.locale = :zh_cn
=> :zh_cn
ruby-1.8.6-p420 > app.url_for(symbolhash)
=> "http://www.example.com/panduit-10-14-ring-terminal-rohs/stores/
7"
ruby-1.8.6-p420 > app.view_product_path(symbolhash)
=> "/panduit-10-14-ring-terminal-rohs/網店/7"

It's very bizarre. Is there an option or argument I can pass to
url_for to force it to use the translated route? Thanks for your help!
Richard


On Nov 29, 5:45 pm, Guillaume Luccisano
<guillaume.luccis...@gmail.com> wrote:
> Hi Richard,
>
> Yes, it should work, that's the purpose of the gem :)
> Which version of Rails are you using ?
> You can print your routes with rake routes to see if you routes are
> correctly translated.
>
> Thanks
>
> 2010/11/29 richard <ran1347...@gmail.com>

Guillaume Luccisano

unread,
Dec 1, 2010, 10:20:13 PM12/1/10
to i18n-r...@googlegroups.com
Hi again Richard,

Thanks for all this explanation, this is strange, url_for should work just as view_product_path
It might be an issue with with locale with - or _ that I need to dig into it.
By the way, using a locale with an underscore is strange, can you try with zh-cn ?

Thanks,
Guillaume

2010/11/30 richard <ran13...@gmail.com>

richard

unread,
Dec 2, 2010, 3:53:14 PM12/2/10
to I18n Routing
Hi Guillaume,

I tried :zh-cn and just :zh and neither worked. I do have a hypothesis
that maybe you could confirm please after I tried to understand the
original Rails code. Calling generate (on the
ActionController:Routing:Routes object) is a call at run time, but at
run time the generate method does not take into account any locales;
it just tries all the routes for a controller/action combination and
stops when the first one works; and presumably the first one that
works is the original, untranslated route. It it is only at build
time, when Routes object is initialized, that locales are iterated to
create translated named routes.

Thanks again,
Richard


On Dec 1, 7:20 pm, Guillaume Luccisano <guillaume.luccis...@gmail.com>
wrote:
> Hi again Richard,
>
> Thanks for all this explanation, this is strange, url_for should work just
> as view_product_path
> It might be an issue with with locale with - or _ that I need to dig into
> it.
> By the way, using a locale with an underscore is strange, can you try with
> zh-cn ?
>
> Thanks,
> Guillaume
>
> 2010/11/30 richard <ran1347...@gmail.com>

Guillaume Luccisano

unread,
Dec 2, 2010, 4:32:39 PM12/2/10
to i18n-r...@googlegroups.com
Yes url_for looks through the route and pick the best one at run time, but i18n_routing add a check when this is done in order to pick one with the correct locale. This is way your issue is strange :/

2010/12/2 richard <ran13...@gmail.com>

richard

unread,
Dec 3, 2010, 3:44:19 AM12/3/10
to I18n Routing
Dear Guillaume,

I figured it out. It was due to my routes.rb configuration which had
multiple url patterns mapping to the same action (view_product) && I
hadn't translated all the aforementioned routes && some keys were
optional. url_for was always picking one of the untranslated patterns
that had more keys than the named route I had translated.

Thank you for your help and thank you for writing such a great plugin.
I got lots of practice deciphering "metaprogramming".

:)

On Dec 2, 1:32 pm, Guillaume Luccisano <guillaume.luccis...@gmail.com>
wrote:
> Yes url_for looks through the route and pick the best one at run time, but
> i18n_routing add a check when this is done in order to pick one with the
> correct locale. This is way your issue is strange :/
>
> 2010/12/2 richard <ran1347...@gmail.com>

Guillaume Luccisano

unread,
Dec 4, 2010, 3:28:30 PM12/4/10
to i18n-r...@googlegroups.com
Ah great, I'm glad you figured it out !
Have a nice project using the plugin ;)

A+

2010/12/3 richard <ran13...@gmail.com>
Reply all
Reply to author
Forward
0 new messages