# Make a language selector for I18n UI

**URL:** <https://www.ruby-forum.com/t/make-a-language-selector-for-i18n-ui/193366>\
**Category:** Rails I18n\
**Created:** [August 16, 2010, 5:53pm UTC](https://www.ruby-forum.com/t/make-a-language-selector-for-i18n-ui/193366 "2010-08-16T17:53:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![andresgutgon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@andresgutgon](https://www.ruby-forum.com/u/andresgutgon)\
**Post date:** [August 16, 2010, 5:53pm UTC](https://www.ruby-forum.com/t/make-a-language-selector-for-i18n-ui/193366/1 "2010-08-16T17:53:49Z")

</div>

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](http://www.mysite.com/en/foo/bar)  
[www.mysite.com/en/foo/bar](http://www.mysite.com/en/foo/bar)  
[www.mysite.com/ca/foo/bar](http://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://github.com/svenfuchs/routing-filter)  
[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](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://github.com/rack/rack-contrib)  
[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](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](http://github.com/svenfuchs/routing-filter/issues/closed) # 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](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](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](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)

---

<div class="post-metadata">

**Author:** ![andresgutgon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@andresgutgon](https://www.ruby-forum.com/u/andresgutgon)\
**Post date:** [August 18, 2010, 8:36am UTC](https://www.ruby-forum.com/t/make-a-language-selector-for-i18n-ui/193366/2 "2010-08-18T08:36:26Z")

</div>

2010/8/16 Andrés gutiérrez [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

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

---

<div class="post-metadata">

**Author:** ![andresgutgon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@andresgutgon](https://www.ruby-forum.com/u/andresgutgon)\
**Post date:** [August 18, 2010, 8:38am UTC](https://www.ruby-forum.com/t/make-a-language-selector-for-i18n-ui/193366/3 "2010-08-18T08:38:11Z")

</div>

On Wed, Aug 18, 2010 at 08:35, Henrik N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 2010/8/16 Andrés gutiérrez [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > 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.

---

<div class="post-metadata">

**Author:** ![andresgutgon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@andresgutgon](https://www.ruby-forum.com/u/andresgutgon)\
**Post date:** [August 18, 2010, 9:34am UTC](https://www.ruby-forum.com/t/make-a-language-selector-for-i18n-ui/193366/4 "2010-08-18T09:34:58Z")

</div>

2010/8/18 Henrik N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> 

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

---

<div class="post-metadata">

**Author:** ![andresgutgon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@andresgutgon](https://www.ruby-forum.com/u/andresgutgon)\
**Post date:** [August 18, 2010, 9:58am UTC](https://www.ruby-forum.com/t/make-a-language-selector-for-i18n-ui/193366/5 "2010-08-18T09:58:59Z")

</div>

2010/8/18 Henrik N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> :locale =\> :en) will be en.meta.language\_name.

Oh!!! i did not that 😮  
Very smart!

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

Thanks

---

<div class="post-metadata">

**Author:** ![andresgutgon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@andresgutgon](https://www.ruby-forum.com/u/andresgutgon)\
**Post date:** [August 18, 2010, 10:00am UTC](https://www.ruby-forum.com/t/make-a-language-selector-for-i18n-ui/193366/6 "2010-08-18T10:00:09Z")

</div>

El 18 de agosto de 2010 09:57, AndrÃ©s gutiÃ©rrez  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)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 😮

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

---

<div class="post-metadata">

**Author:** ![andresgutgon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@andresgutgon](https://www.ruby-forum.com/u/andresgutgon)\
**Post date:** [August 19, 2010, 12:26pm UTC](https://www.ruby-forum.com/t/make-a-language-selector-for-i18n-ui/193366/7 "2010-08-19T12:26:08Z")

</div>

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

> <https://gist.github.com/andresgutgon/535725>

---

<div class="post-metadata">

**Author:** ![andresgutgon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@andresgutgon](https://www.ruby-forum.com/u/andresgutgon)\
**Post date:** [August 18, 2010, 9:39am UTC](https://www.ruby-forum.com/t/make-a-language-selector-for-i18n-ui/193366/8 "2010-08-18T09:39:39Z")

</div>

2010/8/18 Andrés gutiérrez [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

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