A collection of rails3 tasks and helpers that make translating with rails-i18n fun again
Ruby JavaScript CoffeeScript
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
app/assets/javascrips/i18n_screwdriver
lib
spec
.gitignore
.ruby-version
Dockerfile
Gemfile
Gemfile.lock
LICENSE.txt
README.rdoc
Rakefile
docker-compose.yml
i18n_screwdriver.gemspec

README.rdoc

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

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

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