Manage translation and localization with static analysis, for Ruby i18n
Manage translation and localization with static analysis, for Ruby i18n
i18n-tasks helps you find and manage missing and unused translations.
This gem analyses code statically for key usages, such as I18n.t('some.key'), in order to:
Thus addressing the two main problems of [i18n gem][i18n-gem] design:
i18n-tasks can be used with any project using the ruby [i18n gem][i18n-gem] (default in Rails).
Add to your Gemfile:
gem 'i18n-tasks', '~> 1.1.2', group: :development
Copy the default configuration file:
$ cp $(bundle exec i18n-tasks gem-path)/templates/config/i18n-tasks.yml config/
Run your first health check:
$ bundle exec i18n-tasks health
That's it. See Commands for the full list of tasks, or Configuration to tailor the setup to your project.
Optional: copy a test that checks for missing/unused translations on every CI run:
# RSpec
$ cp $(bundle exec i18n-tasks gem-path)/templates/rspec/i18n_spec.rb spec/
# Minitest
$ cp $(bundle exec i18n-tasks gem-path)/templates/minitest/i18n_test.rb test/
Run bundle exec i18n-tasks to get the list of all the tasks with short descriptions.
bundle exec i18n-tasks health checks if any keys are missing or not used,
that interpolation variables are consistent across locales,
and that all the locale files are normalized (auto-formatted):
$ bundle exec i18n-tasks health
See where the keys are used with bundle exec i18n-tasks find:
$ bundle exec i18n-tasks find common.help
$ bundle exec i18n-tasks find 'auth.*'
$ bundle exec i18n-tasks find '{number,currency}.format.*'
Add missing keys with placeholders (base value or humanized key):
$ bundle exec i18n-tasks add-missing
This and other tasks accept arguments:
$ bundle exec i18n-tasks add-missing -v 'TRME %{value}' fr
Pass --help for more information:
$ bundle exec i18n-tasks add-missing --help
Usage: i18n-tasks add-missing [options] [locale ...]
-l, --locales Comma-separated list of locale(s) to process. Default: all. Special: base.
-f, --format Output format: terminal-table, yaml, json, keys, inspect. Default: terminal-table.
-v, --value Value. Interpolates: %{value}, %{human_key}, %{value_or_human_key}, %{key}. Default: %{value_or_human_key}.
-h, --help Display this help message.
Translate missing keys using a backend service of your choice.
$ bundle exec i18n-tasks translate-missing
# accepts backend, from and locales options
$ bundle exec i18n-tasks translate-missing --from=base es fr --backend=google
Available backends:
google – Google Translatedeepl – DeepLyandex – Yandexopenai – OpenAIwatsonx – watsonx$ bundle exec i18n-tasks unused
$ bundle exec i18n-tasks remove-unused
These tasks can infer dynamic keys such as t("category.\#{category.name}") if you set
search.strict to false, or pass --no-strict on the command line.
If you want to keep the ordering from the original language file when using remove-unused, pass
-k or --keep-order.
Remove keys from non-base locales that are absent in the base locale:
$ i18n-tasks prune
Pass -k or --keep-order to preserve the original key ordering in the locale files.
Sort the keys:
$ bundle exec i18n-tasks normalize
Sort the keys, and move them to the respective files as defined by config.write:
$ bundle exec i18n-tasks normalize -p
bundle exec i18n-tasks mv <pattern> <target> is a versatile task to move or delete keys matching the given pattern.
All nodes (leafs or subtrees) matching <pattern> are merged together and moved to <target>.
Rename a node (leaf or subtree):
$ bundle exec i18n-tasks mv user account
Move a node:
$ bundle exec i18n-tasks mv user_alerts user.alerts
Move the children one level up:
$ bundle exec i18n-tasks mv 'alerts.{:}' '\1'
Merge-move multiple nodes:
$ bundle exec i18n-tasks mv '{user,profile}' account
Merge (non-leaf) nodes into parent:
$ bundle exec i18n-tasks mv '{pages}.{a,b}' '\1'
Delete the keys by using the rm task:
$ bundle exec i18n-tasks rm 'user.{old_profile,old_title}' another_key
i18n-tasks also provides composable tasks for reading, writing and manipulating locale data. Examples below.
add-missing implemented with missing, tree-set-value and data-merge:
$ bundle exec i18n-tasks missing -f yaml fr | bundle exec i18n-tasks tree-set-value 'TRME %{value}' | bundle exec i18n-tasks data-merge
remove-unused implemented with unused and data-remove (sans the confirmation):
$ bundle exec i18n-tasks unused -f yaml | bundle exec i18n-tasks data-remove
Remove all keys from fr that do not exist in en. Do not change en:
$ bundle exec i18n-tasks missing -t diff -f yaml en | bundle exec i18n-tasks tree-mv en fr | bundle exec i18n-tasks data-remove
See the full list of tasks with bundle exec i18n-tasks --help.
Configuration is read from config/i18n-tasks.yml or config/i18n-tasks.yml.erb.
Inspect the configuration with bundle exec i18n-tasks config.
Install the [default config file][config] with:
$ cp $(bundle exec i18n-tasks gem-path)/templates/config/i18n-tasks.yml config/
Settings are compatible with Rails by default.
By default, base_locale is set to en and locales are inferred from the paths to data files.
You can override these in the [config][config].
The default data adapter supports YAML and JSON files.
i18n-tasks can manage multiple translation files and read translations from other gems.
To find out more see the data options in the [config][config].
NB: By default, only %{locale}.yml files are read, not namespace.%{locale}.yml. Make sure to check the config.
For writing to locale files i18n-tasks provides three routers.
Pattern router organizes keys based on a list of key patterns, as in the example below:
data:
router: pattern_router
# a list of {key pattern => file} routes, matched top to bottom
write:
# write models.* and views.* keys to the respective files
- ["{models,views}.*", 'config/locales/\1.%{locale}.yml']
# or, write every top-level key namespace to its own file
- ["{:}.*", 'config/locales/\1.%{locale}.yml']
# default, sugar for ['*', path]
- "config/locales/%{locale}.yml"
Conservative router keeps the keys where they are found, or infers the path from base locale. If the key is completely new, conservative router will fall back to pattern router behaviour. Conservative router is the default router.
data:
router: conservative_router
write:
- ["devise.*", "config/locales/devise.%{locale}.yml"]
- "config/locales/%{locale}.yml"
If you want to have i18n-tasks reorganize your existing keys using data.write, either set the router to
pattern_router as above, or run bundle exec i18n-tasks normalize -p (forcing the use of the pattern router for that run).
Isolating router assumes each YAML file is independent and can contain similar keys.
As a result, the translations are written to an alternate target file for each source file
(only the %{locale} part is changed to match target locale). Thus, it is not necessary to
specify any write configuration (in fact, it would be completely ignored).
This can be useful for example when using ViewComponent sidecars
(ViewComponent assigns an implicit scope to each sidecar YAML file but i18n-tasks is not aware of
that logic, resulting in collisions):
app/components/movies_component.en.yml:
en:
title: Movies
app/components/games_component.en.yml
en:
title: Games
This router has a limitation, though: it does not support detecting missing keys from code usage (since it is not aware of the implicit scope logic).
A special syntax similar to file glob patterns is used throughout i18n-tasks to match translation keys:
| syntax | description |
|---|---|
* |
matches everything |
: |
matches a single key |
*: |
matches part of a single key |
{a, b.c} |
match any in set, can use : and *, match is captured |
Example of usage:
$ bundle exec i18n-tasks mv "{:}.contents.{*}_body" "\1.attributes.\2.body"
car.contents.name_body ⮕ car.attributes.name.body
car.contents.description_body ⮕ car.attributes.description.body
truck.contents.name_body ⮕ truck.attributes.name.body
truck.contents.description_body ⮕ truck.attributes.description.body
If you store data somewhere but in the filesystem, e.g. in the database or mongodb, you can implement a custom adapter. If you have implemented a custom adapter please share it on [the wiki][wiki].
If you use Rails credentials and want to load e.g. credentials for translation backends, convert your i18n-tasks.yml to i18n-tasks.yml.erb and add
a `req
No open issues yet, or sync has not completed.