Class: ExtractI18n::Adapters::VuePugAdapter

Inherits:
SlimAdapter show all
Defined in:
lib/extract_i18n/adapters/vue_pug_adapter.rb

Instance Attribute Summary

Attributes inherited from Adapter

#file_key, #file_path, #on_ask, #options

Instance Method Summary collapse

Methods inherited from SlimAdapter

join_multiline, supports_relative_keys?

Methods inherited from Adapter

for, #initialize, supports_relative_keys?

Constructor Details

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

Instance Method Details

#check_javascript_for_strings!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/extract_i18n/adapters/vue_pug_adapter.rb', line 11

def check_javascript_for_strings!
  lines = @new_content
  # drop lines until <script
  start = lines.find_index { |line| line[/^<script/] }
  finish = lines.find_index { |line| line[/^<\/script/] }
  return if start.nil? || finish.nil?

  js_content = lines[(start + 1)...finish].join("\n")

  js_adapter = ExtractI18n::Adapters::JsAdapter.new(file_key: @file_key, on_ask: @on_ask, options: @options)
  js_replaced = js_adapter.run(js_content)

  @new_content = lines[0..start] + js_replaced.split("\n") + lines[(finish)..-1]
end

#process_line(old_line) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/extract_i18n/adapters/vue_pug_adapter.rb', line 26

def process_line(old_line)
  @mode ||= :template
  if old_line[/^<template/]
    @mode = :template
  elsif old_line[/^<script/]
    @mode = :script
  elsif old_line[/^<style/]
    @mode = :style
  end
  if @mode != :template
    return old_line
  end
  word = ExtractI18n::Slimkeyfy::Word.for('.vue').new(old_line)
  ExtractI18n::Slimkeyfy::VueTransformer.new(word, @file_key).transform do |change|
    if change.nil? # nothing to do
      return old_line
    end
    if ExtractI18n.ignore?(change.i18n_string) || change.i18n_string.empty?
      return old_line
    end

    if @on_ask.call(change)
      change.i18n_t
    else
      old_line
    end
  end
end

#run(original_content) ⇒ Object



5
6
7
8
9
# File 'lib/extract_i18n/adapters/vue_pug_adapter.rb', line 5

def run(original_content)
  super
  check_javascript_for_strings!
  @new_content.join("\n")
end