Class: ExtractI18n::Adapters::ErbAdapter

Inherits:
Adapter
  • Object
show all
Defined in:
lib/extract_i18n/adapters/erb_adapter.rb

Instance Attribute Summary

Attributes inherited from Adapter

#file_key, #file_path, #on_ask, #options

Instance Method Summary collapse

Methods inherited from Adapter

for, #initialize, supports_relative_keys?

Constructor Details

This class inherits a constructor from ExtractI18n::Adapters::Adapter

Instance Method Details

#process_change(node) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/extract_i18n/adapters/erb_adapter.rb', line 28

def process_change(node)
  change = ExtractI18n::SourceChange.new(
    i18n_key: "#{@file_key}.#{ExtractI18n.key(node.text.strip)}",
    i18n_string: node.text,
    interpolate_arguments: {},
    source_line: node.to_s,
    remove: node.text,
    t_template: %{ t('%s') },
    interpolation_type: :ruby
  )
  if @on_ask.call(change)
    node.replace_text!(change.key, change.i18n_t)
  end
end

#run(original_content) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/extract_i18n/adapters/erb_adapter.rb', line 3

def run(original_content)
  unless valid_erb?(original_content)
    puts "ERB invalid!"
    return original_content
  end
  document = ExtractI18n::HTMLExtractor::ErbDocument.parse_string(original_content)
  nodes_to_translate = ExtractI18n::HTMLExtractor::Match::Finder.new(document).matches
  nodes_to_translate.each { |node|
    next if node.text == ""

    process_change(node)
  }
  result = document.save

  result
end

#valid_erb?(content) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
# File 'lib/extract_i18n/adapters/erb_adapter.rb', line 20

def valid_erb?(content)
  Parser::CurrentRuby.parse(ERB.new(content).src)
  true
rescue StandardError => e
  warn e.inspect
  false
end