Internationalize (I18n) an Array of Strings in Rails

Sometimes you would like to define an array of strings in your YAML config files, for example in the YAML text files in config/locales which define your translations. It is possible to define arrays in YAML, but it is a bit tricky.

The following line in your yml localization file will result in an ArgumentError (syntax error on line x, col y)

animals: ["duck","pig","cow"]

Why? WTF? Well, if you put in spaces after the comma, then it works:

animals: ["duck", "pig", "cow"]

In YAML, sequences like this are called inline sequences. They require a space between the different items of the sequence. In Ruby itself the spaces do not matter if you want to define an array. Thus the error is not fault of the I18n API, it will also occurs in any YAML file which is loaded and processed by a YAML parser. If you store the line above in a YAML file animals.yml, you will get the ArgumentError if the spaces are missing while trying to open it with

File.open( 'animals.yaml' ) { |yf| YAML::load( yf ) }

If you forget just a few spaces, it won’t work anymore. This is quite common in programming, sometimes it is a single comma, colon or space and the whole program stops working. Software is brittle.