X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fload-gettext.rb;h=6377b88232fb2d7d5eea7d4d25c94615f8a0c736;hb=263e939f1a8b8adf35afc33ab547dff8d4b97a4e;hp=4d39b7e48fc6070f6c7e1394b1d2a5643a5ac8ed;hpb=b233d010765808cfbf34dc763ab9013968212981;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/load-gettext.rb b/lib/rbot/load-gettext.rb index 4d39b7e4..6377b882 100644 --- a/lib/rbot/load-gettext.rb +++ b/lib/rbot/load-gettext.rb @@ -10,10 +10,17 @@ end # try to load gettext, or provide fake getttext functions begin +# workaround for gettext not checking empty LANGUAGE +if ENV["LANGUAGE"] and ENV["LANGUAGE"].empty? + ENV.delete "LANGUAGE" +end + require 'gettext/version' gettext_version = GetText::VERSION.split('.').map {|n| n.to_i} - include Comparable # for Array#>= + class ::Array + include Comparable # for Array#>= + end unless gettext_version >= [1, 8, 0] raise GetTextVersionError, "Unsupported ruby-gettext version installed: #{gettext_version.join '.'}; supported versions are 1.8.0 and above" end @@ -22,24 +29,98 @@ begin include GetText - add_default_locale_path(File.join(Irc::Bot::Config.datadir, "../locale/%{locale}/LC_MESSAGES/%{name}.mo")) + rbot_locale_path = File.join(Irc::Bot::Config.datadir, + gettext_version < [2, 2, 0] ? + "../locale/%{locale}/LC_MESSAGES/%{name}.mo" : + "../locale/%{lang}/LC_MESSAGES/%{name}.mo") + + if gettext_version < [2, 0, 0] + add_default_locale_path(rbot_locale_path) + else + LocalePath.add_default_rule(rbot_locale_path) + end + + if GetText.respond_to? :cached= + GetText.cached = false + elsif TextDomain.respond_to? :cached= + TextDomain.cached = false + else + warning 'This version of ruby-gettext does not support non-cached mode; mo files are not reloaded when setting language' + end + + begin + bindtextdomain 'rbot' + rescue NoMethodError => e + error e + warning 'Trying to work around RubyGems/GetText incompatibility' + module ::Gem + def self.all_load_paths + result = [] + + Gem.path.each do |gemdir| + each_load_path all_partials(gemdir) do |load_path| + result << load_path + end + end + + result + end + end + retry + end + - bindtextdomain 'rbot' module GetText - # patch for ruby-gettext 1.8.0 up to 1.10.0 (and more?) to cope with anonymous - # modules used by rbot - # FIXME remove the patch when ruby-gettext is fixed, or rbot switches to named modules - if !instance_methods.include?('orig_bound_targets') + # patch for ruby-gettext 1.x to cope with anonymous modules used by rbot. + # bound_targets and related methods are not used nor present in 2.x, and + # this patch is not needed + if respond_to? :bound_targets, true alias :orig_bound_targets :bound_targets - end - def bound_targets(*a) # :nodoc: - bt = orig_bound_targets(*a) rescue [] - bt.empty? ? orig_bound_targets(Object) : bt + + def bound_targets(*a) # :nodoc: + bt = orig_bound_targets(*a) rescue [] + bt.empty? ? orig_bound_targets(Object) : bt + end end require 'stringio' + # GetText 2.1.0 does not provide current_textdomain_info, + # so we adapt the one from 1.9.10 + # TODO we would _really_ like to have a future-proof version of this, + # but judging by the ruby gettext source code, this isn't going to + # happen anytime soon. + if not respond_to? :current_textdomain_info + # Show the current textdomain information. This function is for debugging. + # * options: options as a Hash. + # * :with_messages - show informations with messages of the current mo file. Default is false. + # * :out - An output target. Default is STDOUT. + # * :with_paths - show the load paths for mo-files. + def current_textdomain_info(options = {}) + opts = {:with_messages => false, :with_paths => false, :out => STDOUT}.merge(options) + ret = nil + # this is for 2.1.0 + TextDomainManager.each_text_domains(self) {|textdomain, lang| + opts[:out].puts "TextDomain name: #{textdomain.name.inspect}" + opts[:out].puts "TextDomain current locale: #{lang.to_s.inspect}" + opts[:out].puts "TextDomain current mo path: #{textdomain.instance_variable_get(:@locale_path).current_path(lang).inspect}" + if opts[:with_paths] + opts[:out].puts "TextDomain locale file paths:" + textdomain.locale_paths.each do |v| + opts[:out].puts " #{v}" + end + end + if opts[:with_messages] + opts[:out].puts "The messages in the mo file:" + textdomain.current_mo.each{|k, v| + opts[:out].puts " \"#{k}\": \"#{v}\"" + } + end + } + end + end + # This method is used to output debug information on the GetText # textdomain, and it's called by the language setting routines # in rbot @@ -48,7 +129,8 @@ begin gettext_info = StringIO.new current_textdomain_info(:out => gettext_info) # fails sometimes rescue Exception - warning "gettext failed to set call textdomain info. maybe an mo file doesn't exist for your locale." + warning "failed to retrieve textdomain info. maybe an mo file doesn't exist for your locale." + debug $! ensure gettext_info.string.each_line { |l| debug l} end @@ -58,7 +140,7 @@ begin log "gettext loaded" rescue LoadError, GetTextVersionError - warn "failed to load ruby-gettext package: #{$!}; translations are disabled" + warning "failed to load ruby-gettext package: #{$!}; translations are disabled" # undefine GetText, in case it got defined because the error was caused by a # wrong version @@ -87,6 +169,9 @@ rescue LoadError, GetTextVersionError args[0] end + def bindtextdomain_to(*args) + end + # the following extension to String#% is from ruby-gettext's string.rb file. # it needs to be included in the fallback since the source already use this form