Last Updated: December 27, 2016
·
1.587K
· ProGM

My tricks for i18n in Rails

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 articles.filter_data.*.

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
    ...

Output:

articles:
  filter_data:
    some_key: some value
    some_other_key: some value

Explanation

It's composed in two parts. The first one copies articles.filters.* to articles.filter_data.*

  • i18n-tasks data -y yaml outputs 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 articles.filter

  • 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-merge merges the current data with the one in your locale database

The second part instead deletes the original articles.filters.*:

  • 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 en locale:

echo 'en.my.new.tree' | i18n-tasks tree-convert -f keys -t yaml | i18n-tasks tree-set-value Hello | i18n-tasks data-merge

Output

en:
  my:
    new:
      tree: Hello

Explanation

  • echo "en.my.new.tree": Prints/passes to next pipe that string

  • i18n-tasks tree-convert converts one format to another. Valid formats are keys, yaml and json. In this case, translates the string en.my.new.tree in a yaml tree.

  • i18n-tasks tree-set-value Hello Sets the value of the current tree to "Hello"

  • i18n-tasks data-merge Merge the result with your locale database.

2 Responses
Add your response

Great tips! I love that you're using the commands in a way I never imagined they could be used (especially the Create a new tree branch example!).

I've just released v0.9.8 that adds 3 new tasks: mv, rm, and tree-mv.
They make mass-renaming much simpler and also allow for moving and merging the keys.
The first example in the article is now just:

i18n-tasks mv articles.filters articles.filter_data
over 1 year ago ·

Thank you @glebm ! I updated the article, introducing your update! <3

over 1 year ago ·