Forum: Rails I18n Make a language selector for I18n UI

Adce10d7f1dbabcdab8f525a59cec32f?d=identicon&s=25 Andrés Gutiérrez (andresgutgon)
on 2010-08-16 17:53
(Received via mailing list)
Hello everyone, happy holidays if you are enjoying it right now.

I would like to make a simple language selector like this:

SELECTOR (current locale [:es])
------------------------------------------
Select Language: Español | Catala | French

------------------------------------------
Anyway, I'd like to get a few things:
It's a Rails App pulling I18n and that makes running the gem with
svenfuchs  routing-filter [1] is very well because it puts me the
locale (I18n.locale) this way:
www.mysite.com/en/foo/bar
www.mysite.com/en/foo/bar
www.mysite.com/ca/foo/bar

I would like the choice of locale was a mixture of user preferences
and browser locale preference :


   1. If you are a registered User has a field profile imposed by the
locale
   language of the UI
   2. if NOT logged look into some kind of cookie with his last choice
is
   this good idea?
   3. If you are not logged in and had not come before I look in the
   language preferences of your browser with Rack:: locale Rack-contrib
[2]
   4. If not registered, there is no cookie and the locale of your
browser
   is not among those who managed the App, I show the UI with
   I18n.default_locale (Established in initializers/i18n.rb)
   5. Once we get the locale, set the SELECTOR above to be able to
change
   the locale


(1) and (2) will manage in application_controller.rb

before_filter :set_user_language

def set_user_language
  I18n.locale = logged_in? ? current_user.profile.language.to_s :
request.env['rack.locale']

end

*QUESTION*:
When user click on selector Should pass several things:


   1. User logged in: We change the locale of the UI at that time and
update
   his profile with the new selected locale.
   2. User NOT logged. Store this choice of locale in some way for the
next
   time How? Cookie, is a good idea? How is it done?


These two points refer to store the locale on a "permanent" for that
user, but another problem I have as the selector is constructed to
introduce the new locale:

Around here [3] suggested <% = link_to ("English", url_for
(:overwrite_params => (: locale =>: en)))%>
But this [:overwrite_params] is deprecated [4].
How can i do this from Rails >= 2.3.8?

So the main question is: *how to build the selector?*
An idea is walk thought the [I18n.aviable_locales] array and exclude
the locale which is selected.
And for the text (Español, Spanish, Catala ,...) stored in a Hash
LOCALE_NAMES={: is => "Spanish",: in => "Español ",...}

NOTE:
Use Rails 2.3.8 as the version of this gem (routing-filter) must be <=
v0.0.2
You will think it is bullshit, but to download a specific Tag of a gem
(yesterday I was a good time because I was missing the "v" before
0.0.2)

script/plugin install git://github.com/svenfuchs/routing-filter.git
--revision 'refs/tags/v0.0.2 '

NOTE 2:
If you think my idea was wrong and you want to explain a better way,
I'll be happy to listen and try to understand.

Many thanks for the help.

[1] http://github.com/svenfuchs/routing-filter
<http://translate.googleusercontent.com/translate_c...

[2] http://github.com/rack/rack-contrib
<http://translate.googleusercontent.com/translate_c...

[3] http://github.com/svenfuchs/routing-filter/issues/closed # issue /
3
<http://translate.googleusercontent.com/translate_c...

[4] http://github
.com/rails/rails/commit/cbc0201a3e0145da51baa9d228d88633033521ce
<http://translate.googleusercontent.com/translate_c...
4a551074ddba4460f95d011c47190d0e?d=identicon&s=25 Henrik --- (malesca)
on 2010-08-18 08:36
(Received via mailing list)
2010/8/16 Andrés gutiérrez <andresgutgon@gmail.com>:
> And for the text (Español, Spanish, Catala ,...) stored in a Hash
> LOCALE_NAMES={: is => "Spanish",: in => "Español ",...}

I do e.g. es.meta.language_name: "Español" and then
I18n.t(:"#{locale}.meta.language_name")
4a551074ddba4460f95d011c47190d0e?d=identicon&s=25 Henrik --- (malesca)
on 2010-08-18 08:38
(Received via mailing list)
On Wed, Aug 18, 2010 at 08:35, Henrik Nyh <henrik@nyh.se> wrote:
> 2010/8/16 Andrés gutiérrez <andresgutgon@gmail.com>:
>> And for the text (Español, Spanish, Catala ,...) stored in a Hash
>> LOCALE_NAMES={: is => "Spanish",: in => "Español ",...}
>
> I do e.g. es.meta.language_name: "Español" and then
> I18n.t(:"#{locale}.meta.language_name")
>

I mean I18n.t("meta.language_name", :locale => locale) of course.
Adce10d7f1dbabcdab8f525a59cec32f?d=identicon&s=25 Andrés Gutiérrez (andresgutgon)
on 2010-08-18 09:34
(Received via mailing list)
2010/8/18 Henrik Nyh <henrik@nyh.se>

>
But,...
es:
  meta:
    language: %{locale}
How is the interpolation?
4a551074ddba4460f95d011c47190d0e?d=identicon&s=25 Henrik --- (malesca)
on 2010-08-18 09:39
(Received via mailing list)
2010/8/18 Andrés gutiérrez <andresgutgon@gmail.com>:
> But,...
> es:
>   meta:
>     language: %{locale}
> How is the interpolation?

If you pass :locale into I18n.t, it will use that locale rather than
the currently active one. So  I18n.t("meta.language_name", :locale =>
:es) will be es.meta.language_name,  I18n.t("meta.language_name",
:locale => :en) will be en.meta.language_name.
Adce10d7f1dbabcdab8f525a59cec32f?d=identicon&s=25 Andrés Gutiérrez (andresgutgon)
on 2010-08-18 09:58
(Received via mailing list)
2010/8/18 Henrik Nyh <henrik@nyh.se>

> :locale => :en) will be en.meta.language_name.
>

Oh!!! i did not that :O
Very smart!

Should not be meta.language in default .yml files? is very common and
useful

Thanks
Adce10d7f1dbabcdab8f525a59cec32f?d=identicon&s=25 Andrés Gutiérrez (andresgutgon)
on 2010-08-18 10:00
(Received via mailing list)
El 18 de agosto de 2010 09:57, Andrés gutiérrez
<andresgutgon@gmail.com>escribió:

>> > How is the interpolation?
>>
>> If you pass :locale into I18n.t, it will use that locale rather than
>> the currently active one. So  I18n.t("meta.language_name", :locale =>
>> :es) will be es.meta.language_name,  I18n.t("meta.language_name",
>> :locale => :en) will be en.meta.language_name.
>>
>
> Oh!!! i did not that :O
>
Sorry for my english => did not know that (better?)
Adce10d7f1dbabcdab8f525a59cec32f?d=identicon&s=25 Andrés Gutiérrez (andresgutgon)
on 2010-08-19 12:26
(Received via mailing list)
I've made a gist with my solution. Is in Spanish but the code no :)

http://gist.github.com/535725
This topic is locked and can not be replied to.