Add bin script to find untranslated text in erb files (#15072)

This commit is contained in:
Ben Halpern 2021-10-14 12:15:14 -04:00 committed by GitHub
parent ff5a94dbf3
commit 68e6e61141
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

52
bin/untranslated_erb Executable file
View file

@ -0,0 +1,52 @@
#!/usr/bin/env ruby
class I18nExtractor
ERB_TAG = /<%(.*?)%>/
HTML_TAG = /<(.*?)>/
SEPERATOR = '_@@@_'
SKIP_TAGS = [[/<script/i,/<\/script>/i],[/<%/,/%>/],[/<style/i,/\/style>/i]]
def initialize(filename)
@filename = filename
@stack = []
@files_with_results = []
end
def extract
File.open(@filename).each_with_index do |line,i|
next if in_script_block?(line)
text = line.gsub(ERB_TAG, SEPERATOR).gsub(HTML_TAG,SEPERATOR).strip
arr = text.split SEPERATOR
arr.each do |e|
if e.strip.size > 1
unless @files_with_results.include?(@filename)
@files_with_results << @filename
printf "\n\n\e[4m#{@filename}\e[00m\n\n"
end
printf("%3s:%s\n",i+1,e.strip)
end
end
end
end
private
def in_script_block?(s)
return true if s.nil? || s.strip.size == 0
jump_in_tag = SKIP_TAGS.find{ |start_tag,end_tag| s =~ start_tag}
@stack.push jump_in_tag[1] if jump_in_tag
if @stack.last
end_tag_match = s.match(@stack.last)
if end_tag_match
@stack.pop
return in_script_block?(end_tag_match.post_match)
end
end
return !@stack.empty?
end
end
printf "\nSeeking out erb files that need translating. Just one moment...\n\n\e[1mThis script definitely includes false negatives and false positives, but hopefully it helps!!!\e[00m\n\n\n"
Dir.glob("**/*.html.erb").each do |filename|
next if filename.include?("/admin/") # We are not yet focused on this area
I18nExtractor.new(filename).extract
end