Skip to content

A custom scanner example

Gleb Mazovetskiy edited this page Oct 28, 2015 · 10 revisions

Multiple scanners are available since v0.9.

The example below scans for all instances of = page_title in haml and slim files and considers them as t('.page_title') calls.

# lib/my_custom_scanner.rb
require 'i18n/tasks/scanners/file_scanner'
class MyCustomScanner < I18n::Tasks::Scanners::FileScanner
  include I18n::Tasks::Scanners::RelativeKeys
  include I18n::Tasks::Scanners::OccurrenceFromPosition

  # @return [Array<[absolute key, Results::Occurrence]>]
  def scan_file(path)
    text = read_file(path)
    text.scan(/^\s*=\s*page_title\b/).map do |_match|
      occurrence = occurrence_from_position(
          path, text, Regexp.last_match.offset(0).first)
      [absolute_key('.page_title', path), occurrence]
    end
  end
end
# config/i18n-tasks.yml.erb
<%
  require './lib/my_custom_scanner'
  I18n::Tasks::Configuration::DEFAULTS[:search][:scanners].tap do |scanners|
    scanners << ['MyCustomScanner', only: %w(*.haml *.slim)]
    scanners.uniq!
  end
%>

Here we are inheriting from a built-in file scanner, but in general this is not required. See the files in the scanners module to learn more.

Clone this wiki locally