Class: Irc::Bot::Language
Constant Summary collapse
- Lang2Locale =
This constant hash holds the mapping from long language names to the usual POSIX locale specifications
{ :english => 'en', :british_english => 'en_GB', :american_english => 'en_US', :italian => 'it', :french => 'fr', :german => 'de', :dutch => 'nl', :japanese => 'ja', :russian => 'ru', :finnish => 'fi', :traditional_chinese => 'zh_TW', :simplified_chinese => 'zh_CN' }
Instance Attribute Summary collapse
-
#language ⇒ Object
readonly
Returns the value of attribute language.
Class Method Summary collapse
-
.from_locale ⇒ Object
Return the shortest language for the current GetText locale.
Instance Method Summary collapse
- #get(key) ⇒ Object
-
#initialize(bot, language) ⇒ Language
constructor
A new instance of Language.
- #rescan ⇒ Object
- #save ⇒ Object
- #scan ⇒ Object
- #set_language(language) ⇒ Object
Constructor Details
#initialize(bot, language) ⇒ Language
Returns a new instance of Language.
71 72 73 74 |
# File 'lib/rbot/language.rb', line 71 def initialize(bot, language) @bot = bot set_language language end |
Instance Attribute Details
#language ⇒ Object (readonly)
Returns the value of attribute language.
75 76 77 |
# File 'lib/rbot/language.rb', line 75 def language @language end |
Class Method Details
.from_locale ⇒ Object
Return the shortest language for the current GetText locale
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rbot/language.rb', line 36 def Language.from_locale return 'english' unless defined?(GetText) lang = locale.language if locale.country str = lang + "_#{locale.country}" if Lang2Locale.value?(str) # Get the shortest key in Lang2Locale which maps to the given lang_country lang_str = Lang2Locale.select { |k, v| v == str }.transpose.first.map { |v| v.to_s }.sort { |a, b| a.length <=> b.length }.first if File.exist?(File.join(Config::datadir, "languages/#{lang_str}.lang")) return lang_str end end end # lang_country didn't work, let's try lan if Lang2Locale.value?(lang) # Get the shortest key in Lang2Locale which maps to the given lang lang_str = Lang2Locale.select { |k, v| v == lang }.transpose.first.map { |v| v.to_s }.sort { |a, b| a.length <=> b.length }.first if File.exist?(File.join(Config::datadir, "/languages/#{lang_str}.lang")) return lang_str end end # all else fail, return 'english' return 'english' end |
Instance Method Details
#get(key) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/rbot/language.rb', line 127 def get(key) if(@strings.has_key?(key)) return @strings[key][rand(@strings[key].length)] else raise "undefined language key" end end |
#rescan ⇒ Object
123 124 125 |
# File 'lib/rbot/language.rb', line 123 def rescan scan end |
#save ⇒ Object
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rbot/language.rb', line 135 def save File.open(@file, "w") {|file| @strings.each {|key,val| file.puts "#{key}:" val.each_value {|v| file.puts " #{v}" } } } end |
#scan ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rbot/language.rb', line 108 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 |
#set_language(language) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/rbot/language.rb', line 77 def set_language(language) lang_str = language.to_s.downcase.gsub(/\s+/,'_') lang_sym = lang_str.intern if defined?(GetText) and Lang2Locale.key?(lang_sym) setlocale(Lang2Locale[lang_sym]) debug "locale set to #{locale}" rbot_gettext_debug else warning "Unable to set locale, unknown language #{language} (#{lang_str})" end file = Config::datadir + "/languages/#{lang_str}.lang" unless(FileTest.exist?(file)) raise "no such language: #{lang_str} (no such file #{file})" end @language = lang_str @file = file scan return if @bot.plugins.nil? @bot.plugins.core_modules.each { |p| if p.respond_to?('set_language') p.set_language(@language) end } @bot.plugins.plugins.each { |p| if p.respond_to?('set_language') p.set_language(@language) end } end |