Welcome to mirror list, hosted at ThFree Co, Russian Federation.

django.rb « formatters « twine « lib - github.com/mapsme/twine.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5c8876665a573b3640d9d99d60b826f2b5294922 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
96
97
98
99
100
101
102
103
module Twine
  module Formatters
    class Django < Abstract
      def format_name
        'django'
      end

      def extension
        '.po'
      end

      def default_file_name
        'strings.po'
      end

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

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

        last_comment = nil
        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

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

      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

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

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

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

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

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

      def format_key(key)
        escape_quotes(key)
      end

      def format_value(value)
        escape_quotes(value)
      end
    end
  end
end

Twine::Formatters.formatters << Twine::Formatters::Django.new