MultiLanguage.cpp 11 KB

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