Class: GetPomo::MoFile

Inherits:
Object
  • Object
show all
Defined in:
lib/get_pomo/mo_file.rb

Constant Summary collapse

PLURAL_SEPERATOR =
"\000"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MoFile

Returns a new instance of MoFile.



19
20
21
# File 'lib/get_pomo/mo_file.rb', line 19

def initialize(options = {})
  @translations = options[:translations] || []
end

Instance Attribute Details

#translationsObject (readonly)

Returns the value of attribute translations.



17
18
19
# File 'lib/get_pomo/mo_file.rb', line 17

def translations
  @translations
end

Class Method Details

.parse(text) ⇒ Object



8
9
10
# File 'lib/get_pomo/mo_file.rb', line 8

def self.parse(text)
  MoFile.new.add_translations_from_text(text)
end

.to_text(translations) ⇒ Object



12
13
14
15
# File 'lib/get_pomo/mo_file.rb', line 12

def self.to_text(translations)
  m = MoFile.new(:translations=>translations)
  m.to_text
end

Instance Method Details

#add_translations_from_text(text) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/get_pomo/mo_file.rb', line 23

def add_translations_from_text(text)
  text = StringIO.new(text)
  @translations += GetPomo::GetText::MOFile.open(text, "UTF-8").map do |msgid,msgstr|
    translation = Translation.new
    if plural? msgid or plural? msgstr
      translation.msgid = split_plural(msgid)
      translation.msgstr = split_plural(msgstr)
    else
      translation.msgid = msgid
      translation.msgstr = msgstr
    end
    translation
  end
end

#to_textObject



38
39
40
41
42
43
44
45
46
# File 'lib/get_pomo/mo_file.rb', line 38

def to_text
  m = GetPomo::GetText::MOFile.new
  GetPomo.unique_translations(translations).each {|t| m[plural_to_string(t.msgid)] = plural_to_string(t.msgstr)}

  io = StringIO.new
  m.save_to_stream io
  io.rewind
  io.read
end