Rich-i18n

Enrichments (e9s) module for internationalization (i18n)

Introduction

Rich-i18n is a module of E9s (http://github.com/archan937/e9s) which enriches I18n, Formtastic, the String and Symbol classes. This simplifies internationalization of your Rails application making a Rails developers life much easier. A list of features:

I18n

  • Translate on-site – Just specify you want to use Rich-CMS (http://github.com/archan937/rich_cms) and you are set to translate in the front-end
  • Default values – Use the translation key (or a portion) as default value: "continue".t returns "continue" and "text.Welcome_to_our_site".t returns "Welcome to our site"
  • An easy interface – Just call the t method on string or symbols to translate
  • Combine translations – Joining keys with spaces combines translations: "Male / Female".t returns "Man / Vrouw" in Dutch
  • Preserve i18n meta data – Rich-i18n preserves the translation key, value, locale and derivative key (the argument passed for translation). Enquiring this can come in handy when implementing an internationalization CMS (see Rich-CMS).

Formtastic

  • Labels, seatholders and default values – Not only translate labels, but also hint text (so called seatholders) and even translate default values
  • Unobtrusive implementation – Translate labels and seatholders unobtrusively, in other words: leave your semantic_form_for (view) code completely untouched
  • Specific translations – Not only specify general translations for labels and seatholders, but make them model or even form specific

Inflections

  • Preserve character casing – Rich-i18n preserves the casing in your translations: "save".t returns "bewaar", "Save".t returns "Bewaar" and "SAVE".t returns "BEWAAR" in Dutch

More available features when using E9s (enrichments)

  • Localized pluralization – Translations only in singular form are sufficient enough as E9s can pluralize in foreign languages
  • An easy interface for localized pluralizations – Just call the pl method on string or symbols to pluralize
  • Preserve pluralization – E9s singularizes or pluralizes your translations depending on the key: "house".t returns "huis" and "Houses".t returns "Huizen" in Dutch

Note: keep in mind that you will have to use E9s to do this, please visit http://github.com/archan937/e9s for more information.

Installation

Using Rich-i18n as gem in Rails 3

Add Rich-i18n in Gemfile as a gem dependency:


  gem "rich_i18n"

Run the following in your console to install with Bundler:


  bundle install

Using Rich-i18n as gem in Rails 2

Add Rich-i18n in environment.rb as a gem dependency:


  config.gem "rich_i18n"

Run the following in your console:


  sudo rake gems:install

Using Rich-i18n as plugin in Rails 3


  rails plugin install git://github.com/archan937/rich_i18n.git

Using Rich-i18n as plugin in Rails 2


  script/plugin install git://github.com/archan937/rich_i18n.git

Testing Rich-i18n out-of-the-box

Run the Rails console:


  ./script/console

Start translating in Dutch:


  >> I18n.locale = :nl
  => :nl
  >> "Male / Female".t
  => "Man / Vrouw"

Use the provided Rails generators

Rich-i18n requires the following entity:

  • An ActiveRecord model used for translations storage

Fortunately, Rich-i18n is provided with a Rails generator with which you can generate the entity.

In Rails 3

Run the following in your console:


  rails g rich:translation -m

Note: At default, it will create the Translation class and CreateTranslations migration. You can alter the class names with the following:


  rails g rich:translation CodeHeroes::Translation -m

Note: The generator has the -m or --migrate option which runs rake db:migrate after creating the files.

In Rails 2

Run the following in your console:


  script/generate rich_i18n_translation -m

Usage

Just call the t method on string or symbols to translate using Rich-i18n.

Populating config/locales

At default, I18n uses I18n::Backend::Simple of which translations are stored within YAML files located in config/locales. When adding a new language, it is adviced to copy a YAML file from http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale in which you can add your translations. Of course, you can also use other I18n backends like I18n::Backend::ActiveRecord for translations stored in the database.

Note: specified in config/locales/nl.yml


---
nl:

  word:
    "yes":   ja
    "no":    nee
    house:   huis
    letter:  brief
    sign:    teken
    users:   gebruikers
    more:    meer

String methods

Rich-i18n has enriched the String class with several inflection methods such as upcase_first, cp_case, upcase_first! and pluralize!. Please visit http://github.com/archan937/rich_i18n/blob/master/lib/rich/i18n/core/string/inflections.rb to see all the methods.

Default values and case preservation

When not specified, Rich-i18n returns a default value based on the passed key: it splits the key on "." and (sort of) humanizes the last part. Sort of, because it actually replaces "_" with " " and it copies the casing of the key with the cp_case method of the String class.

Combined keys

You can combine translations by using passed string containing translation keys joined with spaces.

Translation meta data with EnrichedString

When translating text, you possibly want to know the key, the value, the locale and the derivative key (the argument passed for translation). Rich-i18n preserves just that in an EnrichedString which is a wrapper containing meta data and the translation. Calling .meta_data returns a hash with the meta data:


  >> "MORE".t.class
  => Rich::I18n::Core::EnrichedString
  >> "MORE".t.meta_data
  => {:locale=>:nl, :value=>"meer", :derivative_key=>"MORE", :key=>"word.more"}

Keep in mind that combined translations are possible and fortunately EnrichedString is able to cope with that. A concatenated translation has merged_strings which contains every segments:


  >> "More users".t
  => "Meer gebruikers"
  >> "More users".t.merged_strings
  => ["Meer", " ", "gebruikers"]
  >> "More users".t.meta_data
  => nil
  >> "More users".t.merged_strings.first.meta_data
  => {:locale=>:nl, :value=>"meer", :derivative_key=>"More", :key=>"word.more"}
  >> "More users".t.merged_strings.last.meta_data
  => {:locale=>:nl, :value=>"gebruiker", :derivative_key=>"users", :key=>"word.user"}
  >> "One".t + " " + "question".t
  => "één vraag"
  >> ("One".t + " " + "question".t).merged_strings
  => ["één", " ", "vraag"]

String.to_output

E9s adds the to_output method to the String class. This returns the an i18n tag with HTML 5 attributes in which the translation meta data is provided:


  >> Rich::I18n::Engine.enable_enriched_output = true
  => true
  >> "More users".t.to_output
  => "<i18n data-value=\"meer\" data-locale=\"nl\" data-key=\"word.more\" data-derivative_key=\"More\">Meer</i18n> <i18n data-value=\"gebruiker\" data-locale=\"nl\" data-key=\"word.user\" data-derivative_key=\"users\">gebruikers</i18n>"

This can be very handy when implementing a CMS in which users change translations. Please note that http://github.com/archan937/e9s-demo uses this feature to highlight translations. Later on this will also be used in Rich-CMS, a gem / plugin that makes inplace translating possible (please be patient for this to be released).

I18n examples

As a result of the YAML file specified above, you will get the following translations in your Rails console:


  >> "house".t
  => "huis"
  >> "LETTER".t
  => "BRIEF"
  >> "application.index.Welcome_to_our_site".t
  => "Welcome to our site"
  >> "Sign".t
  => "Teken"
  >> "MORE USERS".t
  => "MEER GEBRUIKERS"
  >> "Yes / No".t
  => "Ja / Nee"

Labels and seatholders

You can translate labels and seatholders (placeholders :D) within Formtastic forms without altering its code.

Note: specified in config/locales/nl.yml


---
nl:

  word:
    password:       wachtwoord

  label:
    user_name:      gebruikersnaam
    content:        bericht

    Question:
      content:      jouw vraag

    Answer:
      content:      jouw antwoord

    (search_form)
      criteria:     uw zoekcriteria

  seatholder:
    email_address:  [email protected]

    Question:
      content:      Hoeveel uren zitten in een dag?

    Answer:
      content:      24 uur

    (search_form)
      criteria:     '&Voorbeeld'

Contact me

For support, remarks and requests please mail me at [email protected].

Credit

This Rails gem / plugin depends on:

i18n
http://github.com/svenfuchs/i18n

Hpricot
http://github.com/whymirror/hpricot (thanks Why!)

Formtastic (optional)
http://github.com/justinfrench/formtastic

Rich-CMS (optional)
http://codehero.es/rails_gems_plugins/rich_cms
http://github.com/archan937/rich_cms

SeatHolder (optional)
http://codehero.es/jquery_libraries/seat_holder
http://github.com/archan937/seat_holder

ToDo’s

  • Handle the click on inputs with seatholders better
  • Use a better implementation to tackle String interpolation (e.g. “foo #”bar"”bar".t") to preserve meta data
  • Most String inflection methods are also defined in rich_pluralization (keep it DRY)

Enrichments

The all-in-one gem at – http://codehero.es/rails_gems_plugins/e9shttp://github.com/archan937/e9s

E9s modules

License

Copyright © 2010 Paul Engel, released under the MIT license

http://holder.nlhttp://codehero.eshttp://gettopup.comhttp://twitter.com/archan937[email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.