h1. Rich-i18n Enrichments (e9s) module for internationalization (i18n) h2. Introduction Rich-i18n is a module of E9s ("http://github.com/archan937/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: h3. I18n * @Translate on-site@ - Just specify you want to use Rich-CMS ("http://github.com/archan937/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":http://github.com/archan937/rich_cms). h3. 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 h3. 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 h3. 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":http://github.com/archan937/e9s for more information. h2. Installation h3. Using Rich-i18n as gem Install the Rich-i18n gem:
  sudo gem install rich_i18n
Add rich_i18n in environment.rb as a gem dependency:
  config.gem "rich_i18n"
h3. Using Rich-i18n as plugin Install the Rich-i18n plugin:
  ./script/plugin install git://github.com/archan937/rich_i18n.git 
Install i18n if you haven't already:
  sudo gem install i18n
h3. Optional You will have to install the @Formtastic@ plugin when translating @labels@ and @seatholders@ with *Rich-i18n as plugin*:
  ./script/plugin install git://github.com/justinfrench/formtastic.git
When wanting to use @seatholders@, please include @seat_holder.js@ in your template:
  
*Note*: please visit "http://github.com/archan937/seat_holder":http://github.com/archan937/seat_holder for more information about SeatHolder. h3. 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"
h2. Usage Just call the @t@ method on string or symbols to translate using Rich-i18n. h3. 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":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
h3. 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":http://github.com/archan937/rich_i18n/blob/master/lib/rich/i18n/core/string/inflections.rb to see all the methods. h3. 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. h3. Combined keys You can combine translations by using passed string containing translation keys joined with spaces. h3. 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 subclass of @String@. 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"]
h3. 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
  => "Meer gebruikers"
This can be very handy when implementing a CMS in which users change translations. Please note that "http://github.com/archan937/e9s-demo":http://github.com/archan937/e9s-demo uses this feature to highlight translations. Later on this will also be used in "Rich-CMS":http://github.com/archan937/rich_cms, a gem / plugin that makes inplace translating possible (please be patient for this to be released). h3. 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"
h3. 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:  uw.naam@een.website.nl
    
    Question:
      content:      Hoeveel uren zitten in een dag?
    
    Answer:
      content:      24 uur
    
    (search_form)
      criteria:     '&Voorbeeld'
h2. Contact me For support, remarks and requests please mail me at "paul.engel@holder.nl":mailto:paul.engel@holder.nl. h2. Credit This Rails gem / plugin depends on: i18n
"http://github.com/svenfuchs/i18n":http://github.com/svenfuchs/i18n Formtastic (optional)
"http://github.com/justinfrench/formtastic":http://github.com/justinfrench/formtastic Hpricot
"http://github.com/whymirror/hpricot":http://github.com/whymirror/hpricot (thanks Why!) Rich-CMS (optional)
"http://codehero.es/rails_gems_plugins/rich_cms":http://codehero.es/rails_gems_plugins/rich_cms
"http://github.com/archan937/rich_cms":http://github.com/archan937/rich_cms SeatHolder (optional)
"http://codehero.es/jquery_libraries/seat_holder":http://codehero.es/jquery_libraries/seat_holder
"http://github.com/archan937/seat_holder":http://github.com/archan937/seat_holder h2. ToDo's * Provide a rake task to create the Translation model and migration files * Use a better implementation to tackle String interpolation (e.g. "foo #{"bar".t}") to preserve meta data * Most String inflection methods are also defined in rich_pluralization (keep it DRY) h2. Enrichments The all-in-one gem at - "http://codehero.es/rails_gems_plugins/e9s":http://codehero.es/rails_gems_plugins/e9s - "http://github.com/archan937/e9s":http://github.com/archan937/e9s h3. E9s modules * Rich-CMS
"http://codehero.es/rails_gems_plugins/rich_cms":http://codehero.es/rails_gems_plugins/rich_cms
"http://github.com/archan937/rich_cms":http://github.com/archan937/rich_cms * Rich-i18n
"http://codehero.es/rails_gems_plugins/rich_i18n":http://codehero.es/rails_gems_plugins/rich_i18n
"http://github.com/archan937/rich_i18n":http://github.com/archan937/rich_i18n * Rich-pluralization
"http://codehero.es/rails_gems_plugins/rich_pluralization":http://codehero.es/rails_gems_plugins/rich_pluralization
"http://github.com/archan937/rich_pluralization":http://github.com/archan937/rich_pluralization h2. License Copyright (c) 2010 Paul Engel, released under the MIT license "http://holder.nl":http://holder.nl - "http://codehero.es":http://codehero.es - "http://gettopup.com":http://gettopup.com - "http://twitter.com/archan937":http://twitter.com/archan937 - "paul.engel@holder.nl":mailto:paul.engel@holder.nl 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.