Class: RubyGettextExtractor::Extractor

Inherits:
SexpProcessor
  • Object
show all
Defined in:
lib/gettext_i18n_rails/ruby_gettext_extractor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(targets) ⇒ Extractor

Returns a new instance of Extractor.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gettext_i18n_rails/ruby_gettext_extractor.rb', line 27

def initialize(targets)
  @targets = {}
  @results = []

  targets.each do |a|
    k, _v = a
    # things go wrong if k already exists, but this
    # should not happen (according to the gettext doc)
    @targets[k] = a
    @results << a
  end

  super()
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results.



25
26
27
# File 'lib/gettext_i18n_rails/ruby_gettext_extractor.rb', line 25

def results
  @results
end

Instance Method Details

#extract_key_plural(args, separator) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gettext_i18n_rails/ruby_gettext_extractor.rb', line 67

def extract_key_plural(args, separator)
  # this could be n_("aaa", "aaa plural", @retireitems.length)
  # s(s(:str, "aaa"),
  #   s(:str, "aaa plural"),
  #   s(:call, s(:ivar, :@retireitems), :length))
  # all strings arguments are extracted and joined with \004 or \000
  arguments = args[0..(-2)]

  res = []
  arguments.each do |a|
    next unless a.kind_of? Sexp
    str = extract_string(a)
    res << str if str
  end

  key = res.empty? ? nil : res.join(separator)

  return nil unless key
  key.gsub("\n", '\n').gsub("\t", '\t').gsub("\0", '\0')
end

#extract_key_singular(args, separator) ⇒ Object



60
61
62
63
64
65
# File 'lib/gettext_i18n_rails/ruby_gettext_extractor.rb', line 60

def extract_key_singular(args, separator)
  key = extract_string(args) if args.size == 2 || args.size == 4

  return nil unless key
  key.gsub("\n", '\n').gsub("\t", '\t').gsub("\0", '\0')
end

#extract_string(node) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gettext_i18n_rails/ruby_gettext_extractor.rb', line 42

def extract_string(node)
  case node.first
  when :str
    node.last
  when :call
    type, recv, meth, args = node
    # node has to be in form of "string" + "other_string"
    return nil unless recv && meth == :+

    first_part  = extract_string(recv)
    second_part = extract_string(args)

    first_part && second_part ? first_part.to_s + second_part.to_s : nil
  else
    nil
  end
end

#gettext_plural_call(args) ⇒ Object



113
114
115
116
# File 'lib/gettext_i18n_rails/ruby_gettext_extractor.rb', line 113

def gettext_plural_call(args)
  key = extract_key_plural(args, "\000")
  store_key(key, args)
end

#gettext_simple_call(args) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/gettext_i18n_rails/ruby_gettext_extractor.rb', line 102

def gettext_simple_call(args)
  # args comes in 2 forms:
  #   s(s(:str, "Button Group Order:"))
  #   s(:str, "Button Group Order:")
  # normalizing:
  args = args.first if Sexp === args.sexp_type

  key  = extract_key_singular(args, "\004")
  store_key(key, args)
end

#process_call(exp) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/gettext_i18n_rails/ruby_gettext_extractor.rb', line 118

def process_call exp
  _call = exp.shift
  _recv = process exp.shift
  meth  = exp.shift

  case meth
  when :_, :p_, :N_, :pgettext, :s_
    gettext_simple_call(exp)
  when :n_
    gettext_plural_call(exp)
  end

  until exp.empty? do
    process(exp.shift)
  end

  s()
end

#store_key(key, args) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gettext_i18n_rails/ruby_gettext_extractor.rb', line 88

def store_key(key, args)
  if key
    res = @targets[key]

    unless res
      res = [key]
      @results << res
      @targets[key] = res
    end

    res << "#{args.file}:#{args.line}"
  end
end