Class: GetText::MO

Inherits:
Hash
  • Object
show all
Defined in:
lib/gettext/mo.rb

Defined Under Namespace

Classes: Header, HeaderRev1, InvalidFormat

Constant Summary collapse

MAGIC_BIG_ENDIAN =
"\x95\x04\x12\xde".b
MAGIC_LITTLE_ENDIAN =
"\xde\x12\x04\x95".b
HASHWORDBITS =
32

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_charset = nil) ⇒ MO

Returns a new instance of MO.



56
57
58
59
60
61
62
63
64
# File 'lib/gettext/mo.rb', line 56

def initialize(output_charset = nil)
  @filename = nil
  @last_modified = nil
  @little_endian = true
  @charset = nil
  @output_charset = output_charset
  @plural_proc = nil
  super()
end

Instance Attribute Details

#charsetObject (readonly)

Returns the value of attribute charset.



303
304
305
# File 'lib/gettext/mo.rb', line 303

def charset
  @charset
end

#filenameObject (readonly)

Returns the value of attribute filename.



28
29
30
# File 'lib/gettext/mo.rb', line 28

def filename
  @filename
end

#last_modifiedObject

Returns the value of attribute last_modified.



302
303
304
# File 'lib/gettext/mo.rb', line 302

def last_modified
  @last_modified
end

#little_endianObject

Returns the value of attribute little_endian.



302
303
304
# File 'lib/gettext/mo.rb', line 302

def little_endian
  @little_endian
end

#npluralsObject (readonly)

Returns the value of attribute nplurals.



303
304
305
# File 'lib/gettext/mo.rb', line 303

def nplurals
  @nplurals
end

#pathObject

Returns the value of attribute path.



302
303
304
# File 'lib/gettext/mo.rb', line 302

def path
  @path
end

#pluralObject (readonly)

Returns the value of attribute plural.



303
304
305
# File 'lib/gettext/mo.rb', line 303

def plural
  @plural
end

Class Method Details

.open(arg = nil, output_charset = nil) ⇒ Object



51
52
53
54
# File 'lib/gettext/mo.rb', line 51

def self.open(arg = nil, output_charset = nil)
  result = self.new(output_charset)
  result.load(arg)
end

Instance Method Details

#hash_string(str) ⇒ Object

