Skip to content

Instantly share code, notes, and snippets.

@zarzax
Created November 16, 2010 14:00
Show Gist options
  • Select an option

  • Save zarzax/701844 to your computer and use it in GitHub Desktop.

Select an option

Save zarzax/701844 to your computer and use it in GitHub Desktop.
convert YAML into JSON i18n internationalization locale files
# 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 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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment