2013年12月10日 星期二

繁簡轉換Function(C#)

最近有需求是若前面輸入的字串為簡體編碼則先轉換成繁體至資料庫查詢,最後結果呈現上,查出的資料庫裡的繁體資料須置換成簡體後秀出。

我找到一個很不錯的一個範例網站:http://jian-zhoung.blogspot.tw/2012/07/c.html

上述範例網站,介紹到四種方式繁簡轉換,我以這四種為例寫成一個Class檔,其他頁面統一呼叫此Class來達成。

第一步:加入一個名為「LanguageTool.cs」Class。

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Web;
   5: using System.Text;
   6:  
   7: namespace UDP2013
   8: {
   9:     public class LanguageTool
  10:     {
  11:  
  12:         public LanguageTool()
  13:         { 
  14:         
  15:         }
  16:     }
  17: }

第二步:加入一個判斷是否為簡體編碼的程式,名為「IsGBCode」,傳入變數為欲判斷的字串,回傳bool變數。Function如下:



   1: /// <summary>
   2: /// 判斷是否為GB2312編碼
   3: /// </summary>
   4: /// <param name="word"></param>
   5: /// <returns></returns>
   6: public bool IsGBCode(string word)
   7: {
   8:     byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(word);
   9:     // if there is only one byte, it is ASCII code or other code
  10:     if (bytes.Length <= 1)
  11:     {
  12:         return false;
  13:     }
  14:     else
  15:     {
  16:         byte byte1 = bytes[0];
  17:         byte byte2 = bytes[1];
  18:         //判斷是否是GB2312
  19:         if (byte1 >= 176 && byte1 <= 247 && byte2 >= 160 && byte2 <= 254)
  20:         {
  21:             return true;
  22:         }
  23:         else
  24:         {
  25:             return false;
  26:         }
  27:     }
  28: }

第三步:加入繁簡體轉換Function,四種方式皆一步一步列出,如下:


**Kernel32 LCMapString**


須先加入「System.Runtime.InteropServices」參考。



   1: using System.Runtime.InteropServices;

相關Function如下:



   1: #region ---使用Kernel32 LCMapString轉換---
   2:  
   3:  internal const int LOCALE_SYSTEM_DEFAULT = 0x0800;
   4:  internal const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
   5:  internal const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;
   6:  
   7:  [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
   8:  internal static extern int LCMapString(int Locale, int dwMapFlags, string lpSrcStr, int cchSrc, [Out] string lpDestStr, int cchDest);
   9:  
  10:  public string ToSimplified(string source)
  11:  {
  12:      String target = new String(' ', source.Length);
  13:      int ret = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_SIMPLIFIED_CHINESE, source, source.Length, target, source.Length);
  14:      return target;
  15:  }
  16:  
  17:  public string ToTraditional(string source)
  18:  {
  19:      String target = new String(' ', source.Length);
  20:      int ret = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_TRADITIONAL_CHINESE, source, source.Length, target, source.Length);
  21:      return target;
  22:  }
  23:  
  24:  #endregion

**Microsoft.Office.Interop.Word**


先在專案檔案中引用Com元件:Microsoft Word 14.0 Object Library。


AddMSWord


接著在程式檔案中加入「Microsoft.Office.Interop.Word」參考。



   1: using Microsoft.Office.Interop.Word;

最後附上Function,如下:



   1: /// <summary>
   2: /// 使用Microsoft.Office.Interop.Word轉換
   3: /// </summary>
   4: /// <param name="argSource"></param>
   5: /// <param name="argIsCht"></param>
   6: /// <returns></returns>
   7: public string ConvertUsingWord(string argSource, bool argIsCht)
   8: {
   9:  
  10:     var doc = new Document();
  11:     doc.Content.Text = argSource;
  12:     doc.Content.TCSCConverter(
  13:         argIsCht
  14:             ? WdTCSCConverterDirection.wdTCSCConverterDirectionTCSC
  15:             : WdTCSCConverterDirection.wdTCSCConverterDirectionSCTC, true, true);
  16:  
  17:     var ret = doc.Content.Text;
  18:     object saveChanges = false;
  19:     object originalFormat = Missing.Value;
  20:     object routeDocument = Missing.Value;
  21:     doc.Close(ref saveChanges, ref originalFormat, ref routeDocument);
  22:  
  23:     return ret;
  24: }

**Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter**


要先在網路上下載「ChineseConverter.dll」、「ChnCharInfo.dll」參考至專案中。接著再程式檔案中加入參考。



   1: using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;
   2: using Microsoft.International.Converters.PinYinConverter;

**Microsoft.VisualBasic**


首先需引用「MicrosoftVisualBasic」元件,如下。


AddMSVB


接著在程式檔案中加入參考,如下:



   1: using Microsoft.VisualBasic;

最後附上方法程式碼的部分,如下:



   1: /// <summary>
   2: /// 使用Microsoft.VisualBasic轉換
   3: /// </summary>
   4: /// <param name="argSource"></param>
   5: /// <param name="IsTraditional"></param>
   6: /// <returns></returns>
   7: public string ConvertUsingVB(string argSource, bool IsTraditional)
   8: {
   9:     string Rtnstr = "";
  10:     switch (IsTraditional)
  11:     {
  12:         case true://表示要轉換成繁體
  13:             //zh-TW (0x0404), zh-CN (0x0840), ko-KR (0x0412), en (0x0009)
  14:             Rtnstr = Strings.StrConv(argSource, VbStrConv.TraditionalChinese, 0x0404);
  15:             break;
  16:         case false://表示要轉換成簡體
  17:             //zh-TW (0x0404), zh-CN (0x0840), ko-KR (0x0412), en (0x0009)
  18:             Rtnstr = Strings.StrConv(argSource, VbStrConv.SimplifiedChinese, 0x0404);
  19:             break;
  20:     }
  21:  
  22:     return Rtnstr;
  23: }

使用方法(以第三種ChineseConverter為例):



   1: LanguageTool LanTool = new LanguageTool();
   2: string TBoxTxt = curTextBox.Text.Trim();
   3: if(LanTool.IsGBCode(curTextBox.Text.Trim()))
   4: {
   5:     TBoxTxt = LanTool.ConvertUsingCConverter(TBoxTxt,true);
   6: }

上面方法先宣告LanguageTool的物件,接著使用此物件判斷是否為簡體編碼,若是則進入判斷式中將字串轉為繁體。若轉為簡體則相反,將第二個參數設為false即可。


最後附上原始碼檔案連結


https://drive.google.com/file/d/0ByqUJS6xlP4xNkNIb3htOWdFOU0/edit?usp=sharing

沒有留言:

張貼留言