4 min. read

Coming from FastGettext

Michaël Hoste

Michaël Hoste

Founder & CEO

Who is Michaël?

Michaël is the founder of Translation.io. He is always interested in hearing your feedback on the product.

Feel free to get in touch: michael@translation.io

If you are currently using FastGettext and/or gettext_i18n_rails to translate your Rails application and you would like to switch to Translation.io, this article will help you with the process of updating your codebase to make it work.

If you are not sure what you are doing, consider working in branch :-)

The Gemfile

The Gemfile should only contain the translation gem for all environments :

gem 'translation'

Make sure to remove any references to other gems like :

gem 'gettext_i18n_rails'
gem 'gettext',     :require => false
gem 'ruby_parser', :require => false
gem 'gettext'

You don’t have to include gettext neither, translation already depends on its right version and will therefore include it for you.

Initializer

You probably have a fast_gettext.rb or a gettext.rb initializer file containing this kind of configuration :

FastGettext.add_text_domain 'app', :path => 'config/gettext_locales',
                                   :type => :po
FastGettext.default_available_locales = ['en', 'fr', 'de', 'nl']
FastGettext.default_text_domain       = 'app'

You won’t of course need it anymore.

New Translation.io project

When you create a project on Translation.io, you will be asked to create an initializer named translation.rb :

TranslationIO.configure do |config|
  config.api_key        = 'xxxxxxxxxxxxxxxxxxxxxxxx'
  config.source_locale  = 'en'
  config.target_locales = ['fr', 'de', 'nl']

  # Uncomment this if you don't want to use gettext
  # config.disable_gettext = true

  # Uncomment this if you already use gettext or fast_gettext
  # config.locales_path = File.join('path', 'to', 'gettext_locale')
end

There you have to correclty set config.target_locales to the former target locales.

It is very likely that your existing POT/PO files are not in config/locales/gettext. In that case, just move them there (keeping the right structure) or set config.locales_path to the correct base location used by FastGettext (usually Rails.root.join('locale')).

Translation.io only supports one unique textdomain named app. If you were using multiple textdomains, you will have to merge them. If your domain was not named app you will have to rename all the PO/POT files to app.xx.

Controllers and before filter

You probably have a before_action somewhere in your controller hierarchy to set the current locale (I18n.locale) according to the URL, the session, a cookie, etc.

translation ships with a generic implementation that can be called directly from all ActionController::Base descendants :

before_action :set_locale

But you can of course continue to use your own method if you need custom behaviour by defining it. For instance :

before_action :set_locale

def set_locale
  I18n.locale = current_user.locale || I18n.default_locale
end

That’s it

After performing this set of changes, you should be ready to translate your rails applications with Translation.io. Don’t hesitate to contact us if you think something is missing or not clear enough.

Published on July 29, 2014
Ready to give it a try? Start your free 7-days trial