使用微软的 Visual Studio International Pack 1.0 进行网站简体与繁体的互转和得到汉字、拼音、笔画等相关信息

2009年3月31日,微软发布了 Microsoft Visual Studio International Pack 1.0 SR1。Visual Studio International Pack 包含一组类库,该类库扩展了.NET Framework对全球化软件开发的支持。使用该类库提供的类,.NET 开发人员可以更方便的创建支持多文化多语言的软件应用。下载地址是:http://www.microsoft.com/downloads/details.aspx?FamilyID=44cac7f0-633b-477d-aed2-99aee642fc10&DisplayLang=zh-cn  该软件包1.0版提供下面七个组件以增强.NET Framework对全球化软件应用开发的支持。

  1. East Asia Numeric Formatting Library - 支持将小写的数字字符串格式化成简体中文,繁体中文,日文和韩文的大写数字字符串。
  2. Japanese Kana Conversion Library - 支持将日文假名(Kana)转化为另一种日文字符。
  3. Japanese Text Alignment Library - 支持日文特有的一种对齐格式。
  4. Japanese Yomi Auto-Completion Library - 类库支持感知日文输入法的输入自动完成和一个文本框控件的示例。
  5. Korean Auto Complete TextBox Control - 支持韩文输入法的智能感知和输入自动完成的文本框控件。
  6. Simplified Chinese Pin-Yin Conversion Library - 支持获取简体中文字符的常用属性比如拼音,多音字,同音字,笔画数。
  7. Traditional Chinese to Simplified Chinese Conversion Library and Add-In Tool - 支持简繁体中文之间的转换。该组件还包含一个Visual Studio集成开发环境中的插件(Add-in)支持简繁体中文资源文件之间的转换。

 

下载完毕后,进行解压,里面有7个msi文件,其中:CHSPinYinConv.msi就是汉字拼音相关的,CHTCHSConv.msi是进行简体与繁体进行相互转换的,值安装这2个msi即可。安装完毕后,需要在VS里添加引用,分别引用:C:\Program Files\Microsoft Visual Studio International Pack\Simplified Chinese Pin-Yin Conversion Library下和C:\Program Files\Microsoft Visual Studio International Pack\Traditional Chinese to Simplified Chinese Conversion Library and Add-In Tool下的dll即可使用。下面是一个汉字“孟”的测试例子:

 

C# 代码
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.International.Converters.PinYinConverter;
using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;
using System.Collections.ObjectModel;


public partial class Default2 : System.Web.UI.Page
{

  
protected void Page_Load(object sender, EventArgs e)
  {
    ChineseChar chineseChar
= new ChineseChar('');
    Response.Write(
"<h2>关于汉字 “孟” 的相关信息:</h2>");
    Response.Write(
"<li>“孟”笔画数:" + ChineseChar.GetStrokeNumber(''));
    Response.Write(
"<li>“孟”是否是多音字: " + chineseChar.IsPolyphone);
    ReadOnlyCollection
<string> pinyin = chineseChar.Pinyins;
    Response.Write(
"<li>“孟”所有拼音:");
    
foreach (string pin in pinyin)
    {
      Response.Write(pin
+ ",");
    }

    
char[] ChineseChars;

    Response.Write(
"<li>所有拼音 MENG1 的汉字<br/>");
    ChineseChars
= ChineseChar.GetChars("MENG1");
    Response.Write(
"" + ChineseChars.Length.ToString() + "个,分别是:");
    
for (int i = 0; i < ChineseChars.Length; i++)
    {
      Response.Write(ChineseChars[i]
+ ",");
    }

    Response.Write(
"<li>所有拼音 MENG2 的汉字 <br/>");

    ChineseChars
= ChineseChar.GetChars("MENG2");
    Response.Write(
"" + ChineseChars.Length.ToString() + "个,分别是:");
    
for (int i = 0; i < ChineseChars.Length; i++)
    {
      Response.Write(ChineseChars[i]);
    }

    Response.Write(
"<li>所有拼音 MENG3 的汉字<br/>");
    ChineseChars
= ChineseChar.GetChars("MENG3");
    Response.Write(
"" + ChineseChars.Length.ToString() + "个,分别是:");
    
for (int i = 0; i < ChineseChars.Length; i++)
    {
      Response.Write(ChineseChars[i]);
    }
    Response.Write(
"<li>所有拼音 MENG2 的汉字<br/>");
    ChineseChars
= ChineseChar.GetChars("MENG4");
    Response.Write(
"" + ChineseChars.Length.ToString() + "个,分别是:");
    
for (int i = 0; i < ChineseChars.Length; i++)
    {
      Response.Write(ChineseChars[i]);
    }
    Response.Write(
"<li>所有拼音 MENG5 的汉字<br/>");
    ChineseChars
= ChineseChar.GetChars("MENG5");
    Response.Write(
"" + ChineseChars.Length.ToString() + "个,分别是:");
    
for (int i = 0; i < ChineseChars.Length; i++)
    {
      Response.Write(ChineseChars[i]);
    }

    String simText
= "欢迎光临【孟子E章】";
    String traText
= ChineseConverter.Convert(simText, ChineseConversionDirection.SimplifiedToTraditional);
    Response.Write(
"<li>繁简转换测试:" + traText);
  }
}

 