From gettext-0.12.1/gettext-runtime/intl/hash-string.h Defines the so called `hashpjw' function by P.J. Weinberger [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools, 1986, 1987 Bell Telephone Laboratories, Inc.]



182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/gettext/mo.rb', line 182

def hash_string(str)
  hval = 0
  str.each_byte do |b|
    break if b == '\0'
    hval <<= 4
    hval += b.to_i
    g = hval & (0xf << (HASHWORDBITS - 4))
    if (g != 0)
      hval ^= g >> (HASHWORDBITS - 8)
      hval ^= g
    end
  end
  hval
end

#load(arg) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gettext/mo.rb', line 82

def load(arg)
  if arg.kind_of? String
    begin
      st = File.stat(arg)
      @last_modified = [st.ctime, st.mtime]
    rescue Exception
    end
    load_from_file(arg)
  else
    load_from_stream(arg)
  end
  @filename = arg
  self
end

#load_from_file(filename) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
# File 'lib/gettext/mo.rb', line 270

def load_from_file(filename)
  @filename = filename
  File.open(filename, 'rb') do |f|
    begin
      load_from_stream(f)
    rescue => e
      e.set_backtrace(["#{filename}:#{f.lineno}"] + e.backtrace)
      raise e
    end
  end
end

#load_from_stream(io) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/gettext/mo.rb', line 97

def load_from_stream(io)
  magic = io.read(4)
  case magic
  when MAGIC_BIG_ENDIAN
    @little_endian = false
  when MAGIC_LITTLE_ENDIAN
    @little_endian = true
  else
    raise InvalidFormat.new(sprintf("Unknown signature %s", magic.dump))
  end

  endian_type6 = @little_endian ? 'V6' : 'N6'
  endian_type_astr = @little_endian ? 'V*' : 'N*'

  header = HeaderRev1.new(magic, *(io.read(4 * 6).unpack(endian_type6)))

  if header.revision == 1
    # FIXME: It doesn't support sysdep correctly.
    header.n_sysdep_segments = io.read(4).unpack(endian_type6)
    header.sysdep_segments_offset = io.read(4).unpack(endian_type6)
    header.n_sysdep_strings = io.read(4).unpack(endian_type6)
    header.orig_sysdep_tab_offset = io.read(4).unpack(endian_type6)
    header.trans_sysdep_tab_offset = io.read(4).unpack(endian_type6)
  elsif header.revision > 1
    raise InvalidFormat.new(sprintf("file format revision %d isn't supported", header.revision))
  end
  io.pos = header.orig_table_offset
  orig_table_data = io.read((4 * 2) * header.nstrings).unpack(endian_type_astr)

  io.pos = header.translated_table_offset
  trans_table_data = io.read((4 * 2) * header.nstrings).unpack(endian_type_astr)

  msgids = Array.new(header.nstrings)
  for i in 0...header.nstrings
    io.pos = orig_table_data[i * 2 + 1]
    msgids[i] = io.read(orig_table_data[i * 2 + 0])
  end

  clear
  for i in 0...header.nstrings
    io.pos = trans_table_data[i * 2 + 1]
    msgstr = io.read(trans_table_data[i * 2 + 0])

    msgid = msgids[i]
    if msgid.nil? || msgid.empty?
      if msgstr
        @charset = nil
        @nplurals = nil
        @plural = nil
        msgstr.each_line{|line|
          if /^Content-Type:/i =~ line and /charset=((?:\w|-)+)/i =~ line
            @charset = $1
          elsif /^Plural-Forms:\s*nplurals\s*\=\s*(\d*);\s*plural\s*\=\s*([^;]*)\n?/ =~ line
            @nplurals = $1
            @plural = $2
          end
          break if @charset and @nplurals
        }
        @nplurals = "1" unless @nplurals
        @plural = "0" unless @plural
      end
    else
      unless msgstr.nil?
        msgstr = convert_encoding(msgstr, msgid)
      end
    end
    msgid.force_encoding(@charset) if @charset
    self[msgid] = msgstr.freeze
  end
  self
end

#next_prime(seed) ⇒ Object



173
174
175
# File 'lib/gettext/mo.rb', line 173

def next_prime(seed)
  Prime.instance.find{|x| x > seed }
end

#plural_as_procObject



290
291
292
293
294
295
296
297
298
299
300
# File 'lib/gettext/mo.rb', line 290

def plural_as_proc
  unless @plural_proc
    @plural_proc = Proc.new{|n| eval(@plural)}
    begin
      @plural_proc.call(1)
    rescue
      @plural_proc = Proc.new{|n| 0}
    end
  end
  @plural_proc
end

#prime?(number) ⇒ Boolean

Returns:

  • (Boolean)


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

def prime?(number)
  ('1' * number) !~ /^1?$|^(11+?)\1+$/
end

#save_to_file(filename) ⇒ Object



282
283
284
# File 'lib/gettext/mo.rb', line 282

def save_to_file(filename)
  File.open(filename, 'wb'){|f| save_to_stream(f)}
end

#save_to_stream(io) ⇒ Object

Save data as little endian format.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/gettext/mo.rb', line 198

def save_to_stream(io)
  # remove untranslated message
  translated_messages = reject do |msgid, msgstr|
    msgstr.nil?
  end

  size = translated_messages.size
  header_size = 4 * 7
  table_size  = 4 * 2 * size

  hash_table_size = next_prime((size * 4) / 3)
  hash_table_size = 3 if hash_table_size <= 2
  header = Header.new(
                      MAGIC_LITTLE_ENDIAN,          # magic
                      0,                            # revision
                      size,                         # nstrings
                      header_size,                  # orig_table_offset
                      header_size + table_size,     # translated_table_offset
                      hash_table_size,              # hash_table_size
                      header_size + table_size * 2  # hash_table_offset
                      )
  io.write(header.to_a.pack('a4V*'))

  ary = translated_messages.to_a
  ary.sort!{|a, b| a[0] <=> b[0]} # sort by original string

  pos = header.hash_table_size * 4 + header.hash_table_offset

  orig_table_data = Array.new()
  ary.each{|item, _|
    orig_table_data.push(item.bytesize)
    orig_table_data.push(pos)
    pos += item.bytesize + 1 # +1 is <NUL>
  }
  io.write(orig_table_data.pack('V*'))

  trans_table_data = Array.new()
  ary.each{|_, item|
    trans_table_data.push(item.bytesize)
    trans_table_data.push(pos)
    pos += item.bytesize + 1 # +1 is <NUL>
  }
  io.write(trans_table_data.pack('V*'))

  hash_tab = Array.new(hash_table_size)
  j = 0
  ary[0...size].each {|key, _|
    hash_val = hash_string(key)
    idx = hash_val % hash_table_size
    if hash_tab[idx] != nil
      incr = 1 + (hash_val % (hash_table_size - 2))
      begin
        if (idx >= hash_table_size - incr)
          idx -= hash_table_size - incr
        else
          idx += incr
        end
      end until (hash_tab[idx] == nil)
    end
    hash_tab[idx] = j + 1
    j += 1
  }
  hash_tab.collect!{|i| i ? i : 0}

  io.write(hash_tab.pack('V*'))

  ary.each{|item, _| io.write(item); io.write("\0") }
  ary.each{|_, item| io.write(item); io.write("\0") }

  self
end

#set_comment(msgid_or_sym, comment) ⇒ Object



286
287
288
# File 'lib/gettext/mo.rb', line 286

def set_comment(msgid_or_sym, comment)
  #Do nothing
end

#store(msgid, msgstr, options) ⇒ Object



66
67
68
69
# File 'lib/gettext/mo.rb', line 66

def store(msgid, msgstr, options)
  string = generate_original_string(msgid, options)
  self[string] = msgstr
end

#update!Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/gettext/mo.rb', line 71

def update!
  if FileTest.exist?(@filename)
    st = File.stat(@filename)
    load(@filename) unless (@last_modified == [st.ctime, st.mtime])
  else
    warn "#{@filename} was lost." if $DEBUG
    clear
  end
  self
end