TkMsgCatalog (Class)

In: tk/lib/tk/msgcat.rb
Parent: TkObject

class TkMsgCatalog

Constants

TkCommandNames = [ '::msgcat::mc'.freeze, '::msgcat::mcmax'.freeze, '::msgcat::mclocale'.freeze, '::msgcat::mcpreferences'.freeze, '::msgcat::mcload'.freeze, '::msgcat::mcset'.freeze, '::msgcat::mcmset'.freeze, '::msgcat::mcunknown'.freeze
  extend TkMsgCatalog
MSGCAT_EXT = '.msg'
UNKNOWN_CBTBL = Hash.new{|hash,key| hash[key] = {}}.taint

External Aliases

translate -> mc
translate -> []

Attributes

msgcat_ext  [RW] 

Included Modules

TkCore

Public Class methods

[Source]

# File tk/lib/tk/msgcat.rb, line 54
  def self.callback(namespace, locale, src_str)
    cmd_tbl = TkMsgCatalog::UNKNOWN_CBTBL[TkCore::INTERP.__getip]
    cmd = cmd_tbl[namespace]
    cmd = cmd_tbl['::'] unless cmd  # use global scope as interp default

    return src_str unless cmd       # no cmd -> return src-str (default action)

    begin
      cmd.call(locale, src_str)
    rescue Exception => e
      begin
        msg = _toUTF8(e.class.inspect) + ': ' + 
              _toUTF8(e.message) + "\n" + 
              "\n---< backtrace of Ruby side >-----\n" + 
              _toUTF8(e.backtrace.join("\n")) + 
              "\n---< backtrace of Tk side >-------"
        msg.instance_variable_set(:@encoding, 'utf-8')
      rescue Exception
        msg = e.class.inspect + ': ' + e.message + "\n" + 
              "\n---< backtrace of Ruby side >-----\n" + 
              e.backtrace.join("\n") + 
              "\n---< backtrace of Tk side >-------"
      end
      fail(e, msg)
    end
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 261
  def self.def_unknown_proc(cmd=Proc.new)
    TkMsgCatalog::UNKNOWN_CBTBL[TkCore::INTERP.__getip]['::'] = cmd
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 210
  def self.load(dir)
    self.load_rb(dir)
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 182
  def self.load_rb(dir)
    count = 0
    preferences().each{|loc|
      file = File.join(dir, loc + self::MSGCAT_EXT)
      if File.readable?(file)
        count += 1
        eval(open(file){|f| f.read})
      end
    }
    count
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 178
  def self.load_tk(dir)
    number(tk_call('::msgcat::mcload', dir))
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 157
  def self.locale
    tk_call('::msgcat::mclocale')
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 164
  def self.locale=(locale)
    tk_call('::msgcat::mclocale', locale)
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 150
  def self.maxlen(*src_strings)
    tk_call('::msgcat::mcmax', *src_strings).to_i
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 79
  def initialize(namespace = nil)
    if namespace.kind_of?(TkNamespace)
      @namespace = namespace
    elsif namespace == nil
      @namespace = TkNamespace.new('::')  # global namespace

    else
      @namespace = TkNamespace.new(namespace)
    end
    @path = @namespace.path

    @msgcat_ext = '.msg'
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 171
  def self.preferences
    tk_split_simplelist(tk_call('::msgcat::mcpreferences'))
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 215
  def self.set_translation(locale, src_str, trans_str=None, enc='utf-8')
    trans_str = Tk.UTF8_String(_toUTF8(trans_str, enc)) if trans_str != None
    Tk.UTF8_String(tk_call_without_enc('::msgcat::mcset', 
                                       locale, 
                                       _get_eval_string(src_str, true), 
                                       trans_str))
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 232
  def self.set_translation_list(locale, trans_list, enc='utf-8')
    # trans_list ::= [ [src, trans], [src, trans], ... ]

    list = []
    trans_list.each{|src, trans|
      if trans && trans != None
        list << _get_eval_string(src, true) 
        list << Tk.UTF8_Stirng(_toUTF8(trans, enc))
      else
        list << _get_eval_string(src, true) << ''
      end
    }
    number(tk_call_without_enc('::msgcat::mcmset', locale, list))
  end

*args ::= form, arg, arg, …

[Source]

# File tk/lib/tk/msgcat.rb, line 130
  def self.translate(*args)
    dst = args.collect{|src|
      tk_call_without_enc('::msgcat::mc', _get_eval_string(src, true))
    }
    Tk.UTF8_String(sprintf(*dst))
  end

Public Instance methods

[](*args)

Alias for translate

[Source]

# File tk/lib/tk/msgcat.rb, line 264
  def def_unknown_proc(cmd=Proc.new)
    TkMsgCatalog::UNKNOWN_CBTBL[TkCore::INTERP.__getip][@namespace.path] = cmd
  end
load(dir)

Alias for load_rb

[Source]

# File tk/lib/tk/msgcat.rb, line 198
  def load_rb(dir)
    count = 0
    preferences().each{|loc|
      file = File.join(dir, loc + @msgcat_ext)
      if File.readable?(file)
        count += 1
        @namespace.eval(open(file){|f| f.read})
      end
    }
    count
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 194
  def load_tk(dir)
    number(@namespace.eval{tk_call('::msgcat::mcload', dir)})
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 160
  def locale
    @namespace.eval{tk_call('::msgcat::mclocale')}
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 167
  def locale=(locale)
    @namespace.eval{tk_call('::msgcat::mclocale', locale)}
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 153
  def maxlen(*src_strings)
    @namespace.eval{tk_call('::msgcat::mcmax', *src_strings).to_i}
  end
mc(*args)

Alias for translate

[Source]

# File tk/lib/tk/msgcat.rb, line 93
  def method_missing(id, *args)
    # locale(src, trans) ==> set_translation(locale, src, trans)

    loc = id.id2name
    case args.length
    when 0 # set locale

      self.locale=(loc)

    when 1 # src only, or trans_list

      if args[0].kind_of?(Array)
        # trans_list

        #list = args[0].collect{|src, trans|

        #  [ Tk::UTF8_String.new(src), Tk::UTF8_String.new(trans) ]

        #}

        self.set_translation_list(loc, args[0])
      else
        # src

        #self.set_translation(loc, Tk::UTF8_String.new(args[0]))

        self.set_translation(loc, args[0])
      end

    when 2 # src and trans, or, trans_list and enc

      if args[0].kind_of?(Array)
      else
        #self.set_translation(loc, args[0], Tk::UTF8_String.new(args[1]))

        self.set_translation(loc, *args)
      end

    when 3 # src and trans and enc

      self.set_translation(loc, *args)

    else
      fail NameError, "undefined method `#{name}' for #{self.to_s}", error_at

    end
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 174
  def preferences
    tk_split_simplelist(@namespace.eval{tk_call('::msgcat::mcpreferences')})
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 222
  def set_translation(locale, src_str, trans_str=None, enc='utf-8')
    trans_str = Tk.UTF8_String(_toUTF8(trans_str, enc)) if trans_str != None
    Tk.UTF8_String(@namespace.eval{
                     tk_call_without_enc('::msgcat::mcset', 
                                         locale, 
                                         _get_eval_string(src_str, true), 
                                         trans_str)
                   })
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 245
  def set_translation_list(locale, trans_list, enc='utf-8')
    # trans_list ::= [ [src, trans], [src, trans], ... ]

    list = []
    trans_list.each{|src, trans|
      if trans && trans != None
        list << _get_eval_string(src, true) 
        list << Tk.UTF8_String(_toUTF8(trans, enc))
      else
        list << _get_eval_string(src, true) << ''
      end
    }
    number(@namespace.eval{
             tk_call_without_enc('::msgcat::mcmset', locale, list)
           })
  end

[Source]

# File tk/lib/tk/msgcat.rb, line 140
  def translate(*args)
    dst = args.collect{|src|
      @namespace.eval{tk_call_without_enc('::msgcat::mc', 
                                          _get_eval_string(src, true))}
    }
    Tk.UTF8_String(sprintf(*dst))
  end

[Validate]