Module: I18n::Tasks::Scanners::RelativeKeys

Included in:
PatternMapper, PatternScanner, RubyAstScanner
Defined in:
lib/i18n/tasks/scanners/relative_keys.rb

Instance Method Summary collapse

Instance Method Details

#absolute_key(key, path, roots: config[:relative_roots], exclude_method_name_paths: config[:relative_exclude_method_name_paths], calling_method: nil) ⇒ String

Returns absolute version of the key.

Parameters:

  • key (String)

    relative i18n key (starts with a .)

  • path (String)

    path to the file containing the key

  • roots (Array<String>) (defaults to: config[:relative_roots])

    paths to relative roots

  • calling_method (#call, Symbol, String, false, nil) (defaults to: nil)

Returns:

  • (String)

    absolute version of the key



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/i18n/tasks/scanners/relative_keys.rb', line 12

def absolute_key(key, path, roots: config[:relative_roots],
                 exclude_method_name_paths: config[:relative_exclude_method_name_paths],
                 calling_method: nil)
  return key unless key.start_with?(DOT)
  fail 'roots argument is required' unless roots.present?

  normalized_path = File.expand_path(path)
  (root = path_root(normalized_path, roots)) ||
    fail(CommandError, "Cannot resolve relative key \"#{key}\".\n" \
                       "Set search.relative_roots in config/i18n-tasks.yml (currently #{roots.inspect})")
  normalized_path.sub!(root, '')

  if (exclude_method_name_paths || []).map { |p| expand_path(p) }.include?(root)
    "#{prefix(normalized_path)}#{key}"
  else
    "#{prefix(normalized_path, calling_method: calling_method)}#{key}"
  end
end