#!/usr/bin/env ruby -w
#
# Copyright (C) 2002-2004 Satoru Takabayashi <satoru@namazu.org> 
# Copyright (C) 2003-2005 Kouichirou Eto
#     All rights reserved.
#     This is free software with ABSOLUTELY NO WARRANTY.
#
# You can redistribute it and/or modify it under the terms of 
# the GNU General Public License version 2.
#

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/ml-catalog-factory"
require "qwik/ml-utils"
require "qwik/qp"

module QuickML
  module GetText
    def set_catalog (catalog)
      @catalog = nil
      @gettext_catalog = catalog
    end

    def set_charset(charset)
      @message_charset = nil
      @gettext_charset = charset
    end

=begin
    def self.codeconv (text)
      method = @gettext_catalog[:codeconv_method]
      return text.send(method) if @gettext_catalog && method
      return text
    end
=end

    def gettext (text, *args)
      @gettext_catalog	= nil if !defined?(@gettext_catalog)
      @catalog		= nil if !defined?(@catalog)
      @message_charset	= nil if !defined?(@message_charset)
      @gettext_charset	= nil if !defined?(@gettext_charset)
      @gettext_catalog = @catalog if @catalog && !@gettext_catalog
      @gettext_charset = @message_charset if @message_charset && !@gettext_charset
      #qp !@gettext_catalog.nil?, @gettext_charset
      #qp @gettext_catalog.length if @gettext_catalog
      unless @gettext_catalog && @gettext_catalog[:charset] == @gettext_charset
	return sprintf(text, *args)
      end
      translated_message = @gettext_catalog[text]
      if translated_message
	text = sprintf(translated_message, *args)
	method = @gettext_catalog[:codeconv_method]
	return text.send(method) if @gettext_catalog && method
	return text
      end
      return sprintf(text, *args)
    end

    def gettext2 (text)
      @gettext_catalog	= nil if !defined?(@gettext_catalog)
      @catalog		= nil if !defined?(@catalog)
      @message_charset	= nil if !defined?(@message_charset)
      @gettext_charset	= nil if !defined?(@gettext_charset)
      @gettext_catalog = @catalog if @catalog && !@gettext_catalog
      @gettext_charset = @message_charset if @message_charset && !@gettext_charset
      unless @gettext_catalog && @gettext_catalog[:charset] == @gettext_charset
	return text
      end
      translated_message = @gettext_catalog[text]
      if translated_message
	text = translated_message
	method = @gettext_catalog[:codeconv_method]
	return text.send(method) if @gettext_catalog && method
	return text
      end
      return text
    end

    alias :_ :gettext
    alias :__ :gettext2

    module_function :set_catalog
    module_function :set_charset
    module_function :gettext
    module_function :gettext2
  end
end

if $0 == __FILE__
  require "test/unit"
  require "qwik/ml-catalog-factory"
  require "qwik/util-kconv"
  $KCODE = "s"
  $debug = true
end

if $debug
  class TestMLGetText < Test::Unit::TestCase
    def assert_gettext(e, *a)
      assert_equal(e.sjistojis, QuickML::GetText.gettext(*a))
    end

    def test_all
      cf = QuickML::CatalogFactory.new
      #qp cf.catalog_re
      cf.load_all_catalogs(".")
      catalog_ja = cf.get_catalog("ja")
      #qp catalog_ja.length
      assert_equal(true, catalog_ja != nil)

      # test before set catalog
      @catalog		= nil
      @message_charset	= nil
      QuickML::GetText.set_catalog(nil)
      QuickML::GetText.set_charset(nil)
      assert_equal("hello", QuickML::GetText.gettext("hello"))
      assert_gettext("hello", "hello")
      assert_gettext("Info: a\n", "Info: %s\n", "a")
      assert_gettext("If you agree, then,\n", "If you agree, then,\n")

      # test after set catalog
      @catalog		= catalog_ja
      @message_charset	= "iso-2022-jp"
      QuickML::GetText.set_catalog(catalog_ja)
      QuickML::GetText.set_charset("iso-2022-jp")
      assert_equal("こんにちは".sjistojis, QuickML::GetText.gettext("hello"))
      assert_equal("こんにちは".sjistojis, QuickML::GetText.gettext2("hello"))
      assert_gettext("こんにちは", "hello")
      assert_gettext("使い方: a\n", "Info: %s\n", "a")
      assert_gettext("もし承認する場合、\n", "If you agree, then,\n")
    end
  end
end
