Class: Twine::Formatters::Django

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

Instance Attribute Summary

Attributes inherited from Abstract

#options, #strings

Instance Method Summary collapse

Methods inherited from Abstract

#escape_quotes, #format_key_value, #format_section, #format_sections, #initialize, #output_path_for_language, #set_comment_for_key, #set_translation_for_key, #should_include_row, #write_all_files, #write_file

Constructor Details

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

Instance Method Details

#can_handle_directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

def can_handle_directory?(path)
  Dir.entries(path).any? { |item| /^.+\.po$/.match(item) }
end

#default_file_nameObject



16
17
18
# File 'lib/twine/formatters/django.rb', line 16

def default_file_name
  return 'strings.po'
end

#determine_language_given_path(path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/twine/formatters/django.rb', line 20

def determine_language_given_path(path)
    path_arr = path.split(File::SEPARATOR)
    path_arr.each do |segment|
        match = /(..)\.po$/.match(segment)
        if match
            return match[1]
        end
    end
    
  return
end

#extensionObject



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

def extension
  '.po'
end

#format_base_translation(row) ⇒ Object



116
117
118
119
# File 'lib/twine/formatters/django.rb', line 116

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

#format_comment(row, lang) ⇒ Object



126
127
128
# File 'lib/twine/formatters/django.rb', line 126

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

#format_file(strings, lang) ⇒ Object



97
98
99
100
101
102
# File 'lib/twine/formatters/django.rb', line 97

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

#format_header(lang) ⇒ Object



104
105
106
# File 'lib/twine/formatters/django.rb', line 104

def format_header(lang)
  "##\n # Django Strings File\n # Generated by Twine #{Twine::VERSION}\n # Language: #{lang}\n"
end

#format_key(key) ⇒ Object



130
131
132
# File 'lib/twine/formatters/django.rb', line 130

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_row(row, lang) ⇒ Object



112
113
114
# File 'lib/twine/formatters/django.rb', line 112

def format_row(row, lang)
  [format_comment(row, lang), format_base_translation(row), format_key_value(row, lang)].compact.join
end

#format_section_header(section) ⇒ Object



108
109
110
# File 'lib/twine/formatters/django.rb', line 108

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

#format_value(value) ⇒ Object



134
135
136
# File 'lib/twine/formatters/django.rb', line 134

def format_value(value)
  escape_quotes(value)
end

#key_value_patternObject



121
122
123
124
# File 'lib/twine/formatters/django.rb', line 121

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

#read_file(path, lang) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/twine/formatters/django.rb', line 32

def read_file(path, lang)
  comment_regex = /#\. *"?(.*)"?$/
  key_regex = /msgid *"(.*)"$/
  value_regex = /msgstr *"(.*)"$/m

  encoding = Twine::Encoding.encoding_for_path(path)
  sep = nil
  if !encoding.respond_to?(:encode)
    # This code is not necessary in 1.9.3 and does not work as it did in 1.8.7.
    if encoding.end_with? 'LE'
      sep = "\x0a\x00"
    elsif encoding.end_with? 'BE'
      sep = "\x00\x0a"
    else
      sep = "\n"
    end
  end

  if encoding.index('UTF-16')
    mode = "rb:#{encoding}"
  else
    mode = "r:#{encoding}"
  end

  File.open(path, mode) do |f|
    last_comment = nil
    while line = (sep) ? f.gets(sep) : f.gets
      if encoding.index('UTF-16')
        if line.respond_to? :encode!
          line.encode!('UTF-8')
        else
          require 'iconv'
          line = Iconv.iconv('UTF-8', encoding, line).join
        end
      end
      
      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
end