Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GetText / Rails

GetText / Rails

Translations are hard.
Use GetText, a Unix standard, instead of Rails-I18N.

Sylvain Abélard

January 10, 2012
Tweet

More Decks by Sylvain Abélard

Other Decks in Technology

Transcript

  1. $ finger abelar_s • EPITA MTI 2008 • Using Rails

    since my 1st internship, sept.2006 (that was around rails 1.0) • R&D engineer at Faveod • We develop, market and sell our technology Faveod Designer • Faveod Designer helps you create complex web applications faster • You describe your specs and it just works. Easier, better, faster, stronger http://www.linkedin.com/in/sylvainabelard
  2. Meet GetText • Why? • You’ll want to make your

    app global • GetText is a UNIX standard • How? • Translators’ horror stories • Show me some code! • What? • Tools around GetText • Alternatives GNU GetText: GetText’s Never Useless
  3. Rails I18N • Simple • Always has a default •

    No complex keys • Never get « no translation found » • Never get fuzzy translations shown GNU GetText: GetText’s Never Useless
  4. Horror Stories No, really. Did you try? • One word,

    one translation? • Grammar: number, gender, case • Number looks simple enough: car/cars, but child/children and person/people • Many languages have a difference for one, two and more. Some have even more. • Gender is often masculine, feminine or neuter (and rarely intuitive) • Some languages have differences for inanimate and animate (personal or impersonal) • Conjugation: person, tense, mood, voice, aspect • ... ok, let’s not even try this
  5. What to use? • github.com/grosser/fast_gettext • Improvements • 3.5x faster,

    560x less memory • thread safety • simple, clean namespace • Backends • .mo and .po files • .yml • DB • ... code your own Michael Grosser’s FastGetText!
  6. Setup • PO/MO files • These are where you store

    key / values per language • Many great tools for PO files, including an emacs mode • Setting the locale • You select the locale for each use (in Rails: request-wide, it’s thread-safe) • Setup How to GetText part 1 GetText.locale = "fr" init_gettext "my_app" I18n.supported_locales = ["en", "fr", "de", "ar", "ru"] # better: make it guess from existing files I18n.default_locale = "fr" # en is the default
  7. Usage • The « underscore » function • _("string") •

    Variables, the printf way • _("string with %s") % "arg" • _("string with %s and %s") % ["arg1", "arg2"] • Variables, the right way • _("Could not find this %{obj}") % {:obj => _("stuff")} • Now on to our horror stories How to GetText part II
  8. Tricks • Singular / plural • n_("File not found!", "%{n}

    files not found!", n) % {:n => n} • That’s great, but how about tricky plurals? "Plural-Forms: nplurals=2; plural=n == 1 ? 0 : 1;\n" • FastGettext.pluralisation_rule = lambda{|count| count > 5 ? 1 : (count > 2 ? 0 : 2)} • Context • s_("File|Open") # ouvrir • s_("Sports|Open") # open • s_("Even|More|Contexts|Open") # only translates ‘open‘ • p_("even more bindinds", "for contexts", "open") # open How to GetText part III
  9. PO files • Update your files • rake updatepo •

    This will flag unused translations as obsolete and try to guess new ones (« fuzzy ») • Edit your files # translator comments #. extracted comments #: reference #, flag msgid "One file removed" msgid_plural "%d files removed" msgstr[0] "%d slika uklonjenih" msgstr[1] "%d slika uklonjenih" msgstr[2] "%d slika uklonjenih" • Update your translations • rake makemo Don’t do this manually, use tools.
  10. Questions? Also, we’re hiring, but that’s the next slide http://ruby-i18n.org/wiki

    http://www.gnu.org/software/gettext/manual/gettext.html