lib/i18n/tasks/scanners/pattern_scanner.rb in i18n-tasks-0.2.22 vs lib/i18n/tasks/scanners/pattern_scanner.rb in i18n-tasks-0.3.0.rc1
- old
+ new
@@ -2,28 +2,54 @@
module I18n::Tasks::Scanners
# Scans for I18n.t usages
#
class PatternScanner < BaseScanner
- LITERAL_RE = /:?".+?"|:?'.+?'|:\w+/
- DEFAULT_PATTERN = /\bt(?:ranslate)?[( ]\s*(#{LITERAL_RE})/
-
# Extract i18n keys from file based on the pattern which must capture the key literal.
# @return [String] keys found in file
def scan_file(path, text = read_file(path))
keys = []
text.scan(pattern) do |match|
src_pos = Regexp.last_match.offset(0).first
- key = extract_key_from_match(match, path)
+ key = match_to_key(match, path)
next unless valid_key?(key)
keys << ::I18n::Tasks::Key.new(key, usage_context(text, src_pos))
end
keys
end
+ def default_pattern
+ # capture only the first argument
+ /
+ #{translate_call_re} [\( ] \s* (?# fn call begin )
+ (#{literal_re}) (?# capture the first argument)
+ /x
+ end
+
protected
+ # Given
+ # @param [MatchData] match
+ # @param [String] path
+ # @return [String] full absolute key name
+ def match_to_key(match, path)
+ key = strip_literal(match[0])
+ key = absolutize_key(key, path) if path && key.start_with?('.')
+ key
+ end
+
def pattern
- @pattern ||= config[:pattern].present? ? Regexp.new(config[:pattern]) : DEFAULT_PATTERN
+ @pattern ||= config[:pattern].present? ? Regexp.new(config[:pattern]) : default_pattern
+ end
+
+ def translate_call_re
+ /\bt(?:ranslate)?/
+ end
+
+ # Match literals:
+ # * String: '', "#{}"
+ # * Symbol: :sym, :'', :"#{}"
+ def literal_re
+ /:?".+?"|:?'.+?'|:\w+/
end
end
end