Skip to content

Instantly share code, notes, and snippets.

@jugyo
Created February 23, 2011 06:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jugyo/840127 to your computer and use it in GitHub Desktop.
Save jugyo/840127 to your computer and use it in GitHub Desktop.
print translations
namespace :locale do
desc "print translations"
task :translations => :environment do
printer = lambda { |hash_or_string, keys = []|
case hash_or_string
when String
puts "#{keys.join('.')} => #{hash_or_string.inspect}"
when Hash
hash_or_string.keys.sort.each do |key|
value = hash_or_string[key]
printer.call(value, keys + [key])
end
end
}
if locale = ENV["LOCALE"]
printer.call(I18n.backend.send(:translations)[locale.to_sym], [locale.to_sym])
else
printer.call(I18n.backend.send(:translations))
end
end
namespace :translation do
desc "find translations"
task :find do
if key = ENV["KEY"]
find_translations(Dir[Rails.root.join("config/locales/**/*.yml")], key)
else
puts "specify a KEY"
end
end
end
end
def find_translations(files, find_key)
results = []
files.each do |filepath|
File.open(filepath) do |file|
key_stack = []
prev_indent = 0
indent_spaces = nil
file.each_with_index do |line, index|
line = line.chomp
line_number = index + 1
next unless line =~ /\s*\w+:/
key = line[/\w+:/].sub(':', '')
word = line.gsub(/.*:\s*/, '')
indent = (line =~ /\w/)
if indent_spaces.nil? && indent > 0
indent_spaces = indent
end
indent /= indent_spaces if indent > 0
unless indent == prev_indent
if indent < prev_indent
(prev_indent - indent + 1).times { key_stack.pop }
end
else
key_stack.pop
end
key_stack.push(key)
prev_indent = indent
joined_key = key_stack.join('.')
if joined_key =~ /#{find_key}/
puts "#{filepath}:#{line_number} - #{joined_key}: #{word}"
results << [filepath, line_number]
end
end
end
end
results
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment