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
20
21
22
# File 'lib/get_pomo/po_file.rb', line 17

def initialize(options = {})
  @translations = options[:translations] || []
  # no object options should be set through initialize arguments for now,
  # as they are meant to be changeable for each method call
  @options =  {:parse_obsoletes => false, :merge => false}
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, options = {}) ⇒ Object



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

def self.parse(text, options = {})
  PoFile.new.add_translations_from_text(text, options)
end

.to_text(translations, options = {}) ⇒ Object



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

def self.to_text(translations, options = {})
  p = PoFile.new(:translations => translations)
  p.to_text(options)
end

Instance Method Details

#add_translations_from_text(text, options = {}) ⇒ 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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/get_pomo/po_file.rb', line 27

def add_translations_from_text(text, options = {})
  # only keep valid options for this method
  options.delete_if do |key, value|
    !([:parse_obsoletes].include?(key))
  end
  # default options for this method
  default_options = {:parse_obsoletes => false}
  @options.merge!(default_options.merge(options))
  start_new_translation
  text.sub!(/^#{"\357\273\277"}/, "") #remove bom (utf8 byte order mark)
  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_text(options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/get_pomo/po_file.rb', line 52

def to_text(options = {})
  # only keep valid options for this method
  options.delete_if do |key, value|
    !([:merge].include?(key))
  end
  # default options for this method
  default_options = {:merge => false}
  @options.merge!(default_options.merge(options))
  GetPomo.unique_translations(translations, options[:merge]).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
      translation.obsolete? ? "" : %Q(msgid "#{translation.msgid}"\n)+
      %Q(msgstr "#{translation.msgstr}")
    end

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