Skip to content

Instantly share code, notes, and snippets.

@ifcwebserver
Forked from massive/locale_diff.rb
Last active December 29, 2015 20:49
Show Gist options
  • Save ifcwebserver/7726555 to your computer and use it in GitHub Desktop.
Save ifcwebserver/7726555 to your computer and use it in GitHub Desktop.
This script helps to compare and merge an old version of YAML local file of your target language against an up-to-date master language file
# This script helps to compare and merge an old version of YAML local file of your target language against an up-to-date master language file, which contains new strings
# It still need some improvments so please do not forget to validate the YAML by pasteing it into http://yamllint.com/
# Run: curl https://gist.github.com/raw/7726555/locale_diff.rb | ruby - client.en.yml client.ar.yml > new_client.ar.yml
require 'rubygems'
require 'yaml'
require 'pp'
master_local = ARGV[0]
my_local = ARGV[1]
master = YAML.load_file(master_local )
local = YAML.load_file(my_local )
def diff(root, compared, structure = [])
$level += 1
root.each do |key,value|
if value.class != String
print " "*($level-1)
print key + ":" + "\n"
end
next_root = root[key]
next_compared = compared.nil? ? nil : compared[key]
new_structure = structure.dup << key
if compared.nil? || compared[key].nil?
if value.class == String
print " "*($level-1)
print key
string_quote = value.include?("\"") ? "'" : "\""
print ": " + string_quote + value + string_quote + "\n"
end
else
if next_compared.class == String
print " "*($level-1)
print key
string_quote = next_compared.include?("\"") ? "'" : "\""
print ": " + string_quote + next_compared + string_quote + "\n"
end
end
diff(next_root, next_compared, new_structure) if next_root.kind_of? Hash
end
$level -= 1
# print "\n" # uncomment if you wish to have an empty line when the indentation level changes
end
$level=0
diff(master, local, [my_local])
@ifcwebserver
Copy link
Author

Run:
curl https://gist.github.com/raw/7726555/locale_diff.rb | ruby - client.en.yml client.ar.yml > new_client.ar.yml

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