#include "StdAfx.h"
#include "Lang.h"

CLang g_lang;

CLang::CLang(void)
{
	m_strPath = _T("");
	m_nCurLang = -1;
	m_nLangCount = 0;
}

CLang::~CLang(void)
{
}

void CLang::SetLangFile(const CString& strPath)
{
	m_strPath = strPath;
}

BOOL CLang::SetCurLang(const int& index)
{
	if(index>m_nLangCount-1 || index<0)
	{
		return FALSE;
	}
	else
	{
		m_nCurLang = index;
	}
	return TRUE;
}

const CString& CLang::GetLangFile() const
{
	return m_strPath;
}

const int& CLang::GetCurLang() const
{
	return m_nCurLang;
}

const int& CLang::GetLangCount() const
{
	return m_nLangCount;
}

const CString& CLang::GetLangName(const int& index) const
{
	return m_arrLangName.GetAt(index);
}

void CLang::TranslateMenu(CMenu* pMenu)
{
	return;
}

void CLang::TranslateDialog(CDialog* pDlg, UINT ID)
{
	// ini的节：对话框名
	CString strDlgID;
	strDlgID.Format(_T("%d"), ID);
	CString strSection = _T("DLG_") + strDlgID;

	// 遍历控件
	CWnd* pWndChild = NULL;
	pWndChild = pDlg->GetWindow(GW_CHILD);
	while(pWndChild)
	{
		// 当前控件ID
		CString strChildID;
		strChildID.Format(_T("%d"), pWndChild->GetDlgCtrlID());

		// 获取翻译值
		CString strValue;
		GetPrivateProfileString(strSection, strChildID, _T(""), strValue.GetBuffer(MAX_PATH), MAX_PATH, m_strPath);

		// 获取到则应用到界面
		if(strValue != _T(""))
		{
			pDlg->SetDlgItemText(pWndChild->GetDlgCtrlID(), strValue);
		}
		strValue.ReleaseBuffer();

		pWndChild = pWndChild->GetNextWindow();
	}
}

const CString CLang::TranslateString(const CString& str)
{
	// 获取翻译值
	CString strSection = _T("STRINGTABLE");
	CString strValue;
	GetPrivateProfileString(strSection, str, _T(""), strValue.GetBuffer(MAX_PATH), MAX_PATH, m_strPath);
	strValue.ReleaseBuffer();

	return strValue;
}

void CLang::AfxMsgBox(CString strContent, UINT nType /*= MB_OK*/, UINT nIDHelp /*= 0*/)
{
	return;
}

void CLang::Clear()
{
	m_strPath = _T("");
	m_nCurLang = -1;
	m_nLangCount = 0;
	m_arrLangName.RemoveAll();
}

void CLang::AddLang(const CString& strLangName)
{
	m_nLangCount++;
	m_arrLangName.Add(strLangName);
}

BOOL CLang::DeleteLang()
{
	if(m_nLangCount > 0)
	{
		m_arrLangName.RemoveAt(m_nLangCount-1);
		m_nLangCount--;
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}
