Class: Twine::Formatters::Abstract

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

Direct Known Subclasses

Android, Apple, Django, Flash, Gettext, JQuery, Tizen

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strings, options) ⇒ Abstract

Returns a new instance of Abstract.



13
14
15
16
17
# File 'lib/twine/formatters/abstract.rb', line 13

def initialize(strings, options)
  @strings = strings
  @options = options
  @output_processor = Processors::OutputProcessor.new @strings, @options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/twine/formatters/abstract.rb', line 7

def options
  @options
end

#stringsObject (readonly)

Returns the value of attribute strings.



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

def strings
  @strings
end

Class Method Details

.can_handle_directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/twine/formatters/abstract.rb', line 9

def self.can_handle_directory?(path)
  return false
end

Instance Method Details

#default_file_nameObject

Raises:

  • (NotImplementedError)


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

def default_file_name
  raise NotImplementedError.new("You must implement default_file_name in your formatter class.")
end

#determine_language_given_path(path) ⇒ Object

Raises:

  • (NotImplementedError)


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

def determine_language_given_path(path)
  raise NotImplementedError.new("You must implement determine_language_given_path in your formatter class.")
end

#escape_quotes(text) ⇒ Object



150
151
152
# File 'lib/twine/formatters/abstract.rb', line 150

def escape_quotes(text)
  text.gsub('"', '\\\\"')
end

#format_comment(row, lang) ⇒ Object



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

def format_comment(row, lang)
end

#format_file(strings, lang) ⇒ Object



83
84
85
86
87
88
# File 'lib/twine/formatters/abstract.rb', line 83

def format_file(strings, lang)
  header = format_header(lang)
  result = ""
  result += header + "\n" if header
  result += format_sections(strings, lang)
end

#format_header(lang) ⇒ Object



90
91
# File 'lib/twine/formatters/abstract.rb', line 90

def format_header(lang)
end

#format_key(key) ⇒ Object



142
143
144
# File 'lib/twine/formatters/abstract.rb', line 142

def format_key(key)
  key
end

#format_key_value(row, lang) ⇒ Object



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

def format_key_value(row, lang)
  value = row.translated_string_for_lang(lang)
  key_value_pattern % { key: format_key(row.key.dup), value: format_value(value.dup) }
end

#format_row(row, lang) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/twine/formatters/abstract.rb', line 122

def format_row(row, lang)
  return nil unless row.translated_string_for_lang(lang)

  result = row_pattern.scan(/%\{([a-z_]+)\}/).flatten
  result.map! { |element| send("format_#{element}".to_sym, row, lang) }
  result.flatten.join
end

#format_section(section, lang) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/twine/formatters/abstract.rb', line 101

def format_section(section, lang)
  rows = section.rows.dup

  result = ""
  unless rows.empty?
    if section.name && section.name.length > 0
      section_header = format_section_header(section)
      result += "\n#{section_header}" if section_header
    end
  end

  rows.map! { |row| format_row(row, lang) }
  rows.compact! # remove nil entries
  rows.map! { |row| "\n#{row}" }  # prepend newline
  result += rows.join
end

#format_section_header(section) ⇒ Object



98
99
# File 'lib/twine/formatters/abstract.rb', line 98

def format_section_header(section)
end

#format_sections(strings, lang) ⇒ Object



93
94
95
96
# File 'lib/twine/formatters/abstract.rb', line 93

def format_sections(strings, lang)
  sections = strings.sections.map { |section| format_section(section, lang) }
  sections.join("\n")
end

#format_value(value) ⇒ Object



146
147
148
# File 'lib/twine/formatters/abstract.rb', line 146

def format_value(value)
  value
end

#key_value_patternObject

Raises:

  • (NotImplementedError)


138
139
140
# File 'lib/twine/formatters/abstract.rb', line 138

def key_value_pattern
  raise NotImplementedError.new("You must implement key_value_pattern in your formatter class.")
end

#output_path_for_language(lang) ⇒ Object



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

def output_path_for_language(lang)
  lang
end

#read_file(path, lang) ⇒ Object

Raises:

  • (NotImplementedError)


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

def read_file(path, lang)
  raise NotImplementedError.new("You must implement read_file in your formatter class.")
end

#row_patternObject



118
119
120
# File 'lib/twine/formatters/abstract.rb', line 118

def row_pattern
  "%{comment}%{key_value}"
end

#set_comment_for_key(key, comment) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/twine/formatters/abstract.rb', line 53

def set_comment_for_key(key, comment)
  return unless @options[:consume_comments]
  
  if @strings.strings_map.include?(key)
    row = @strings.strings_map[key]
    
    reference = @strings.strings_map[row.reference_key] if row.reference_key

    if !reference or comment != reference.raw_comment
      row.comment = comment
    end
  end
end

#set_translation_for_key(key, lang, value) ⇒ Object



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

def set_translation_for_key(key, lang, value)
  value = value.gsub("\n", "\\n")

  if @strings.strings_map.include?(key)
    row = @strings.strings_map[key]
    reference = @strings.strings_map[row.reference_key] if row.reference_key

    if !reference or value != reference.translations[lang]
      row.translations[lang] = value
    end
  elsif @options[:consume_all]
    Twine::stderr.puts "Adding new string '#{key}' to strings data file."
    current_section = @strings.sections.find { |s| s.name == 'Uncategorized' }
    unless current_section
      current_section = StringsSection.new('Uncategorized')
      @strings.sections.insert(0, current_section)
    end
    current_row = StringsRow.new(key)
    current_section.rows << current_row
    
    if @options[:tags] && @options[:tags].length > 0
      current_row.tags = @options[:tags]            
    end
    
    @strings.strings_map[key] = current_row
    @strings.strings_map[key].translations[lang] = value
  else
    Twine::stderr.puts "Warning: '#{key}' not found in strings data file."
  end
  if !@strings.language_codes.include?(lang)
    @strings.add_language_code(lang)
  end
end

#write_all_files(path) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/twine/formatters/abstract.rb', line 164

def write_all_files(path)
  file_name = @options[:file_name] || default_file_name
  if @options[:create_folders]
    @strings.language_codes.each do |lang|
      output_path = File.join(path, output_path_for_language(lang))

      FileUtils.mkdir_p(output_path)

      write_file(File.join(output_path, file_name), lang)
    end
  else
    language_written = false
    Dir.foreach(path) do |item|
      next if item == "." or item == ".."

      item = File.join(path, item)
      next unless File.directory?(item)

      lang = determine_language_given_path(item)
      next unless lang

      write_file(File.join(item, file_name), lang)
      language_written = true
    end

    if !language_written
      raise Twine::Error.new("Failed to generate any files: No languages found at #{path}")
    end
  end
end

#write_file(path, lang) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/twine/formatters/abstract.rb', line 154

def write_file(path, lang)
  encoding = @options[:output_encoding] || 'UTF-8'

  processed_strings = @output_processor.process(lang)

  File.open(path, "w:#{encoding}") do |f|
    f.puts format_file(processed_strings, lang)
  end
end