Make a language selector for I18n UI

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] GitHub - svenfuchs/routing-filter: routing-filter wraps around the complex beast that the Rails routing system is, allowing for unseen flexibility and power in Rails URL recognition and generation.
http://translate.googleusercontent.com/translate_c?hl=es&ie=UTF-8&sl=es&tl=en&u=http://github.com/svenfuchs/routing-filter&prev=_t&rurl=translate.google.com&twu=1&usg=ALkJrhiKS-2ErsP9K2S_S4LtNuUUmvo-_A

[2] GitHub - rack/rack-contrib: Contributed Rack Middleware and Utilities
http://translate.googleusercontent.com/translate_c?hl=es&ie=UTF-8&sl=es&tl=en&u=http://github.com/rack/rack-contrib&prev=_t&rurl=translate.google.com&twu=1&usg=ALkJrhj-0rvyBiYWxRyjbEjUqmG_E5qZ8A

[3] Issues · svenfuchs/routing-filter · GitHub # issue /
3
http://translate.googleusercontent.com/translate_c?hl=es&ie=UTF-8&sl=es&tl=en&u=http://github.com/svenfuchs/routing-filter/issues/closed&prev=_t&rurl=translate.google.com&twu=1&usg=ALkJrhiMvkzLphRNkAU97N6a9rhJPOqy5g#issue/3

[4] http://github
.com/rails/rails/commit/cbc0201a3e0145da51baa9d228d88633033521ce
http://translate.googleusercontent.com/translate_c?hl=es&ie=UTF-8&sl=es&tl=en&u=http://github.com/rails/rails/commit/cbc0201a3e0145da51baa9d228d88633033521ce&prev=_t&rurl=translate.google.com&twu=1&usg=ALkJrhhB1WA05czd1h8KAEOw9fCGE-V1NA

2010/8/16 Andrés gutiérrez [email protected]:

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”)

On Wed, Aug 18, 2010 at 08:35, Henrik N. [email protected] wrote:

2010/8/16 Andrés gutiérrez [email protected]:

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.

2010/8/18 Henrik N. [email protected]

But,…
es:
meta:
language: %{locale}
How is the interpolation?

2010/8/18 Henrik N. [email protected]

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

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

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

Thanks

El 18 de agosto de 2010 09:57, Andrés gutiérrez
[email protected]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 :open_mouth:

Sorry for my english => did not know that (better?)

I’ve made a gist with my solution. Is in Spanish but the code no :slight_smile:

2010/8/18 Andrés gutiérrez [email protected]:

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.