#ifndef LanguageHeader
#define LanguageHeader

#include "lib.h"
#include "qpair.h"

typedef struct translation {
	char *phrase;
	QPairList *translations;
	struct translation *next;
} Translation;

typedef struct translations {
	Wordlist *supported;
	Translation **table;
	int size,isize;
	int ref;
} Translations;

typedef struct language {
	Translations *t;
	char *language;
	int isenglish;
	char *last;
} Language;

extern Translations *lang_bin;
extern Translations *lang_web;

/* call with 0 if size unknown */
Translations *lang_init(int size);

/* call if you called lang_init with 0 */
/* will not re-allocate */
int lang_allocate(Translations *t, int size);

/* free it */
void lang_free(Translations **t);

/* load language file */
int lang_load(Translations *l, char *file, Wordlist *support, int list_only, char *error, int bfsz);

/* save language file, 'header' may be passed with 'hdata' to write custom lines at the top of the file */
int lang_save(Translations *l, char *file, char *error, int bfsz, void (*header)(FILE *f, void *), void *hdata);
void lang_write(FILE *f, Translation *t, Wordlist *languages);

/* iterate thru translations */
void lang_iterate(Translations *l, int (*callback)(void *, Translation *t), void *data);

/* list of supported languages */
/* must be word_free()'d */
Wordlist *lang_list(Translations *t);
Wordlist *lang_list2(Translations *t);

/* add support for langauge */
void lang_support(Translations *t, char *language);

/* is language supported? */
int lang_supported(Translations *t, char *language);

/* do we have a translation for this phrase */
int lang_exist_phrase(Translations *l, char *phrase);

/* get new language struct */
/* optional specify a language */
Language *lang_instance(Translations *t, char *language);
void lang_free_instance(Language **l);

/* return selected language */
char *lang_selected(Language *l);

/* are we set to 'language'? */
int lang_isset(Language *l, char *language);

int lang_set_product(char *s);
char *lang_get_product(Language *l, char *phrase, char *tmp) ;


/* choose a language to use */
int lang_set(Language *l, char *language);

/* do we have a translation for this phrase */
int lang_exist(Language *l, char *phrase, char **result);

/* translate a phrase */
/* returns 'phrase' if none found */
char *lang_get_bf(Language *l, char *phrase, char *bf, int bfsz);
char *lang_get(Language *l, char *phrase);

/* add a new translation */
void lang_add(Language *l, char *phrase, char *translation);

typedef struct charset {
	char *language;
	char *charset;
} Charset;

extern Charset charset_list[];

/* language charset? */
/* returns "iso-8859-1" if not found */
char *lang_charset(char *language);

typedef struct locale {
	char *language;
	char *locale;
} Locale;

extern Locale locale_list[];

/* language charset? */
/* returns "C" if not found */
char *lang_locale(char *language);

size_t strftime_translate(Language *l, char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);



typedef struct stringtable {
	char *language;
	char *native;
	int show;
	int istest;
	QPairList *table;
	struct stringtable *base;
	struct stringtable *next;
} Stringtable;

Stringtable *stringtable_retrieve(char *lang, int create);
int stringtable_load(char *fname, char *result);
int stringtable_fallbacks(char *lang);
Wordlist *stringtable_list(int sort_native, int nodemo, int all);
char *stringtable_native(char *name);
void stringtable_log_extra(int enable);

enum {L_NONE, L_DEMO_MORE, L_DEMO_STD, L_FULL};

#endif
