浏览代码

added multilanguage files

git-svn-id: svn://svn.code.sf.net/p/ditto-cp/code/trunk@189 595ec19a-5cb4-439b-94a8-42fb3063c22c
sabrogden 20 年之前
父节点
当前提交
63f3e56c2b
共有 2 个文件被更改,包括 501 次插入0 次删除
  1. 395 0
      MultiLanguage.cpp
  2. 106 0
      MultiLanguage.h

+ 395 - 0
MultiLanguage.cpp

@@ -0,0 +1,395 @@
+// MultiLanguage.cpp: implementation of the CMultiLanguage class.
+//
+//////////////////////////////////////////////////////////////////////
+
+#include "stdafx.h"
+#include "cp_main.h"
+#include "MultiLanguage.h"
+
+#ifdef _DEBUG
+#undef THIS_FILE
+static char THIS_FILE[]=__FILE__;
+#define new DEBUG_NEW
+#endif
+
+//////////////////////////////////////////////////////////////////////
+// Construction/Destruction
+//////////////////////////////////////////////////////////////////////
+
+CMultiLanguage::CMultiLanguage()
+{
+	m_csAuthor = "";
+	m_lFileVersion = 0;
+	m_bOnlyGetHeader = false;
+}
+
+CMultiLanguage::~CMultiLanguage()
+{
+	ClearArrays();
+}
+
+void CMultiLanguage::ClearArrays()
+{
+	m_csAuthor = "";
+	m_lFileVersion = 0;
+	m_bOnlyGetHeader = false;
+
+	ClearArray(m_RightClickMenu);
+	ClearArray(m_ClipProperties);
+	ClearArray(m_OptionsGeneral);
+	ClearArray(m_OptionsSupportedTypes);
+	ClearArray(m_OptionsShortcuts);
+	ClearArray(m_OptionsQuickPaste);
+	ClearArray(m_OptionsFriends);
+	ClearArray(m_OptionsFriendsDetail);
+	ClearArray(m_OptionsStats);
+	ClearArray(m_OptionsSupportedTypesAdd);
+	ClearArray(m_MoveToGroups);
+	ClearArray(m_TrayIconRightClickMenu);
+	ClearArray(m_OptionsSheet);
+
+	ClearMap(m_StringMap);
+}
+
+void CMultiLanguage::ClearArray(LANGUAGE_ARRAY &Array)
+{
+	int nSize = Array.GetSize();
+	for(int i = 0; i < nSize; i++)
+	{
+		CLangItem *plItem = Array[i];
+
+		delete plItem;
+		plItem = NULL;
+	}
+	Array.RemoveAll();
+}
+
+void CMultiLanguage::ClearMap(LANGUAGE_MAP &Map)
+{
+	POSITION pos = Map.GetStartPosition();
+
+	CLangItem *plItem;
+	CString csKey;
+
+	while(pos)
+	{
+		Map.GetNextAssoc(pos, csKey, plItem);
+
+		if(plItem)
+		{
+			delete plItem;
+			plItem = NULL;
+		}
+	}
+
+	Map.RemoveAll();
+}
+
+CString CMultiLanguage::GetString(CString csID, CString csDefault)
+{
+	CLangItem *pItem;
+	
+	if(m_StringMap.Lookup(csID, pItem) == FALSE)
+	{
+		return csDefault;
+	}
+
+	if(pItem->m_csForeignLang.GetLength() <= 0)
+		return csDefault;
+
+	return pItem->m_csForeignLang;
+}
+
+bool CMultiLanguage::UpdateRightClickMenu(CMenu *pMenu)
+{
+	return UpdateMenuToLanguage(pMenu, m_RightClickMenu);
+}
+
+bool CMultiLanguage::UpdateTrayIconRightClickMenu(CMenu *pMenu)
+{
+	return UpdateMenuToLanguage(pMenu, m_TrayIconRightClickMenu);
+}
+
+bool CMultiLanguage::UpdateClipProperties(CWnd *pParent)
+{
+	return UpdateWindowToLanguage(pParent, m_ClipProperties);
+}
+
+bool CMultiLanguage::UpdateOptionGeneral(CWnd *pParent)
+{
+	return UpdateWindowToLanguage(pParent, m_OptionsGeneral);
+}
+
+bool CMultiLanguage::UpdateOptionSupportedTypes(CWnd *pParent)
+{
+	return UpdateWindowToLanguage(pParent, m_OptionsSupportedTypes);
+}
+
+bool CMultiLanguage::UpdateOptionShortcuts(CWnd *pParent)
+{
+	return UpdateWindowToLanguage(pParent, m_OptionsShortcuts);
+}
+
+bool CMultiLanguage::UpdateOptionQuickPaste(CWnd *pParent)
+{
+	return UpdateWindowToLanguage(pParent, m_OptionsQuickPaste);
+}
+
+bool CMultiLanguage::UpdateOptionFriends(CWnd *pParent)
+{
+	return UpdateWindowToLanguage(pParent, m_OptionsFriends);
+}
+
+bool CMultiLanguage::UpdateOptionFriendsDetail(CWnd *pParent)
+{
+	return UpdateWindowToLanguage(pParent, m_OptionsFriendsDetail);
+}
+
+bool CMultiLanguage::UpdateOptionStats(CWnd *pParent)
+{
+	return UpdateWindowToLanguage(pParent, m_OptionsStats);
+}
+
+bool CMultiLanguage::UpdateOptionSupportedTypesAdd(CWnd *pParent)
+{
+	return UpdateWindowToLanguage(pParent, m_OptionsSupportedTypesAdd);
+}
+
+bool CMultiLanguage::UpdateMoveToGroups(CWnd *pParent)
+{
+	return UpdateWindowToLanguage(pParent, m_MoveToGroups);
+}
+
+bool CMultiLanguage::UpdateOptionsSheet(CWnd *pParent)
+{
+	return UpdateWindowToLanguage(pParent, m_OptionsSheet);
+}
+
+bool CMultiLanguage::UpdateMenuToLanguage(CMenu *pMenu, LANGUAGE_ARRAY &Array)
+{
+	int nSize = Array.GetSize();
+	for(int i = 0; i < nSize; i++)
+	{
+		CLangItem *plItem = Array[i];
+
+		if(plItem->m_csForeignLang.GetLength() > 0)
+		{
+			if(plItem->m_nID > 0)
+			{
+				pMenu->ModifyMenu(plItem->m_nID, MF_BYCOMMAND, plItem->m_nID, plItem->m_csForeignLang);
+			}
+			else
+			{
+				//If an item doesn't have a menu id then its a group menu
+				//just search for the text and update the text with the foreign text
+				int nMenuPos;
+				CMenu *pNewMenu = GetMenuPos(pMenu, plItem->m_csEnglishLang, nMenuPos);
+				if(pNewMenu)
+				{
+					pNewMenu->ModifyMenu(nMenuPos, MF_BYPOSITION, -1, plItem->m_csForeignLang);
+				}
+			}
+		}
+	}
+
+	return true;
+}
+
+bool CMultiLanguage::UpdateWindowToLanguage(CWnd *pParent, LANGUAGE_ARRAY &Array)
+{
+	int nSize = Array.GetSize();
+	for(int i = 0; i < nSize; i++)
+	{
+		CLangItem *plItem = Array[i];
+
+		if(plItem->m_csForeignLang.GetLength() > 0)
+		{
+			if(plItem->m_nID > 0)
+			{
+				CWnd *pWnd = pParent->GetDlgItem(plItem->m_nID);
+				if(pWnd)
+				{
+					pWnd->SetWindowText(plItem->m_csForeignLang);
+				}
+			}
+			//If item id is -1 then set the title for the dialog
+			else if(plItem->m_nID == -1)
+			{
+				pParent->SetWindowText(plItem->m_csForeignLang);
+			}
+		}
+	}
+
+	return true;
+}
+
+CMenu * CMultiLanguage::GetMenuPos(CMenu *pMenu, const CString &csLookingForMenuText, int &nMenuPos)
+{
+	CMenu *pSubMenu;
+	CString csMenuText;
+
+	int nCount = pMenu->GetMenuItemCount();
+	for(int i = 0; i < nCount; i++)
+	{
+		pMenu->GetMenuString(i, csMenuText, MF_BYPOSITION);
+
+		if(csMenuText == csLookingForMenuText)
+		{
+			nMenuPos = i;
+			return pMenu;
+		}
+
+		pSubMenu = pMenu->GetSubMenu(i);
+		if(pSubMenu)
+		{
+			CMenu *pMenuReturn = GetMenuPos(pSubMenu, csLookingForMenuText, nMenuPos);
+			if(pMenuReturn)
+				return pMenuReturn;
+		}
+	}
+
+	return NULL;
+}
+
+bool CMultiLanguage::LoadLanguageFile(CString csFile)
+{
+	m_csLastError = "";
+	
+	CString csPath = CGetSetOptions::GetExeFileName();
+	csPath = GetFilePath(csPath);
+	csPath += "language\\";
+	csPath += csFile;
+	csPath += ".xml";
+
+	ClearArrays();
+
+	if(csFile.GetLength() <= 0)
+	{
+		m_csLastError = "Language file is blank";
+		return false;
+	}
+
+	TiXmlDocument doc(csPath);
+	if(!doc.LoadFile())
+	{
+		m_csLastError.Format("Error loading file %s - reasion = %s", csFile, doc.ErrorDesc());
+		Log(m_csLastError);
+		return false;
+	}
+
+	TiXmlElement *ItemHeader = doc.FirstChildElement("Ditto_Language_File");
+	if(!ItemHeader)
+	{
+		m_csLastError.Format("Error finding the section %s", "Ditto_Language_File");
+		ASSERT(!m_csLastError);
+		Log(m_csLastError);
+		return false;
+	}
+
+	CString csVersion = ItemHeader->Attribute("Version");
+	m_lFileVersion = atoi(csVersion);
+	m_csAuthor = ItemHeader->Attribute("Author");
+	m_csNotes = ItemHeader->Attribute("Notes");
+
+	if(m_bOnlyGetHeader)
+		return true;
+
+	bool bRet = LoadSection(*ItemHeader, m_RightClickMenu, "Ditto_Right_Click_Menu");
+	bRet = LoadSection(*ItemHeader, m_ClipProperties, "Ditto_Clip_Properties");
+	bRet = LoadSection(*ItemHeader, m_OptionsGeneral, "Ditto_Options_General");
+	bRet = LoadSection(*ItemHeader, m_OptionsSupportedTypes, "Ditto_Options_Supported_Types");
+	bRet = LoadSection(*ItemHeader, m_OptionsShortcuts, "Ditto_Options_Shortcuts");
+	bRet = LoadSection(*ItemHeader, m_OptionsQuickPaste, "Ditto_Options_Quick_Paste");
+	bRet = LoadSection(*ItemHeader, m_OptionsFriends, "Ditto_Options_Friends");
+	bRet = LoadSection(*ItemHeader, m_OptionsFriendsDetail, "Ditto_Options_Friends_Detail");
+	bRet = LoadSection(*ItemHeader, m_OptionsStats, "Ditto_Options_Stats");
+	bRet = LoadSection(*ItemHeader, m_OptionsSupportedTypesAdd, "Ditto_Options_Supported_Types_Add");
+	bRet = LoadSection(*ItemHeader, m_MoveToGroups, "Ditto_Move_To_Groups");
+	bRet = LoadSection(*ItemHeader, m_OptionsSheet, "Ditto_Options_Sheet");
+	bRet = LoadSection(*ItemHeader, m_TrayIconRightClickMenu, "Ditto_Tray_Icon_Menu");
+	
+	bRet = LoadStringTableSection(*ItemHeader, m_StringMap, "Ditto_String_Table");
+
+	return true;
+}
+
+bool CMultiLanguage::LoadSection(TiXmlNode &doc, LANGUAGE_ARRAY &Array, CString csSection)
+{
+	TiXmlNode *node = doc.FirstChild(csSection);
+	if(!node)
+	{
+		m_csLastError.Format("Error finding the section %s", csSection);
+		ASSERT(!m_csLastError);
+		Log(m_csLastError);
+		return false;
+	}
+
+	TiXmlNode* ForeignNode;
+	CString csID;
+		
+	TiXmlElement *ItemElement = node->FirstChildElement();
+
+	//load all items for this section
+	//they look like
+	//<Item English_Text = "Use Ctrl - Num" ID= "32777"></Item>
+	while(ItemElement)
+	{
+		CLangItem *plItem = new CLangItem;
+
+		plItem->m_csEnglishLang = ItemElement->Attribute("English_Text");
+		csID = ItemElement->Attribute("ID");
+		plItem->m_nID = atoi(csID);
+
+		ForeignNode = ItemElement->FirstChild();
+		if(ForeignNode)
+		{
+			plItem->m_csForeignLang = ForeignNode->Value();
+		}
+
+		Array.Add(plItem);
+
+		ItemElement = ItemElement->NextSiblingElement();
+	}
+	
+	return true;
+}
+
+bool CMultiLanguage::LoadStringTableSection(TiXmlNode &doc, LANGUAGE_MAP &Map, CString csSection)
+{
+	TiXmlNode *node = doc.FirstChild(csSection);
+	if(!node)
+	{
+		CString cs;
+		cs.Format("Error finding the section %s", csSection);
+		ASSERT(!cs);
+		Log(cs);
+		return false;
+	}
+
+	TiXmlNode* ForeignNode;
+		
+	TiXmlElement *ItemElement = node->FirstChildElement();
+
+	//load all items for this section
+	//they look like
+	//<Item English_Text = "Use Ctrl - Num" ID= "32777"></Item>
+	while(ItemElement)
+	{
+		CLangItem *plItem = new CLangItem;
+
+		plItem->m_csEnglishLang = ItemElement->Attribute("English_Text");
+		plItem->m_csID = ItemElement->Attribute("ID");
+
+		ForeignNode = ItemElement->FirstChild();
+		if(ForeignNode) 
+		{
+			plItem->m_csForeignLang = ForeignNode->Value();
+		}
+
+		Map.SetAt(plItem->m_csID, plItem);
+
+		ItemElement = ItemElement->NextSiblingElement();
+	}
+
+	return true;
+}

