Class: GetPomo::PoFile

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PoFile

Returns a new instance of PoFile.



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

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

Instance Attribute Details

#translationsObject (readonly)

Returns the value of attribute translations.



15
16
17
# File 'lib/get_pomo/po_file.rb', line 15

def translations
  @translations
end

Class Method Details

.parse(text) ⇒ Object



6
7
8
# File 'lib/get_pomo/po_file.rb', line 6

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

.to_text(translations) ⇒ Object



10
11
12
13
# File 'lib/get_pomo/po_file.rb', line 10

def self.to_text(translations)
  p = PoFile.new(:translations=>translations)
  p.to_text
end

Instance Method Details

#add_translations_from_text(text) ⇒ Object

the text is split into lines and then converted into logical translations each translation consists of comments(that come before a translation) and a msgid / msgstr



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/get_pomo/po_file.rb', line 24

def add_translations_from_text(text)
  start_new_translation
  text.gsub!(/^#{"\357\273\277"}/, "") #remove boom
  text.split(/$/).each_with_index do |line,index|
    @line_number = index + 1
    next if line.empty?
    if method_call? line
      parse_method_call line
    elsif comment? line
      add_comment line
    else
      add_string line
    end
  end
  start_new_translation #instance_variable has to be overwritten or errors can occur on next add
  translations
end

#to_textObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/get_pomo/po_file.rb', line 42

def to_text
  GetPomo.unique_translations(translations).map do |translation|
    comment = translation.comment.to_s.split(/\n|\r\n/).map{|line|"#{line}\n"}*''

    msgctxt = if translation.msgctxt
      %Q(msgctxt "#{translation.msgctxt}"\n)
    else
      ""
    end

    msgid_and_msgstr = if translation.plural?
      msgids =
      %Q(msgid "#{translation.msgid[0]}"\n)+
      %Q(msgid_plural "#{translation.msgid[1]}"\n)

      msgstrs = []
      translation.msgstr.each_with_index do |msgstr,index|
        msgstrs << %Q(msgstr[#{index}] "#{msgstr}")
      end

      msgids + (msgstrs*"\n")
    else
      %Q(msgid "#{translation.msgid}"\n)+
      %Q(msgstr "#{translation.msgstr}")
    end

    comment + msgctxt + msgid_and_msgstr
  end * "\n\n"
end