Class: TranslationSupport

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

Class Method Summary collapse

Class Method Details

.create_hash(data, basename) ⇒ Object

Creates hash of translation data



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/translation_support.rb', line 17

def create_hash(data, basename)
  words = Hash.new
  return words if !data
  parent = Array.new
  previous_key = 'base'
  data.split("\n").each do |w|
    next if w.strip.blank?
    (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 unless key.blank?
  end
  words
end

.get_translation_keys(language_root, suffix = nil) ⇒ Object

Retrieve US word set



5
6
7
8
# File 'lib/translation_support.rb', line 5

def get_translation_keys(language_root, suffix=nil)
  (dummy_comments, words) = read_file("#{language_root}/en#{suffix}.yml", 'en')
  words
end

.open_available_tags(filename) ⇒ Object



36
37
38
39
# File 'lib/translation_support.rb', line 36

def open_available_tags(filename)
  data = YAML::load(File.open("#{filename}"))
  data.to_s
end

.read_file(filename, basename) ⇒ Object

Retrieve comments, translation data in hash form



11
12
13
14
# File 'lib/translation_support.rb', line 11

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, basename)
end

.write_file(filename, basename, comments, words) ⇒ Object

Writes to file from translation data hash structure



42
43
44
45
46
47
48
49
50
51
# File 'lib/translation_support.rb', line 42

def write_file(filename,basename,comments,words)
  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
      log.puts(keys[keys.size-1]+':'+v+"\n")
    end
  end
end