//	Zinc Application Framework - M_I18N.CPP
//	COPYRIGHT (C) 1990-1999.  All Rights Reserved.
//	Zinc Software Incorporated.  Pleasant Grove, Utah  USA

// We need the OS functions (getenv, tolower, toupper).
#include <ctype.h>
#include <stdlib.h>
#include <z_idata.hpp>
#include <z_scrdsp.hpp>

#if defined(ZAF_UNICODE)

#if defined(_ALPHA_) || defined(__DECCXX) || (defined(__GNUC__) && defined(__alpha__)) || defined(_IBM_RS6000) || defined(sun)
static ZafIChar shiftJIS[] = ZAF_ITEXT("932");
static ZafIChar eucJIS[] =   ZAF_ITEXT("EUC_JIS");
static ZafIChar big5[] =     ZAF_ITEXT("950");
static ZafIChar ksc5601[] =  ZAF_ITEXT("949");
static ZafIChar gb2312[] =   ZAF_ITEXT("1381");
#endif

static struct
{
	char *osMap;
	ZafIChar *map;
} fontMap[] =
{
#	if defined(_ALPHA_) || defined(__DECCXX) || (defined(__GNUC__) && defined(__alpha__))
	{ "ja_JP",					eucJIS },
	{ "ja_JP.SJIS",				shiftJIS },
	{ "ja_JP.deckanji",			eucJIS },
	{ "ja_JP.eucJP",			eucJIS },
	{ "ja_JP.sdeckanji",		shiftJIS },
	{ "ko_KR",					ksc5601 },
	{ "ko_KR.deckorean",		ksc5601 },
	{ "ko_KR.eucKR",			ksc5601 },
	{ "zh_CN",					gb2312 },
	{ "zh_CN.dechanzi",			gb2312 },
	{ "zh_CN.dechanzi@pinyin",	gb2312 },
	{ "zh_CN.dechanzi@radical",	gb2312 },
	{ "zh_CN.dechanzi@stroke",	gb2312 },
	{ "zh_HK.big5",				big5 },
	{ "zh_HK.dechanyu",			big5 },
	{ "zh_HK.dechanzi",			gb2312 },
	{ "zh_HK.eucTW",			big5 },
	{ "zh_TW",					big5 },
	{ "zh_TW.big5",				big5 },
	{ "zh_TW.dechanyu",			big5 },
	{ "zh_TW.eucTW",			big5 },
	{ "zh_TW.big5@chuyin",		big5 },
	{ "zh_TW.big5@radical",		big5 },
	{ "zh_TW.big5@stroke",		big5 },
	{ "zh_TW.dechanyu@chuyin",	big5 },
	{ "zh_TW.dechanyu@radical",	big5 },
	{ "zh_TW.dechanyu@stroke",	big5 },
	{ "zh_TW.eucTW@chuyin",		big5 },
	{ "zh_TW.eucTW@radical",	big5 },
	{ "zh_TW.eucTW@stroke",		big5 },
#	elif defined(_IBM_RS6000)
	{ "ko_KR",					ksc5601 },
	{ "ko_KR.IBM-eucKR",		ksc5601 },
	{ "zh_TW",					big5 },
	{ "zh_TW.IBM-eucTW",		big5 },
	{ "ja_JP",					eucJIS },
	{ "Ja_JP",					eucJIS },
	{ "ja_JP.IBM-eucJP",		eucJIS },
	{ "Ja_JP.IBM-932",			shiftJIS },
#	elif defined(sun)
	{ "ja",						eucJIS },
	{ "ja_JP",					eucJIS },
	{ "japan",					eucJIS },
	{ "japanese",				eucJIS },
	{ "ja_JP.EUC",				eucJIS },
	{ "ja_JP.SJIS",				shiftJIS },
	{ "ko",						ksc5601 },
	{ "ko_KR",					ksc5601 },
	{ "korean",					ksc5601 },
	{ "ko_KR.EUC",				ksc5601 },
	{ "zh",						gb2312 },
	{ "chinese",				gb2312 },
	{ "zh_CN.EUC",				gb2312 },
	{ "zh_TW",					big5 },
	{ "tchinese",				big5 },
	{ "zh_TW.EUC",				big5 },
#	endif
	{ ZAF_NULLP(char), 			ZAF_NULLP(ZafIChar) }
};
#endif

