Rails - Multi Language with Fast_Gettext

  • sudo gem install gettext --no-ri --no-rdoc
  • sudo gem install fast_gettext --no-ri --no-rdoc
  • script/plugin install git://github.com/grosser/gettext_i18n_rails.git (didn't work as gem)
  • environment.rb: see code example at the bottom
  • if this is your first translation: cp locale/app.pot locale/de/app.po for every locale you want to use
  • use method "_" like _('text') in your rails code
  • run rake gettext:find to let GetText find all translations used
  • 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
  • touch tmp/restart.txt to apply the translations

Be careful using unicode characters!

The time running rake gettext:find grows exponentially with the number of unicode characters between two write outs (<%= %>). So up 5 unicode characters may still be o.k.

Variables inside Strings

puts _("Welcome, %{name}") % { :name => current_user.name }

Plural

puts n_("A file was removed", "%{num} files were removed", n) % { :num => n }

environment.rb:

    require 'fast_gettext'
    FastGettext.available_locales = [ 'en', 'de' ]
    FastGettext.add_text_domain('app', :path=>'locale', :type=>:po)
    FastGettext.text_domain = 'app'

    Rails::Initializer.run do |config|
      # Versionsnummern selber rausfinden:
      config.gem 'spreadsheet', :version => '=0.6.4.1'
      config.gem "fast_gettext", :version => "=0.5.7"
      config.gem "gettext", :version => '=2.1.0'
Over 13 years ago