# Locale model to store language value not working

**URL:** <https://www.ruby-forum.com/t/locale-model-to-store-language-value-not-working/148409>\
**Category:** Rails\
**Created:** [September 12, 2008, 4:08pm UTC](https://www.ruby-forum.com/t/locale-model-to-store-language-value-not-working/148409 "2008-09-12T16:08:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jalb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f17d59/32.png) [@jalb](https://www.ruby-forum.com/u/jalb)\
**Post date:** [September 12, 2008, 4:08pm UTC](https://www.ruby-forum.com/t/locale-model-to-store-language-value-not-working/148409/1 "2008-09-12T16:08:35Z")

</div>

hi

I have a problem to store the current locale (language) used by a user .

I have this model that represent the locale:

class LocaleModel \< ActiveRecord::Base  
@@global\_locale = nil

def self.global  
@@global\_locale  
end

def self.global=( locale )  
if locale.is\_a? Locale  
@@global\_locale = locale  
elsif locale.is\_a? String  
locale = LocaleModel.find(:first, :conditions =\> ['short = ? ',  
locale])  
return false if (! locale)  
@@global\_locale = locale  
else  
# empty  
@@global\_locale = nil  
end  
end

def master?  
self.master == true  
end  
end

In my application controller I do :

LocaleModel.global = ‘fr’ if LocaleModel.global == nil

And in some other controller I have an action that is used to change the  
locale:

def change\_locale  
LocaleModel.global = params[:lang]  
end

The problem is that when I change the view or reload the page, the  
locale always resets. How come ?

Thank You

---

<div class="post-metadata">

**Author:** ![jalb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f17d59/32.png) [@jalb](https://www.ruby-forum.com/u/jalb)\
**Post date:** [September 12, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/locale-model-to-store-language-value-not-working/148409/2 "2008-09-12T16:15:02Z")

</div>

I think you misunderstand what’s a global variable in Ruby/Rails  
It’s not storing data between sessions. Whenever a user requests  
a page, it’s reset.  
You must use the session object to store data between requests

---

<div class="post-metadata">

**Author:** ![jalb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f17d59/32.png) [@jalb](https://www.ruby-forum.com/u/jalb)\
**Post date:** [September 12, 2008, 4:17pm UTC](https://www.ruby-forum.com/t/locale-model-to-store-language-value-not-working/148409/3 "2008-09-12T16:17:23Z")

</div>

On 12 Sep 2008, at

> def change\_locale  
> LocaleModel.global = params[:lang]  
> end
> 
> The problem is that when I change the view or reload the page, the  
> locale always resets. How come ?

Because classes are reloaded between requests in development mode.

Fred
