class Twine::Formatters::Gettext

Public Instance Methods

default_file_name() click to toggle source
# File lib/twine/formatters/gettext.rb, line 14
def default_file_name
  'strings.po'
end
extension() click to toggle source
# File lib/twine/formatters/gettext.rb, line 10
def extension
  '.po'
end
format_base_translation(definition) click to toggle source
# File lib/twine/formatters/gettext.rb, line 82
def format_base_translation(definition)
  "msgid \"#{escape_quotes(definition.translations[@default_lang])}\"\n"
end
format_comment(definition, lang) click to toggle source
# File lib/twine/formatters/gettext.rb, line 69
def format_comment(definition, lang)
  "#. \"#{escape_quotes(definition.comment)}\"\n" if definition.comment
end
format_file(lang) click to toggle source
Calls superclass method Twine::Formatters::Abstract#format_file
# File lib/twine/formatters/gettext.rb, line 50
def format_file(lang)
  @default_lang = twine_file.language_codes[0]
  result = super
  @default_lang = nil
  result
end
format_header(lang) click to toggle source
# File lib/twine/formatters/gettext.rb, line 57
def format_header(lang)
  "msgid \"\"\nmsgstr \"\"\n\"Language: #{lang}\"\n\"X-Generator: Twine #{Twine::VERSION}\"\n"
end
format_key(key) click to toggle source
# File lib/twine/formatters/gettext.rb, line 78
def format_key(key)
  "msgctxt \"#{escape_quotes(key)}\"\n"
end
format_key_value(definition, lang) click to toggle source
# File lib/twine/formatters/gettext.rb, line 73
def format_key_value(definition, lang)
  value = definition.translation_for_lang(lang)
  [format_key(definition.key.dup), format_base_translation(definition), format_value(value.dup)].compact.join
end
format_name() click to toggle source
# File lib/twine/formatters/gettext.rb, line 6
def format_name
  'gettext'
end
format_section_header(section) click to toggle source
# File lib/twine/formatters/gettext.rb, line 61
def format_section_header(section)
  "# SECTION: #{section.name}"
end
format_value(value) click to toggle source
# File lib/twine/formatters/gettext.rb, line 86
def format_value(value)
  "msgstr \"#{escape_quotes(value)}\"\n"
end
read(io, lang) click to toggle source
# File lib/twine/formatters/gettext.rb, line 18
def read(io, lang)
  comment_regex = /#.? *"(.*)"$/
  key_regex = /msgctxt *"(.*)"$/
  value_regex = /msgstr *"(.*)"$/m
  
  while item = io.gets("\n\n")
    key = nil
    value = nil
    comment = nil

    comment_match = comment_regex.match(item)
    if comment_match
      comment = comment_match[1]
    end
    key_match = key_regex.match(item)
    if key_match
      key = key_match[1].gsub('\\"', '"')
    end
    value_match = value_regex.match(item)
    if value_match
      value = value_match[1].gsub(/"\n"/, '').gsub('\\"', '"')
    end
    if key and key.length > 0 and value and value.length > 0
      set_translation_for_key(key, lang, value)
      if comment and comment.length > 0 and !comment.start_with?("SECTION:")
        set_comment_for_key(key, comment)
      end
      comment = nil
    end
  end
end
should_include_definition(definition, lang) click to toggle source
# File lib/twine/formatters/gettext.rb, line 65
def should_include_definition(definition, lang)
  super and !definition.translation_for_lang(@default_lang).nil?
end