I see your point. How do you maintain the chosen locale? If the locale
comes from the params, it doesn't only produce ugly urls, but you also
need some way to add it to every link you generate. There is no
elegant solution for this at the moment.

The other option is using the hostname (i.e. a subdomain), but you're
not always able to use this (if you're not the only website on one
domain or if you're using https with a valid certificate).

I'd love to hear some advice.

What seems to me an elegant solution is some routes work:

map.with_options :path_prefix => '/:locale' do |localized|
  localized.resources :projects
end

It seems nice, but how to fill the locale with the currently selected
locale? I don't like to do this all the time: projects_path(:locale =>
I18n.locale)

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

While typing this message I found something perculiar. It only happens
with new_post_path and posts_path in my own little example (without
the route described above).

I have a scaffolded view and when in index the edit/delete and show
links all get the parameter properly, but not the new and index links.
Why not those two? The only difference I found between those two and
the rest is that new and index don't use params[:id].

Furthermore, I found that if I use the route I specified above, the
post object gets assigned to the locale-param. This results in an
error like:

edit_post_url failed to generate from {:action=>"edit",
:locale=>#<Post id: 1, title: "Foo", etc...

What is going on?



[1] http://api.rubyonrails.com/classes/ActionController/Base.html#M000851


On Fri, Oct 3, 2008 at 14:51, Karel Minarik <[EMAIL PROTECTED]> wrote:
>
> Hello Iain,
>
> *please* don't advise people to use `session` for setting locale! That
> is an unfortunate choice by the official i18n demo app and completely
> breaks RESTfullnes of anything. (ie. completely breaks even such
> simple thing like sending someone a URL and expecting that she'll see
> the same thing.)
>
> One should set locale from params, hostname, accepted-language header,
> whatever, just *not* session.
>
>
> Karel
>
> On Oct 2, 9:01 pm, "Iain Hecker" <[EMAIL PROTECTED]> wrote:
> > Hi,
> >
> > You're missing a few parts. First is the loading of the files into
> > I18n. Put this into your environment:
> >
> > I18n.load_translations(*Dir.glob(LOCALES_DIRECTORY+'/**/*.yml'))
> >
> > look here how to get 
> > available_locales:http://rails-i18n.org/wiki/pages/i18n-available_locales(put
> >  it in an
> > initializer)
> >
> > Secondly, you need to set the proper locale at every request, so in
> > your before_filter you need to default to the locale stored in the
> > user's session or from preferences stored in the database.
> >
> > def set_locale
> >   I18n.locale = params[:locale] or session[:locale] or I18n.default_locale
> >   session[:locale] = I18n.locale
> > end
> >
> > Hope this helps,
> >
> > Iain
> >
> > On Thu, Oct 2, 2008 at 19:14, Redd Vinylene <[EMAIL PROTECTED]> wrote:
> >
> > > Hello hello! I just finished translating my app, however my
> > > translations won't load. So first, what am I doing wrong? And second,
> > > is there anything that can be done better? I'm a bit confused since
> > > every tutorial and demo app seems to have its own way of doing things.
> > > Alright, that's it. Thanks everyone!
> >
> > > /myapp
> > > /myapp/lang
> > > /myapp/lang/en
> > > /myapp/lang/en/en.yml
> > > /myapp/lang/en/en_about.yml
> > > /myapp/lang/en/en_app.yml
> > > /myapp/lang/is/is.yml
> > > /myapp/lang/is/is_about.yml
> > > /myapp/lang/is/is_app.yml
> >
> > > ## /myapp/config/environment.rb
> >
> > > I18n.default_locale = "en"
> >
> > > LOCALES_DIRECTORY = "#{RAILS_ROOT}/lang/"
> >
> > > LOCALES_AVAILABLE = Dir["#{LOCALES_DIRECTORY}/*/*.yml"].collect do 
> > > |locale_file|
> > >  File.basename(locale_file, ".yml")
> > > end.uniq
> >
> > > ## /myapp/app/controllers/application.rb
> >
> > > before_filter :set_locale
> >
> > > def set_locale
> > >  I18n.locale = params[:locale] if params[:locale]
> > > end
> >
> > > ## /myapp/app/helpers/application_helper.rb
> >
> > > def t(*args)
> > >  translate(*args)
> > > end
> >
> > > ## /myapp/app/views/admin/preferences/edit.html.erb
> >
> > > <%= f.label "locale", t(:"locale") %><%= f.select("locale",
> > > options_for_select(LOCALES_AVAILABLE, I18n.locale) %>
> >
> > > --
> > >http://www.home.no/reddvinylene
> >

--~--~---------~--~----~------------~-------~--~----~
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