Does Globalize supports properties files

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

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 :slight_smile:

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 :slight_smile:

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/

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 :slight_smile:

Oh, and this article is really cool:
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

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 :slight_smile:

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 :slight_smile:

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/

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 :slight_smile:

Oh, and this article is really cool:
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

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

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

Knowledge is the mother of Creation :wink:

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