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!
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
On Thu, Oct 2, 2008 at 19:14, Redd Vinylene <reddvinyl...@gmail.com> 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!
> 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 <reddvinyl...@gmail.com> 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!
*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" <i...@iain.nl> wrote:
> 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 <reddvinyl...@gmail.com> 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!
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...
On Fri, Oct 3, 2008 at 14:51, Karel Minarik <karel.mina...@gmail.com> 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" <i...@iain.nl> wrote: > > Hi,
> > You're missing a few parts. First is the loading of the files into > > I18n. Put this into your environment:
> > 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 <reddvinyl...@gmail.com> 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!
> 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.
well, I think the REST principle: "every resource represenatation
should be identifiable by an unique URL" is really something one
should *never, ever* break. It is the backbone of internet. (And
probably everyone of us saw some unbelievably ugly .NET/PHP/etc apps
which are totally broken in this sense. The bigger the app, the worse,
usually.)
> 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.
Yes, that is true. Usually it is done by prefixing the locale in URL (/
en/some/resource, /es/some/resource) and people are really used to it.
Raul's solutions looks very nice.
> 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).
Yes, I like the hostname option very much (meaning application.com vs
application.de, etc), and it is a preferred way now for me for content-
oriented projects. I did it like that in the demo app and provided
guide how to setup it locally with /etc/hosts. As stated in the demo
app README, search engines love this separation as well (for obvious
reasons).
Of course, there are edge cases, where it's tricky.
> Rails seems to have an answer to it, called default_url_options[1],
Wow, I didn't knew about it, guess Rails is really stretched now. This
would be of course *absolutely* awesome, because it could solve the
most trivial scenario: prefixing locale into every URL, and do that
transparently. This is something we should investigate. Could you
submit a ticket at Lightouse about this?
On Fri, Oct 3, 2008 at 2:51 PM, Karel Minarik <karel.mina...@gmail.com> 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.
How? Like this?
def set_locale I18n.locale = params[:locale] if params[:locale] end
Could you please explain how using the session to store the preferred
language is going against 'RESTfullnes'.
Someone copying and pasting an url would just see the page in the
default language, not really a problem there, except choosing the
right language.
If you advise to pass the language in the url everytime, I think it
would be against the clean url Rails produces.
I think in the long time, you could find yourself with very cluttered
urls full of parameters, which would be a big step back.
Geoffroy Gomet
On Oct 4, 1:20 pm, "Redd Vinylene" <reddvinyl...@gmail.com> wrote:
> On Fri, Oct 3, 2008 at 2:51 PM, Karel Minarik <karel.mina...@gmail.com> 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.
> How? Like this?
> def set_locale
> I18n.locale = params[:locale] if params[:locale]
> end
> *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" <i...@iain.nl> wrote: >> Hi,
>> You're missing a few parts. First is the loading of the files into >> I18n. Put this into your environment:
>> 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 >> <reddvinyl...@gmail.com> 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!
sessions are not restful by the definition of rest :)
Storing a locale in the session of a client means that state is stored on the clientside. The simple test for this aspect of rest probably is: if you can copy and paste the URL to an email, send it off to a buddy and be sure that your buddy will see the same page as you're looking at ... then that's ok. If he sees a different page, it's not.
To me displaying a page in an entirely different language means that it's a "different" page, so I agree with Karel that we definitely should not advise peopel to use the session for this.
Instead, use subdomains, parts of the path or even query params for that. Even though it's more work or might look ugly, it does not break the internet ;)
As for the "more work" part, when it comes to locales as path segments: that's a question of better support in Rails. Raul already plugged his solution, so lemme add mine:
> Could you please explain how using the session to store the preferred > language is going against 'RESTfullnes'. > Someone copying and pasting an url would just see the page in the > default language, not really a problem there, except choosing the > right language. > If you advise to pass the language in the url everytime, I think it > would be against the clean url Rails produces.
> I think in the long time, you could find yourself with very cluttered > urls full of parameters, which would be a big step back.
> Geoffroy Gomet
> On Oct 4, 1:20 pm, "Redd Vinylene" <reddvinyl...@gmail.com> wrote: >> On Fri, Oct 3, 2008 at 2:51 PM, Karel Minarik >> <karel.mina...@gmail.com> 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.
>> How? Like this?
>> def set_locale >> I18n.locale = params[:locale] if params[:locale] >> end
Thank you for the explanation, it's always nice to read crystal clear
explanation.
I did thought of including the language in the route, but then again,
thought about extremely long url.
But now I understand why you advice not to use the session.
Thanks a lot
Geoffroy
On Oct 5, 1:14 pm, Sven Fuchs <svenfu...@artweb-design.de> wrote:
> sessions are not restful by the definition of rest :)
> Storing a locale in the session of a client means that state is stored
> on the clientside. The simple test for this aspect of rest probably
> is: if you can copy and paste the URL to an email, send it off to a
> buddy and be sure that your buddy will see the same page as you're
> looking at ... then that's ok. If he sees a different page, it's not.
> To me displaying a page in an entirely different language means that
> it's a "different" page, so I agree with Karel that we definitely
> should not advise peopel to use the session for this.
> Instead, use subdomains, parts of the path or even query params for
> that. Even though it's more work or might look ugly, it does not break
> the internet ;)
> As for the "more work" part, when it comes to locales as path
> segments: that's a question of better support in Rails. Raul already
> plugged his solution, so lemme add mine:
> > Could you please explain how using the session to store the preferred
> > language is going against 'RESTfullnes'.
> > Someone copying and pasting an url would just see the page in the
> > default language, not really a problem there, except choosing the
> > right language.
> > If you advise to pass the language in the url everytime, I think it
> > would be against the clean url Rails produces.
> > I think in the long time, you could find yourself with very cluttered
> > urls full of parameters, which would be a big step back.
> > Geoffroy Gomet
> > On Oct 4, 1:20 pm, "Redd Vinylene" <reddvinyl...@gmail.com> wrote:
> >> On Fri, Oct 3, 2008 at 2:51 PM, Karel Minarik
> >> <karel.mina...@gmail.com> 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.
> >> How? Like this?
> >> def set_locale
> >> I18n.locale = params[:locale] if params[:locale]
> >> end
> >> is an unfortunate choice by the official i18n demo app and completely
> >> breaks RESTfullnes of anything.
> Which one? where? something we need to correct?
Okay guys, here's the latest. I hope I've got it right this time.
Also:
1. Will I (in Preferences#edit) be able to choose locale from a select box, like in Clemens Kofler's demo? I don't think I need any of that URL stuff. I just want it plain and simple, one language only.
2. Is the organization of my locales proper? Or must en.yml, en_about.yml and en_app.yml be put into a single file for the Preferences#edit select box to work?
> Okay guys, here's the latest. I hope I've got it right this time.
> Also:
> 1. Will I (in Preferences#edit) be able to choose locale from a select
> box, like in Clemens Kofler's demo? I don't think I need any of that
> URL stuff. I just want it plain and simple, one language only.
> 2. Is the organization of my locales proper? Or must en.yml,
> en_about.yml and en_app.yml be put into a single file for the
> Preferences#edit select box to work?