{"id":34963,"date":"2005-07-12T10:00:10","date_gmt":"2005-07-12T10:00:10","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/07\/12\/converting-from-traditional-to-simplified-chinese-part-2-using-the-dictionary\/"},"modified":"2005-07-12T10:00:10","modified_gmt":"2005-07-12T10:00:10","slug":"converting-from-traditional-to-simplified-chinese-part-2-using-the-dictionary","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050712-10\/?p=34963","title":{"rendered":"Converting from traditional to simplified Chinese, part 2: Using the dictionary"},"content":{"rendered":"<p><P>\nNow that we have our traditional-to-simplified pseudo-dictionary,\nwe can use it to generate simplified Chinese words in our\nChinese\/English dictionary.\n<\/P>\n<PRE>\nclass StringPool\n{\npublic:\n StringPool();\n ~StringPool();\n LPWSTR AllocString(const WCHAR* pszBegin, const WCHAR* pszEnd);\n <FONT COLOR=\"blue\">LPWSTR DupString(const WCHAR* pszBegin)\n {\n  return AllocString(pszBegin, pszBegin + lstrlen(pszBegin));\n }<\/FONT>\n &#8230;\n};\n<\/PRE>\n<P>\nThe <CODE>DupString<\/CODE> method is a convenience we will use\nbelow.\n<\/P>\n<PRE>\nDictionary::Dictionary()\n{\n &#8230;\n    if (de.Parse(buf, buf + cchResult, m_pool)) {\n     <FONT COLOR=\"blue\">bool fSimp = false;\n     for (int i = 0; de.m_pszTrad[i]; i++) {\n      if (pmap-&gt;Map(de.m_pszTrad[i])) {\n       fSimp = true;\n       break;\n      }\n     }\n     if (fSimp) {\n      de.m_pszSimp = m_pool.DupString(de.m_pszTrad);\n      for (int i = 0; de.m_pszTrad[i]; i++) {\n       if (pmap-&gt;Map(de.m_pszTrad[i])) {\n        de.m_pszSimp[i] = pmap-&gt;Map(de.m_pszTrad[i]);\n       }\n      }\n     } else {\n      de.m_pszSimp = NULL;\n     }<\/FONT>\n     v.push_back(de);\n    }\n &#8230;\n}\n<\/PRE>\n<P>\nAfter we parse each entry from the dictionary, we scan the\ntraditional Chinese characters to see if any of them have\nbeen simplified.\nIf so, then we copy the traditional Chinese string and\nuse the <CODE>Trad2Simp<\/CODE> object to convert it to\nsimplified Chinese.\n<\/P>\n<P>\nIf the string is the same in both simplified and traditional\nChinese, then we set <CODE>m_pszSimp<\/CODE> to <CODE>NULL<\/CODE>.\nThis may seem a bit odd, but it&#8217;ll come in handy later.\nYes, it makes the <CODE>m_pszSimp<\/CODE> member difficult\nto use.  I could have created an accessor function for it\n(so that it falls back to traditional Chinese if\nthe simplified Chinese is <CODE>NULL<\/CODE>),\nbut I&#8217;m feeling lazy right now, and this is just a one-shot program.\n<\/P>\n<PRE>\nvoid RootWindow::OnGetDispInfo(NMLVDISPINFO* pnmv)\n{\n &#8230;\n  switch (pnmv-&gt;item.iSubItem) {\n   case COL_TRAD:    pszResult = de.m_pszTrad;    break;\n   <FONT COLOR=\"blue\">case COL_SIMP:    pszResult =\n      de.m_pszSimp ? de.m_pszSimp : de.m_pszTrad; break;<\/FONT>\n   case COL_PINYIN:  pszResult = de.m_pszPinyin;  break;\n   case COL_ENGLISH: pszResult = de.m_pszEnglish; break;\n  }\n &#8230;\n}\n<\/PRE>\n<P>\nFinally, we tell our <CODE>OnGetDispInfo<\/CODE> handler\nwhat to return when the listview asks for the text that goes\ninto the simplified Chinese column.\nWith these changes, we can display both the traditional and\nsimplified Chinese for each entry in our dictionary.\n<\/P>\n<P>\nNext time, a minor tweak to our display code,\nwhich happens to illustrate custom-draw as a nice side-effect.\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now that we have our traditional-to-simplified pseudo-dictionary, we can use it to generate simplified Chinese words in our Chinese\/English dictionary. class StringPool { public: StringPool(); ~StringPool(); LPWSTR AllocString(const WCHAR* pszBegin, const WCHAR* pszEnd); LPWSTR DupString(const WCHAR* pszBegin) { return AllocString(pszBegin, pszBegin + lstrlen(pszBegin)); } &#8230; }; The DupString method is a convenience we will use [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-34963","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Now that we have our traditional-to-simplified pseudo-dictionary, we can use it to generate simplified Chinese words in our Chinese\/English dictionary. class StringPool { public: StringPool(); ~StringPool(); LPWSTR AllocString(const WCHAR* pszBegin, const WCHAR* pszEnd); LPWSTR DupString(const WCHAR* pszBegin) { return AllocString(pszBegin, pszBegin + lstrlen(pszBegin)); } &#8230; }; The DupString method is a convenience we will use [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/34963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=34963"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/34963\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=34963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=34963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=34963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}