Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/archan937/rich_i18n
Enrichments (e9s) module for i18n
https://github.com/archan937/rich_i18n
Last synced: about 2 months ago
JSON representation
Enrichments (e9s) module for i18n
- Host: GitHub
- URL: https://github.com/archan937/rich_i18n
- Owner: archan937
- License: mit
- Created: 2010-07-18T21:44:47.000Z (over 14 years ago)
- Default Branch: master
- Last Pushed: 2011-06-22T06:16:34.000Z (over 13 years ago)
- Last Synced: 2024-10-06T13:07:26.031Z (3 months ago)
- Language: Ruby
- Homepage: http://codehero.es/rails_gems_plugins/rich_i18n
- Size: 2.46 MB
- Stars: 9
- Watchers: 3
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.textile
- Changelog: CHANGELOG
- License: MIT-LICENSE
Awesome Lists containing this project
README
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 specifich3. 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 in Rails 3
Add Rich-i18n in @Gemfile@ as a gem dependency:
gem "rich_i18n"Run the following in your console to install with Bundler:
sudo bundle installh3. Using Rich-i18n as gem in Rails 2
Add Rich-i18n in @environment.rb@ as a gem dependency:
config.gem "rich_i18n"Run the following in your console:
sudo rake gems:installh3. Using Rich-i18n as plugin in Rails 3
rails plugin install git://github.com/archan937/rich_i18n.gith3. Using Rich-i18n as plugin in Rails 2
script/plugin install git://github.com/archan937/rich_i18n.gith3. 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. Use the provided Rails generators
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.
h4. In Rails 3
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.
h4. In Rails 2
Run the following in your console:
script/generate rich_i18n_translation -mh2. 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 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"]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: wachtwoordlabel:
user_name: gebruikersnaam
content: berichtQuestion:
content: jouw vraagAnswer:
content: jouw antwoord(search_form)
criteria: uw zoekcriteriaseatholder:
email_address: [email protected]Question:
content: Hoeveel uren zitten in een dag?Answer:
content: 24 uur(search_form)
criteria: '&Voorbeeld'h2. Disabling Rich-i18n logging
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.
h2. Contact me
For support, remarks and requests please mail me at "[email protected]":mailto:[email protected].
h2. Credit
This Rails gem / plugin depends on:
i18n
"http://github.com/svenfuchs/i18n":http://github.com/svenfuchs/i18nHpricot
"http://github.com/whymirror/hpricot":http://github.com/whymirror/hpricot (thanks Why!)Formtastic (optional)
"http://github.com/justinfrench/formtastic":http://github.com/justinfrench/formtasticRich-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_cmsSeatHolder (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_holderh2. ToDo's
* 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)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_pluralizationh2. 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 - "[email protected]":mailto:[email protected]
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.