Localize Your SLIM Templates In A Second With Slimkeyfy
When we started localizing Phrase’s Translation Center we knew that plenty of work lay ahead. Before we could start using Phrase 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).
See here for the source code https://github.com/phrase/slimkeyfy
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:
gem install slimkeyfy
Or if you prefer Bundler, put it in your Rails Gemfile within your development group:
group :development do gem ‘slimkeyfy’ end bundle install
Using Slimkeyfy for your localization process is as just as simple as the setup:
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 at
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.
Suggested Workflow With Slimkeyfy
We suggest that the overall workflow looks something like this:
Create a branch for a view folder
git checkout -b branch_localization
slimkeyfy the view folder you would like to tag
slimkeyfy app/views/folder/ locale
Go through all files and verify/add missing translations (check against the .bak files (use git diff))
git diff app/views/folder/views.html.slim
Add your translations + keys to your locale file(s) [optional: Use the I18n-tasks gem to find missing translations]
Go through all your views and click through everything to actually “see” what changed.
If everything is fine - clean up (remove .baks, remove temporary YAML files)
rm app/views/folder/*.bak rm config/locales/users.en.yml
After processing your files it is quite easy to push them to Phrase. If you have not set up your account yet take a look. Make sure that the latest version of the phrase gem is installed:
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.
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 Phrase and enrich the experience of your users by translating your Rails app into any language you want.
- Slimkeyfy on Github
- Phrase on Github
- Rails Internationalization Guide applications.**
Be sure to subscribe and receive all updates from the Phrase 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!
Last updated on October 28, 2022.