Topic: Adding global language variables

Where should I put global language variables? So if a page has a variable @la_headline (or should it be @@ ??) it will show the right string depending on the selected language, taking it from a special language file included on every page.

Re: Adding global language variables

I don't really understand exactly what you are trying to do, but a good place for globals/config type stuff is in your environment.rb

ProgrammingBooks.org - Programming Books Ranked by Programmers
Turn of the Crank - My Personal Blog
Robot Walrus - My art prints & posters blog
My Game Ideas - Submit, rate, view video game Ideas

Re: Adding global language variables

What you'll probably want is to create a separate module or class for handling all of these bits of text.  Then you can import them into your views easily.  For example:

your application_helper.rb (this is where you put methods you want to have available in your views)

module ApplicationHelper
  include LocalizeHelper
end

You'll need to specify in config/environment.rb that you'll be needing the file that has LocalizeHelper in it.  You can do so by adding the following line to the bottom of environment.rb:
require "#{RAILS_ROOT}/lib/localize.rb"

Then, to make this 'LanguageHelper into a real thing, we'll make a file at /lib/localize.rb that has the following contents:

module LocalizeHelper

  @@language = :en

  # accepts a string name like :title or :can_i_help_you
  def get_string(string_name)
    strings[string_name][@@language]
  end

  def strings
    {:title => {:en => 'title',
                :klingon => 'whatever the hell "title" is in klingon'},
     :can_i_help_you => {:en => 'Can I help you?' }
    }
  end 
end


And then you'd want some way to set which language you wanted to use.  Well, that's pretty simple too.  You'd just need a method in the LocalizeHelper module to set the language


module LocalizeHelper

  @@language = :en

  # accepts a string name like :title or :can_i_help_you
  def get_string(string_name)
    strings[string_name][@@language]
  end

  def strings
    {:title => {:en => 'title',
                :klingon => 'whatever the hell "title" is in klingon'},
     :can_i_help_you => {:en => 'Can I help you?' }
    }
  end

  def self.set_language(lang)
    @@language = lang.to_sym # (this makes it into a symbol - like ':en')
  end
end


And you can set it anywhere you want.  Like in environment.rb, ApplicationController via a before_filter (probably the best idea):
class ApplicationController < ActionController::Base

  before_filter :set_language
 
  def set_language
    LocalizeHelper.set_language(params[:lang] || :en)
  end

end


And that's it.  Then you can do this kind of thing in your view:

  <%= get_string :title %>

And it'll show the :title string in whatever language you have specified.  The beauty of this is there's only ever one file to modify - localize.rb.

This is just a beginning too.  If you have more complex needs - like defaulting to an English string when the current language's string is not available - then you can do that easily too.

Last edited by danger (2007-01-11 16:26:41)

Re: Adding global language variables

Wow. Thanks danger. :-) I really enjoy this forum. It's the best place for a newbie like me.

Re: Adding global language variables

Hey, you're welcome!  Glad to have you :-)

Re: Adding global language variables

In fact, thanks for the inspiration Palb.  I think I'm going to extract this into a plugin and release it :-)

Re: Adding global language variables

danger wrote:

In fact, thanks for the inspiration Palb.  I think I'm going to extract this into a plugin and release it :-)

Good idea. I think it can be useful for many of us non-english speaking people.