- class Language
- BotConfig.register BotConfigEnumValue.new('core.language',
- :default => "english", :wizard => true,
+ # 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
+ # 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
+
+ Config.register Config::EnumValue.new('core.language',
+ :default => Irc::Bot::Language.from_locale, :wizard => true,