X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Flanguage.rb;h=67114dac58277eb176b8340317a86f0901252c65;hb=81e0b9c7405c3731f6df1e50f6b32a35c224dfa7;hp=57e9a6d272fcb72fce018578dd4b0eb69e722259;hpb=e73fb21bbc3c947280cb884c8c48d642f8c7f6b2;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/language.rb b/lib/rbot/language.rb index 57e9a6d2..67114dac 100644 --- a/lib/rbot/language.rb +++ b/lib/rbot/language.rb @@ -8,55 +8,57 @@ # .lang file etc. module Irc -module Language +class Bot + class Language - # This constant hash holds the mapping - # from long language names to the usual POSIX - # locale specifications - Lang2Locale = { - :english => 'en', - :british_english => 'en_GB', - :american_english => 'en_US', - :italian => 'it', - :french => 'fr', - :german => 'de', - :dutch => 'nl', - :japanese => 'ja', - :russian => 'ru', - :traditional_chinese => 'zh_TW', - :simplified_chinese => 'zh_CN' - } + # This constant hash holds the mapping + # from long language names to the usual POSIX + # locale specifications + Lang2Locale = { + :english => 'en', + :british_english => 'en_GB', + :american_english => 'en_US', + :italian => 'it', + :french => 'fr', + :german => 'de', + :dutch => 'nl', + :japanese => 'ja', + :russian => 'ru', + :traditional_chinese => 'zh_TW', + :simplified_chinese => 'zh_CN' + } + # On WIN32 it appears necessary to have ".UTF-8" explicitly for gettext to use UTF-8 + Lang2Locale.each_value {|v| v.replace(v + '.UTF-8')} - # Return the shortest language for the current - # GetText locale - 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 + # Return the shortest language for the current + # GetText locale + 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 - 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 + # 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 - # all else fail, return 'english' - return 'english' - end - class Language - BotConfig.register BotConfigEnumValue.new('core.language', - :default => Irc::Language.from_locale, :wizard => true, + Config.register Config::EnumValue.new('core.language', + :default => Irc::Bot::Language.from_locale, :wizard => true, :values => Proc.new{|bot| Dir.new(Config::datadir + "/languages").collect {|f| f =~ /\.lang$/ ? f.gsub(/\.lang$/, "") : nil