Nitro and Og, It's all Greek to me

By eerieshadow.
Tags:

Localization (how to be multilingual)

It is common to have pages that need to switch languages. Here is quicly how to do that:

1) in run.rb add the transformation of the template through the Localization object:

# the localization object
# :en and :jp are the symbols for each language
# and their corresponding files in yml format
Localization.add(
  :en => 'conf/en.yml',
  :jp => 'conf/jp.yml'
)

Compiler.setup_template_transformation do |template|
  template = Elements.transform(template)
  # the following line handled localization by passing the 
  # template through the localization object
  template = Localization.transform(template)
  template = Markup.transform(template)
  template = Template.transform(template)
end

2) create the yml files in the conf directory and add content of the type:

a) conf/en.yml

---
home: Home
name: Name
# and so on...

b) conf/jp.yml

---
home: ホーム
name: 名前
# and so on...

3) in the .xhtml files when using the tags do the following

Content of test.xhtml

<!-- various code -->

<p> Back to [[home]] page </p>
<p> My [[name]] is ... </p>
<a href="toggle_locales">English - Japanese</a>

4) In the controller add some code to correspond to the tongle_locales function that switches the language

def toggle_locales
  # the session variable must be :LOCALE
  if session[:LOCALE] == :jp
     session[:LOCALE] =  :en
  else
     session[:LOCALE] =  :jp
  end
  redirect_referer
end

5) Just in case you need to localize something in the controller, create an instance of a Localization object and use it around like this:

# before any function put
  # wrap a localization aspect
  wrap LocalizationAspect, :pre => :localize
  @lc = Localization.get

# in a function use it like this
  def index
    @localized_string = @lc['name']
    # @localized_string contains the localized version of 'name' at the
    # time of evaluation
  end

That is how simple it is…

first
last