Skip to content

Instantly share code, notes, and snippets.

@tigris
Created September 17, 2010 00:12
Show Gist options
  • Save tigris/583424 to your computer and use it in GitHub Desktop.
Save tigris/583424 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'pathname'
require 'yaml'
def expand(new_yaml, yaml)
yaml.keys.each do |k|
append(new_yaml, k, yaml[k])
end
end
def append(new_yaml, key, val)
tree = key.split(/\./)
current = tree.shift
if tree.size == 0
new_yaml[current] = val.to_s unless val.to_s == ''
else
new_yaml[current] = {} unless new_yaml.key?(current)
append(new_yaml[current], tree.join('.'), val)
end
end
file = Pathname.new(ARGV[0])
locale = file.to_s.match(/\/([a-z]{2}(?:\-[A-Z]{2})?)\//)[1] || raise(ArgumentError.new('Could not decipher locale from filename.'))
yaml = YAML.load_file(ARGV[0])
new_yaml = {}
expand(new_yaml, yaml)
if matches = file.to_s.match(/common\.yaml$/)
new_file = Pathname.new("./config/locales/#{locale}.yml")
else
new_file = Pathname.new("./config/locales/#{locale}/#{file.basename.to_s.gsub(/\.yaml$/, '.yml')}")
end
new_file.open('w') do |f|
f.write({ locale => new_yaml }.to_yaml)
end
#!/usr/bin/env ruby
require 'yaml'
require 'json'
require 'pathname'
require 'fileutils'
def flatten(new_yaml, yaml, tree = nil)
yaml.each do |k, v|
full_key = tree.nil? ? k : [tree, k].join('.')
if v.is_a? String
new_yaml[full_key] = v.gsub(/"/, '"')
elsif v.nil?
$stderr.puts "#{full_key}: nil value"
else
flatten(new_yaml, v, full_key)
end
end
end
file = Pathname.new(ARGV[0])
yaml = YAML.load(file.read)
locale = yaml.keys.first
yaml = yaml[locale]
FileUtils.mkdir_p "./tmp/mygengo/#{locale}"
if matches = file.to_s.match(/locales\/([^\/]+)$/)
# Mygengo uses the filename as the "section" name, so need to rename the "generic" yaml to something more useful.
new_file = Pathname.new("./tmp/mygengo/#{locale}/common.yml")
else
new_file = Pathname.new("./tmp/mygengo/#{locale}/#{file.basename}")
end
new_yaml = {}
flatten(new_yaml, yaml)
new_file.open('w') do |f|
f.write new_yaml.to_json
end
namespace 'mygengo' do
desc 'Exports the rails i18n locale files from config/locales into tmp/mygengo as flat json.'
task :export => [:clear_tmp_dir] do
%w(en es <your locales here>).each do |locale|
sh "#{RAILS_ROOT}/bin/locales-flatten.rb #{RAILS_ROOT}/config/locales/#{locale}.yml"
sh "for file in `find #{RAILS_ROOT}/config/locales/#{locale} -type f`; do #{RAILS_ROOT}/bin/locales-flatten.rb $file; done"
end
end
desc 'Imports yaml files from mygengo into config/locales ready for commiting.'
task :import => [:clear_tmp_dir] do
sh "wget -q -O #{RAILS_ROOT}/tmp/mygengo/all.zip http://mygengo.com/string/p/<project name>/export/all/<project token>"
sh "cd #{RAILS_ROOT}/tmp/mygengo && unzip all.zip"
%w(en es <your locales here>).each do |locale|
sh "for file in `ls #{RAILS_ROOT}/tmp/mygengo/#{locale}/*`; do #{RAILS_ROOT}/bin/locales-expand.rb $file; done"
end
end
task :clear_tmp_dir do
sh "rm -rf #{RAILS_ROOT}/tmp/mygengo"
sh "mkdir -p #{RAILS_ROOT}/tmp/mygengo"
end
end
@tigris
Copy link
Author

tigris commented Sep 17, 2010

.rb files go in RAILS_ROOT/bin, rake file goes in RAILS_ROOT/lib/tasks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment