# Does Globalize supports properties files

**URL:** <https://www.ruby-forum.com/t/does-globalize-supports-properties-files/101653>\
**Category:** Rails I18n\
**Created:** [August 7, 2006, 7:44am UTC](https://www.ruby-forum.com/t/does-globalize-supports-properties-files/101653 "2006-08-07T07:44:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sathya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c0e974/32.png) [@Sathya](https://www.ruby-forum.com/u/Sathya)\
**Post date:** [August 7, 2006, 7:44am UTC](https://www.ruby-forum.com/t/does-globalize-supports-properties-files/101653/1 "2006-08-07T07:44:08Z")

</div>

Hi, i just wanna know how does Globalize work? Is it capable of reading  
data from properties files as in Java? If so where should i define these  
properties files?

I have a scenario where there are some labels in a web page in the  
default english

such as :user code 2: user name 3: user company

and now user has an option to chose a language of his choice which he  
selects from a dropdown provided

Now how can i display these labels in the chosen user language? If user  
opted for Spanish, how can i display these labels in spanish  
immediately?

Thanks in Advance  
Sathya

---

<div class="post-metadata">

**Author:** ![Sathya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c0e974/32.png) [@Sathya](https://www.ruby-forum.com/u/Sathya)\
**Post date:** [August 10, 2006, 2:41am UTC](https://www.ruby-forum.com/t/does-globalize-supports-properties-files/101653/2 "2006-08-10T02:41:34Z")

</div>

Hey Sathya… i was working on the same problem while globalizing my  
Typo blog, I can give you all my code if you need it.

Here’s what I did 🙂

I am saving the user locale in the user session object. application.rb:

before\_filter :set\_locale  
def set\_locale  
Locale.set LOCALES[session[:locale]] if  
LOCALES.keys.include?(session[:locale])  
true  
end

I set the default locale in environment.rb:  
#Globalize plugin  
include Globalize  
Locale.set\_base\_language(‘en-US’)  
Locale.set(‘en-US’) #this is the default site locale  
LOCALES = { ‘English’ =\> ‘en-US’,  
‘Slovenian’ =\> ‘sl-SI’}.freeze

You don’t need the whole language name. You could use also  
LOCALES = { ‘en’ =\> ‘en-US’,  
‘sl’ =\> ‘sl-SI’}.freeze  
But I did it, so I can display it also 🙂

Then i made links in the view for each language i support.

\<% LOCALES.each do |loc\_key, loc\_name| %\>  
\<%= link\_to Language.pick(LOCALES[loc\_key]), {:controller =\>  
‘accounts’, :action =\> ‘switch\_locale’, :locale =\> loc\_key } %\>  
\<%end%\>

This method is in my account controller, the method that switches the  
locale and redirects to a main page, you could redirect anywhere:

def switch\_locale  
switch\_language  
redirect\_to :controller =\> “articles”, :action =\> “index”  
end

And switch\_language is located in lib/login\_system.rb :  
#language switching  
def switch\_language  
session[:locale] = params[:locale] unless params[:locale].blank?  
Locale.set LOCALES[session[:locale]] if  
LOCALES.keys.include?(session[:locale])  
end

You could put this method in a helper also.

The translations are located in the database. You can even translate  
your data at the ActiveRecord level:  
translated :product\_name, :product\_description

Read more on that and everything else on globalize wiki :  
[http://globalize-rails.org/wiki/](http://globalize-rails.org/wiki/)

But if you translate only static content, then use this method on  
strings, like this:

\<%=h “English text”.t%\>

In java you have keys for every translation. With globalize your  
untranslated strings are the keys! The translations are cached from the  
db, so you don’t need any files, and you can make a scaffold based on  
the globalize\_translations database table, and yoou get your very own  
translations editor!

Hope this helpes a bit 🙂

Oh, and this article is really cool:  
[http://globalize-rails.org/wiki/pages/Examplary+application](http://globalize-rails.org/wiki/pages/Examplary+application)

Enjoy!

Sathya wrote:

> Hi, i just wanna know how does Globalize work? Is it capable of reading  
> data from properties files as in Java? If so where should i define these  
> properties files?
> 
> I have a scenario where there are some labels in a web page in the  
> default english
> 
> such as :user code 2: user name 3: user company
> 
> and now user has an option to chose a language of his choice which he  
> selects from a dropdown provided
> 
> Now how can i display these labels in the chosen user language? If user  
> opted for Spanish, how can i display these labels in spanish  
> immediately?
> 
> Thanks in Advance  
> Sathya

---

<div class="post-metadata">

**Author:** ![Sathya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c0e974/32.png) [@Sathya](https://www.ruby-forum.com/u/Sathya)\
**Post date:** [August 10, 2006, 7:20am UTC](https://www.ruby-forum.com/t/does-globalize-supports-properties-files/101653/3 "2006-08-10T07:20:03Z")

</div>

Thanq very much Zarni.

Actually i am newbie to Ruby, so part of ur syntax i dont understand, no  
problem, for the time being iam just evaluating RoR for my project, so i  
hope i need much more help from you in the future.At first, i need to go  
through the fundamentals of RoR.

Well, Zarni, if you dont mind, i have one more doubt in RoR…

How can i read .properties files in RoR? Where should i store these  
files and how can i load them in to my rails application?

Lastly, does RoR support web services? How can i access a web service  
through RoR ?

Awaiting ur reply  
Sathya

Zarni Worgl wrote:

> Hey Sathya… i was working on the same problem while globalizing my  
> Typo blog, I can give you all my code if you need it.
> 
> Here’s what I did 🙂
> 
> I am saving the user locale in the user session object. application.rb:
> 
> before\_filter :set\_locale  
> def set\_locale  
> Locale.set LOCALES[session[:locale]] if  
> LOCALES.keys.include?(session[:locale])  
> true  
> end
> 
> I set the default locale in environment.rb:  
> #Globalize plugin  
> include Globalize  
> Locale.set\_base\_language(‘en-US’)  
> Locale.set(‘en-US’) #this is the default site locale  
> LOCALES = { ‘English’ =\> ‘en-US’,  
> ‘Slovenian’ =\> ‘sl-SI’}.freeze
> 
> You don’t need the whole language name. You could use also  
> LOCALES = { ‘en’ =\> ‘en-US’,  
> ‘sl’ =\> ‘sl-SI’}.freeze  
> But I did it, so I can display it also 🙂
> 
> Then i made links in the view for each language i support.
> 
> \<% LOCALES.each do |loc\_key, loc\_name| %\>  
> \<%= link\_to Language.pick(LOCALES[loc\_key]), {:controller =\>  
> ‘accounts’, :action =\> ‘switch\_locale’, :locale =\> loc\_key } %\>  
> \<%end%\>
> 
> This method is in my account controller, the method that switches the  
> locale and redirects to a main page, you could redirect anywhere:
> 
> def switch\_locale  
> switch\_language  
> redirect\_to :controller =\> “articles”, :action =\> “index”  
> end
> 
> And switch\_language is located in lib/login\_system.rb :  
> #language switching  
> def switch\_language  
> session[:locale] = params[:locale] unless params[:locale].blank?  
> Locale.set LOCALES[session[:locale]] if  
> LOCALES.keys.include?(session[:locale])  
> end
> 
> You could put this method in a helper also.
> 
> The translations are located in the database. You can even translate  
> your data at the ActiveRecord level:  
> translated :product\_name, :product\_description
> 
> Read more on that and everything else on globalize wiki :  
> [http://globalize-rails.org/wiki/](http://globalize-rails.org/wiki/)
> 
> But if you translate only static content, then use this method on  
> strings, like this:
> 
> \<%=h “English text”.t%\>
> 
> In java you have keys for every translation. With globalize your  
> untranslated strings are the keys! The translations are cached from the  
> db, so you don’t need any files, and you can make a scaffold based on  
> the globalize\_translations database table, and yoou get your very own  
> translations editor!
> 
> Hope this helpes a bit 🙂
> 
> Oh, and this article is really cool:  
> [http://globalize-rails.org/wiki/pages/Examplary+application](http://globalize-rails.org/wiki/pages/Examplary+application)
> 
> Enjoy!
> 
> Sathya wrote:
> 
> > Hi, i just wanna know how does Globalize work? Is it capable of reading  
> > data from properties files as in Java? If so where should i define these  
> > properties files?
> > 
> > I have a scenario where there are some labels in a web page in the  
> > default english
> > 
> > such as :user code 2: user name 3: user company
> > 
> > and now user has an option to chose a language of his choice which he  
> > selects from a dropdown provided
> > 
> > Now how can i display these labels in the chosen user language? If user  
> > opted for Spanish, how can i display these labels in spanish  
> > immediately?
> > 
> > Thanks in Advance  
> > Sathya

---

<div class="post-metadata">

**Author:** ![Sathya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c0e974/32.png) [@Sathya](https://www.ruby-forum.com/u/Sathya)\
**Post date:** [August 10, 2006, 12:11pm UTC](https://www.ruby-forum.com/t/does-globalize-supports-properties-files/101653/4 "2006-08-10T12:11:28Z")

</div>

If you really really need to use some kind of properties files, then you  
can use this little goodie :  
[http://mir.aculo.us/articles/2005/10/03/ruby-on-rails-i18n-revisited](http://mir.aculo.us/articles/2005/10/03/ruby-on-rails-i18n-revisited)

I used it for a project… works great!

Web services with RoR are simple as cake. Read this one:  
[http://www-128.ibm.com/developerworks/java/library/j-cb08016/index.html](http://www-128.ibm.com/developerworks/java/library/j-cb08016/index.html)

Knowledge is the mother of Creation 😉

Sathya wrote:

> Thanq very much Zarni.
> 
> Actually i am newbie to Ruby, so part of ur syntax i dont understand, no  
> problem, for the time being iam just evaluating RoR for my project, so i  
> hope i need much more help from you in the future.At first, i need to go  
> through the fundamentals of RoR.
> 
> Well, Zarni, if you dont mind, i have one more doubt in RoR…
> 
> How can i read .properties files in RoR? Where should i store these  
> files and how can i load them in to my rails application?
> 
> Lastly, does RoR support web services? How can i access a web service  
> through RoR ?
> 
> Awaiting ur reply  
> Sathya
