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_i18nAdd 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.gitInstall i18n if you haven't already:
sudo gem install i18nh3. 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.gitWhen 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/consoleStart 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: meerh3. 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 => "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:Meer gebruikers "
>> "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