Class: GetPomo::PoFile
- Inherits:
-
Object
- Object
- GetPomo::PoFile
- Defined in:
- lib/get_pomo/po_file.rb
Instance Attribute Summary collapse
-
#translations ⇒ Object
readonly
Returns the value of attribute translations.
Class Method Summary collapse
Instance Method Summary collapse
-
#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.
-
#initialize(options = {}) ⇒ PoFile
constructor
A new instance of PoFile.
- #to_text ⇒ Object
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( = {}) @translations = [:translations] || [] end |
Instance Attribute Details
#translations ⇒ Object (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 |
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_text ⇒ Object
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 |