简体翻译成繁体(一)

2020-03-07  本文已影响0人  泱千澈
using System;
using System.Runtime.InteropServices;

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

    /// <summary> 
            /// 使用OS的kernel.dll做為簡繁轉換工具,只要有裝OS就可以使用,不用額外引用dll,但只能做逐字轉換,無法進行詞意的轉換 
            /// <para>所以無法將電腦轉成計算機</para> 
            /// </summary> 
    [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);

    /// <summary> 
            /// 繁体转简体
            /// </summary> 
            /// <param name="pSource">要转换的繁体字</param> 
            /// <returns>转换后的简体字</returns> 
    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;
    }

    /// <summary> 
            /// 简体转繁体 
            /// </summary> 
            /// <param name="pSource">要转换的简体字</param> 
            /// <returns>转换后的繁体字</returns> 
    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;
    }
}

ChineseConverter.ToSimplifiedChineseConverter.ToTraditional的翻译都是逐字翻译,但是简体和繁体其实有些词语的差异,这种功能这个类是没有的。

上一篇下一篇

猜你喜欢

热点阅读