Class: I18nPoTools::Formats::PoFormat

Inherits:
BaseFormat
  • Object
show all
Defined in:
lib/i18n_po_tools/formats/po_format.rb

Instance Attribute Summary

Attributes inherited from BaseFormat

#options

Instance Method Summary collapse

Methods inherited from BaseFormat

#banner_msg, #file_encoding, #read_file, #read_with_options, #write, #write_file

Constructor Details

#initialize(options = {}) ⇒ PoFormat

Returns a new instance of PoFormat.



10
11
12
# File 'lib/i18n_po_tools/formats/po_format.rb', line 10

def initialize(options = {})
  @pot = options.fetch(:pot, false)
end

Instance Method Details

#read(input_filename) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/i18n_po_tools/formats/po_format.rb', line 14

def read(input_filename)
  content = read_file(input_filename)

  translations = GetPomo::PoFile.parse(content)
  h = {}
  translations.each do |t|
    t.fix_after_read

    key = t.msgctxt
    if key.blank?
      if t.msgid.present?
        STDERR.puts "WARNING: #{msgid} can not be imported (empty key in the msgctxt field)"
      end
      next
    end

    if t.plural?
      if @pot
        h[key] = PluralMessage.new(translations: t.msgid)
        STDERR.puts "WARNING: Plural forms for #{key} can be trancated (only 2 forms for msgid, format limitation)"
      else
        h[key] = PluralMessage.new(translations: t.msgstr)
      end
    else
      if @pot
        h[key] = t.msgid
      else
        h[key] = t.msgstr
      end
    end
  end
  h
end

#write_with_options(data, base_data, options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/i18n_po_tools/formats/po_format.rb', line 48

def write_with_options(data, base_data, options)
  @options = options

  translations = []
  translations << header_entity(@pot)

  if @pot
    data.each do |k,v|
      translations << message_entity(k,v,nil)
    end
  else
    base_data.each do |k,v|
      translations << message_entity(k,v,data[k])
    end
  end

  po = GetPomo::PoFile.new(:translations=>translations)
  content = po.to_text
  write_file(options[:output_filename], content)
end