/i18n_screwdriver

A collection of rails3 tasks and helpers that make translating with rails-i18n fun again

Primary LanguageRubyMIT LicenseMIT

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")

Starting with version 6.0 you can also use symbols as keys

_(:my_long_text)

Starting with version 7.0 we added namespaces. A translation in the form of _(“User|Name”) will use the full string as a key but only display the part after the pipe, so “Name”. This is useful if you want to have text translated differently according to the context in some languages but have it the same in the source language. For example “User|Name” (in German “Benutzername”) and “Account|Name” (in German “Accountname”).

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

Node Package

Requires i18n-js as a peer dependency.

First install the npm-package

yarn add i18n-screwdriver

And then configure I18n with the i18n-screwdriver plugin

import I18n from "i18n-js"
import configure from "i18n-screwdriver"

configure(I18n)

I18n.screw("Your translations")

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 - 2021 Tobias Miesel & Corin Langosch. Released unter the MIT license.