class GetPomo::MoFile

Constants

CONTEXT_SEPARATOR
PLURAL_SEPARATOR

Attributes

translations[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/get_pomo/mo_file.rb, line 20
def initialize(options = {})
  @translations = options[:translations] || []
end
parse(text) click to toggle source
# File lib/get_pomo/mo_file.rb, line 9
def self.parse(text)
  MoFile.new.add_translations_from_text(text)
end
to_text(translations) click to toggle source
# File lib/get_pomo/mo_file.rb, line 13
def self.to_text(translations)
  m = MoFile.new(:translations=>translations)
  m.to_text
end

Public Instance Methods

add_translations_from_text(text) click to toggle source
# File lib/get_pomo/mo_file.rb, line 24
def add_translations_from_text(text)
  text = StringIO.new(text)
  @translations += GetPomo::GetText::MOFile.open(text, "UTF-8").map do |msgid,msgstr|
    translation = Translation.new

    if msgid.include? CONTEXT_SEPARATOR
      translation.msgctxt, msgid = msgid.split CONTEXT_SEPARATOR, 2
    end

    if plural? msgid or plural? msgstr
      translation.msgid = split_plural(msgid)
      translation.msgstr = split_plural(msgstr)
    else
      translation.msgid = msgid
      translation.msgstr = msgstr
    end

    translation
  end
end
to_text() click to toggle source
# File lib/get_pomo/mo_file.rb, line 45
def to_text
  m = GetPomo::GetText::MOFile.new
  GetPomo.unique_translations(translations).each do |t|
    key = plural_to_string(t.msgid)
    if msgctxt = t.msgctxt
      key = "#{msgctxt}#{CONTEXT_SEPARATOR}#{key}"
    end
    m[key] = plural_to_string(t.msgstr)
  end

  io = StringIO.new
  m.save_to_stream io
  io.rewind
  io.read
end

Private Instance Methods

plural?(string) click to toggle source
# File lib/get_pomo/mo_file.rb, line 67
def plural? string
  string.include? PLURAL_SEPARATOR
end
plural_to_string(plural_or_singular) click to toggle source
# File lib/get_pomo/mo_file.rb, line 63
def plural_to_string(plural_or_singular)
  [*plural_or_singular] * PLURAL_SEPARATOR
end
split_plural(string) click to toggle source
# File lib/get_pomo/mo_file.rb, line 71
def split_plural string
  string.split PLURAL_SEPARATOR
end