MultiLanguage.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // MultiLanguage.cpp: implementation of the CMultiLanguage class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "cp_main.h"
  6. #include "MultiLanguage.h"
  7. #include "TextConvert.h"
  8. #include "TextConvert.h"
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char THIS_FILE[]=__FILE__;
  12. #define new DEBUG_NEW
  13. #endif
  14. //////////////////////////////////////////////////////////////////////
  15. // Construction/Destruction
  16. //////////////////////////////////////////////////////////////////////
  17. CMultiLanguage::CMultiLanguage()
  18. {
  19. m_csAuthor = "";
  20. m_lFileVersion = 0;
  21. m_bOnlyGetHeader = false;
  22. }
  23. CMultiLanguage::~CMultiLanguage()
  24. {
  25. ClearArrays();
  26. }
  27. void CMultiLanguage::ClearArrays()
  28. {
  29. m_csAuthor = "";
  30. m_lFileVersion = 0;
  31. m_bOnlyGetHeader = false;
  32. ClearArray(m_RightClickMenu);
  33. ClearArray(m_ClipProperties);
  34. ClearArray(m_OptionsGeneral);
  35. ClearArray(m_OptionsSupportedTypes);
  36. ClearArray(m_OptionsShortcuts);
  37. ClearArray(m_OptionsQuickPaste);
  38. ClearArray(m_OptionsFriends);
  39. ClearArray(m_OptionsFriendsDetail);
  40. ClearArray(m_OptionsStats);
  41. ClearArray(m_OptionsSupportedTypesAdd);
  42. ClearArray(m_MoveToGroups);
  43. ClearArray(m_TrayIconRightClickMenu);
  44. ClearArray(m_OptionsSheet);
  45. ClearMap(m_StringMap);
  46. }
  47. void CMultiLanguage::ClearArray(LANGUAGE_ARRAY &Array)
  48. {
  49. int nSize = Array.GetSize();
  50. for(int i = 0; i < nSize; i++)
  51. {
  52. CLangItem *plItem = Array[i];
  53. delete plItem;
  54. plItem = NULL;
  55. }
  56. Array.RemoveAll();
  57. }
  58. void CMultiLanguage::ClearMap(LANGUAGE_MAP &Map)
  59. {
  60. POSITION pos = Map.GetStartPosition();
  61. CLangItem *plItem;
  62. CString csKey;
  63. while(pos)
  64. {
  65. Map.GetNextAssoc(pos, csKey, plItem);
  66. if(plItem)
  67. {
  68. delete plItem;
  69. plItem = NULL;
  70. }
  71. }
  72. Map.RemoveAll();
  73. }
  74. CString CMultiLanguage::GetString(CString csID, CString csDefault)
  75. {
  76. CLangItem *pItem;
  77. if(m_StringMap.Lookup(csID, pItem) == FALSE)
  78. {
  79. return csDefault;
  80. }
  81. if(pItem->m_csForeignLang.GetLength() <= 0)
  82. return csDefault;
  83. return pItem->m_csForeignLang;
  84. return "";
  85. }
  86. bool CMultiLanguage::UpdateRightClickMenu(CMenu *pMenu)
  87. {
  88. return UpdateMenuToLanguage(pMenu, m_RightClickMenu);
  89. }
  90. bool CMultiLanguage::UpdateTrayIconRightClickMenu(CMenu *pMenu)
  91. {
  92. return UpdateMenuToLanguage(pMenu, m_TrayIconRightClickMenu);
  93. }
  94. bool CMultiLanguage::UpdateClipProperties(CWnd *pParent)
  95. {
  96. return UpdateWindowToLanguage(pParent, m_ClipProperties);
  97. }
  98. bool CMultiLanguage::UpdateOptionGeneral(CWnd *pParent)
  99. {
  100. return UpdateWindowToLanguage(pParent, m_OptionsGeneral);
  101. }
  102. bool CMultiLanguage::UpdateOptionSupportedTypes(CWnd *pParent)
  103. {
  104. return UpdateWindowToLanguage(pParent, m_OptionsSupportedTypes);
  105. }
  106. bool CMultiLanguage::UpdateOptionShortcuts(CWnd *pParent)
  107. {
  108. return UpdateWindowToLanguage(pParent, m_OptionsShortcuts);
  109. }
  110. bool CMultiLanguage::UpdateOptionQuickPaste(CWnd *pParent)
  111. {
  112. return UpdateWindowToLanguage(pParent, m_OptionsQuickPaste);
  113. }
  114. bool CMultiLanguage::UpdateOptionFriends(CWnd *pParent)
  115. {
  116. return UpdateWindowToLanguage(pParent, m_OptionsFriends);
  117. }
  118. bool CMultiLanguage::UpdateOptionFriendsDetail(CWnd *pParent)
  119. {
  120. return UpdateWindowToLanguage(pParent, m_OptionsFriendsDetail);
  121. }
  122. bool CMultiLanguage::UpdateOptionStats(CWnd *pParent)
  123. {
  124. return UpdateWindowToLanguage(pParent, m_OptionsStats);
  125. }
  126. bool CMultiLanguage::UpdateOptionSupportedTypesAdd(CWnd *pParent)
  127. {
  128. return UpdateWindowToLanguage(pParent, m_OptionsSupportedTypesAdd);
  129. }
  130. bool CMultiLanguage::UpdateMoveToGroups(CWnd *pParent)
  131. {
  132. return UpdateWindowToLanguage(pParent, m_MoveToGroups);
  133. }
  134. bool CMultiLanguage::UpdateOptionsSheet(CWnd *pParent)
  135. {
  136. return UpdateWindowToLanguage(pParent, m_OptionsSheet);
  137. }
  138. bool CMultiLanguage::UpdateMenuToLanguage(CMenu *pMenu, LANGUAGE_ARRAY &Array)
  139. {
  140. int nSize = Array.GetSize();
  141. for(int i = 0; i < nSize; i++)
  142. {
  143. CLangItem *plItem = Array[i];
  144. if(plItem->m_csForeignLang.GetLength() > 0)
  145. {
  146. if(plItem->m_nID > 0)
  147. {
  148. pMenu->ModifyMenu(plItem->m_nID, MF_BYCOMMAND, plItem->m_nID, plItem->m_csForeignLang);
  149. }
  150. else
  151. {
  152. //If an item doesn't have a menu id then its a group menu
  153. //just search for the text and update the text with the foreign text
  154. int nMenuPos;
  155. CMenu *pNewMenu = GetMenuPos(pMenu, plItem->m_csEnglishLang, nMenuPos);
  156. if(pNewMenu)
  157. {
  158. pNewMenu->ModifyMenu(nMenuPos, MF_BYPOSITION, -1, plItem->m_csForeignLang);
  159. }
  160. }
  161. }
  162. }
  163. return true;
  164. }
  165. bool CMultiLanguage::UpdateWindowToLanguage(CWnd *pParent, LANGUAGE_ARRAY &Array)
  166. {
  167. int nSize = Array.GetSize();
  168. for(int i = 0; i < nSize; i++)
  169. {
  170. CLangItem *plItem = Array[i];
  171. if(plItem->m_csForeignLang.GetLength() > 0)
  172. {
  173. if(plItem->m_nID > 0)
  174. {
  175. CWnd *pWnd = pParent->GetDlgItem(plItem->m_nID);
  176. if(pWnd)
  177. {
  178. pWnd->SetWindowText(plItem->m_csForeignLang);
  179. }
  180. }
  181. //If item id is -1 then set the title for the dialog
  182. else if(plItem->m_nID == -1)
  183. {
  184. pParent->SetWindowText(plItem->m_csForeignLang);
  185. }
  186. }
  187. }
  188. return true;
  189. }
  190. CMenu * CMultiLanguage::GetMenuPos(CMenu *pMenu, const CString &csLookingForMenuText, int &nMenuPos)
  191. {
  192. CMenu *pSubMenu;
  193. CString csMenuText;
  194. int nCount = pMenu->GetMenuItemCount();
  195. for(int i = 0; i < nCount; i++)
  196. {
  197. pMenu->GetMenuString(i, csMenuText, MF_BYPOSITION);
  198. if(csMenuText == csLookingForMenuText)
  199. {
  200. nMenuPos = i;
  201. return pMenu;
  202. }
  203. pSubMenu = pMenu->GetSubMenu(i);
  204. if(pSubMenu)
  205. {
  206. CMenu *pMenuReturn = GetMenuPos(pSubMenu, csLookingForMenuText, nMenuPos);
  207. if(pMenuReturn)
  208. return pMenuReturn;
  209. }
  210. }
  211. return NULL;
  212. }
  213. bool CMultiLanguage::LoadLanguageFile(CString csFile)
  214. {
  215. m_csLastError = "";
  216. CString csPath = CGetSetOptions::GetPath(PATH_LANGUAGE);
  217. csPath += csFile;
  218. csPath += ".xml";
  219. ClearArrays();
  220. if(csFile.GetLength() <= 0)
  221. {
  222. m_csLastError = "Language file is blank";
  223. return false;
  224. }
  225. CStringA csPathA = CTextConvert::ConvertToChar(csPath);
  226. TiXmlDocument doc(csPathA);
  227. if(!doc.LoadFile())
  228. {
  229. m_csLastError.Format(_T("Error loading file %s - reason = %s"), csFile, doc.ErrorDesc());
  230. Log(m_csLastError);
  231. return false;
  232. }
  233. TiXmlElement *ItemHeader = doc.FirstChildElement("Ditto_Language_File");
  234. if(!ItemHeader)
  235. {
  236. m_csLastError.Format(_T("Error finding the section Ditto_Language_File"));
  237. ASSERT(!m_csLastError);
  238. Log(m_csLastError);
  239. return false;
  240. }
  241. CString csVersion = ItemHeader->Attribute("Version");
  242. m_lFileVersion = ATOI(csVersion);
  243. m_csAuthor = ItemHeader->Attribute("Author");
  244. m_csNotes = ItemHeader->Attribute("Notes");
  245. if(m_bOnlyGetHeader)
  246. return true;
  247. bool bRet = LoadSection(*ItemHeader, m_RightClickMenu, "Ditto_Right_Click_Menu");
  248. bRet = LoadSection(*ItemHeader, m_OptionsGeneral, "Ditto_Options_General");
  249. bRet = LoadSection(*ItemHeader, m_ClipProperties, "Ditto_Clip_Properties");
  250. bRet = LoadSection(*ItemHeader, m_OptionsSupportedTypes, "Ditto_Options_Supported_Types");
  251. bRet = LoadSection(*ItemHeader, m_OptionsShortcuts, "Ditto_Options_Shortcuts");
  252. bRet = LoadSection(*ItemHeader, m_OptionsQuickPaste, "Ditto_Options_Quick_Paste");
  253. bRet = LoadSection(*ItemHeader, m_OptionsFriends, "Ditto_Options_Friends");
  254. bRet = LoadSection(*ItemHeader, m_OptionsFriendsDetail, "Ditto_Options_Friends_Detail");
  255. bRet = LoadSection(*ItemHeader, m_OptionsStats, "Ditto_Options_Stats");
  256. bRet = LoadSection(*ItemHeader, m_OptionsSupportedTypesAdd, "Ditto_Options_Supported_Types_Add");
  257. bRet = LoadSection(*ItemHeader, m_MoveToGroups, "Ditto_Move_To_Groups");
  258. bRet = LoadSection(*ItemHeader, m_OptionsSheet, "Ditto_Options_Sheet");
  259. bRet = LoadSection(*ItemHeader, m_TrayIconRightClickMenu, "Ditto_Tray_Icon_Menu");
  260. bRet = LoadStringTableSection(*ItemHeader, m_StringMap, "Ditto_String_Table");
  261. return true;
  262. }
  263. bool CMultiLanguage::LoadSection(TiXmlNode &doc, LANGUAGE_ARRAY &Array, CString csSection)
  264. {
  265. CStringA csSectionA = CTextConvert::ConvertToChar(csSection);
  266. TiXmlNode *node = doc.FirstChild(csSectionA);
  267. if(!node)
  268. {
  269. m_csLastError.Format(_T("Error finding the section %s"), csSection);
  270. ASSERT(!m_csLastError);
  271. Log(m_csLastError);
  272. return false;
  273. }
  274. TiXmlNode* ForeignNode;
  275. CString csID;
  276. CString csLineFeed("\n");
  277. TiXmlElement *ItemElement = node->FirstChildElement();
  278. //load all items for this section
  279. //they look like
  280. //<Item English_Text = "Use Ctrl - Num" ID= "32777"></Item>
  281. while(ItemElement)
  282. {
  283. ForeignNode = ItemElement->FirstChild();
  284. if(ForeignNode)
  285. {
  286. CLangItem *plItem = new CLangItem;
  287. if(plItem)
  288. {
  289. plItem->m_csEnglishLang = ItemElement->Attribute("English_Text");
  290. csID = ItemElement->Attribute("ID");
  291. plItem->m_nID = ATOI(csID);
  292. LPCSTR Value = ForeignNode->Value();
  293. CTextConvert::ConvertFromUTF8(Value, plItem->m_csForeignLang);
  294. //Replace the literal "\n" with line feeds
  295. plItem->m_csForeignLang.Replace(_T("\\n"), csLineFeed);
  296. Array.Add(plItem);
  297. }
  298. }
  299. ItemElement = ItemElement->NextSiblingElement();
  300. }
  301. return true;
  302. }
  303. bool CMultiLanguage::LoadStringTableSection(TiXmlNode &doc, LANGUAGE_MAP &Map, CString csSection)
  304. {
  305. CStringA csSectionA = CTextConvert::ConvertToChar(csSection);
  306. TiXmlNode *node = doc.FirstChild(csSectionA);
  307. if(!node)
  308. {
  309. CString cs;
  310. cs.Format(_T("Error finding the section %s"), csSection);
  311. ASSERT(!cs);
  312. Log(cs);
  313. return false;
  314. }
  315. CString csLineFeed("\n");
  316. TiXmlNode* ForeignNode;
  317. TiXmlElement *ItemElement = node->FirstChildElement();
  318. //load all items for this section
  319. //they look like
  320. //<Item English_Text = "Use Ctrl - Num" ID= "32777"></Item>
  321. while(ItemElement)
  322. {
  323. CLangItem *plItem = new CLangItem;
  324. plItem->m_csEnglishLang = ItemElement->Attribute("English_Text");
  325. plItem->m_csID = ItemElement->Attribute("ID");
  326. ForeignNode = ItemElement->FirstChild();
  327. if(ForeignNode)
  328. {
  329. LPCSTR Value = ForeignNode->Value();
  330. CTextConvert::ConvertFromUTF8(Value, plItem->m_csForeignLang);
  331. //Replace the literal "\n" with line feeds
  332. plItem->m_csForeignLang.Replace(_T("\\n"), csLineFeed);
  333. }
  334. Map.SetAt(plItem->m_csID, plItem);
  335. ItemElement = ItemElement->NextSiblingElement();
  336. }
  337. return true;
  338. }