MultiLanguage.cpp 13 KB

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