Skip to content

Instantly share code, notes, and snippets.

@srih4ri
Created March 16, 2011 15:35
Show Gist options
  • Save srih4ri/872678 to your computer and use it in GitHub Desktop.
Save srih4ri/872678 to your computer and use it in GitHub Desktop.
Generates a new ml.yml using the current en.yml , using old ml.yml's translated string wherever possible . This is really ugly though , there must be something that actually does this neatly
# encoding: UTF-8
require 'yaml'
sf = File.open("original_ml.yml", "r")
src = YAML::load(sf.read)
sf.close
rf = File.open("current_en.yml", "r")
ref = YAML::load(rf.read)
rf.close
@ml_orig = src['ml_IN']
@current_en = ref['en']
@h = Hash.new { |l, k| l[k] = Hash.new(&l.default_proc) }
def walk_and_translate(hash,source_key = nil)
hash.each do |key,value|
if value.is_a? Hash
next_key = source_key.nil? ? "#{key}" : "#{source_key},#{key}"
walk_and_translate(value,next_key)
else
current_key = source_key.nil? ? "#{key}" : "#{source_key},#{key}"
p"Existing #{current_key} => #{value}"
available_translation = fetch_translation(@ml_orig,"#{current_key}")
new_translation = available_translation.nil? ? fetch_translation(@reference_hash,"#{current_key}") : available_translation
p "Generated #{current_key} => #{new_translation}"
key_string = current_key.split(",").collect{|n| "['#{n}']"}.join
eval "@h#{key_string} = #{new_translation.to_s.inspect}"
end
end
end
def fetch_translation(mother_hash,key_chain)
f,l = key_chain.split(",",2)
begin
val = mother_hash.fetch f unless mother_hash.nil?
(l.nil? ? val : fetch_translation(val,l)) unless val.nil?
rescue IndexError
end
end
walk_and_translate(@current_en)
File.open( 'new.yaml', 'w' ) do |out|
YAML.dump( {:ml_IN=>@h}, out )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment