Localize Your SLIM Templates In A Second With Slimkeyfy

Slimkeyfy allows you to kickstart a localization project and prepare an app using Slim partials to be translated in a matter of minutes instead of hours or days. Slimkeyfy is a tool that lets you extract plain Strings from Slim template views and replaces them with calls to the I18n.t(:key)-method. The resulting keys and their translations will be streamed to a YAML file.

When we started localizing PhraseApp’s Translation Center we knew that plenty of work lay ahead. Before we could start using PhraseApp as the translation management system for this part of our software, we had to make it localizable; that meant: extracting all the plain strings, naming them with a key, putting them into a YAML structure and finally replacing them in the partials with a call to Rails’ translation method I18n.t(:key).

What Is Slimkeyfy?

Slimkeyfy is a tool that lets you extract plain Strings from Slim template views (and to some extend Rails controllers) and to replace them with calls to I18n’s t(:key_name)method. The resulting keys and their translations will be streamed to a YAML file.

The Pain Points

The pain points here are obvious. Walking through all your views manually can be a…

identifying translations

thinking of key names

replacing the translations with I18n.t(‘.key’) tags

managing your locale YAML files

… truly messy task. Some automation would be useful!

How Slimkeyfy Tackles The Problem

In order to help you with your work, Slimkeyfy semi-automates these tasks for you. Generating translation keys from identified translations. Using these keys to replace the translations in your views and adding them to a YAML file of choice for any given locale.

Before we dive right in let’s first discuss the problem with HTML parsing tools. Most of the time tools like this go for a 80/20 approach stating that 80% can be done right and 20% have to be done manually.

Translating and tagging your Rails app can be an error prone ordeal.

In order to improve this, Slimkeyfy streams in every line that matches the regular expression engine and prompts you to take an action.

Setting Up Slimkeyfy

The setup is quite simple. Go to your Rails app folder and install the gem:

1

gem install slimkeyfy

Or if you prefer Bundler, put it in your Rails Gemfile within your development group:

1

2

3

4

5

group :development do

gem ‘slimkeyfy’

end

bundle install

Slimkeyfy Usage

Using Slimkeyfy for your localization process is as just as simple as the setup:

1

2

slimkeyfy app/views/users/show.html.slim en

Slimkeyfy then starts its work by going through your file line by line. It automatically creates a backup of your original and creates a new YAML file atconfig/locales/view_name.locale.yml.

You will then be prompted to keep the changes or to discard them line by line. If in doubt, just “tag” the line for later rework after slimkeyfy has finished.

Example

Suggested Workflow With Slimkeyfy

We suggest that the overall workflow looks something like this:

Create a branch for a view folder

1

git checkout-bbranch_localization

slimkeyfy the view folder you would like to tag

1

slimkeyfy app/views/folder/locale

Go through all files and verify/add missing translations (check against the .bak files (use git diff))

PhraseApp Integration

After processing your files it is quite easy to push them to PhraseApp. If you have not set up your account yet take a look. Make sure that the latest version of the phrase gem is installed:

1

gem install phrase

If you’re already familiar with the phrase gem you can upload your translation/localization files now. If you need more information on the setup have a look at our detailed guide.

Summary

Making your existing Rails app localizable with Slimkeyfy can be done easily. It’s a great tool to get you started with your localization process. When you are up to speed you can easily start with PhraseApp and enrich the experience of your users by translating your Rails app into any language you want.

Happy translating!

References

Be sure to subscribe and receive all updates from the PhraseApp blog straight to your inbox. You’ll receive localization best practices, about cultural aspects of breaking into new markets, guides and tutorials for optimizing software translation and other industry insights and information. Don’t miss out!