C#中文簡繁轉換的類

簡體中文 (Simplified Chinese)和繁體中文(傳統漢字,Traditional Chinese)之間的轉換,單純使用字字對照轉換是絕對無法準確的。

所以,在使用下面的方法進行了 C# 簡繁轉換後,務必對轉換結果進行修正,才能保證可以使用。

修正主要包括:

1、一對多關係時根據詞義換用字; 2、兩岸(或港臺和內地)詞彙差異,如「網上鄰居」和「網路上的芳鄰」、「最終用戶許可協議」和「使用者授權合約」。

推薦使用 OpenCC 進行轉換,而不是本文的代碼。

    public static class SystemChineseConverter
    {
        internal const int LOCALE_SYSTEM_DEFAULT = 0x0800;
        internal const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
        internal const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;

        [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
        internal static extern int LCMapString(int Locale, int dwMapFlags, string lpSrcStr, int cchSrc, [Out] string lpDestStr, int cchDest);

        public static string ToSimplified(string pSource)
        {
            String tTarget = new String(' ', pSource.Length);
            int tReturn = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_SIMPLIFIED_CHINESE, pSource, pSource.Length, tTarget, pSource.Length);
            return tTarget;
        }

        public static string ToTraditional(string pSource)
        {
            String tTarget = new String(' ', pSource.Length);
            int tReturn = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_TRADITIONAL_CHINESE, pSource, pSource.Length, tTarget, pSource.Length);
            return tTarget;
        }
    }
当前页阅读量为: