### Home Classes Methods

### In Files

- lib/rbot/language.rb

### Parent

Object

### Methods

- ::from_locale
- ::new
- #get
- #rescan
- #save
- #scan
- #set_language

### Class/Module Index

Quicksearch

- Irc
- Irc::AsciiCasemap
- Irc::BanlistMessage
- Irc::BasicUserMessage
- Irc::Bot
- Irc::Bot::Auth
- Irc::Bot::Auth::BotOwnerClass
- Irc::Bot::Auth::BotUser
- Irc::Bot::Auth::Command
- Irc::Bot::Auth::DefaultBotUserClass
- Irc::Bot::Auth::InvalidPassword
- Irc::Bot::Auth::ManagerClass
- Irc::Bot::Auth::PermissionSet
- Irc::Bot::Config
- Irc::Bot::Config::ArrayValue
- Irc::Bot::Config::BooleanValue
- Irc::Bot::Config::EnumValue
- Irc::Bot::Config::FloatValue
- Irc::Bot::Config::IntegerValue
- Irc::Bot::Config::ManagerClass
- Irc::Bot::Config::StringValue
- Irc::Bot::Config::Value
- Irc::Bot::Config::Wizard
- Irc::Bot::DataFilter
- Irc::Bot::DataStream
- Irc::Bot::Language
- Irc::Bot::MessageMapper
- Irc::Bot::MessageMapper::Failure
- Irc::Bot::MessageMapper::FriendlyFailure
- Irc::Bot::MessageMapper::NoActionFailure
- Irc::Bot::MessageMapper::NoMatchFailure
- Irc::Bot::MessageMapper::NotPrivateFailure
- Irc::Bot::MessageMapper::NotPublicFailure
- Irc::Bot::MessageMapper::PartialMatchFailure
- Irc::Bot::MessageParameter
- Irc::Bot::MessageTemplate
- Irc::Bot::Plugins
- Irc::Bot::Plugins::BotModule
- Irc::Bot::Plugins::CoreBotModule
- Irc::Bot::Plugins::Plugin
- Irc::Bot::Plugins::PluginManagerClass
- Irc::Bot::Plugins::RecurseTooDeep
- Irc::Bot::Plugins::RemoteBotModule
- Irc::Bot::Plugins::RemoteCoreBotModule
- Irc::Bot::Plugins::RemotePlugin
- Irc::Bot::Registry
- Irc::Bot::Registry::Accessor
- Irc::Bot::RemoteDispatcher
- Irc::Bot::RemoteMessage
- Irc::Bot::RemoteObject
- Irc::Bot::Wordlist
- Irc::BotConfig
- Irc::Casemap
- Irc::Channel
- Irc::Channel::Mode
- Irc::Channel::ModeHash
- Irc::Channel::ModeTypeA
- Irc::Channel::ModeTypeB
- Irc::Channel::ModeTypeC
- Irc::Channel::ModeTypeD
- Irc::Channel::Topic
- Irc::Channel::UserMode
- Irc::ChannelList
- Irc::Client
- Irc::DBHash
- Irc::DBTree
- Irc::InviteMessage
- Irc::JoinMessage
- Irc::KickMessage
- Irc::MessageQueue
- Irc::ModeChangeMessage
- Irc::MotdMessage
- Irc::NamesMessage
- Irc::Netmask
- Irc::NetmaskDb
- Irc::NetmaskDb::Tree
- Irc::NetmaskList
- Irc::NickMessage
- Irc::NoSuchTargetMessage
- Irc::NoticeMessage
- Irc::PartMessage
- Irc::PrivMessage
- Irc::QueueRing
- Irc::QuitMessage
- Irc::RfcCasemap
- Irc::Server
- Irc::ServerOrCasemap
- Irc::Socket
- Irc::Socket::IdentityFilter
- Irc::StrictRfcCasemap
- Irc::TopicMessage
- Irc::UnknownMessage
- Irc::User
- Irc::UserList
- Irc::UserMessage
- Irc::Utils
- Irc::Utils::HttpUtil
- Irc::Utils::HttpUtil::CachedObject
- Irc::Utils::ParseTime
- Irc::WelcomeMessage
- Irc::WhoisMessage
- BDB
- BDB::CIBtree
- MonitorMixin
- MonitorMixin::ConditionVariable
- Net
- Net::HTTPResponse
- Regexp
- Regexp::Irc
- Singleton
- Singleton::SingletonClassMethods
- Timer
- Timer::Action
- TokyoCabinet
- TokyoCabinet::CIBDB
- UnicodePlugin
- UnicodePlugin::UnicodeFilter
- Array
- ArrayOf
- AuthModule
- BasicsModule
- ConditionVariable
- ConfigModule
- DottedIndex
- Enumerable
- Exception
- FiltersModule
- GetText
- GetTextVersionError
- HttpUtilPlugin
- IrcLogModule
- Module
- Numeric
- Object
- Range
- RemoteModule
- ServerError
- String
- Symbol
- UserDataModule
- WordlistModule
- YAML

No matching classes.

# Irc::Bot::Language

### Constants

Lang2Locale

  This constant hash holds the mapping from long language names to the usual POSIX locale specifications

### Attributes

language[R]

### Public Class Methods

from_locale() click to toggle source

Return the shortest language for the current GetText locale

    # 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

new(bot, language) click to toggle source

    # File lib/rbot/language.rb, line 71
    def initialize(bot, language)
      @bot = bot
      set_language language
    end

### Public Instance Methods

get(key) click to toggle source

    # 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() click to toggle source

    # File lib/rbot/language.rb, line 123
    def rescan
      scan
    end

save() click to toggle source

    # 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() click to toggle source

    # 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) click to toggle source

    # 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.