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.



153
154
155
# File 'lib/zlocalize/translation_file.rb', line 153

def initialize
   @entries = TranslationEntryCollection.new
end

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.



133
134
135
# File 'lib/zlocalize/translation_file.rb', line 133

def entries
  @entries
end

Class Method Details

.load(file_name) ⇒ Object



147
148
149
150
151
# File 'lib/zlocalize/translation_file.rb', line 147

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

Instance Method Details

#add_entry(key, ref) ⇒ Object



172
173
174
# File 'lib/zlocalize/translation_file.rb', line 172

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

#clear_entriesObject



168
169
170
# File 'lib/zlocalize/translation_file.rb', line 168

def clear_entries
  @entries.clear
end

#ensure_entry_idsObject



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

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



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

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



161
162
163
164
165
166
# File 'lib/zlocalize/translation_file.rb', line 161

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

#load(filename) ⇒ Object



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

def load(filename)
  if File.exist?(filename)
    content = File.open(filename,"r") { |f| f.read }
    read_yaml_header(content)
    begin
      entries = YAML::load(content)
    rescue StandardError => e
      raise TranslationFileError.new("Error reading ZLocalize TranslationFile #{filename} : #{e.message}")
    end
    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



245
246
247
# File 'lib/zlocalize/translation_file.rb', line 245

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

#output_headerObject



229
230
231
232
233
# File 'lib/zlocalize/translation_file.rb', line 229

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



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

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



157
158
159
# File 'lib/zlocalize/translation_file.rb', line 157

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

#synchronize_with(translation_file) ⇒ Object



176
177
178
# File 'lib/zlocalize/translation_file.rb', line 176

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

#to_yamlObject



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

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