summaryrefslogtreecommitdiff
path: root/rbot/language.rb
diff options
context:
space:
mode:
authorTom Gilbert <tom@linuxbrit.co.uk>2004-10-09 01:51:05 +0000
committerTom Gilbert <tom@linuxbrit.co.uk>2004-10-09 01:51:05 +0000
commit0f3e302547363ea237454dda891ddb5de1be4476 (patch)
treeec45be24a669ee28b5e2da4ff65e39265a418e0b /rbot/language.rb
initial import of rbot
Diffstat (limited to 'rbot/language.rb')
-rw-r--r--rbot/language.rb55
1 files changed, 55 insertions, 0 deletions
diff --git a/rbot/language.rb b/rbot/language.rb
new file mode 100644
index 00000000..9788b2bb
--- /dev/null
+++ b/rbot/language.rb
@@ -0,0 +1,55 @@
+module Irc
+
+ class Language
+ def initialize(language, file="")
+ @language = language
+ if file.empty?
+ file = File.dirname(__FILE__) + "/languages/#{@language}.lang"
+ end
+ unless(FileTest.exist?(file))
+ raise "no such language: #{@language} (no such file #{file})"
+ end
+ @file = file
+ scan
+ end
+
+ def scan
+ @strings = Hash.new
+ current_key = nil
+ IO.foreach(@file) {|l|
+ next if l =~ /^$/
+ next if l =~ /^\s*#/
+ if(l =~ /^(\S+):$/)
+ @strings[$1] = Array.new
+ current_key = $1
+ elsif(l =~ /^\s*(.*)$/)
+ @strings[current_key] << $1
+ end
+ }
+ end
+
+ def rescan
+ scan
+ end
+
+ def get(key)
+ if(@strings.has_key?(key))
+ return @strings[key][rand(@strings[key].length)]
+ else
+ raise "undefined language key"
+ end
+ end
+
+ def save
+ File.open(@file, "w") {|file|
+ @strings.each {|key,val|
+ file.puts "#{key}:"
+ val.each_value {|v|
+ file.puts " #{v}"
+ }
+ }
+ }
+ end
+ end
+
+end