Skip to content

Instantly share code, notes, and snippets.

@kazjote
Last active December 17, 2015 19:39
Show Gist options
  • Save kazjote/5662382 to your computer and use it in GitHub Desktop.
Save kazjote/5662382 to your computer and use it in GitHub Desktop.
Script for finding missing translations in rails projects.
#!/usr/bin/ruby
require 'yaml'
files = Dir["**/*.rb"] + Dir["**/*.haml"]
patterns = [
/ t\('([^']+)'\)/, # t('')
/ t\("([^"]+)"\)/, # t("")
/I18n.translate\("([^"]+)"\)/, # I18n.translate("")
/I18n.translate\('([^']+)'\)/] # I18n.translate('')
locale = YAML.load($stdin.read)["en"]
def key_present?(locale_branch, key)
current = key.first
return true if current.nil? # We found the key
return false unless locale_branch.is_a?(Hash) && locale_branch.has_key?(current) # key does not exist
key_present? locale_branch[current], key[1..-1]
end
files.each do |file_path|
if File.stat(file_path).file?
namespace = file_path.split('/')[2..-1]
if namespace && !namespace.empty?
namespace.map! { |e| e.start_with?("_") ? e[1..-1] : e } # remove _ from partial names '_form' => 'form'
namespace.map! { |e| e.gsub("_helper", "") } # Helpers do not use helper prefix
namespace[-1].gsub!(/\..+/, "") # remove file extensions
else
namespace = []
end
content = File.read(file_path)
patterns.each do |pattern|
content.scan(pattern) do |match|
match = match.first
key = if match.start_with?(".")
namespace + match.split(".")[1..-1] # add namespace and remove dot in front of the key
else
match.split(".")
end
puts "#{file_path}: #{key.join('.')}" unless key_present?(locale, key)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment