MailingList


 « Return to Thread: Monkey patch

Re: Monkey patch

by ginger72 :: Rate this Message:

Reply to Author | View in Thread

Hi Judith,

excellent point! I have similar issues when formatting translated  
text. I have gotten around by adding a monkey patch myself, which I  
derived from gettext (another l18n plugin for rails) it works like this:

"%{firstname}, %{familyname}" % {:firstname => "Masao", :familyname  
=> "Mutoh"}
-> Masao Mutoh""

Hope this helps.

Greetings,
Juergen



   # call-seq:
   #  %(arg)
   #  %(hash)
   #
   # Format - Uses str as a format specification, and returns the  
result of applying it to arg.
   # If the format specification contains more than one substitution,  
then arg must be
   # an Array containing the values to be substituted. See  
Kernel::sprintf for details of the
   # format string. This is the default behavior of the String class.
   # * arg: an Array or other class except Hash.
   # * Returns: formatted String
   #
   #  (e.g.) "%s, %s" % ["Masao", "Mutoh"]
   #
   # Also you can use a Hash as the "named argument". This is  
recommanded way for Ruby-GetText
   # because the translators can understand the meanings of the  
msgids easily.
   # * hash: {:key1 => value1, :key2 => value2, ... }
   # * Returns: formatted String
   #
   #  (e.g.) "%{firstname}, %{familyname}" % {:firstname =>  
"Masao", :familyname => "Mutoh"}
   #
   # Note: This code was derived from gettext string.rb package and  
adopted for Probono.
   #       We allow {} and ${} formatting, though, will be deprecated  
in the future
   def %(args)
     if args.kind_of?(Hash)
       ret = dup
       args.each {|key, value|
         ret.gsub!(/\%\{#{key}\}/, value.to_s)
         # support deprecated formats as well, such as "${...}" and  
"{...}"
         if ret=~/\$\{#{key}\}/
           ret.gsub!(/\$\{#{key}\}/, value.to_s)
           $stderr.puts "PROBONO DEPRECATED: string format using '${#
{key}}' is deprecated, use '%{#{key}}' in the future"
         end
         if ret=~/\{#{key}\}/
           ret.gsub!(/\{#{key}\}/, value.to_s)
           $stderr.puts "PROBONO DEPRECATED: string format using '{#
{key}}' is deprecated, use '%{#{key}}' in the future"
         end
       }
       ret
     else
       ret = gsub(/%\{/, '%%{')
         begin
           ret._old_format_m(args)
         rescue ArgumentError => e
           if $DEBUG
             $stderr.puts "  The string:#{ret}"
             $stderr.puts "  args:#{args.inspect}"
             puts e.backtrace
           else
             raise ArgumentError, e.message
           end
         end
       end
     end
   end


On Apr 22, 2008, at 8:06 AM, Judith Meyer wrote:

> Hello,
>
> another quick question from me: I suppose the monkey patch leads to
> interesting results when the order of %s is exchanged in a
> translation? The whole point of having %s was to allow translators to
> move these variables to whatever place they needed to be in the
> translation, so that languages like Japanese could put the verb last
> and so on. Using %s several times without disambiguation however
> assumes that the variables will always be required in the same order.
> Can this be fixed? I developed the grammar system for Cantr.net and
> there the translation system uses variables like #NAME#, #WEAPON#,
> #LOCATION# and so on.
>
> Best wishes,
>
> Judith


 « Return to Thread: Monkey patch