Class: ZLocalize::TranslationFile

Inherits:
Object
  • Object
show all
Defined in:
lib/zlocalize/translation_file.rb

Overview

:nodoc: all

Constant Summary collapse

REGEX_REVISION =

regex to match Revision line

/^#\s*Revision:\s+([0-9]+)/i
REGEX_ENTRY =

regex to match an entry delimiter

/^#\s*entry\s+([0-9]+)/
REGEX_FILE_REF =

regex to match file and line number references for an entry

/^#\s*file:\s+(.*?)\s*,\s*line\s+([0-9]{1,6})/
REGEX_IGNORE_ENTRY =

regex to match an optional # IGNORE line indicating that entry should be ignored

/^#\s*IGNORE/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTranslationFile

Returns a new instance of TranslationFile.



155
156
157
# File 'lib/zlocalize/translation_file.rb', line 155

def initialize
   @entries = TranslationEntryCollection.new
end

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.



135
136
137
# File 'lib/zlocalize/translation_file.rb', line 135

def entries
  @entries
end

Class Method Details

.load(file_name) ⇒ Object



149
150
151
152
153
# File 'lib/zlocalize/translation_file.rb', line 149

def self.load(file_name)
  f = new
  f.load(file_name)
  f
end

Instance Method Details

#add_entry(key, ref) ⇒ Object



174
175
176
# File 'lib/zlocalize/translation_file.rb', line 174

def add_entry(key,ref)
  @entries.add_entry(key,ref)
end

#clear_entriesObject



170
171
172
# File 'lib/zlocalize/translation_file.rb', line 170

def clear_entries
  @entries.clear
end

#ensure_entry_idsObject



190
191
192
193
194
195
196
197
198
# File 'lib/zlocalize/translation_file.rb', line 190

def ensure_entry_ids
  next_id = get_max_entry_id + 1
  @entries.values.each do |e|
    unless e.id.to_i > 0
      e.id = next_id
      next_id += 1
    end
  end
end

#get_max_entry_idObject



182
183
184
185
186
187
188
# File 'lib/zlocalize/translation_file.rb', line 182

def get_max_entry_id
  max_id = 0
  @entries.values.each do |e|
    max_id = e.id.to_i if e.id.to_i > max_id
  end
  max_id
end

#list_entriesObject



163
164
165
166
167
168
# File 'lib/zlocalize/translation_file.rb', line 163

def list_entries
  @entries.each do |e|
    puts e.inspect
    puts "\n"
  end
end

#load(filename) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/zlocalize/translation_file.rb', line 200

def load(filename)
  if File.exists?(filename)
    content = File.open(filename,"r") { |f| f.read }
    read_yaml_header(content)
    entries = YAML::load(content)
    if entries && !entries.is_a?(Hash)
      raise TranslationFileError.new("Invalid YAML translation file #{filename}\n\n#{entries.inspect}")
    end
  else
    raise TranslationFileError.new("Specified translation file does not exist: #{filename}")
  end
  entries ||= {}
  @entries.clear
  entries.each_pair do |k,entry|
    @entries[entry['source']] = TranslationEntry.new(entry)
  end
end

#non_translated_entriesObject



243
244
245
# File 'lib/zlocalize/translation_file.rb', line 243

def non_translated_entries
  @entries.sort_by_id.collect { |e| e.translation.to_s.strip == "" }
end

#output_headerObject



227
228
229
230
231
# File 'lib/zlocalize/translation_file.rb', line 227

def output_header
  out = "# Generated on: #{Time.now.strftime('%Y/%m/%d %H:%M')}\n"
  out << "# Revision: #{revision}\n\n"
  out
end

#read_yaml_header(content) ⇒ Object



218
219
220
221
222
223
224
225
# File 'lib/zlocalize/translation_file.rb', line 218

def read_yaml_header(content)
  re = /#\s*Revision:\s+([0-9]+)/i
  if m = re.match(content)
    @revision = m[1].to_i
  else
    @revision = 0
  end
end

#revisionObject



159
160
161
# File 'lib/zlocalize/translation_file.rb', line 159

def revision
  @revision.to_i > 0 ? @revision + 1 : 1
end

#synchronize_with(translation_file) ⇒ Object



178
179
180
# File 'lib/zlocalize/translation_file.rb', line 178

def synchronize_with(translation_file)
  @entries.synchronize_with(translation_file.entries)
end

#to_yamlObject



233
234
235
236
237
238
239
240
241
# File 'lib/zlocalize/translation_file.rb', line 233

def to_yaml
  ensure_entry_ids
  out = output_header
  @entries.sort_by_id.each do |e|
    out << e.to_yaml
    out << "\n"
  end
  out
end