Search my site
Twitter
Flickr
Archives
« Finally cured of the 64-bit Windows 7 SVN "file or directory is corrupted or unreadable" problem | Main | Find the current_controller? in Rails »
Tuesday
Nov162010

Convert YAML to JSON i18n internationalization locale files

This is a convenient ruby script that I use to transform Rails i18n YAML locales into JSON for use in javascript UI. I've been developeing a lot thick client ExtJS interfaces

Download the GIST

# Transform a YAML i18n local files into JSON object.
# suitable when using Rails YAML for javascript UI translations.
#
# This takes files DIR_ROOT/locale and outputs DIR_ROOT/js/locale
# filenames are determined by YAML filename.  
#
# Mark Young @zarzax http://www.zarzax.com Nov 2010
#
# Transforms YAML en.yml:
# en:
#   home:
#     link_name: 'testing'
# Into compressed JSON en.js:
# Ext.locale = {'home': {'home': {'link_name': 'testing'}}

require 'rubygems'
require 'json'
require 'yaml'

DIR_ROOT = File.expand_path(File.dirname(__FILE__))
YAML_ROOT = File.join(DIR_ROOT, 'locales')
JS_ROOT = File.join(DIR_ROOT, 'js', 'locales')

# locale javascript namespace
JS_NAMESPACE = 'Ext.locale = '

Dir[File.join(YAML_ROOT, '*.yml')].sort.each { |locale| 
  locale_yml = YAML::load(IO.read(locale))
  puts 'Filename: ' + locale
  puts 'Filename JSON: ' + locale_yml.to_json
  File.open(
    File.join(JS_ROOT, File.basename(locale, '.*') + '.js'), 'w') {
      |f| f.write(JS_NAMESPACE + locale_yml[File.basename(locale, '.*')].to_json)
  }
}

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (7)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    Convert YAML to JSON i18n internationalization locale files - Posts - zarzax the blog
  • Response
    serrurier sur paris 13
  • Response
    We have to locate the different file through the source of internet. It may concern with the few new reason. These are the main causes of the main purpose. These are the main causes of the new development according to the international features through the help of internet.
  • Response
    Response: website building
    Convert YAML to JSON i18n internationalization locale files - Posts - zarzax the blog
  • Response
    Response: irk website users
    Convert YAML to JSON i18n internationalization locale files - Posts - zarzax the blog
  • Response
    Response: computer viruses
    Convert YAML to JSON i18n internationalization locale files - Posts - zarzax the blog
  • Response
    Response: Tutaj
    Convert YAML to JSON i18n internationalization locale files - Posts - zarzax the blog

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.