Skip to content

Instantly share code, notes, and snippets.

@iboard
Created February 27, 2012 08:44
Show Gist options
  • Save iboard/1922608 to your computer and use it in GitHub Desktop.
Save iboard/1922608 to your computer and use it in GitHub Desktop.
Rake-Task to find undefined locale-keys in a rails project
# -*- encoding : utf-8 -*-
namespace :locales do
desc "Find undefined locales"
task :undefined_locales => :environment do
used_strings = []
Dir['**/*'].each do |file|
if File::ftype(file) == "file"
begin
File::read(file).each_line do |line|
line.gsub( /t\(\:(\S+)\)/ ) do |x|
used_strings << x.gsub(/t\(\:/,'').gsub(/\)$/,'')
end
end
rescue => e
# ignore
end
end
end
locale_en = YAML::load( File::read( 'config/locales/en.yml') )['en']
defined_keys = []
not_defined_keys = []
used_strings.each do |key|
if locale_en[key].present?
defined_keys << key
else
not_defined_keys << key
end
end
if not_defined_keys.any?
puts "NOT DEFINED KEYS:"
not_defined_keys.uniq.sort.each do |key|
puts " #{key}: #{key.to_s.humanize}"
end
else
puts "ALL KEYS DEFINED"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment