Sha256: ebf326f6095df7ca71aafe08afca6898937c780193f5e1897931012d3e5edf3f

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

module I18nAdminUtils
  class SearchTranslation
    #Regex that match plain text in html
    HTML_PLAIN_TEXT_REGEX = /(?<=>)(([^><])+)(?=<)/

    def self.search
      find_translation_usage
    end

    #Search for all t('') usage in the given directory
    def self.find_translation_usage
      results = SearchResult.new
      dirs = I18nAdminUtils::Config.search_folders
      dirs.each do |dir|
        Dir.glob("#{dir}/**/*.*").each do |filename|
          i = 1
          File.open(filename).each_line do |line|
            line.scan(/t\(("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*')\)/).each do |result|
              key = result[0][1...-1]
              results << I18nAdminUtils::Translation.new(key, {:filename => filename, :line => i})
            end
            i += 1
          end

        end
      end
      results
    end

    def self.find_plain_text
      results = SearchResult.new
      dirs = I18nAdminUtils::Config.search_folders
      dirs.each do |dir|
        Dir.glob("#{dir}/**/*.*").each do |filename|
          results += find_plain_text_in_file(filename)
        end
      end
      results
    end

    def self.find_plain_text_in_file(filename)
      results = SearchResult.new
      File.open(filename).read.scan(HTML_PLAIN_TEXT_REGEX).each do |result|
        results << I18nAdminUtils::Translation.new(result[0], {:filename => filename}) unless result[0].blank?
      end
      results
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
i18n_admin_utils-1.1.0 lib/i18n_admin_utils/search_translation.rb