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;
        }
    }

本文版权遵循 CC BY-NC-SA 4.0发布,转载需附带本文链接。

当前页阅读量为: