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.



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

def initialize(strings, options)
  @strings = strings
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#stringsObject

Returns the value of attribute strings.



4
5
6
# File 'lib/twine/formatters/abstract.rb', line 4

def strings
  @strings
end

Class Method Details

.can_handle_directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.can_handle_directory?(path)
  return false
end

Instance Method Details

#androidify_substitutions(str) ⇒ Object



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

def androidify_substitutions(str)
  # 1) use "s" instead of "@" for substituting strings
  str.gsub!(/%([0-9\$]*)@/, '%\1s')

  # 1a) escape strings that begin with a lone "@"
  str.sub!(/^@ /, '\\@ ')

  # 2) if there is more than one substitution in a string, make sure they are numbered
  substituteCount = 0
  startFound = false
  str.each_char do |c|
    if startFound
      if c == "%"
        # ignore as this is a literal %
      elsif c.match(/\d/)
        # leave the string alone if it already has numbered substitutions
        return str
      else
        substituteCount += 1
      end
      startFound = false
    elsif c == "%"
      startFound = true
    end
  end

  if substituteCount > 1
    currentSub = 1
    startFound = false
    newstr = ""
    str.each_char do |c|
      if startFound
        if !(c == "%")
          newstr = newstr + "#{currentSub}$"
          currentSub += 1
        end
        startFound = false
      elsif c == "%"
        startFound = true
      end
      newstr = newstr + c
    end
    return newstr
  else
    return str
  end
end

#default_file_nameObject

Raises:

  • (NotImplementedError)


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

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)


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

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

#iosify_substitutions(str) ⇒ Object



16
17
18
19
20
# File 'lib/twine/formatters/abstract.rb', line 16

def iosify_substitutions(str)
  # use "@" instead of "s" for substituting strings
  str.gsub!(/%([0-9\$]*)s/, '%\1@')
  return str
end

#read_file(path, lang) ⇒ Object

Raises:

  • (NotImplementedError)


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

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

#set_comment_for_key(key, comment) ⇒ Object



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

def set_comment_for_key(key, comment)
  if @strings.strings_map.include?(key)
    @strings.strings_map[key].comment = comment
  end
end

#set_translation_for_key(key, lang, value) ⇒ Object



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

def set_translation_for_key(key, lang, value)
  if @strings.strings_map.include?(key)
    @strings.strings_map[key].translations[lang] = value
  elsif @options[:consume_all]
    STDERR.puts "Adding new string '#{key}' to strings data file."
    arr = @strings.sections.select { |s| s.name == 'Uncategorized' }
    current_section = arr ? arr[0] : nil
    if !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
    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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/twine/formatters/abstract.rb', line 120

def write_all_files(path)
  if !File.directory?(path)
    raise Twine::Error.new("Directory does not exist: #{path}")
  end

  file_name = @options[:file_name] || default_file_name
  langs_written = []
  Dir.foreach(path) do |item|
    if item == "." or item == ".."
      next
    end
    item = File.join(path, item)
    if File.directory?(item)
      lang = determine_language_given_path(item)
      if lang
        write_file(File.join(item, file_name), lang)
        langs_written << lang
      end
    end
  end
  if langs_written.empty?
    raise Twine::Error.new("Failed to generate any files: No languages found at #{path}")
  end
end

#write_file(path, lang) ⇒ Object

Raises:

  • (NotImplementedError)


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

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