Parsing YAML in Rails i18n locale files to get JSON

Testen Sie igumbi kostenlos für 30 Tage. Eine Kreditkarte ist nicht erforderlich.

Ever needed to do internationalization for your rails app? The files in config/locales/ work nice for translations within the app. But what about making these locale files of the different languages available for your DOM manipulating javascript?
YAML and JSON and Rake to the rescue!

It would be nice to have a subset of the locale content available as a JSON object.

The main point is maintainability of the language / locale files. I got the feeling the my app logic will grow over time. New text snippets will be added, as features mature. More languages will be added, even languages I do not even speak.

This calls for a proper setup of the components so you don't end up pulling out your hairs later on. The ideal workflow I envisage is - push the files to be translated to a git repo, so the translator can pick it up and work on it, edit the one specific language file and enter the high quality translations, pull the file back into your project, and presto, you're done.

The core of this is getting the language items from the locale files out of your rails languages files in config/locales. You need a javascript object that you can consume in the DOM.
In the rails app we want only one locale .yml file for each language. And we don't want all the items from the rails locale files be made available for javascript. To accomplish this we create a namespace in the .yml file. I called it "obt", the abbreviation for the Online Booking Tool for Hotels (in German)

        online_booking_tool: "online Buchungstool"
        book: "Buchen"
        save: "Speichern"
        edit: "Bearbeiten"´

... you sort of get the point.

So the exercise is accessing and parsing the YAML of the Rails i18n language files and outputting part of the tree as JSON Object into a language file. I did this as a rake task as I want a static javascript file. Translation don't change that often. As a benefit we get to translation changes versioned with git.

Here the rake task that reads YML structures and writes the static language blocks into a single file:

desc "create a static javascript translation file with each language available as a JSON object"
task :jslang => :environment do
  languages  = %w(de en)
  langfilejs = "public/javascripts/seller/obtlang.js"
  namespace  = "obt","w")
  languages.each do |lang|

I use the javascript language snippet (obtlang.js) as a rails partial. I am a huge fan of javascript file concatenation. This speeds up things and there is no need for any fancy load order management.

In my javascript files I define a language/locale variable and have a very simple translation method. The Igumbi.lang value is set elsewhere in the logic.

var Obt = {
    t: function(key){
        if (Igumbi[Igumbi.lang][key]) {
    return("<span style='color:red;     font-size:2em;'>"+key+"("+Igumbi.lang+") translation missing</span>");

So every time I need to access a translated version of a call like this is used:


Works very nice for me. Hope this helps you figure out how to approach your internationalization.

I also put the code up as a gist so you can grab it from there.

Author: , . You can find me on twitter: @smtm, and as roland.oth on Facebook

igumbi Hotelsoftware - jetzt unverbindlich probieren

Hotelsoftware Demo: System Übersicht der Funktionen

igumbi ist eine online Hotelsoftware mit einem einfachen und schnellen Buchungstool für Ihre Website.

Wir steigern Ihre provisionsfreien Direktbuchungen. Sie sparen Zeit und haben auch mit der iPhone App Zugriff auf Ihre Daten.

Testen Sie igumbi für 30 Tage. Eine Kreditkarte ist nicht erforderlich.


Diese Artikel könnten Sie auch interessieren:


vor almost 5 years

I used your rake file to make json files. I wanted to load them into Globalize.js, but I could not get it working. Now I just put it in a global variable, so I can do t["welcome"] or even t.welcome. The json is parsed into the object at every page request, but I guess that is inherent to JS.

Add a comment


 required, will not be published

Cookies helfen uns bei der Bereitstellung unserer Dienste. Durch die Nutzung unserer Dienste erklären Sie sich damit einverstanden, dass wir Cookies setzen. Datenschutzerklärung & Cookies