MultiLanguage.cpp 10 KB

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