+ 106 - 0
MultiLanguage.h

@@ -0,0 +1,106 @@
+// MultiLanguage.h: interface for the CMultiLanguage class.
+//
+//////////////////////////////////////////////////////////////////////
+
+#if !defined(AFX_MULTILANGUAGE_H__DA57BA64_C421_4368_9498_1EFCE49A5C52__INCLUDED_)
+#define AFX_MULTILANGUAGE_H__DA57BA64_C421_4368_9498_1EFCE49A5C52__INCLUDED_
+
+#include "Tinyxml.h"
+#include "tinystr.h"
+
+#if _MSC_VER > 1000
+#pragma once
+#endif // _MSC_VER > 1000
+
+class CLangItem
+{
+public:
+	CLangItem()
+	{
+		m_nID = -1;
+		m_csEnglishLang = "";
+		m_csForeignLang = "";
+		m_csID = "";
+	}
+	int m_nID;
+	CString m_csID;
+	CString m_csEnglishLang;
+	CString m_csForeignLang;
+};
+
+typedef CArray<CLangItem*, CLangItem*> LANGUAGE_ARRAY;
+
+typedef CMap<CString, LPCSTR, CLangItem*, CLangItem*> LANGUAGE_MAP;
+
+class CMultiLanguage  
+{
+public:
+	CMultiLanguage();
+	virtual ~CMultiLanguage();
+
+	bool LoadLanguageFile(CString csFile);
+
+	bool UpdateRightClickMenu(CMenu *pMenu);
+	bool UpdateTrayIconRightClickMenu(CMenu *pMenu);
+
+	bool UpdateClipProperties(CWnd *pParent);
+	bool UpdateOptionGeneral(CWnd *pParent);
+	bool UpdateOptionSupportedTypes(CWnd *pParent);
+	bool UpdateOptionShortcuts(CWnd *pParent);
+	bool UpdateOptionQuickPaste(CWnd *pParent);
+	bool UpdateOptionFriends(CWnd *pParent);
+	bool UpdateOptionFriendsDetail(CWnd *pParent);
+	bool UpdateOptionStats(CWnd *pParent);
+	bool UpdateOptionSupportedTypesAdd(CWnd *pParent);
+	bool UpdateMoveToGroups(CWnd *pParent);
+	bool UpdateOptionsSheet(CWnd *pParent);
+
+	CString GetString(CString csID, CString csDefault);
+
+	CString GetAuthor()	{ return m_csAuthor;		}
+	long	GetVersion(){ return m_lFileVersion;	}
+	CString GetNotes()	{ return m_csNotes;			}
+
+	void	SetOnlyGetHeader(bool bVal)	{ m_bOnlyGetHeader = true;	}
+
+	CString m_csLastError;
+
+protected:
+	LANGUAGE_ARRAY m_RightClickMenu;
+	LANGUAGE_ARRAY m_TrayIconRightClickMenu;
+	LANGUAGE_ARRAY m_ClipProperties;
+	LANGUAGE_ARRAY m_OptionsGeneral;
+	LANGUAGE_ARRAY m_OptionsSupportedTypes;
+	LANGUAGE_ARRAY m_OptionsShortcuts;
+	LANGUAGE_ARRAY m_OptionsQuickPaste;
+	LANGUAGE_ARRAY m_OptionsFriends;
+	LANGUAGE_ARRAY m_OptionsFriendsDetail;
+	LANGUAGE_ARRAY m_OptionsStats;
+	LANGUAGE_ARRAY m_OptionsSupportedTypesAdd;
+	LANGUAGE_ARRAY m_MoveToGroups;
+	LANGUAGE_ARRAY m_OptionsSheet;
+
+	LANGUAGE_MAP	m_StringMap;
+
+	CString m_csAuthor;
+	CString m_csNotes;
+	long	m_lFileVersion;
+
+	bool	m_bOnlyGetHeader;
+
+protected:
+	bool LoadSection(TiXmlNode &doc, LANGUAGE_ARRAY &Array, CString csSection);
+	bool LoadStringTableSection(TiXmlNode &doc, LANGUAGE_MAP &Map, CString csSection);
+
+	CMenu* GetMenuPos(CMenu *pMenu, const CString &csLookingForMenuText, int &nMenuPos);
+
+	bool UpdateMenuToLanguage(CMenu *pMenu, LANGUAGE_ARRAY &Array);
+	bool UpdateWindowToLanguage(CWnd *pParent, LANGUAGE_ARRAY &Array);
+
+
+	void ClearArrays();
+	void ClearArray(LANGUAGE_ARRAY &Array);
+	void ClearMap(LANGUAGE_MAP &Map);
+};
+
+#endif // !defined(AFX_MULTILANGUAGE_H__DA57BA64_C421_4368_9498_1EFCE49A5C52__INCLUDED_)