#
# 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/util-path"

module Qwik
  class CatalogFactory
    def initialize
      @catalogs = {}
    end

    def get_catalog(lang)
      @catalogs[lang]
    end

    def load_all_catalogs(dir)
      # lang has just two letters length.
      # @catalogs = load_all_catalogs_impl(dir, /\Acatalog-(..)\.rb\z/)
      @catalogs = load_all_catalogs_impl(dir, /\Acatalog-(..)\.rb\z/)
    end

    private

    def load_all_catalogs_impl(dir, re)
      catalogs = Hash.new
      dir.path.each_entry {|file|
	if re =~ file.to_s
	  lang = $1
	  fullpath = dir.path + file
	  require fullpath
	  #catalogs[lang] = send("catalog_"+lang)
	  #catalogs[lang] = klass.send("catalog_"+lang)
	  catalogs[lang] = self.class.send("catalog_"+lang)
	end
      }
      catalogs["en"] = Hash.new {|h, k| k }
      return catalogs
    end
  end
end

if $0 == __FILE__
  require "test/unit"
  $KCODE = "s"
  $debug = true
end

if defined?($debug) && $debug
  class TestCatalogFactory < Test::Unit::TestCase
    def test_all
      cf = Qwik::CatalogFactory.new
      cf.load_all_catalogs(".")
      catalog_ja = cf.get_catalog("ja")
      assert_equal(true, catalog_ja != nil)
    end
  end
end
