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