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 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. This task does not work on Ruby 1.8.6.
  61. You need to use Ruby 1.8.7 or later.
  62. Options:
  63. key=key_1,key_2 Comma-separated list of keys to delete
  64. skip=en,de Comma-separated list of locale files to ignore (filename without extension)
  65. END_DESC
  66. task :remove_key do
  67. dir = ENV['DIR'] || './config/locales'
  68. files = Dir.glob(File.join(dir,'*.yml'))
  69. skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
  70. deletes = ENV['key'] ? Regexp.union(ENV['key'].split(',')) : nil
  71. # Ignore multiline keys (begin with | or >) and keys with children (nothing meaningful after :)
  72. delete_regex = /\A #{deletes}: +[^\|>\s#].*\z/
  73. files.each do |path|
  74. # Skip certain locales
  75. (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
  76. puts "Deleting selected keys from #{path}"
  77. orig_content = File.open(path, 'r') {|file| file.read}
  78. File.open(path, 'w') {|file| orig_content.each_line {|line| file.puts line unless line.chomp =~ delete_regex}}
  79. end
  80. end
  81. desc <<-END_DESC
  82. 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).
  83. Options:
  84. key="some_key=foo"
  85. key1="another_key=bar"
  86. key_fb="foo=bar" Keys to add in the form key=value, every option of the form key[,\\d,_*] will be recognised
  87. skip=en,de Comma-separated list of locale files to ignore (filename without extension)
  88. END_DESC
  89. task :add_key do
  90. dir = ENV['DIR'] || './config/locales'
  91. files = Dir.glob(File.join(dir,'*.yml'))
  92. skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
  93. keys_regex = /\Akey(\d+|_.+)?\z/
  94. adds = ENV.reject {|k,v| !(k =~ keys_regex)}.values.collect {|v| Array.new v.split("=",2)}
  95. key_list = adds.collect {|v| v[0]}.join(", ")
  96. files.each do |path|
  97. # Skip certain locales
  98. (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
  99. # TODO: Check for dupliate/existing keys
  100. puts "Adding #{key_list} to #{path}"
  101. File.open(path, 'a') do |file|
  102. adds.each do |kv|
  103. Hash[*kv].to_yaml.each_line do |line|
  104. file.puts " #{line}" unless (line =~ /^---/ || line.empty?)
  105. end
  106. end
  107. end
  108. end
  109. end
  110. desc 'Duplicates a key. Exemple rake locales:dup key=foo new_key=bar'
  111. task :dup do
  112. dir = ENV['DIR'] || './config/locales'
  113. files = Dir.glob(File.join(dir,'*.yml'))
  114. skips = ENV['skip'] ? Regexp.union(ENV['skip'].split(',')) : nil
  115. key = ENV['key']
  116. new_key = ENV['new_key']
  117. abort "Missing key argument" if key.blank?
  118. abort "Missing new_key argument" if new_key.blank?
  119. files.each do |path|
  120. # Skip certain locales
  121. (puts "Skipping #{path}"; next) if File.basename(path, ".yml") =~ skips
  122. puts "Adding #{new_key} to #{path}"
  123. strings = File.read(path)
  124. unless strings =~ /^( #{key}: .+)$/
  125. puts "Key not found in #{path}"
  126. next
  127. end
  128. line = $1
  129. File.open(path, 'a') do |file|
  130. file.puts(line.sub(key, new_key))
  131. end
  132. end
  133. end
  134. desc 'Check parsing yaml by psych library on Ruby 1.9.'
  135. # On Fedora 12 and 13, if libyaml-devel is available,
  136. # in case of installing by rvm,
  137. # Ruby 1.9 default yaml library is psych.
  138. task :check_parsing_by_psych do
  139. begin
  140. require 'psych'
  141. parser = Psych::Parser.new
  142. dir = ENV['DIR'] || './config/locales'
  143. files = Dir.glob(File.join(dir,'*.yml'))
  144. files.sort.each do |filename|
  145. next if File.directory? filename
  146. puts "parsing #{filename}..."
  147. begin
  148. parser.parse File.open(filename)
  149. rescue Exception => e1
  150. puts(e1.message)
  151. puts("")
  152. end
  153. end
  154. rescue Exception => e
  155. puts(e.message)
  156. end
  157. end
  158. end