github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

grosser / gettext_i18n_rails

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 47
    • 5
  • Source
  • Commits
  • Network (5)
  • Issues (1)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Rails: FastGettext, I18n integration -- simple, threadsafe and fast! — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

give the readme some looove 
grosser (author)
Thu Jan 28 12:03:23 -0800 2010
commit  0a671d442a74d490da2034f413b74112974eb14a
tree    b246d42f1eb51c18f1148283cff392dfa8cbef4b
parent  7446498bc216857be218b79464d49b4cac995ac1
gettext_i18n_rails /
name age
history
message
file README.markdown Thu Jan 28 12:03:23 -0800 2010 give the readme some looove [grosser]
file Rakefile Loading commit data...
file init.rb Sat May 16 22:22:58 -0700 2009 refixed rake gems:install bug, thanks @ matthuh... [grosser]
directory lib/ Thu Aug 13 23:15:49 -0700 2009 fixing bug introduced through last merge [grosser]
directory spec/ Sun Dec 13 02:28:34 -0800 2009 preparing for Rails 3 [grosser]
directory tasks/ Sun Dec 13 02:28:34 -0800 2009 preparing for Rails 3 [grosser]
README.markdown

Simple FastGettext / Rails integration.

Do all translations you want with FastGettext, use any other I18n backend as extension/fallback.

Rails does: I18n.t('weir.rails.syntax.i.hate')
We do: _('Just translate my damn text!')
To use I18n calls define a weir.rails.syntax.i.hate translation.

See it working in the example application.

Setup

Installation

This plugin: script/plugin install git://github.com/grosser/gettext_i18n_rails.git

FastGettext: sudo gem install fast_gettext

Want to find used messages in your ruby files ?

GetText 1.93 or GetText 2.0: sudo gem install gettext
GetText 2.0 will render 1.93 unusable, so only install if you do not have apps that use 1.93!

sudo gem install ruby_parser

Locales & initialisation

Copy default locales with dates/sentence-connectors/AR-errors you want from e.g. rails i18n into 'config/locales'

#environment.rb
Rails::Initializer.run do |config|
  ...
  config.gem "fast_gettext", :version => '~>0.4.17'
  #only used for mo/po file generation in development, !do not load(:lib=>false), will needlessly eat ram!
  config.gem "gettext", :lib => false, :version => '>=1.9.3'
end

#config/initialisers/fast_gettext.rb
FastGettext.add_text_domain 'app', :path => 'locale'

#application_controller
class ApplicationController < ...
  before_filter :set_gettext_locale
  def set_gettext_locale
    FastGettext.text_domain = 'app'
    FastGettext.available_locales = ['en','de'] #all you want to allow
    super
  end

Translating

Getting started

Option A: Traditional mo/po files

  • use some _('translations')
  • run rake gettext:find, to let GetText find all translations used
  • (optional) run rake gettext:store_model_attributes, to parse the database for columns that can be translated
  • if this is your first translation: cp locale/app.pot locale/de/app.po for every locale you want to use
  • translate messages in 'locale/de/app.po' (leave msgstr blank and msgstr == msgid)
    new translations will be marked "fuzzy", search for this and remove it, so that they will be used. Obsolete translations are marked with ~#, they usually can be removed since they are no longer needed
  • run rake gettext:pack to write GetText format translation files

Option B: Database

This is the most scalable method, since all translators can work simultanousely and online.

Most easy to use with the translation database Rails engine. FastGettext setup would look like:

include FastGettext::TranslationRepository::Db.require_models #load and include default models
FastGettext.add_text_domain 'app', :type=>:db, :model=>TranslationKey

Translations can be edited under /translation_keys

I18n

I18n.locale <==> FastGettext.locale.to_sym
I18n.locale = :de <==> FastGettext.locale = 'de'

Any call to I18n that matches a gettext key will be translated through FastGettext.

Namespaces

Car|Model means Model in namespace Car.
You do not have to translate this into english "Model", if you use the namespace-aware translation

s_('Car|Model') == 'Model' #when no translation was found

ActiveRecord - error messages

ActiveRecord error messages are translated through Rails::I18n, but model names and model attributes are translated through FastGettext. Therefore a validation error on a BigCar's wheels_size needs _('big car') and _('BigCar|Wheels size') to display localized.

The model/attribute translations can be found through rake gettext:store_model_attributes, (which ignores some commonly untranslated columns like id,type,xxx_count,...).

Error messages can be translated through FastGettext, if the ':message' is a translation-id or the matching Rails I18n key is translated. In any other case they go through the SimpleBackend.

Option A:

Define a translation for "I need my rating!" and use it as message.

validates_inclusion_of :rating, :in=>1..5, :message=>N_('I need my rating!')

Option B:

Do not use :message

validates_inclusion_of :rating, :in=>1..5

and make a translation for the I18n key: activerecord.errors.models.rating.attributes.rating.inclusion

Option C:

Add a translation to each config/locales/*.yml files

en:
  activerecord:
    errors:
      models:
        rating:
          attributes:
            rating:
              inclusion: " -- please choose!"

The rails I18n guide can help with Option B and C.

Plurals

FastGettext supports pluralization

n_('Apple','Apples',3) == 'Apples'

Unfound translations

Sometimes translations like _("x"+"u") cannot be fond. You have 4 options:

  • add N_('xu') somewhere else in the code, so the parser sees it
  • add N_('xu') in a totally seperate file like locale/unfound_translations.rb, so the parser sees it
  • use the gettext_test_log rails plugin to find all translations that where used while testing
  • add a Logger to a translation Chain, so every unfound translations is logged (example))

Author

  • ruby gettext extractor from retoo

Michael Grosser
grosser.michael@gmail.com
Hereby placed under public domain, do what you want, just do not hold me accountable...

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server