You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

locales.rake 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. desc 'Updates and checks locales against en.yml'
  2. task :locales do
  3. %w(locales:update locales:check_interpolation).collect do |task|
  4. Rake::Task[task].invoke
  5. end
  6. end
  7. namespace :locales do
  8. desc 'Updates language files based on en.yml content (only works for new top level keys).'
  9. task :update do
  10. dir = ENV['DIR'] || './config/locales'
  11. en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en']
  12. files = Dir.glob(File.join(dir,'*.{yaml,yml}'))
  13. files.sort.each do |file|
  14. puts "Updating file #{file}"
  15. file_strings = YAML.load(File.read(file))
  16. file_strings = file_strings[file_strings.keys.first]
  17. missing_keys = en_strings.keys - file_strings.keys
  18. next if missing_keys.empty?
  19. puts "==> Missing #{missing_keys.size} keys (#{missing_keys.join(', ')})"
  20. lang = File.open(file, 'a')
  21. missing_keys.each do |key|
  22. {key => en_strings[key]}.to_yaml.each_line do |line|
  23. next if line =~ /^---/ || line.empty?
  24. puts " #{line}"
  25. lang << " #{line}"
  26. end
  27. end
  28. lang.close
  29. end
  30. end
  31. desc 'Checks interpolation arguments in locals against en.yml'
  32. task :check_interpolation do
  33. dir = ENV['DIR'] || './config/locales'
  34. en_strings = YAML.load(File.read(File.join(dir,'en.yml')))['en']
  35. files = Dir.glob(File.join(dir,'*.{yaml,yml}'))
  36. files.sort.each do |file|
  37. puts "parsing #{file}..."
  38. file_strings = YAML.load_file(file)
  39. unless file_strings.is_a?(Hash)
  40. puts "#{file}: content is not a Hash (#{file_strings.class.name})"
  41. next
  42. end
  43. unless file_strings.keys.size == 1
  44. puts "#{file}: content has multiple keys (#{file_strings.keys.size})"
  45. next
  46. end
  47. file_strings = file_strings[file_strings.keys.first]
  48. file_strings.each do |key, string|
  49. next unless string.is_a?(String)
  50. string.scan /%\{\w+\}/ do |match|
  51. unless en_strings[key].nil? || en_strings[key].include?(match)
  52. puts "#{file}: #{key} uses #{match} not found in en.yml"
  53. end
  54. end
  55. end
  56. end
  57. end
  58. desc <<-END_DESC
  59. Removes a translation string from all locale file (only works for top-level childless non-multiline keys, probably doesn\'t work on windows).
  60. Options:
  61. key=key_1,key_2 Comma-separated list of keys to delete
  62. skip=en,de Comma-separated list of locale files to ignore (filename without extension)
  63. END_DESC
  64. task :remove_key do
  65. dir = ENV['DIR'] || './config/locales'
  66. files = Dir.glob(File.join(dir,'*.yml'))
  67. skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
  68. deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil
  69. # Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :)
  70. delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/
  71. files.each do |path|
  72. # Skip certain locales
  73. (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
  74. puts "Deleting selected keys from #{path}"
  75. orig_content = File.open(path, 'r') {|file| file.read}
  76. File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}}
  77. end
  78. end
  79. desc <<-END_DESC
  80. Adds a new top-level translation string to all locale file (only works for childless keys, probably doesn\'t work on windows, doesn't check for duplicates).
  81. Options:
  82. key="some_key=foo"
  83. key1="another_key=bar"
  84. key_fb="foo=bar" Keys to add in the form key=value, every option of the form key[,\\d,_*] will be recognised
  85. skip=en,de Comma-separated list of locale files to ignore (filename without extension)
  86. END_DESC
  87. task :add_key do
  88. dir = ENV['DIR'] || './config/locales'
  89. files = Dir.glob(File.join(dir,'*.yml'))
  90. skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
  91. keys_regex = /\Akey(\d+|_.+)?\z/
  92. adds = ENV.reject {|k,v| !(k =~ keys_regex)}.values.collect {|v| Array.new v.split("=",2)}
  93. key_list = adds.collect {|v| v[0]}.join(", ")
  94. files.each do |path|
  95. # Skip certain locales
  96. (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
  97. # TODO: Check for duplicate/existing keys
  98. puts "Adding #{key_list} to #{path}"
  99. File.open(path, 'a') do |file|
  100. adds.each do |kv|
  101. Hash[*kv].to_yaml.each_line do |line|
  102. file.puts " #{line}" unless (line =~ /^---/ || line.empty?)
  103. end
  104. end
  105. end
  106. end
  107. end
  108. desc 'Duplicates a key. Exemple rake locales:dup key=foo new_key=bar'
  109. task :dup do
  110. dir = ENV['DIR'] || './config/locales'
  111. files = Dir.glob(File.join(dir,'*.yml'))
  112. skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
  113. key = ENV['key']
  114. new_key = ENV['new_key']
  115. abort "Missing key argument" if key.blank?
  116. abort "Missing new_key argument" if new_key.blank?
  117. files.each do |path|
  118. # Skip certain locales
  119. (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
  120. puts "Adding #{new_key} to #{path}"
  121. strings = File.read(path)
  122. unless strings =~ /^( #{key}: .+)$/
  123. puts "Key not found in #{path}"
  124. next
  125. end
  126. line = $1
  127. File.open(path, 'a') do |file|
  128. file.puts(line.sub(key, new_key))
  129. end
  130. end
  131. end
  132. desc 'Check parsing yaml by psych library on Ruby 1.9.'
  133. # On Fedora 12 and 13, if libyaml-devel is available,
  134. # in case of installing by rvm,
  135. # Ruby 1.9 default yaml library is psych.
  136. task :check_parsing_by_psych do
  137. begin
  138. require 'psych'
  139. parser = Psych::Parser.new
  140. dir = ENV['DIR'] || './config/locales'
  141. files = Dir.glob(File.join(dir,'*.yml'))
  142. files.sort.each do |filename|
  143. next if File.directory? filename
  144. puts "parsing #{filename}..."
  145. begin
  146. parser.parse File.open(filename)
  147. rescue => e1
  148. puts(e1.message)
  149. puts("")
  150. end
  151. end
  152. rescue => e
  153. puts(e.message)
  154. end
  155. end
  156. end