Class: Twine::Formatters::Gettext

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

Instance Attribute Summary

Attributes inherited from Abstract

#options, #twine_file

Instance Method Summary collapse

Methods inherited from Abstract

#can_handle_directory?, #escape_quotes, #format_definition, #format_section, #format_sections, #initialize, #key_value_pattern, #output_path_for_language, #set_comment_for_key, #set_translation_for_key

Constructor Details

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

Instance Method Details

#default_file_nameObject



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

def default_file_name
  'strings.po'
end

#determine_language_given_path(path) ⇒ Object



18
19
20
21
22
23
24
25
26
# 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 = /([a-z]{2}(-[A-Za-z]{2})?)\.po$/.match(segment)
    return match[1] if match
  end

  return
end

#extensionObject



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

def extension
  '.po'
end

#format_base_translation(definition) ⇒ Object



92
93
94
# File 'lib/twine/formatters/gettext.rb', line 92

def format_base_translation(definition)
  "msgid \"#{definition.translations[@default_lang]}\"\n"
end

#format_comment(definition, lang) ⇒ Object



79
80
81
# File 'lib/twine/formatters/gettext.rb', line 79

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

#format_file(lang) ⇒ Object



60
61
62
63
64
65
# File 'lib/twine/formatters/gettext.rb', line 60

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

#format_header(lang) ⇒ Object



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

def format_header(lang)
  "msgid \"\"\nmsgstr \"\"\n\"Language: #{lang}\\n\"\n\"X-Generator: Twine #{Twine::VERSION}\\n\"\n"
end

#format_key(key) ⇒ Object



88
89
90
# File 'lib/twine/formatters/gettext.rb', line 88

def format_key(key)
  "msgctxt \"#{key}\"\n"
end

#format_key_value(definition, lang) ⇒ Object



83
84
85
86
# File 'lib/twine/formatters/gettext.rb', line 83

def format_key_value(definition, lang)
  value = definition.translation_for_lang(lang)
  [format_key(definition.key.dup), format_base_translation(definition), format_value(value.dup)].compact.join
end

#format_nameObject



6
7
8
# File 'lib/twine/formatters/gettext.rb', line 6

def format_name
  'gettext'
end

#format_section_header(section) ⇒ Object



71
72
73
# File 'lib/twine/formatters/gettext.rb', line 71

def format_section_header(section)
  "# SECTION: #{section.name}"
end

#format_value(value) ⇒ Object



96
97
98
# File 'lib/twine/formatters/gettext.rb', line 96

def format_value(value)
  "msgstr \"#{value}\"\n"
end

#read(io, lang) ⇒ Object



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

def read(io, lang)
  comment_regex = /#.? *"(.*)"$/
  key_regex = /msgctxt *"(.*)"$/
  value_regex = /msgstr *"(.*)"$/m
  
  while item = io.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

#should_include_definition(definition, lang) ⇒ Object



75
76
77
# File 'lib/twine/formatters/gettext.rb', line 75

def should_include_definition(definition, lang)
  super and !definition.translation_for_lang(@default_lang).nil?
end