const ZafIChar *ZafI18nData::OSI18nName(void)
{
	// Initialize the i18n string.
	static ZafIChar osI18nName[ZAF_MAXNAMELEN];
	osI18nName[0] = '\0';

	// Check for ZAF_LANG.
	char *environmentName = getenv("ZAF_LANG");

	// Check for LANG.
	if (!environmentName || !environmentName[0])
		environmentName = getenv("LANG");

	// Check for an environment match.
	if (!environmentName || !environmentName[0])
		return (osI18nName[0] ? osI18nName : ZAF_NULLP(ZafIChar));

	int i, offset = 0;
	const ZafIChar *zafLanguageName, *zafLocaleName, *zafCodeSetName;
	ZafIChar osLanguageName[16], osLocaleName[16], osCodeSetName[16];

	// Convert the language.
	for (i = 0; environmentName[offset]; i++)
	{
		osLanguageName[i] = environmentName[offset++];
		if (environmentName[offset-1] == '_')
			break;
	}
	osLanguageName[i] = '\0';
	if (osLanguageName[0])
		zafLanguageName = OSLanguageToZafLanguage(osLanguageName);
	else
		zafLanguageName = ZAF_NULLP(ZafIChar);

	// Convert the locale.
	for (i = 0; environmentName[offset]; i++)
	{
		osLocaleName[i] = environmentName[offset++];
		if (environmentName[offset-1] == '.')
			break;
	}
	osLocaleName[i] = '\0';
	if (!osLocaleName[0] && zafLanguageName)
		zafLocaleName = ZafLanguageToZafLocale(zafLanguageName);
	else
		zafLocaleName = ZAF_NULLP(ZafIChar);
	if (!zafLanguageName && zafLocaleName)
		zafLanguageName = ZafLocaleToZafLanguage(zafLocaleName);

	// Convert the code-set.
	for (i = 0; environmentName[offset]; i++)
		osCodeSetName[i] = environmentName[offset++];
	osCodeSetName[i] = '\0';

	// Determine the default UNICODE font.
	zafCodeSetName = ZAF_NULLP(ZafIChar);
#	if defined(ZAF_UNICODE)
	XFontSet fontSet;
	if (zafDisplay)
	{
		fontSet = ((ZafScreenDisplay *)zafDisplay)->MtfGetFontSet(((ZafScreenDisplay *)zafDisplay)->fontTable[ZAF_FNT_DIALOG]);
		if (fontSet || environmentName)
		{
			char *locale = fontSet ? XLocaleOfFontSet(fontSet) : environmentName;
			for (i = 0; fontMap[i].osMap; i++)
				if (!::strcmp(fontMap[i].osMap, locale))
				{
					int j = 0;
					while (fontMap[i].map[j])
						osCodeSetName[j] = (ZafIChar)fontMap[i].map[j++];
					osCodeSetName[j] = '\0';
					break;
				}
		}
	}
#	endif

	// Construct the i18n name.
	offset = 0;
	if (zafLanguageName && zafLanguageName[0])
	{
		for (i = 0; zafLanguageName[i]; i++)
			osI18nName[offset++] = zafLanguageName[i];
	}
	else if (osLanguageName[0])
	{
		for (i = 0; osLanguageName[i]; i++)
			osI18nName[offset++] = (ZafIChar)osLanguageName[i];
	}
	if (zafLocaleName && zafLocaleName[0])
	{
		osI18nName[offset++] = (ZafIChar)'_';
		for (i = 0; zafLocaleName[i]; i++)
			osI18nName[offset++] = zafLocaleName[i];
	}
	else if (osLocaleName[0])
	{
		osI18nName[offset++] = (ZafIChar)'_';
		for (i = 0; osLocaleName[i]; i++)
			osI18nName[offset++] = (ZafIChar)osLocaleName[i];
	}
	if (zafCodeSetName && zafCodeSetName[0])
	{
		osI18nName[offset++] = (ZafIChar)'.';
		for (i = 0; zafCodeSetName[i]; i++)
			osI18nName[offset++] = zafCodeSetName[i];
	}
	else if (osCodeSetName[0])
	{
		osI18nName[offset++] = (ZafIChar)'.';
		for (i = 0; osCodeSetName[i]; i++)
			osI18nName[offset++] = (ZafIChar)osCodeSetName[i];
	}
	osI18nName[offset] = '\0';

	// Return the osI18nName.
	return (osI18nName[0] ? osI18nName : ZAF_NULLP(ZafIChar));
}

