Hi guys,

I was playing around with this and I worked out a solution which lets
url helpers locale wise:

  map.user ':locale/user/:id'

Then we can do:

  I18n.locale = 'en-US'
  user_url(1) #=> http://host/en-US/user/1

  I18n.locale = 'pt-BR'
  user_url(1) #=> http://host/pt-BR/user/1

  I18n.locale = 'pt-BR'
  user_url('en-US', 1) #=> http://host/en-US/user/1

How it works?

If the route has a :locale segment, we add the locale to url hash
options: { :locale => I18n.locale }

Even more, if the route has a :locale segment and the number of
arguments are less than it was supposed to (for example, user_url
expects 2 arguments), we discard the :locale segment assigning
correctly the :id.

Gist is here: http://gist.github.com/25476

Tell me how it's working out for you! =)

Regards,
José Valim.

--
http://josevalim.blogspot.com/
http://www.pagestacker.com/

On Oct 22, 6:18 pm, "Iain Hecker" <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm sorry, I forgot all about it... Let's recap:
>
> The problem is that Rails assigns non-hash objects to the first
> variables in the route and only after that the default_url_options get
> assigned. So if you have user_url(1) for routes like
> "/:locale/users/:id" then 1 will be assigned to :locale, and after
> that the default_url_options kick in. They want to set :locale to
> "en-US", but there is no :locale left, so it appends it ?locale=en-US.
> Then it raises a routing error because :id hasn't been filled.
>
> If you don't have :locale in front, then this method works really
> nice. You'd get what google uses (foo/bar?hl=en).
> But I want to add the locale in front! (/en/foo/bar)
>
> I have been wandering on lighthouse and found this 
> ticket:http://rails.lighthouseapp.com/projects/8994/tickets/22-default_url_o...
>
> I don't know enough about the routing and url generation to solve
> this, so I made a 
> ticket:http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/12...
>
> Iain
>
> On Wed, Oct 22, 2008 at 18:51, Karel Minarik <[EMAIL PROTECTED]> wrote:
>
> > Hi,
>
> > a while ago Iain mentioned the "default_url_options" method in Rails,
> > which could be just what we need for preserving locale in URLs (thus
> > being restafarians! :)
>
> > Have you looked into any further, Iain? Has anybody else? It really
> > looks like this would be perfect & transparent solution.
>
> > Here are the docs:
>
> >http://www.railsbrain.com/api/rails-2.1.0/doc/index.html?a=M000494&na...
>
> > Karel
>
> >> From: "Iain Hecker" <[EMAIL PROTECTED]>
> >> Date: Oct 3, 3:50 pm
> >> Subject: Am I doing it right? (translating my first app)
> >> To: rails-i18n
>
> >> ...
>
> >> Rails seems to have an answer to it, called default_url_options[1],
> >> but it doesn't seem to work. I've overwritten the method like the api
> >> tells me, but somehow it is ignored into generating the path. It is
> >> called, because it will break if I raise somthing in it. The options
> >> passed to it are what you'd expect (containing all the parameters
> >> you'd pass to url_for)
>
> >> Here's my method:
>
> >>   def default_url_options(options = {})
> >>     { :locale => I18n.locale }
> >>   end
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"rails-i18n" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rails-i18n?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to