h1. E9s Enrichments (e9s) for internationalization (i18n) and localized pluralization h2. Introduction E9s was created due to the need of simply implementing I18n within a Rails application. This simplifies internationalization of your Rails application making a Rails developers life much easier. E9s is divided into modules (as gem and plugin): "Rich-i18n":http://github.com/archan937/rich_i18n and "Rich-pluralization":http://github.com/archan937/rich_pluralization. A list of E9s' features: h3. I18n * @Localized pluralization@ - Translations only in singular form are sufficient enough as E9s can pluralize in foreign languages * @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 and @pl@ to pluralize * @Combine translations@ - Joining keys with spaces combines translations: @"More houses".t@ returns @"Meer huizen"@ in Dutch * @Preserve i18n meta data@ - E9s preserves the key, the actual used I18n key and the actual translation which you can enquire (this can come in handy when implementing a 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@ - E9s preserves the casing in your translations: @"save".t@ returns @"bewaar"@, @"Save".t@ returns @"Bewaar"@ and @"SAVE".t@ returns @"BEWAAR"@ in Dutch * @Preserve pluralization@ - E9s singularizes or pluralizes your translations depending on the key: @"house".t@ returns @"huis"@ and @"Houses".t@ returns @"Huizen"@ in Dutch h2. Installation Install the E9s gem:
sudo gem install e9sAdd e9s in environment.rb as a gem dependency:
config.gem "e9s"h3. Optional 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 E9s out-of-the-box Set the default locale to @:nl@ in @environment.rb@:
config.i18n.default_locale = :nlRun the Rails console:
./script/consoleStart translating in Dutch:
>> "MORE HOUSES".t => "MEER HUIZEN"h2. Usage 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. h3. Words As E9s automatically singularizes or pluralizes the translation (depending on the passed key), you only have to specify translations in singular form. *Note*: specified in @config/locales/nl.yml@
--- nl: word: "yes": ja "no": nee house: huis letter: brief sign: teken user: gebruiker more: meerh3. Pluralization A *very powerful* feature of E9s is pluralization which resembles the inflections of @ActiveSupport::Inflector@. Unfortunately, specifying inflections within @config/initializers/inflections.rb@ also influences your Rails application and thus causes great problems. E9s provides you to specify pluralization rules for different locales. You have to use regular expressions in order to specify them. *Note*: specified in @config/locales/nl.yml@
--- nl: e9s: singular: - rule: en$ replacement: "" plural: - rule: ee$ replacement: eeën - rule: heid$ replacement: heden - rule: (c|m|n|t)us$ replacement: \1i - rule: | abc, acme, acne, (a|ë|i|o|u|y)$ replacement: \1's exceptions: (ai|eau|ei|li|lieu|ooi|ou|shampoo|spray|vlo)$ - rule: (blad|kind)$ replacement: \1eren exceptions: (aanrecht|advertentie)blad - rule: (e|em|el|er|erd|aar|aard|um|eur|foon|oor|ier|en|ie|eau|show|festival|é)$ replacement: \1s - rule: | (a|e|o|u)\1([^aeiou])$ replacement: \1\2en - rule: | (aï|alia)(s), ([^aeiou][aeiou])([^aeiou])$ replacement: \1\2\2en exceptions: dal, pad, slot, vat, weg, (blad|dag|dak|engel|gat|weg)$ - rule: f$ replacement: ven - rule: s$ replacement: zen - rule: $ replacement: en irregular: gelid: gelederen uncountable: - geld - informatie - rijstFor a complete example, please open "http://github.com/archan937/rich_pluralization/blob/master/locales/nl.yml":http://github.com/archan937/rich_pluralization/blob/master/locales/nl.yml which contains Dutch inflections. h3. String / Symbol methods E9s adds the following methods to strings and symbols: * @t@ - which translates the string or symbol * @pl@ - which pluralizes the string or symbol with inflections of the current I18n locale Further more, E9s has enriched the String class with other 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, E9s 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 @used key@, the @actual used I18n key@ and its @actual translation@. E9s 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 => {:actual_key=>"word.more", :key=>"MORE", :actual_value=>"MEER"}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 => {:actual_key=>"word.more", :key=>"More", :actual_value=>"Meer"} >> "More users".t.merged_strings.last.meta_data => {:actual_key=>"word.user", :key=>"users", :actual_value=>"gebruiker"} >> "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:
>> "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 "
>> "word.house".t => "huis" >> "word.Event".t => "Event" >> "LETTERS".t => "BRIEVEN" >> "application.index.Welcome_to_our_site".t => "Welcome to our site" >> "word.users".t => "gebruikers" >> "Signs".t => "Tekens" >> "MORE USERS".t => "MEER GEBRUIKERS" >> "More houses".t => "Meer huizen"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 depends on: rich_pluralization