Class: Twine::Formatters::Django

Inherits:
Abstract
  • Object
show all
Defined in:
lib/twine/formatters/django.rb

Constant Summary

Constants inherited from Abstract

Abstract::LANGUAGE_CODE_WITH_OPTIONAL_REGION_CODE

Instance Attribute Summary

Attributes inherited from Abstract

#options, #twine_file

Instance Method Summary collapse

Methods inherited from Abstract

#can_handle_directory?, #determine_language_given_path, #escape_quotes, #format_key_value, #format_section, #format_sections, #initialize, #output_path_for_language, #set_comment_for_key, #set_translation_for_key, #should_include_definition

Constructor Details

This class inherits a constructor from Twine::Formatters::Abstract

Instance Method Details

#default_file_nameObject



12
13
14
# File 'lib/twine/formatters/django.rb', line 12

def default_file_name
  'strings.po'
end

#extensionObject



8
9
10
# File 'lib/twine/formatters/django.rb', line 8

def extension
  '.po'
end

#format_base_translation(definition) ⇒ Object



67
68
69
70
# File 'lib/twine/formatters/django.rb', line 67

def format_base_translation(definition)
  base_translation = definition.translations[@default_lang]
  "# base translation: \"#{base_translation}\"\n" if base_translation
end

#format_comment(definition, lang) ⇒ Object



77
78
79
# File 'lib/twine/formatters/django.rb', line 77

def format_comment(definition, lang)
  "#. #{escape_quotes(definition.comment)}\n" if definition.comment
end

#format_definition(definition, lang) ⇒ Object



63
64
65
# File 'lib/twine/formatters/django.rb', line 63

def format_definition(definition, lang)
  [format_comment(definition, lang), format_base_translation(definition), format_key_value(definition, lang)].compact.join
end

#format_file(lang) ⇒ Object



48
49
50
51
52
53
# File 'lib/twine/formatters/django.rb', line 48

def format_file(lang)
  @default_lang = @twine_file.language_codes[0]
  result = super
  @default_lang = nil
  result
end

#format_header(lang) ⇒ Object



55
56
57
# File 'lib/twine/formatters/django.rb', line 55

def format_header(lang)
  "##\n # 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) ⇒ Object



81
82
83
# File 'lib/twine/formatters/django.rb', line 81

def format_key(key)
  escape_quotes(key)
end

#format_nameObject



4
5
6
# File 'lib/twine/formatters/django.rb', line 4

def format_name
  'django'
end

#format_section_header(section) ⇒ Object



59
60
61
# File 'lib/twine/formatters/django.rb', line 59

def format_section_header(section)
  "#--------- #{section.name} ---------#\n"
end

#format_value(value) ⇒ Object



85
86
87
# File 'lib/twine/formatters/django.rb', line 85

def format_value(value)
  escape_quotes(value)
end

#key_value_patternObject



72
73
74
75
# File 'lib/twine/formatters/django.rb', line 72

def key_value_pattern
  "msgid \"%{key}\"\n" +
  "msgstr \"%{value}\"\n"
end

#read(io, lang) ⇒ Object



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/twine/formatters/django.rb', line 16

def read(io, lang)
  comment_regex = /#\. *"?(.*)"?$/
  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