]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/load-gettext.rb
* (gettext) work around failing current_textdomain_info()
[user/henk/code/ruby/rbot.git] / lib / rbot / load-gettext.rb
1 # load gettext module and provide fallback in case of failure
2
3 require 'stringio'
4
5 # try to load gettext, or provide fake getttext functions
6 begin
7   require 'gettext'
8   include GetText
9   bindtextdomain 'rbot'
10
11   module GetText
12     # patch for ruby-gettext 1.9.0 to cope with anonymous modules used by rbot
13     # FIXME remove the patch when ruby-gettext is fixed, or rbot switches to named modules
14   # fix for module names that are not constant names
15     def bound_targets(klass)  # :nodoc:
16       ret = []
17       ary = klass.name.split(/::/)
18       while(v = ary.shift)
19         begin
20           ret.unshift(((ret.size == 0) ?
21             Object.const_get(v) : ret[0].const_get(v)))
22         rescue NameError
23           # when an anonymous module is encountered, only the previous modules
24           # are returned
25           break
26         end
27       end
28       ((ret + klass.ancestors + [GetText]) & @@__textdomainmanagers.keys).uniq
29     end
30   end
31
32   begin
33     gettext_info = StringIO.new
34     current_textdomain_info(:out=>gettext_info) # fails sometimes
35     debug 'using ruby-gettext'
36     gettext_info.string.each_line {|l| debug l}
37   rescue Exception
38   end
39
40 rescue LoadError
41   warn 'ruby-gettext package not available; translations are disabled'
42
43   # dummy functions that return msg_id without translation
44   def _(s)
45     s
46   end
47
48   def N_(s)
49     s
50   end
51
52   def n_(s_single, s_plural, n)
53     n > 1 ? s_plural : s_single
54   end
55
56   def s_(*args)
57     args[0]
58   end
59
60   # the following extension to String#% is from ruby-gettext's string.rb file.
61   # it needs to be included in the fallback since the source already use this form
62
63 =begin
64   string.rb - Extension for String.
65
66   Copyright (C) 2005,2006 Masao Mutoh
67
68   You may redistribute it and/or modify it under the same
69   license terms as Ruby.
70 =end
71
72   # Extension for String class.
73   #
74   # String#% method which accept "named argument". The translator can know
75   # the meaning of the msgids using "named argument" instead of %s/%d style.
76   class String
77     alias :_old_format_m :% # :nodoc:
78
79     # call-seq:
80     #  %(arg)
81     #  %(hash)
82     #
83     # Format - Uses str as a format specification, and returns the result of applying it to arg.
84     # If the format specification contains more than one substitution, then arg must be
85     # an Array containing the values to be substituted. See Kernel::sprintf for details of the
86     # format string. This is the default behavior of the String class.
87     # * arg: an Array or other class except Hash.
88     # * Returns: formatted String
89     #
90     #  (e.g.) "%s, %s" % ["Masao", "Mutoh"]
91     #
92     # Also you can use a Hash as the "named argument". This is recommanded way for Ruby-GetText
93     # because the translators can understand the meanings of the msgids easily.
94     # * hash: {:key1 => value1, :key2 => value2, ... }
95     # * Returns: formatted String
96     #
97     #  (e.g.) "%{firstname}, %{familyname}" % {:firstname => "Masao", :familyname => "Mutoh"}
98     def %(args)
99       if args.kind_of?(Hash)
100         ret = dup
101         args.each {|key, value|
102           ret.gsub!(/\%\{#{key}\}/, value.to_s)
103         }
104         ret
105       else
106         ret = gsub(/%\{/, '%%{')
107         begin
108     ret._old_format_m(args)
109         rescue ArgumentError
110     $stderr.puts "  The string:#{ret}"
111     $stderr.puts "  args:#{args.inspect}"
112         end
113       end
114     end
115   end
116 end