Class: GetText::Tools::MsgMerge::PoData

Inherits:
Object
  • Object
show all
Defined in:
lib/gettext/tools/msgmerge.rb,
lib/gettext/tools/poparser.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePoData

Returns a new instance of PoData.



36
37
38
# File 'lib/gettext/tools/msgmerge.rb', line 36

def initialize
  @po = PO.new
end

Instance Attribute Details

#poObject (readonly)

Returns the value of attribute po.



34
35
36
# File 'lib/gettext/tools/msgmerge.rb', line 34

def po
  @po
end

Instance Method Details

#[](msgid) ⇒ Object



77
78
79
80
# File 'lib/gettext/tools/msgmerge.rb', line 77

def [](msgid)
  msgctxt, msgid, _ = split_msgid(msgid)
  @po[msgctxt, msgid].msgstr
end

#[]=(msgid, value) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gettext/tools/msgmerge.rb', line 82

def []=(msgid, value)
  msgctxt, msgid, msgid_plural = split_msgid(msgid)
  id = [msgctxt, msgid]

  if value.instance_of?(POEntry)
    @po[*id] = value
    return value
  end

  msgstr = value
  if @po.has_key?(*id)
    @po[*id] = msgstr
    @po[*id].msgctxt = msgctxt
    @po[*id].msgid_plural = msgid_plural
  else
    type = detect_entry_type(msgctxt, msgid_plural)
    entry = POEntry.new(type)
    entry.msgctxt = msgctxt
    entry.msgid = msgid
    entry.msgid_plural = msgid_plural
    entry.msgstr = msgstr
    @po[*id] = entry
    entry
  end
end

#__conv(str) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/gettext/tools/msgmerge.rb', line 154

def __conv(str)
  s = ""

  if str.count("\n") > 1
    s << '""' << "\n"
    str.each_line do |line|
      s << '"' << escape(line) << '"' << "\n"
    end
  else
    s << '"' << escape(str) << '"'
  end

  s.rstrip
end

#comment(msgid) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gettext/tools/msgmerge.rb', line 58

def comment(msgid)
  msgctxt, msgid, _ = split_msgid(msgid)
  id = [msgctxt, msgid]
  entry = @po[*id]
  return nil if entry.nil?

  formatted_comments = entry.format_translator_comment
  formatted_comments << entry.format_extracted_comment
  formatted_comments << entry.format_reference_comment
  formatted_comments << entry.format_flag_comment
  formatted_comments << entry.format_previous_comment

  unless entry.comment.nil?
    formatted_comments = entry.format_comment("#", entry.comment)
  end

  formatted_comments.chomp
end

#each_msgidObject



108
109
110
111
112
113
# File 'lib/gettext/tools/msgmerge.rb', line 108

def each_msgid
  msgids.each do |id|
    next if id.kind_of?(Symbol) or id.empty?
    yield(id)
  end
end

#escape(string) ⇒ Object



169
170
171
# File 'lib/gettext/tools/msgmerge.rb', line 169

def escape(string)
  POEntry.escape(string)
end

#generate_poObject



145
146
147
# File 'lib/gettext/tools/msgmerge.rb', line 145

def generate_po
  @po.to_s
end

#generate_po_entry(msgid) ⇒ Object



149
150
151
152
# File 'lib/gettext/tools/msgmerge.rb', line 149

def generate_po_entry(msgid)
  msgctxt, msgid, _ = split_msgid(msgid)
  @po[msgctxt, msgid].to_s
end

#msgid?(msgid) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
127
128
# File 'lib/gettext/tools/msgmerge.rb', line 123

def msgid?(msgid)
  return false if msgid.kind_of?(Symbol)
  return true if msgid.empty?
  msgctxt, msgid, _ = split_msgid(msgid)
  @po.has_key?(msgctxt, msgid)
end

#msgidsObject



115
116
117
118
119
120
121
# File 'lib/gettext/tools/msgmerge.rb', line 115

def msgids
  @po.collect do |entry|
    msgctxt = entry.msgctxt
    msgid = entry.msgid
    generate_original_string(msgctxt, msgid)
  end
end

#msgstr(msgid) ⇒ Object



54
55
56
# File 'lib/gettext/tools/msgmerge.rb', line 54

def msgstr(msgid)
  self[msgid]
end

#npluralsObject



135
136
137
138
139
140
141
142
143
# File 'lib/gettext/tools/msgmerge.rb', line 135

def nplurals
  return 0 if @po[""].msgstr.nil?

  if /\s*nplurals\s*=\s*(\d+)/ =~ @po[""].msgstr
    return $1.to_i
  else
    return 0
  end
end

#search_msgid_fuzzy(msgid, used_msgids) ⇒ Object

Is it necessary to implement this method?



131
132
133
# File 'lib/gettext/tools/msgmerge.rb', line 131

def search_msgid_fuzzy(msgid, used_msgids)
  nil
end

#set_comment(msgid, comments, msgctxt = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gettext/tools/msgmerge.rb', line 40

def set_comment(msgid, comments, msgctxt=nil)
  entry = generate_entry(msgid)

  if msgid == :last
    entry.comment = comments
    return
  end

  comments.each_line do |_line|
    line = _line.chomp
    entry = parse_comment(line, entry)
  end
end