I hate working with i18n. Refactoring partials, rename controllers, actions and many other simple tasks becomes really a pain in the ass, when you have to deal with many translations and locales.
Luckily there's this great gem, that makes our life easier. :D
It's main features are to manage unused translations, add missing one, but also propagate a certain translation to other or new locales.
Over these simple but useful features, i18n-tasks gem provides also a set of composable tasks that are very useful to mass-rename, insert or delete data from locales.
Unfortunately these features are not really straight forward, so I created a small cheatsheet with a few examples, with a short explanation.
Mass move a translation tree
It moves all
articles.filters.* branch under
i18n-tasks data -f yaml | i18n-tasks tree-filter articles.filters.* | i18n-tasks tree-rename-key *.filters filter_data | i18n-tasks data-merge i18n-tasks data -f yaml | i18n-tasks tree-filter articles.filters.* | i18n-tasks data-remove
Example data set:
articles: filters: some_key: some value some_other_key: some value ...
articles: filter_data: some_key: some value some_other_key: some value
It's composed in two parts. The first one copies
i18n-tasks data -y yamloutputs all data as yaml. (any locale you have installed in your project).
i18n-tasks tree-filter [pattern]filters the previously outputted data given a pattern. In my example, it gets only children of
i18n-tasks tree-rename-key [pattern] [new_value]renames everything that starts with .filters with the word
filter_data. Take care: it renames only the last word of the chain. If your pattern is `.a.b.c.d
, only **d** will get renamed. For instance, if you want to rename both **c** and **d**, you have to calltree-rename-key` two times.
data-mergemerges the current data with the one in your locale database
The second part instead deletes the original
data-remove: removes the filtered data from your locale database.
EDIT: As i18n-tasks v0.9.8 this can be replaced by:
i18n-tasks mv articles.filters articles.filter_data
Thanks @glebm in the comments below!
Create a new tree branch
Creates a branch with key
my.new.tree in your
echo 'en.my.new.tree' | i18n-tasks tree-convert -f keys -t yaml | i18n-tasks tree-set-value Hello | i18n-tasks data-merge
en: my: new: tree: Hello
echo "en.my.new.tree": Prints/passes to next pipe that string
i18n-tasks tree-convertconverts one format to another. Valid formats are
json. In this case, translates the string
en.my.new.treein a yaml tree.
i18n-tasks tree-set-value HelloSets the value of the current tree to "Hello"
i18n-tasks data-mergeMerge the result with your locale database.