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, 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.
-
#initialize(options = {}) ⇒ PoFile
constructor
A new instance of PoFile.
- #to_text(options = {}) ⇒ Object
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( = {}) @translations = [: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
#translations ⇒ Object (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, = {}) PoFile.new.add_translations_from_text(text, ) 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, = {}) # only keep valid options for this method .delete_if do |key, value| !([:parse_obsoletes].include?(key)) end # default options for this method = {:parse_obsoletes => false} @options.merge!(.merge()) 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( = {}) # only keep valid options for this method .delete_if do |key, value| !([:merge].include?(key)) end # default options for this method = {:merge => false} @options.merge!(.merge()) GetPomo.unique_translations(translations, [: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 |