grosser / fast_gettext

Ruby GetText, but 3.5x faster + 560x less memory + simple + clean namespace + threadsave + extendable + multiple backends

This URL has Read+Write access

grosser (author)
Tue Feb 24 01:33:59 -0800 2009
commit  1d294aaaaf8ff6025a1ed86ffba4922e45c1150d
tree    c195a2e92a8d5d712debc22c1407475883122c95
parent  f87a09f96bb4e18b46ee25a9d287e440a31d96ca
name age message
file .gitignore Thu Feb 19 00:09:00 -0800 2009 release of 0.1 as gem [grosser]
file README.markdown Loading commit data...
file Rakefile Mon Feb 23 10:55:49 -0800 2009 added i18n::backend::simple benchmark [grosser]
file VERSION.yml
directory benchmark/ Mon Feb 23 10:55:49 -0800 2009 added i18n::backend::simple benchmark [grosser]
file fast_gettext.gemspec
directory lib/
directory spec/
directory vendor/
README.markdown

FastGettext

GetText but 8 times faster, simple, clean namespace (7 vs 34) and threadsave!

Example Rails application

Setup

sudo gem install grosser-fast_gettext -s http://gems.github.com/

Tell Gettext where your mo-files lie:

#e.g. for locale/de/LC_MESSAGES/my_app.mo
FastGettext.add_text_domain('my_app',:path=>'locale')

Choose text domain, and locale for translation

FastGettext.text_domain = 'my_app'
FastGettext.available_locales = ['de','en','fr','en_US','en_UK'] # only allow these locales to be set (optional)
FastGettext.locale = 'de'

Start translating

include FastGettext::Translation
_('Car') == 'Auto'
_('not-found') == 'not-found'
s_('Namespace|no-found') == 'not-found'
n_('Axis','Axis',3) == 'Achsen' #German plural of Axis

Disable translation errors(like no text domain setup) while doing e.g. console session

FastGettext.silence_errors

Speed

50_000 translations small translation file <-> large translation file

Baseline: (doing nothing in a loop)
0.390000s / 2904K

Ideal: (primitive Hash lookup)
1.010000s / 3016K <-> 1.040000s / 3016K

FastGettext:
1.860000s / 3040K <-> 1.830000s / 3040K

GetText:
14.880000s / 5816K <-> 14.810000s / 6008K

Rails I18n Simple:
31.200000s / 10044K

Thread Safety and Rails

text_domains is not stored thread-save, so that they can be added inside the environment.rb,
and do not need to be readded for every thread (parsing takes time...).

Rails

Try the gettext_i18n_rails plugin, it simplifies the setup.

Setting available_locales,text_domain or locale will not work inside the evironment.rb, since it runs in a different thread then e.g. controllers, so set them inside your application_controller.

#environment.rb after initializers
FastGettext.add_text_domain('accounting',:path=>'locale')
FastGettext.add_text_domain('frontend',:path=>'locale')
...

#application_controller.rb
class ApplicationController ...
  include FastGettext::Translation
  before_filter :set_locale
  def set_locale
    FastGettext.available_locales = ['de','en',...]
    FastGettext.text_domain = 'frontend'
    sessions[:locale] = I18n.locale = FastGettext.set_locale(params[:locale] || sessions[:locale] || 'en')
  end

#application_helper.rb
module ApplicationHelper
  include FastGettext::Translation
  ...

Updating translations

ATM you have to use the original GetText to create and manage your po/mo-files.

Author

Mofile parsing from Masao Mutoh, see vendor/README

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