Module: Spree::I18nUtils

Defined in:
lib/spree/i18n_utils.rb

Instance Method Summary collapse

Instance Method Details

#create_hash(data) ⇒ Object

Creates hash of translation data



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/spree/i18n_utils.rb', line 13

def create_hash(data)
  words = Hash.new
  return words if !data
  parent = Array.new
  previous_key = 'base'
  data.split("\n").each do |w|
    next if w.strip.blank? || w.strip[0]=='#'
    (key, value) = w.split(':', 2)
    value ||= ''
    shift = (key =~ /\w/)/2 - parent.size                             #Determine level of current key in comparison to parent array
    key = key.sub(/^\s+/,'')
    parent << previous_key if shift > 0                               #If key is child of previous key, add previous key as parent
    (shift*-1).times { parent.pop } if shift < 0                      #If key is not related to previous key, remove parent keys
    previous_key = key                                                #Track key in case next key is child of this key
    words[parent.join(':')+':'+key] = value
  end
  words
end

#read_file(filename, basename) ⇒ Object

#Retrieve comments, translation data in hash form



7
8
9
10
# File 'lib/spree/i18n_utils.rb', line 7

def read_file(filename, basename)
  (comments, data) = IO.read(filename).split(/\n#{basename}:\s*\n/)   #Add error checking for failed file read?
  return comments, create_hash(data)
end

#write_file(filename, basename, comments, words, comment_values = true, fallback_values = {}) ⇒ Object

Writes to file from translation data hash structure



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/spree/i18n_utils.rb', line 33

def write_file(filename,basename,comments,words,comment_values=true, fallback_values={})
  File.open(filename, "w") do |log|
    log.puts(comments+"\n"+basename+": \n")
    words.sort.each do |k,v|
      keys = k.split(':')
      (keys.size-1).times { keys[keys.size-1] = '  ' + keys[keys.size-1] }   #Add indentation for children keys
      value = v.strip
      value = ("#" + value) if comment_values and not value.blank?
      log.puts "#{keys[keys.size-1]}: #{value}\n"
    end
  end
end