MultiLanguage.cpp 9.9 KB

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