# User interface locale VS content locale

**URL:** <https://www.ruby-forum.com/t/user-interface-locale-vs-content-locale/210325>\
**Category:** Rails I18n\
**Created:** [August 5, 2011, 12:34am UTC](https://www.ruby-forum.com/t/user-interface-locale-vs-content-locale/210325 "2011-08-05T00:34:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [August 5, 2011, 12:34am UTC](https://www.ruby-forum.com/t/user-interface-locale-vs-content-locale/210325/1 "2011-08-05T00:34:05Z")

</div>

Hi,

I’m working on a little content management system where the user can  
manage content that is translated in multiple languages. The UI of the  
CMS itself is also available in multiple languages so I need to be  
able to display the UI in english while the user is managing french  
content. I am not sure of the smart way to handle this. Using  
something like Globalize3 you end up having to keep switching the  
value of I18n.locale back and forth when calling I18n.translate for  
the UI or when displaying data from the model.

Anyone has tips or ideas?

---

<div class="post-metadata">

**Author:** ![hydrozen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hydrozen](https://www.ruby-forum.com/u/hydrozen)\
**Post date:** [August 5, 2011, 12:42am UTC](https://www.ruby-forum.com/t/user-interface-locale-vs-content-locale/210325/2 "2011-08-05T00:42:07Z")

</div>

Perhaps Object#with\_options plus the :locale param could be helpful.  
Something like

I18n.locale = :en

# English

t(:“welcome”)  
with\_options(:locale =\> :fr) do |trans|

# French

trans.t(:“hello”)  
end

# English

t(:“goodbye”)

Or define a method like

def self.in\_locale(locale)  
old\_locale = I18n.locale  
I18n.locale = locale  
yield  
ensure  
I18n.locale = old\_locale  
end

and you could do stuff like

I18n.locale = :en

# English

t(:“welcome”)  
I18n.in\_locale(:fr) do

# French

t(:“hello”)  
end

# English

t(:“goodbye”)

---

<div class="post-metadata">

**Author:** ![hydrozen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hydrozen](https://www.ruby-forum.com/u/hydrozen)\
**Post date:** [August 5, 2011, 4:09pm UTC](https://www.ruby-forum.com/t/user-interface-locale-vs-content-locale/210325/3 "2011-08-05T16:09:10Z")

</div>

Yes I thought of this and Globalize3 already has a utility for this  
called called with\_locale to which you can pass a locale and a block.  
But even with this it seems like it ends up being an hassle.  
So basically, every time I want to display data from a model, I’ll  
need to wrap the call into that function to make sure I display using  
the content locale and not the ui locale. If I display forms, I’ll  
also need to wrap each field into that function too… making sure I  
don’t wrap any calls to I18n.translate.

---

<div class="post-metadata">

**Author:** ![hydrozen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hydrozen](https://www.ruby-forum.com/u/hydrozen)\
**Post date:** [August 5, 2011, 4:40pm UTC](https://www.ruby-forum.com/t/user-interface-locale-vs-content-locale/210325/4 "2011-08-05T16:40:03Z")

</div>

Well, why not hack up I18n a bit?

Let I18n.locale be the content locale, but hack in a new I18n.ui\_locale  
method, and a I18n.ui\_translate method that wraps I18n.translate inside  
a  
I18n.with\_locale(I18n.ui\_locale) {…}

Then you can mix ui and content freely by simply calling different  
translation methods for them.

Or something… never tried it. Personally I’d just use I18n.with\_locale  
here and there, but we don’t mix locales as much.

- Martin

PS. I had fun looking up how I18n.locale is stored (considering I18n is  
a  
module).

---

<div class="post-metadata">

**Author:** ![hydrozen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hydrozen](https://www.ruby-forum.com/u/hydrozen)\
**Post date:** [August 5, 2011, 8:08pm UTC](https://www.ruby-forum.com/t/user-interface-locale-vs-content-locale/210325/5 "2011-08-05T20:08:56Z")

</div>

Yeah I think I’ll look into the rails-i18n code and Globalize3 code  
over the weekend. It seems like Globalize3 also stores a setting in  
the Thread called :globalize\_locale. I wonder if that means I could  
have a different setting for the UI and for the Content. I was mostly  
opening up a discussion to see if others had the same kinda problems  
to solve.

Thanks for the feedback.

---

<div class="post-metadata">

**Author:** ![hydrozen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hydrozen](https://www.ruby-forum.com/u/hydrozen)\
**Post date:** [August 5, 2011, 12:43am UTC](https://www.ruby-forum.com/t/user-interface-locale-vs-content-locale/210325/6 "2011-08-05T00:43:21Z")

</div>

On Fri, Aug 5, 2011 at 00:40, Henrik N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def self.in\_locale(locale)  
> …  
> end

Make that

module I18n  
def self.in\_locale(locale)  
…  
end  
end

---

<div class="post-metadata">

**Author:** ![hydrozen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hydrozen](https://www.ruby-forum.com/u/hydrozen)\
**Post date:** [August 7, 2011, 1:19am UTC](https://www.ruby-forum.com/t/user-interface-locale-vs-content-locale/210325/7 "2011-08-07T01:19:03Z")

</div>

Actually, for Globalize2 it might also be better to store in  
Thread.current. Correct me if I’m wrong but changing it in  
ActiveRecord::Base would affect all users and not just this single  
request?

---

<div class="post-metadata">

**Author:** ![hydrozen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hydrozen](https://www.ruby-forum.com/u/hydrozen)\
**Post date:** [August 6, 2011, 4:24pm UTC](https://www.ruby-forum.com/t/user-interface-locale-vs-content-locale/210325/8 "2011-08-06T16:24:33Z")

</div>

After a big of digging, it turns out that both Globalize2 and  
Globalize3 use I18n.locale as a default locale but provide a way to  
override that setting.

When using Globalize2 you can do:  
ActiveRecord::Base.locale = :fr

When using Globalize3 you can do:  
Thread.current[:globalize\_locale] = :fr

and then you don’t have to keep toggling I18n.locale back and forth 😉
