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.



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

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

Instance Attribute Details

#translationsObject (readonly)

Returns the value of attribute translations.



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

def translations
  @translations
end

Class Method Details

.parse(text) ⇒ Object



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

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

.to_text(translations) ⇒ Object



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

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



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

def add_translations_from_text(text)
  start_new_translation
  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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/get_pomo/po_file.rb', line 40

def to_text
  GetPomo.unique_translations(translations).map {|translation|
    comment = translation.comment.to_s.split(/\n|\r\n/).map{|line|"#{line}\n"}*''
    msgid_and_msgstr = if translation.plural?
      msgids =
      %Q(msgid "#{escape_quotes(translation.msgid[0])}"\n)+
      %Q(msgid_plural "#{escape_quotes(translation.msgid[1])}"\n)

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

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

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