
#include "iwear/i18n.h"
#include "iwear/debugstream.h"
#include "iwear/moloader.h"

using namespace std;
using namespace iwear;

class hloader : public i18nLoader
{
protected:
    multimap<const char *, string, cstring_less > msgs;
public:
    inline void clear( void ) { msgs.clear(); }
    inline void add ( const char * c, string s ) { msgs.insert(make_pair(c,s)); }
    virtual inline multimap<const char *, string, cstring_less >& get_langmap( iwear_language ) { return msgs; }
    virtual void release( void ) { }
    virtual ~hloader() { }
};


void transtest( const std::string& w )
{
    cout << "Translating \"" << w << "\" => \"" << i18n::trans(w) << "\"" << endl;
}

void transtest( const std::string& w, int i )
{
    cout << "Translating \"" << w << "\" [" << i << "] => \"" << i18n::trans(w,i) << "\"" << endl;
}

int main ( void )
{
    try
    {
//	d_dbg.set_debuglevel(nonsens,debugarea::iwear_i18n);
	hloader hl;
	hl.add("Hello World", "Hallo Welt");
	//    hl.add("email", "Email");
	//    hl.add("email", "Emails");

	const char * hw = "Hello World";

	cout << "No Language Loaded" << endl;
	transtest(hw);

	i18n::add_loader(hl,iwear_english);
	cout << "HL english loaded" << endl;
	transtest(hw);

	i18n::add_loader(hl,iwear_german);
	cout << "HL german loaded" << endl;
	transtest(hw);

	i18n::set_language(iwear_german);
	cout << "german set" << endl;
	transtest(hw);

	cout << "Unknown String" << endl;
	transtest("Doodeli Doodeli hoodeli");

	cout << "Cardinality (only HL loaded)" << endl;

	transtest("email",0);
	transtest("email",1);
	transtest("email",2);
	transtest("email",3);
	transtest("email",4);
	transtest("email",5);

	cout << "Printing locale codes: " << endl;

	cout << "iwear_english : " << i18n::get_locale_code(iwear_english) << endl;
	cout << "iwear_german  : " << i18n::get_locale_code(iwear_german) << endl;


	MoLoader ml("iwear_core");
	MoLoader ml2("iwear_core");
	i18n::add_loader(ml);
	i18n::add_loader(ml2);
	cout << "Added iwear_core loader twice" << endl;
	i18n::set_language(iwear_german);
	cout << "(Re)Loaded iwear_german" << endl;

	transtest("class");
	transtest("Class");
	transtest("Exception");
	transtest("exception");
//	transtest(""); // This would output .mo file information
	cout << "Cardinality (iwear_core loaded)" << endl;

	transtest("email",0);
	transtest("email",1);
	transtest("email",2);
	transtest("email",3);
	transtest("email",4);
	transtest("email",5);

	cout << "The previous contained a double list of translations for other cardinalities too" << endl;
	i18n::remove_loader(ml2);
	i18n::set_language(iwear_german);
	cout << "Removed the second loader" << endl;

	transtest("email",0);
	transtest("email",1);
	transtest("email",2);
	transtest("email",3);
	transtest("email",4);
	transtest("email",5);

	i18n::set_language(iwear_locale);
	cout << "Set locale" << endl;
    }
    catch( const exception& e )
    {
	d_err << e << endl;
    }
}
