]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/load-gettext.rb
Survive active_support idiocy
[user/henk/code/ruby/rbot.git] / lib / rbot / load-gettext.rb
index 165a81c581bbba5d4fe9fa26f7808deb3fc1b32a..b896557b18a26b38624a4b1cce023473fdb0f1b3 100644 (file)
@@ -1,35 +1,88 @@
-# load gettext module and provide fallback in case of failure
+#-- vim:sw=2:et
+#++
+#
+# :title: GetText interface for rbot
+#
+# Load gettext module and provide fallback in case of failure
 
-require 'stringio'
+class GetTextVersionError < Exception
+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#>=
+  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
+
   require 'gettext'
+
   include GetText
+
+  rbot_locale_path = File.join(Irc::Bot::Config.datadir, "../locale/%{locale}/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
   bindtextdomain 'rbot'
 
   module GetText
-    # patch for ruby-gettext 1.9.0 to cope with anonymous modules used by rbot
-    # FIXME remove the patch when ruby-gettext is fixed, or rbot switches to named modules
-  # fix for module names that are not constant names
-    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
+
+      def bound_targets(*a)  # :nodoc:
+        bt = orig_bound_targets(*a) rescue []
+        bt.empty? ? orig_bound_targets(Object) : bt
+      end
     end
-    def bound_targets(*a)  # :nodoc:
-      orig_bound_targets(*a) rescue orig_bound_targets(Object)
+
+    require 'stringio'
+
+    # This method is used to output debug information on the GetText
+    # textdomain, and it's called by the language setting routines
+    # in rbot
+    def rbot_gettext_debug
+      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."
+      ensure
+        gettext_info.string.each_line { |l| debug l}
+      end
     end
   end
 
-  begin
-    gettext_info = StringIO.new
-    current_textdomain_info(:out=>gettext_info) # fails sometimes
-    debug 'using ruby-gettext'
-    gettext_info.string.each_line {|l| debug l}
-  rescue Exception
-  end
+  log "gettext loaded"
 
-rescue LoadError
-  warn 'ruby-gettext package not available; translations are disabled'
+rescue LoadError, GetTextVersionError
+  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
+  if defined?(GetText)
+    Object.module_eval { remove_const("GetText") }
+  end
 
   # dummy functions that return msg_id without translation
   def _(s)
@@ -44,10 +97,17 @@ rescue LoadError
     n > 1 ? s_plural : s_single
   end
 
+  def Nn_(s_single, s_plural)
+    n_(s_single, s_plural)
+  end
+
   def s_(*args)
     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