以下是代码:
{
/// <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, string lpDestStr, int cchDest);
internal const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
internal const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;
internal const int LOCALE_SYSTEM_DEFAULT = 0x0800;
/// <summary>
/// 繁体转简体
/// </summary>
/// <param name="pSource">要转换的繁体字:體</param>
/// <returns>转换后的简体字:体</returns>
public string ToSimplified(string? pSource)
{
if (string.IsNullOrEmpty(pSource) || pSource.Length < 1)
return "";
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 string ToTraditional(string pSource)
{
if (string.IsNullOrEmpty(pSource) || pSource.Length < 1)
return "";
String tTarget = new String(' ', pSource.Length);
int tReturn = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_TRADITIONAL_CHINESE, pSource, pSource.Length, tTarget, pSource.Length);
return tTarget;
}