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

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