1 class TranslationSupport
2 class << self
3
4 #Retrieve US word set
5 def get_translation_keys(language_root, suffix=nil)
6 (dummy_comments, words) = read_file("#{language_root}/en#{suffix}.yml", 'en')
7 words
8 end
9
10 #Retrieve comments, translation data in hash form
11 def read_file(filename, basename)
12 (comments, data) = IO.read(filename).split(/\n#{basename}:\s*\n/) #Add error checking for failed file read?
13 return comments, create_hash(data, basename)
14 end
15
16 #Creates hash of translation data
17 def create_hash(data, basename)
18 words = Hash.new
19 return words if !data
20 parent = Array.new
21 previous_key = 'base'
22 data.split("\n").each do |w|
23 next if w.strip.blank?
24 (key, value) = w.split(':', 2)
25 value ||= ''
26 shift = (key =~ /\w/)/2 - parent.size #Determine level of current key in comparison to parent array
27 key = key.sub(/^\s+/,'')
28 parent << previous_key if shift > 0 #If key is child of previous key, add previous key as parent
29 (shift * -1).times { parent.pop } if shift < 0 #If key is not related to previous key, remove parent keys
30 previous_key = key #Track key in case next key is child of this key
31 words[parent.join(':') + ':' + key] = value unless key.blank?
32 end
33 words
34 end
35
36 def open_available_tags(filename)
37 data = YAML::load(File.open("#{filename}"))
38 data.to_s
39 end
40
41 #Writes to file from translation data hash structure
42 def write_file(filename,basename,comments,words)
43 File.open(filename, "w") do |log|
44 log.puts(comments+"\n"+basename+": \n")
45 words.sort.each do |k,v|
46 keys = k.split(':')
47 (keys.size-1).times { keys[keys.size-1] = ' ' + keys[keys.size-1] } #Add indentation for children keys
48 log.puts(keys[keys.size-1]+':'+v+"\n")
49 end
50 end
51 end
52
53 end
54 en