module I18n::Tasks::KeyPatternMatching
Constants
- MATCH_NOTHING
Public Instance Methods
Source
# File lib/i18n/tasks/key_pattern_matching.rb, line 26 def compile_key_pattern(key_pattern) return key_pattern if key_pattern.is_a?(Regexp) /\A#{key_pattern_re_body(key_pattern)}\z/ end
convert pattern to regex In patterns:
* is like .* in regexs : matches a single key *: matches part of a single key, equivalent to `[^.]+?` regex { a, b.c } match any in set, can use : and *, match is captured
Source
# File lib/i18n/tasks/key_pattern_matching.rb, line 11 def compile_patterns_re(key_patterns) if key_patterns.blank? # match nothing MATCH_NOTHING else /(?:#{key_patterns.map { |p| compile_key_pattern p } * '|'})/m end end
one regex to match any
Source
# File lib/i18n/tasks/key_pattern_matching.rb, line 32 def key_pattern_re_body(key_pattern) key_pattern .gsub('.', '\.') .gsub('*:', '[^.]+?') .gsub('*', '.*') .gsub(':', '(?<=^|\.)[^.]+?(?=\.|$)') .gsub(/\{(.*?)}/) { "(#{Regexp.last_match(1).strip.gsub(/\s*,\s*/, '|')})" } end