i18n_screwdriver

Translating applications is often a pain in the ass. The problem with rails i18n is that you have to use keys for every string to translate. That is one too many abstraction layers - I love the gettext syntax but I want to stick to the rails standard libraries. Therefor I created this small toolbox.

Installation

Just include the screwdriver gem in your Bundler Gemfile

gem 'i18n_screwdriver'

Then run the generator that copies the translation rake task to your application task dir.

rails g screwdriver

Usage

Set these constants to make i18n screwdriver aware of the languages you are using

I18n.default_locale
I18n.available_locales

In your views, helpers and controllers use the convenient underscore helper method for all your translations

_("your translation")

Variable interpolation works as for normal strings

_("Hello %{name}!") % {name: "Liah"}

For awesome support for links inside translations you can use this syntax, which uses the normal rails view/ route helpers internally

_("Please <<click here>> for more details or visit <<this link>> to continue."){ [@item, "http://continue.com"] }
_("Open this <<fancy link>> in new window or visit <<this link>> to continue."){ [[@item, target: "_blank"], "http://continue.com"] }

When you are done you have 2 helper rake tasks. The first one scans all your views, controllers and helpers for translations. It removes unused translation strings and creates an application.<lang>.yml file for each of your I18n.available_locales.

rake i18n:update

The second one lets you translate your application.<lang>.yml file line by line. Of course you can edit the application.<lang>.yml file itself - but dont do that for your default language file. It gets recreated everytime you run the tasks - edit those translations in your views e.g. itself.

TRANSLATE=en rake i18n:translate

Test Helpers

In your tests (functionals and integration) you can use the same translation helper

_("your translation")

Next steps

  • also recognize model validation error messages

  • DRY the code

  • test more (= test at all)

  • use ruby_parser instead of regex

  • support interpolation like the following (take care about naming - could be different in other languages)

    _(“my name is %name and I am living in %location”, :name => @name, :location => @location)

Contributing to i18n_screwdriver

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2010 - 2013 Tobias Miesel & Corin Langosch. Released unter the MIT license.