Class: Twine::Formatters::Gettext

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

Constant Summary collapse

FORMAT_NAME =
'gettext'
EXTENSION =
'.po'
DEFAULT_FILE_NAME =
'strings.po'

Instance Attribute Summary

Attributes inherited from Abstract

#options, #strings

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract

#androidify_substitutions, #initialize, #iosify_substitutions, #set_comment_for_key, #set_translation_for_key, #write_all_files

Constructor Details

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

Class Method Details

.can_handle_directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/twine/formatters/gettext.rb', line 10

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

Instance Method Details

#default_file_nameObject



14
15
16
# File 'lib/twine/formatters/gettext.rb', line 14

def default_file_name
  return DEFAULT_FILE_NAME
end

#determine_language_given_path(path) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/twine/formatters/gettext.rb', line 18

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

#read_file(path, lang) ⇒ Object



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
# File 'lib/twine/formatters/gettext.rb', line 30

def read_file(path, lang)
  comment_regex = /#.? *"(.*)"$/
  key_regex = /msgctxt *"(.*)"$/
  value_regex = /msgstr *"(.*)"$/m
  File.open(path, 'r:UTF-8') do |f|
    while item = f.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
end

#write_file(path, lang) ⇒ Object



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
104
105
106
107
# File 'lib/twine/formatters/gettext.rb', line 63

def write_file(path, lang)
  default_lang = @strings.language_codes[0]
  encoding = @options[:output_encoding] || 'UTF-8'
  File.open(path, "w:#{encoding}") do |f|
    f.puts "msgid \"\"\nmsgstr \"\"\n\"Language: #{lang}\\n\"\n\"X-Generator: Twine #{Twine::VERSION}\\n\"\n\n"
    @strings.sections.each do |section|
      printed_section = false
      section.rows.each do |row|
        if row.matches_tags?(@options[:tags], @options[:untagged])
          if !printed_section
            f.puts ''
              if section.name && section.name.length > 0
                section_name = section.name.gsub('--', '')
                f.puts "# SECTION: #{section_name}"
              end
            printed_section = true
          end

          basetrans = row.translated_string_for_lang(default_lang)

          if basetrans
            key = row.key
            key = key.gsub('"', '\\\\"')

            comment = row.comment
            if comment
              comment = comment.gsub('"', '\\\\"')
            end

            if comment && comment.length > 0
              f.print "#. \"#{comment}\"\n"
            end

            f.print "msgctxt \"#{key}\"\nmsgid \"#{basetrans}\"\n"
            value = row.translated_string_for_lang(lang)
            if value
              value = value.gsub('"', '\\\\"')
            end
            f.print "msgstr \"#{value}\"\n\n"
          end
        end
      end
    end
  end
end