Class: I18n::Tasks::Scanners::PrismScanners::TranslationCall

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n/tasks/scanners/prism_scanners/nodes.rb

Defined Under Namespace

Classes: ScopeError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node:, key:, receiver:, options:, parent:, candidate_keys: nil) ⇒ TranslationCall

Returns a new instance of TranslationCall.



73
74
75
76
77
78
79
80
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 73

def initialize(node:, key:, receiver:, options:, parent:, candidate_keys: nil)
  @node = node
  @key = key
  @receiver = receiver
  @options = options
  @parent = parent
  @candidate_keys = candidate_keys || []
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



71
72
73
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 71

def key
  @key
end

#nodeObject (readonly)

Returns the value of attribute node.



71
72
73
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 71

def node
  @node
end

#optionsObject (readonly)

Returns the value of attribute options.



71
72
73
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 71

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



71
72
73
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 71

def parent
  @parent
end

#receiverObject (readonly)

Returns the value of attribute receiver.



71
72
73
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 71

def receiver
  @receiver
end

Instance Method Details

#full_keyObject

Returns either a single key string or an array of candidate key strings for this call.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 111

def full_key
  return nil if key.nil?
  return nil unless key.is_a?(String)
  return nil if relative_key? && !support_relative_keys?

  base_parts = [scope].compact

  if relative_key? && support_candidate_keys?
    # For relative keys in controllers/methods, generate candidate keys by
    # progressively stripping trailing path segments from the parent path.
    # Example: parent.path = ["events", "create"], key = ".success"
    # yields: ["events.create.success", "events.success"]
    parent_path = parent&.path || []
    rel_key = key[1..] # strip leading dot # rubocop:disable Performance/ArraySemiInfiniteRangeSlice

    candidates = []
    parent_path_length = parent_path.length
    # Do not generate an unscoped bare key (keep_count = 0). Start from full parent path
    parent_path_length.downto(1) do |keep_count|
      parts = base_parts + parent_path.first(keep_count) + [rel_key]
      candidates << parts.compact.join(".")
    end

    candidates.map { |c| c.gsub("..", ".") }
  elsif relative_key?
    # For relative keys in views, just append to the full path
    [base_parts + parent.path + [key[1..]]].flatten.compact.join(".").gsub("..", ".") # rubocop:disable Performance/ChainArrayAllocation
  elsif key.start_with?(".")
    [base_parts + [key[1..]]].flatten.compact.join(".").gsub("..", ".") # rubocop:disable Performance/ArraySemiInfiniteRangeSlice,Performance/ChainArrayAllocation
  elsif @candidate_keys.present?
    ([key] + @candidate_keys).map do |c|
      [base_parts + [c]].flatten.compact.join(".").gsub("..", ".") # rubocop:disable Performance/ChainArrayAllocation
    end
  else
    [base_parts + [key]].flatten.compact.join(".").gsub("..", ".") # rubocop:disable Performance/ChainArrayAllocation
  end
end

#occurrences(file_path) ⇒ Object



106
107
108
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 106

def occurrences(file_path)
  occurrence(file_path)
end

#relative_key?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 82

def relative_key?
  @key&.start_with?(".") && @receiver.nil?
end

#with_node(node) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 96

def with_node(node)
  self.class.new(
    node: node,
    key: @key,
    receiver: @receiver,
    options: @options,
    parent: @parent
  )
end

#with_parent(parent) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/i18n/tasks/scanners/prism_scanners/nodes.rb', line 86

def with_parent(parent)
  self.class.new(
    node: @node,
    key: @key,
    receiver: @receiver,
    options: @options,
    parent: parent
  )
end