Class: ExtractI18n::SourceChange

Inherits:
Object
  • Object
show all
Defined in:
lib/extract_i18n/source_change.rb

Constant Summary collapse

PASTEL =

Data class for a proposed source change

Pastel.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(i18n_key:, i18n_string:, interpolate_arguments:, source_line:, remove:, t_template: %{I18n.t("%s"%s)}, interpolation_type: :ruby) ⇒ SourceChange



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

def initialize(
  i18n_key:,
  i18n_string:,
  interpolate_arguments:,
  source_line:,
  remove:,
  t_template: %{I18n.t("%s"%s)},
  interpolation_type: :ruby
)
  @i18n_string = i18n_string
  @key = i18n_key
  @interpolate_arguments = interpolate_arguments
  @source_line = source_line.nil? ? "" : source_line
  @remove = remove
  @t_template = t_template
  @interpolation_type = interpolation_type
end

Instance Attribute Details

#i18n_stringObject (readonly)

Returns the value of attribute i18n_string.



11
12
13
# File 'lib/extract_i18n/source_change.rb', line 11

def i18n_string
  @i18n_string
end

#keyObject (readonly)

Returns the value of attribute key.



11
12
13
# File 'lib/extract_i18n/source_change.rb', line 11

def key
  @key
end

Instance Method Details

#formatObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/extract_i18n/source_change.rb', line 43

def format
  s = ""
  s += PASTEL.cyan("\nreplace:  ") + PASTEL.blue(@source_line).
       gsub(@remove, PASTEL.red(@remove))
  unless @source_line.end_with?("\n")
    s += "\n"
  end
  if @source_line[@remove]
    s += PASTEL.cyan("with:     ") + PASTEL.blue(@source_line).
      gsub(@remove, PASTEL.green(i18n_t))
  else
    s += PASTEL.cyan("with:     ") + PASTEL.green(i18n_t)
  end
  unless @source_line.end_with?("\n")
    s += "\n"
  end
  s += PASTEL.cyan("add i18n: ") + PASTEL.blue("#{@key}: #{@i18n_string}")
  s
end

#i18n_arguments_stringObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/extract_i18n/source_change.rb', line 83

def i18n_arguments_string
  case @interpolation_type
  when :ruby
    if @interpolate_arguments.keys.length > 0
      ", " + @interpolate_arguments.map { |k, v| "#{k}: (#{v})" }.join(', ')
    else
      ""
    end
  when :vue
    if @interpolate_arguments.keys.length > 0
      ", { " + @interpolate_arguments.map { |k, v| "#{k}: (#{v.strip})" }.join(', ') + " }"
    else
      ""
    end
  else
    raise NotImplementedError, @interpolation_type
  end
end

#i18n_t(relative: false) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/extract_i18n/source_change.rb', line 63

def i18n_t(relative: false)
  i18n_key = if relative
               "." + key.split('.').last
             else
               key
             end
  sprintf(@t_template, i18n_key, i18n_arguments_string)
end

#increment_key!Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/extract_i18n/source_change.rb', line 72

def increment_key!
  if @key[/(.+)([0-9]+)$/]
    rest = $1
    number = $2.to_i
  else
    number = 1
    rest = @key
  end
  @key = "#{rest}#{number + 1}"
end