class Twine::Formatters::Django

For a description of the .po file format, see www.gnu.org/software/gettext/manual/html_node/PO-Files.html

Public Instance Methods

default_file_name() click to toggle source
# File lib/twine/formatters/django.rb, line 13
def default_file_name
  'strings.po'
end
extension() click to toggle source
# File lib/twine/formatters/django.rb, line 9
def extension
  '.po'
end
format_base_translation(definition) click to toggle source
# File lib/twine/formatters/django.rb, line 69
def format_base_translation(definition)
  base_translation = definition.translations[@default_lang]
  "# base translation: \"#{base_translation}\"\n" if base_translation
end
format_comment(definition, lang) click to toggle source
# File lib/twine/formatters/django.rb, line 79
def format_comment(definition, lang)
  "#. #{escape_quotes(definition.comment)}\n" if definition.comment
end
format_definition(definition, lang) click to toggle source
# File lib/twine/formatters/django.rb, line 65
def format_definition(definition, lang)
  [format_comment(definition, lang), format_base_translation(definition), format_key_value(definition, lang)].compact.join
end
format_file(lang) click to toggle source
Calls superclass method Twine::Formatters::Abstract#format_file
# File lib/twine/formatters/django.rb, line 49
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/django.rb, line 56
def format_header(lang)
  # see https://www.gnu.org/software/trans-coord/manual/gnun/html_node/PO-Header.html for details
  "# Django Strings File\n# Generated by Twine #{Twine::VERSION}\n# Language: #{lang}\nmsgid \"\"\nmsgstr \"\"\n\"Content-Type: text/plain; charset=UTF-8\\n\""
end
format_key(key) click to toggle source
# File lib/twine/formatters/django.rb, line 83
def format_key(key)
  escape_quotes(key)
end
format_name() click to toggle source
# File lib/twine/formatters/django.rb, line 5
def format_name
  'django'
end
format_section_header(section) click to toggle source
# File lib/twine/formatters/django.rb, line 61
def format_section_header(section)
  "# --------- #{section.name} --------- #\n"
end
format_value(value) click to toggle source
# File lib/twine/formatters/django.rb, line 87
def format_value(value)
  escape_quotes(value)
end
key_value_pattern() click to toggle source
# File lib/twine/formatters/django.rb, line 74
def key_value_pattern
  "msgid \"%{key}\"\n" +
  "msgstr \"%{value}\"\n"
end
read(io, lang) click to toggle source
# File lib/twine/formatters/django.rb, line 17
def read(io, lang)
  comment_regex = /^\s*#\. *"?(.*)"?$/
  key_regex = /^msgid *"(.*)"$/
  value_regex = /^msgstr *"(.*)"$/m

  while line = io.gets          
    comment_match = comment_regex.match(line)
    if comment_match
      comment = comment_match[1]
    end

    key_match = key_regex.match(line)
    if key_match
      key = key_match[1].gsub('\\"', '"')
    end
    value_match = value_regex.match(line)
    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?("--------- ")
        set_comment_for_key(key, comment)
      end
      key = nil
      value = nil
      comment = nil
    end
  end
end