Parsing YAML in Rails i18n locale files to get JSON

 

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)

de:
    obt:
        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"
  file=File.new(langfilejs,"w")
  languages.each do |lang|
    langfile=YAML::load(File.open("config/locales/#{lang}.yml"))
    file.write(namespace+lang+"="+langfile[lang][namespace].to_json+";\n\n")
  end
  file.close
end

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(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:

Obt.t('book').

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: , igumbi.com. You can find me on twitter: @smtm, and as roland.oth On Facebook.
igumbi online 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. Mit dynamischen Preisen auf Ihrer Website und in den Portalen erhöhen Sie Ihren Umsatz und Gewinn.

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

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

Diese Artikel könnten Sie auch interessieren:
  • Hotel Homepage ohne Flash Werfen Sie Flash raus aus Ihrer Hotelwebsite: Verwenden Sie Javascript für Animationen und Bildgallerien und ein CMS für Ihre Seiten.
  • Mehrsprachigkeit - Umstellung in der Hotelsoftware Die mehrsprachige Version des online Buchungstools geht nun in Vorbereitung. Ziel ist es die Conversion Rate des online Buchungstools zu steigern
  • Facebook locale Settings für Österreich Um Seiten im Facebook "likeable "zu machen gibt es Open Graph Tags für den HEAD Bereich einer Webpage. Sprachversion für Österreich mit de_DE.
  • Offline App with rails 3.1 and rack-offline Use rack-offline for your mobile HTML5 offline App for a dynamic application.manifest with a revision hash that updates when your source code changes.
  • Grep i18n elements from the rails app/views Using Grep to find the lines that need attention in your rails app/views folder. this speeds up you rails i18n internationalization efforts.
  • Liste der wichtigsten Länder Die Reihenfolge in der Länderliste kann nun angepasst werden . Die Länder werden bei Adressen in der Datenbank mit dem zwei Buchstaben ISO Codes gespeichert. Grundlage für Übersetzung der Ländernamen bei Vorlagen und Mails.

Comments

Marcel
vor about 10 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

required, will not be published