Advertisement
Guest User

t9n_tool

a guest
Feb 26th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.01 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'yaml'
  4.  
  5. module LocaleTools
  6.   module_function
  7.  
  8.   def flatten_keys(hash, prefix="", out_hash = {})
  9.     keys = []
  10.     hash.keys.each do |key|
  11.       if hash[key].is_a? Hash
  12.         current_prefix = prefix + "#{key}."
  13.         keys << flatten_keys(hash[key], current_prefix, out_hash)
  14.       else
  15.         keys << "#{prefix}#{key}"
  16.         out_hash["#{prefix}#{key}"] = hash[key]
  17.       end
  18.     end
  19.     prefix == "" ? keys.flatten : keys
  20.   end
  21.  
  22.   def compare(locale_1, locale_2, only_missing = false)
  23.     yaml_1 = YAML.load(IO.read(locale_1))
  24.     yaml_2 = YAML.load(IO.read(locale_2))
  25.  
  26.     h1 = {}
  27.     h2 = {}
  28.     keys_1 = flatten_keys(yaml_1[yaml_1.keys.first], "",h1)
  29.     keys_2 = flatten_keys(yaml_2[yaml_2.keys.first], "",h2)
  30.  
  31.     if only_missing
  32.       missing = keys_2 - keys_1
  33.       missing.each { |key| puts "#{key}|#{h2[key]}" }
  34.     else
  35.       changed = keys_2.select{|k| h1[k] != h2[k] }
  36.       changed.each { |key| puts "#{key}|#{h1[key]}|#{h2[key]}" }
  37.     end
  38.   end
  39.  
  40.   def patch(csv_file, locale_file)
  41.     patch = {}
  42.     IO.read(csv_file).each_line do |line|
  43.       key, old, t9n = line.chomp.split("|")[0, 3]
  44.       next unless key.to_s.index('.')
  45.       patch[key] = t9n
  46.     end
  47.     data = YAML.load(IO.read(locale_file))
  48.     root = data[data.keys.first]
  49.     patch.each do |path, value|
  50.       parts = path.split(".")
  51.       node = root
  52.       while parts.size > 1
  53.         key = parts.shift
  54.         node[key] ||= {}
  55.         node = node[key]
  56.       end
  57.       node[parts.first] = value
  58.     end
  59.     print_yaml(data)
  60.   end
  61.  
  62.   def print_yaml(hash, prefix="")
  63.     hash.each do |k,v|
  64.       if v.is_a? Hash
  65.         puts prefix+k+":"
  66.         print_yaml(v, prefix+"  ")
  67.       elsif v.nil?
  68.         puts prefix+k+": ~"
  69.       elsif v.is_a?(String)
  70.         if v =~ /\n/
  71.           puts prefix+k+": |-"
  72.           v.split(/\n/).each do |line|
  73.             puts prefix+"    "+line
  74.           end
  75.         elsif v =~ /\s|[:,{}&%]/
  76.           puts prefix+k+": "+v.inspect
  77.         else
  78.           puts prefix+k+": "+v
  79.         end
  80.       else
  81.         puts prefix+k+": "+v.to_s
  82.       end
  83.     end
  84.   end
  85. end
  86.  
  87. if ARGV[0] == 'compare'
  88.   # compare REVISION config/locales/en.yml > new_and_changed.csv
  89.   `git show #{ARGV[1]}:#{ARGV[2]} >tmp/#{ARGV[1]}`
  90.   LocaleTools.compare("tmp/#{ARGV[1]}", ARGV[2])
  91. elsif ARGV[0] == 'missing'
  92.   # missing config/locales/en.yml config/locales/ru.yml > missing.csv
  93.   LocaleTools.compare(ARGV[1], ARGV[2], true)
  94. elsif ARGV[0] == 'patch'
  95.   # patch new_and_changed.csv confing/locales/ru.yml > ru-new.yml
  96.   LocaleTools.patch(ARGV[1], ARGV[2])
  97. else
  98.   puts <<-EOT
  99. t9n_tool: simple utility script to find missing and changed i18n keys in rails locale files, and apply batch translations from CSV
  100.  
  101. Usage:
  102.  
  103. script/t9n_tool missing config/locales/en.yml config/locales/ru.yml >missing.csv
  104.  
  105. script/t9n_tool compare GIT_REVISION:config/locales/en.yml >new_and_changed.csv
  106.  
  107. script/t9n_tool patch new_and_changed.csv config/locales/ru.yml >ru-new.yml
  108.  
  109. EOT
  110. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement