# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

module Qwik
  class Action
    def plg_lang(lang)
      # lang should be only two letters length.
      return "error" unless /\A[a-z][a-z]\z/ =~ lang

      alang = @req.accept_language[0]

      return "" unless alang && alang == lang

      s = yield
      return c_res(s)
    end
  end
end

if $0 == __FILE__
  $LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
  require "t/test"
  $debug = true
end

if $debug
  class TC_ActLang < Test::Unit::TestCase
    include TestSession

    def test_lang
      assert_wiki("error", "{{lang(nosuchlang)\na\n}}")
      assert_wiki([""], "{{lang(ja)\nj\n}}")
      assert_wiki([:p, "e"], "{{lang(en)\ne\n}}")

#      t_lang_ja
      assert_wiki([:p, "j"], "{{lang(ja)\nj\n}}") {|req|
	req.accept_language = ["ja"]
      }
      assert_wiki("", "{{lang(en)\ne\n}}") {|req|
	req.accept_language = ["ja"]
      }
    end
  end
end
