archan937 / rich_pluralization

Enrichments (e9s) module for localized pluralization
http://codehero.es/rails_gems_plugins/rich_pluralization
MIT License
9 stars 4 forks source link

h1. Rich-pluralization

Enrichments (e9s) module for localized pluralization

h2. Introduction

Rich-pluralization is a module of E9s ("http://github.com/archan937/e9s":http://github.com/archan937/e9s) which provides localized pluralization in combination with i18n. Doing this enables you to only translate words in singular form as the module pluralizes the translation for you.

h3. Pluralization

As as example, in the Dutch locale you can do the following:

  >> "Appendix".pl
  => "Appendices"

Dutch pluralization rules are provided along with Rich-pluralization and has a 86% test coverage out of 54977 words which are provided by "INL (Instituut voor Nederlandse Lexicologie)":http://www.inl.nl. Unfortunately we can't distribute the complete list of nouns due to the closed nature of the database.

h3. Inflections

Rich-pluralization preserves the casing in the pluralized result:

  >> "huis".pl
  => "huizen"
  >> "Huis".pl
  => "Huizen"
  >> "HUIS".pl
  => "HUIZEN"

Note: @I18n.locale@ is @:nl@

h2. Installation

h3. Using Rich-pluralization as gem

Install the Rich-pluralization gem:

  sudo gem install rich_pluralization

Add rich_pluralization in @environment.rb@ as a gem dependency:

  config.gem "rich_pluralization"

h3. Using Rich-pluralization as plugin

Install the Rich-pluralization plugin:

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

Install i18n if you haven't already:

  sudo gem install i18n

h3. Testing Rich-pluralization out-of-the-box

Set the default locale to @:nl@ in @environment.rb@:

  config.i18n.default_locale = :nl

Run the Rails console:

  ./script/console

Start pluralizing in Dutch:

  >> "Academicus".pl
  => "Academici"

h2. Usage

Just call the @pl@ method on string or symbols to pluralize.

Note: @I18n.locale@ is @:nl@

  >> "brief".pl
  => "brieven"
  >> "gebruiker".pl
  => "gebruikers"
  >> :museum.pl
  => "musea"
  >> "afspraak".pl
  => "afspraken"

h3. String methods

Rich-pluralization 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_pluralization/blob/master/lib/rich/pluralization/core/string/inflections.rb":http://github.com/archan937/rich_pluralization/blob/master/lib/rich/pluralization/core/string/inflections.rb to see all the methods.

h3. Adding pluralization rules / inflections

Rich-pluralization inflections resembles the inflections of @ActiveSupport::Inflector@. Unfortunately, specifying inflections within @config/initializers/inflections.rb@ influences your Rails application and thus causes great problems.

To specify pluralization rules for different locales. You have to use regular expressions and put them in @config/locales/language.yml@.

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
      - rijst

For 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.

Note: The Dutch pluralization are tested on 54977 words provided by "INL (Instituut voor Nederlandse Lexicologie)":http://www.inl.nl. The test coverage is 86% (46366 words passed).

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

h2. ToDo's

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

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.