Class: GetText::RMsgMerge::PoData

Inherits:
Object
  • Object
show all
Defined in:
lib/gettext/tools/rmsgmerge.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePoData

Returns a new instance of PoData.



26
27
28
29
30
# File 'lib/gettext/tools/rmsgmerge.rb', line 26

def initialize
  @msgid2msgstr = {}
  @msgid2comment = {}
  @msgids = []
end

Instance Attribute Details

#msgidsObject (readonly)

Returns the value of attribute msgids.



24
25
26
# File 'lib/gettext/tools/rmsgmerge.rb', line 24

def msgids
  @msgids
end

Instance Method Details

#[](msgid) ⇒ Object



44
45
46
# File 'lib/gettext/tools/rmsgmerge.rb', line 44

def [](msgid)
  @msgid2msgstr[msgid]
end

#[]=(msgid, msgstr) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/gettext/tools/rmsgmerge.rb', line 48

def []=(msgid, msgstr)
  # Retain the order
  unless @msgid2msgstr[msgid]
    @msgids << msgid
  end

  @msgid2msgstr[msgid] = msgstr
end

#__conv(str) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/gettext/tools/rmsgmerge.rb', line 141

def __conv(str)
  s = ''

  if str.count("\n") > 1
    s << '""' << "\n"
    s << str.gsub(/^(.*)$/, '"\1\n"')
  else
    s << '"' << str.sub("\n", "\\n") << '"'
  end

  s.rstrip
end

#comment(msgid) ⇒ Object



40
41
42
# File 'lib/gettext/tools/rmsgmerge.rb', line 40

def comment(msgid)
  @msgid2comment[msgid]
end

#each_msgidObject



57
58
59
60
61
62
# File 'lib/gettext/tools/rmsgmerge.rb', line 57

def each_msgid
  arr = @msgids.delete_if{|i| Symbol === i or i == ''}
  arr.each do |i|
    yield i
  end
end

#generate_poObject



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/gettext/tools/rmsgmerge.rb', line 86

def generate_po
  str = ''
  str << generate_po_header

  self.each_msgid do |id|
    str << self.generate_po_entry(id)
  end

  str << @msgid2comment[:last]
  str
end

#generate_po_entry(msgid) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/gettext/tools/rmsgmerge.rb', line 112

def generate_po_entry(msgid)
  str = ""
  str << @msgid2comment[msgid]
  if str[-1] != "\n"[0]
    str << "\n"
  end

  id = msgid.gsub(/"/, '\"').gsub(/\r/, '')
  msgstr = @msgid2msgstr[msgid].gsub(/"/, '\"').gsub(/\r/, '')

  if id.include?("\000")
    ids = id.split(/\000/)
    str << "msgid " << __conv(ids[0]) << "\n"
    ids[1..-1].each do |single_id|
      str << "msgid_plural " << __conv(single_id) << "\n"
    end

    msgstr.split("\000").each_with_index do |m, n|
      str << "msgstr[#{n}] " << __conv(m) << "\n"
    end
  else
    str << "msgid "  << __conv(id) << "\n"
    str << "msgstr " << __conv(msgstr) << "\n"
  end

  str << "\n"
  str
end

#generate_po_headerObject



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gettext/tools/rmsgmerge.rb', line 98

def generate_po_header
  str = ""

  str << @msgid2comment[''].strip << "\n"
  str << 'msgid ""'  << "\n"
  str << 'msgstr ""' << "\n"
  msgstr = @msgid2msgstr[''].gsub(/"/, '\"').gsub(/\r/, '')
  msgstr = msgstr.gsub(/^(.*)$/, '"\1\n"')
  str << msgstr
  str << "\n"

  str
end

#msgid?(msgid) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/gettext/tools/rmsgmerge.rb', line 64

def msgid?(msgid)
  !(Symbol === msgid) and  @msgid2msgstr[msgid] and (msgid != '')
end

#msgstr(msgid) ⇒ Object



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

def msgstr(msgid)
  @msgid2msgstr[msgid]
end

#npluralObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gettext/tools/rmsgmerge.rb', line 73

def nplural
  unless @msgid2msgstr['']
    return 0
  else
    if /\s*nplural\s*=\s*(\d+)/ =~ @msgid2msgstr['']
      return $1.to_i
    else
      return 0
    end

  end
end

#search_msgid_fuzzy(msgid, used_msgids) ⇒ Object

Is it necessary to implement this method?



69
70
71
# File 'lib/gettext/tools/rmsgmerge.rb', line 69

def search_msgid_fuzzy(msgid, used_msgids)
  nil
end

#set_comment(msgid_or_sym, comment) ⇒ Object



32
33
34
# File 'lib/gettext/tools/rmsgmerge.rb', line 32

def set_comment(msgid_or_sym, comment)
  @msgid2comment[msgid_or_sym] = comment
end