Skip to content

Instantly share code, notes, and snippets.

@alexandre-mbm
Forked from ifcwebserver/locale_diff.rb
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexandre-mbm/c3a19edd3303d955d803 to your computer and use it in GitHub Desktop.
Save alexandre-mbm/c3a19edd3303d955d803 to your computer and use it in GitHub Desktop.

locale_merge.rb

Dependencies:

sudo apt-get install ruby rails

Use:

curl -L http://git.io/locale_merge.rb | ruby - en.yml pt.yml > new-pt.yml

yamldiff

Installation:

sudo gem install yamldiff

Use:

$ yamldiff new-pt.yml pt.yml 
Missing key: taginfo.project
Missing key: taginfo.projects
Missing key: taginfo.comparison
$ yamldiff -i new-pt.yml en.yml 

Proposal: workflow to translate the taginfo

Let's illustrate the translation from English to Portuguese.

  1. Satisfies the initial dependencies on Ubuntu (preferably):

$ sudo apt-get install ruby rails

  2. Generate the a new pt.yml file with keys updated:

$ curl -L http://git.io/locale_merge.rb | ruby - en.yml pt.yml > new-pt.yml
$ cp pt.yml old-pt.yml  # creates a backup file
$ cp new-pt.yml pt.yml  # replaces the working version to git

  3. Install the Yamldiff fork that I did:

$ bundle
$ gem build yamldiff.gemspec
$ sudo gem install yamldiff-VERSION.gem

    Where VERSION is something as 0.0.10. See ls *.gem or version.rb.

  4. Use the yamldiff with the new file generated in step 2:

$ yamldiff pt.yml old-pt.yml  # what was missing in the old file
Missing key: taginfo.project
Missing key: taginfo.projects
Missing key: taginfo.comparison
Missing key: help.keyboard.projects_page
Missing key: help.keyboard.compare_page
$ yamldiff -i en.yml pt.yml  # keys untranslated in the new file
pages.key.filter.label
pages.key.overview.objects_last_edited_by
pages.key.other_keys_used.to_count_tooltip
pages.key.other_keys_used.other_key_tooltip
pages.key.other_keys_used.from_count_tooltip
$ yamldiff new-pt.yml pt.yml  # follow your changes
Key content differs: osm.nodes
Diff:
-Pontos
+Ponto

    But the colored git diff line by line yet is very useful!

References

# 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 -L http://git.io/locale_merge.rb | ruby - en.yml pt.yml > new-pt.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 ? "\n" : "" # uncomment if you wish to have an empty line always that begins level 1
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
if value.include?("\n")
value = value.gsub(/\n*$/,"")
value = "|\n"+value
value = value.gsub("\n","\n"+" "*$level)
end
print ": " + value + "\n"
end
else
if next_compared.class == String
print " "*($level-1)
print key
if next_compared.include?("\n")
next_compared = next_compared.gsub(/\n*$/,"")
next_compared = "|\n"+next_compared
next_compared = next_compared.gsub("\n","\n"+" "*$level)
end
print ": " + next_compared + "\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])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment