class I18n::Tasks::Scanners::PatternMapper
Maps the provided patterns to keys.
Constants
- KEY_GROUP
Public Class Methods
new(config:, **args)
click to toggle source
@param patterns [Array<[String, String]> the list of pattern-key pairs
the patterns follow the regular expression syntax, with a syntax addition for matching string/symbol literals: you can include %{key} in the pattern, and it will be converted to a named capture group, capturing ruby strings and symbols, that can then be used in the key: patterns: [['Spree\.t[( ]\s*%{key}', 'spree.%{key}']] All of the named capture groups are interpolated into the key with %{group_name} interpolations.
Calls superclass method
I18n::Tasks::Scanners::FileScanner::new
# File lib/i18n/tasks/scanners/pattern_mapper.rb, line 24 def initialize(config:, **args) super @patterns = configure_patterns(config[:patterns] || []) end
Protected Instance Methods
scan_file(path)
click to toggle source
@return [Array<[absolute key, Results::Occurrence]>]
# File lib/i18n/tasks/scanners/pattern_mapper.rb, line 32 def scan_file(path) text = read_file(path) @patterns.flat_map do |pattern, key| result = [] text.scan(pattern) do |_| match = Regexp.last_match matches = Hash[match.names.map(&:to_sym).zip(match.captures)] if matches.key?(:key) matches[:key] = strip_literal(matches[:key]) next unless valid_key?(matches[:key]) end result << [absolute_key(format(key, matches), path), occurrence_from_position(path, text, match.offset(0).first)] end result end end
Private Instance Methods
configure_patterns(patterns)
click to toggle source
# File lib/i18n/tasks/scanners/pattern_mapper.rb, line 54 def configure_patterns(patterns) patterns.map do |(pattern, key)| [pattern.is_a?(Regexp) ? pattern : Regexp.new(format(pattern, key: KEY_GROUP)), key] end end