]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/language.rb
rework various modules to use the installed directories
[user/henk/code/ruby/rbot.git] / lib / rbot / language.rb
1 module Irc
2
3   class Language
4     def initialize(bot, language, file="")
5       @bot = bot
6       @language = language
7       if file.empty?
8         file = bot.datadir + "/languages/#{@language}.lang"
9       end
10       unless(FileTest.exist?(file))
11         raise "no such language: #{@language} (no such file #{file})"
12       end
13       @file = file
14       scan
15     end
16
17     def scan
18       @strings = Hash.new
19       current_key = nil
20       IO.foreach(@file) {|l|
21         next if l =~ /^$/
22         next if l =~ /^\s*#/
23         if(l =~ /^(\S+):$/)
24           @strings[$1] = Array.new
25           current_key = $1
26         elsif(l =~ /^\s*(.*)$/)
27           @strings[current_key] << $1
28         end
29       }
30     end
31
32     def rescan
33       scan
34     end
35
36     def get(key)
37       if(@strings.has_key?(key))
38         return @strings[key][rand(@strings[key].length)]
39       else
40         raise "undefined language key"
41       end
42     end
43
44     def save
45       File.open(@file, "w") {|file|
46         @strings.each {|key,val|
47           file.puts "#{key}:"
48           val.each_value {|v|
49             file.puts "   #{v}"
50           }
51         }
52       }
53     end
54   end
55
56 end