Sha256: b0af23b08eff89d40bd7d6dcf6e6cc0e3b42231f45c04b746b0a9d8b1a5689b6
Contents?: true
Size: 1.87 KB
Versions: 4
Compression:
Stored size: 1.87 KB
Contents
require 'i18n/tasks/scanners/pattern_scanner' module I18n::Tasks::Scanners # Scans for I18n.t(key, scope: ...) usages # both scope: "literal", and scope: [:array, :of, 'literals'] forms are supported # Caveat: scope is only detected when it is the first argument class PatternWithScopeScanner < PatternScanner protected def default_pattern # capture the first argument and scope argument if present /#{super} (?: \s*,\s* #{scope_arg_re} )? (?# capture scope in second argument ) /x end # Given # @param [MatchData] match # @param [String] path # @return [String] full absolute key name with scope resolved if any def match_to_key(match, path, location) key = super scope = match[1] if scope scope_ns = scope.gsub(/[\[\]\s]+/, ''.freeze).split(','.freeze).map { |arg| strip_literal(arg) } * '.'.freeze "#{scope_ns}.#{key}" else key unless match[0] =~ /\A\w/ end end # also parse expressions with literals def literal_re /(?: (?: #{super} ) | #{expr_re} )/x end # strip literals, convert expressions to #{interpolations} def strip_literal(val) if val =~ /\A\w/ "\#{#{val}}" else super(val) end end # scope: literal or code expression or an array of these def scope_arg_re /(?: :scope\s*=>\s* | (?# :scope => :home ) scope:\s* (?# scope: :home ) ) (#{array_or_one_literal_re})/x end # match code expression # matches characters until , as a heuristic to parse scopes like [:categories, cat.key] # can be massively improved by matching parenthesis def expr_re /[\w():"'.\s]+/x end # match +el+ or array of +el+ def array_or_one_literal_re(el = literal_re) /#{el} | \[\s* (?:#{el}\s*,\s*)* #{el} \s*\]/x end end end
Version data entries
4 entries across 4 versions & 1 rubygems