MultiLanguage.cpp 11 KB

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