得到的结果是:
 

关于汉字 “孟” 的相关信息:

  • “孟”笔画数:8
  • “孟”是否是多音字: True
  • “孟”所有拼音:MENG4,MENG5,,,,,,,
  • 所有拼音 MENG1 的汉字
    共4个,分别是:擝,濛,矇,蒙,
  • 所有拼音 MENG2 的汉字
    共45个,分别是:儚冡夣嫇尨幪庬懜懞掹曚朦橗檬氋氓溕濛獴甍甿盟瞢矇矒礞艨莔萌蒙蕄虻蝱鄳鄸雺霚霿靀顭饛鯍鸏鹲鼆
  • 所有拼音 MENG3 的汉字
    共16个,分别是:勐幪懜懵猛瓾矇艋蒙蜢蠓錳锰鯭黽鼆
  • 所有拼音 MENG2 的汉字
    共6个,分别是:夢夣孟懜梦霥
  • 所有拼音 MENG5 的汉字
    共61个,分别是:儚冡勐夢夣嫇孟尨幪庬懜懞懵掹擝曚朦梦橗檬氋氓溕濛猛獴瓾甍甿盟瞢矇矒礞艋艨莔萌蒙蕄虻蜢蝱蠓鄳鄸錳锰雺霚霥霿靀顭饛鯍鯭鸏鹲黽鼆
  • 繁简转换测试:歡迎光臨【孟子E章】

 

 不过:有几个方面主要注意:

  1. 不清楚这个工具参照的是哪个汉字标准,SDK上也没有看到说明,“孟”这个字在字典中并不是多音字;
  2. 拼音后面的1,2,3,4,5猜测应当是声调标记:阴平,阳平,上声,去声,轻声。具体是否是有待查证;

中文简体与中文繁体之间的转换比较简单,下面就是一个将简体转换成繁体的代码:

 

C# 代码
String simText = "欢迎光临【孟子E章】";
String traText
= ChineseConverter.Convert(simText, ChineseConversionDirection.SimplifiedToTraditional);
Response.Write(
"<li>繁简转换测试:" + traText);

 

下面是Microsoft.International.Converters.PinYinConverter.ChineseChar类的成员:

封装了简体中文的读音和笔画等基本信息。

成员类型 成员 描述
  ChineseCharNew(Char)
ChineseChar类的构造函数。
  ChineseCharacter
获取这个汉字字符。
  CompareStrokeNumber(Char)
将给出的字符和实例字符的笔画数进行比较。
静态成员 GetCharCount(Int16)
检索具有指定笔画数的字符个数。
静态成员 GetChars(String)
获取给定拼音的所有同音字。
静态成员 GetChars(Int16)
检索具有指定笔画数的所有字符串。
静态成员 GetHomophoneCount(String)
检索具有指定拼音的字符数。
静态成员 GetStrokeNumber(Char)
检索指定字符的笔画数。
  HasSound(String)
识别字符是否有指定的读音。
  IsHomophone(Char)
识别给出的字符是否是实例字符的同音字。
静态成员 IsHomophone(Char, Char)
识别给出的两个字符是否是同音字。
  IsPolyphone
获取这个字符是否是多音字。
静态成员 IsValidChar(Char)
识别给出的字符串是否是一个有效的汉字字符。
静态成员 IsValidPinyin(String)
识别给出的拼音是否是一个有效的拼音字符串。
静态成员 IsValidStrokeNumber(Int16)
识别给出的笔画数是否是一个有效的笔画数。
  PinyinCount
获取这个字符的拼音个数。
  Pinyins
获取这个字符的拼音。
  StrokeNumber
获取这个字符的笔画数。

其他信息请参与随机安装的chm帮助文件。

来源:http://dotnet.aspx.cc/article/47aa2fc3-ff55-4a57-9491-cc05f555ea3c/read.aspx

加支付宝好友偷能量挖...


评论(0)网络
阅读(90)喜欢(0)Asp.Net/C#/WCF