Class: ExtractI18n::Adapters::Rewriter
- Inherits:
-
Parser::TreeRewriter
- Object
- Parser::TreeRewriter
- ExtractI18n::Adapters::Rewriter
- Defined in:
- lib/extract_i18n/adapters/ruby_adapter.rb
Constant Summary collapse
- PROMPT =
TTY::Prompt.new
- PASTEL =
Pastel.new
Instance Method Summary collapse
-
#initialize(file_key:, on_ask:) ⇒ Rewriter
constructor
A new instance of Rewriter.
- #on_dstr(node) ⇒ Object
- #on_str(node) ⇒ Object
- #process(node) ⇒ Object
Constructor Details
#initialize(file_key:, on_ask:) ⇒ Rewriter
Returns a new instance of Rewriter.
32 33 34 35 |
# File 'lib/extract_i18n/adapters/ruby_adapter.rb', line 32 def initialize(file_key:, on_ask:) @file_key = file_key @on_ask = on_ask end |
Instance Method Details
#on_dstr(node) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/extract_i18n/adapters/ruby_adapter.rb', line 44 def on_dstr(node) if ignore?(node, parent: @nesting[-2]) return end interpolate_arguments = {} out_string = "" node.children.each do |i| if i.type == :str out_string += i.children.first else inner_source = i.children[0].loc.expression.source.gsub(/^#\{|}$/, '') interpolate_key = ExtractI18n.key(inner_source) out_string += "%{#{interpolate_key}}" interpolate_arguments[interpolate_key] = inner_source end end i18n_key = ExtractI18n.key(node.children.select { |i| i.type == :str }.map { |i| i.children[0] }.join(' ')) ask_and_continue( i18n_key: i18n_key, i18n_string: out_string, interpolate_arguments: interpolate_arguments, node: node, ) end |
#on_str(node) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/extract_i18n/adapters/ruby_adapter.rb', line 68 def on_str(node) string = node.children.first if ignore?(node, parent: @nesting[-2]) return end ask_and_continue(i18n_key: ExtractI18n.key(string), i18n_string: string, node: node) end |
#process(node) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/extract_i18n/adapters/ruby_adapter.rb', line 37 def process(node) @nesting ||= [] @nesting.push(node) super @nesting.pop end |