Enrichments (e9s) module for internationalization (i18n)
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:
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-endDefault 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 thet
method on string or symbols to translateCombine translations
– Joining keys with spaces combines translations:"Male / Female".t
returns"Man / Vrouw"
in DutchPreserve i18n meta data
– Rich-i18n preserves the translationkey
,value
,locale
andderivative key
(the argument passed for translation). Enquiring this can come in handy when implementing an internationalization CMS (see Rich-CMS).
Labels, seatholders and default values
– Not only translate labels, but also hint text (so calledseatholders
) and even translate default valuesUnobtrusive implementation
– Translate labels and seatholders unobtrusively, in other words: leave yoursemantic_form_for
(view) code completely untouchedSpecific translations
– Not only specify general translations for labels and seatholders, but make them model or even form specific
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
Localized pluralization
– Translations only in singular form are sufficient enough as E9s can pluralize in foreign languagesAn easy interface for localized pluralizations
– Just call thepl
method on string or symbols to pluralizePreserve 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.
Add Rich-i18n in Gemfile
as a gem dependency:
gem "rich_i18n"
Run the following in your console to install with Bundler:
sudo bundle install
Add Rich-i18n in environment.rb
as a gem dependency:
config.gem "rich_i18n"
Run the following in your console:
sudo rake gems:install
rails plugin install git://github.com/archan937/rich_i18n.git
script/plugin install git://github.com/archan937/rich_i18n.git
Run the Rails console:
./script/console
Start translating in Dutch:
>> I18n.locale = :nl => :nl >> "Male / Female".t => "Man / Vrouw"
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.
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.
Run the following in your console:
script/generate rich_i18n_translation -m
Just call the t
method on string or symbols to translate using Rich-i18n.
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
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.
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.
You can combine translations by using passed string containing translation keys joined with spaces.
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"]
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).
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"
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'
Just like ActiveRecord::Base.logger
and ActionController::Base.logger
, you can set Rich::I18n::Engine.logger
to nil
in order to disable logging messages printed by Rich-i18n.
For support, remarks and requests please mail me at paul.engel@holder.nl.
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
- Handle the click on inputs with seatholders better
- 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)
The all-in-one gem at – http://codehero.es/rails_gems_plugins/e9s – http://github.com/archan937/e9s
- Rich-CMS
http://codehero.es/rails_gems_plugins/rich_cms
http://github.com/archan937/rich_cms - Rich-i18n
http://codehero.es/rails_gems_plugins/rich_i18n
http://github.com/archan937/rich_i18n - Rich-pluralization
http://codehero.es/rails_gems_plugins/rich_pluralization
http://github.com/archan937/rich_pluralization
Copyright © 2010 Paul Engel, released under the MIT license
http://holder.nl – http://codehero.es – http://gettopup.com – http://twitter.com/archan937 – 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.