欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

ASP.NET中实现中文简/繁体自动转换的类

时间:2022-07-28 11:01:12|栏目:.NET代码|点击:

          在中文网站开发中,为了满足台湾/香港等有中文繁体阅读习惯的用户,很多网站需要实现中文简/繁体转换。

当然在实际的开发中,还要注意网站的图片上如果有中文的,也需要根据当前显示的是简体,还是繁体进行切换,否则无法达到效果!在本文中,我只帖上汉字转换的代码。如果有不清楚怎么调用的,请在我的Blog下留下Email。我直接把调用代码例子发给你!

注意:如在实际应用中,发现有本例子字库以外的汉字,可以自己添加,繁体简体对应字在_sGB和_tGB中的对应位置要一致!否则转换会出问题!

          代码例子:


/// <summary>
 /// G2B5Filter 的摘要说明。
 /// 将GB2312字符和Big5字符进行对照转换
 /// 作者: 程式猎人 2003-06-24
 /// </summary>
 public class G2B5Filter : Stream
 {
  private Stream _sink;
  private long _position;

  private const string _sGB=@"皑蔼碍爱袄奥坝罢摆败颁办绊帮绑镑谤剥饱宝报鲍辈贝钡狈备惫绷笔毕毙币闭边编贬变辩辫标鳖别瘪濒滨宾摈饼并拨钵铂驳卜补财参蚕残惭惨灿苍舱仓沧厕侧册测层诧搀掺蝉馋谗缠铲产阐颤场尝长偿肠厂畅钞车彻尘陈衬撑称惩诚骋痴迟驰耻齿炽冲虫宠畴踌筹绸丑橱厨锄雏础储触处传疮闯创锤纯绰辞词赐聪葱囱从丛凑蹿窜错达带贷担单郸掸胆惮诞弹当挡党荡档捣岛祷导盗灯邓敌涤递缔颠点垫电淀钓调谍叠钉顶锭订丢东动栋冻斗犊独读赌镀锻断缎兑队对吨顿钝夺堕鹅额讹恶饿儿尔饵贰发罚阀珐矾钒烦范贩饭访纺飞诽废费纷坟奋愤粪丰枫锋风疯冯缝讽凤肤辐抚辅赋复负讣妇缚该钙盖干赶秆赣冈刚钢纲岗镐搁鸽阁铬个给龚宫巩贡钩沟构购够蛊顾剐挂关观馆惯贯广规硅归龟闺轨诡柜贵刽辊滚锅国过骇韩汉号阂鹤贺横轰鸿红后壶护沪户哗华画划话怀坏欢环还缓换唤痪焕涣黄谎挥辉毁贿秽会烩汇讳诲绘荤浑伙获货祸击机积饥迹讥鸡绩缉极辑级挤几蓟剂济计记际继纪夹荚颊贾钾价驾歼监坚笺间艰缄茧检碱硷拣捡简俭减荐槛鉴践贱见键舰剑饯渐溅涧将浆蒋桨奖讲酱胶浇骄娇搅铰矫侥脚饺缴绞轿较阶节杰洁结诫届紧锦仅谨进晋烬尽劲荆茎鲸惊经颈静镜径痉竞净纠厩旧驹举据锯惧剧鹃绢觉决诀绝钧军骏开凯颗壳课垦恳抠库裤夸块侩宽矿旷况亏岿窥馈溃扩阔蜡腊莱来赖蓝栏拦篮阑兰澜谰揽览懒缆烂滥捞劳涝乐镭垒类泪篱离里鲤礼丽厉励砾历沥隶俩联莲连镰怜涟帘敛脸链恋炼练粮凉两辆谅疗辽镣猎临邻鳞凛赁龄铃凌灵岭领馏刘龙聋咙笼垄拢陇楼娄搂篓芦卢颅庐炉"
   +"掳卤虏鲁赂禄录陆驴吕铝侣屡缕虑滤绿峦挛孪滦乱抡轮伦仑沦纶论萝罗逻锣箩骡骆络妈玛码蚂马骂吗买麦卖迈脉瞒馒蛮满谩猫锚铆贸么霉没镁门闷们锰梦谜弥觅幂绵缅庙灭悯闽鸣铭谬谋亩钠纳难挠脑恼闹馁内拟腻撵捻酿鸟聂啮镊镍柠狞宁拧泞钮纽脓浓农疟诺欧鸥殴呕沤盘庞抛赔喷鹏骗飘频贫苹凭评泼颇扑铺朴谱栖凄脐齐骑岂启气弃讫牵扦铅迁签谦钱钳潜浅谴堑枪呛墙蔷强抢锹桥乔侨翘窍窃钦亲寝轻氢倾顷请庆琼穷趋区躯驱龋颧权劝却鹊确让饶扰绕热韧认纫荣绒软锐闰润洒萨鳃赛叁伞丧骚扫涩杀纱筛晒删闪陕赡缮伤赏烧绍赊摄慑设绅审婶肾渗声绳胜圣师狮湿诗尸时蚀实识驶势适释饰视试寿兽枢输书赎属术树竖数帅双谁税顺说硕烁丝饲耸怂颂讼诵擞苏诉肃虽随绥岁孙损笋缩琐锁獭挞态摊贪瘫滩坛谭谈叹汤烫涛绦讨腾誊锑题体屉条贴铁厅听烃铜统头秃图涂团颓蜕脱鸵驮驼椭洼袜弯湾顽万网韦违围为潍维苇伟伪纬谓卫温闻纹稳问瓮挝蜗涡窝卧呜钨乌污诬无芜吴坞雾务误锡牺袭习铣戏细虾辖峡侠狭厦吓鲜纤咸贤衔闲显险现献县馅羡宪线厢镶乡详响项萧嚣销晓啸蝎协挟携胁谐写泻谢锌衅兴汹锈绣虚嘘须许叙绪续轩悬选癣绚学勋询寻驯训讯逊压鸦鸭哑亚讶阉烟盐严颜阎艳厌砚彦谚验鸯杨扬疡阳痒养样瑶摇尧遥窑谣药爷页业叶医铱颐遗仪蚁艺亿忆义诣议谊译异绎荫阴银饮隐樱婴鹰应缨莹萤营荧蝇赢颖哟拥佣痈踊咏涌优忧邮铀犹诱舆鱼渔娱与屿语吁御狱誉预驭鸳渊辕园员圆缘远愿约跃钥岳粤悦阅云郧匀陨运蕴酝晕韵杂灾载攒暂赞赃脏凿枣责择则泽贼赠扎"
   +"札轧铡闸栅诈斋债毡盏斩辗崭栈战绽张涨帐账胀赵蛰辙锗这贞针侦诊镇阵挣睁狰争帧郑证织职执纸挚掷帜质滞钟终种肿众诌轴皱昼骤猪诸诛烛瞩嘱贮铸筑驻专砖转赚桩庄装妆壮状锥赘坠缀谆着浊兹资渍踪综总纵邹诅组钻亘芈啬厍厣靥赝匦匮赜刭刿剀伛伥伧伫侪侬俦俨俪俣偾偬偻傥傧傩佥籴黉冁凫兖衮亵脔禀冢讦讧讪讴讵讷诂诃诋诏诒诓诔诖诘诙诜诟诠诤诨诩诮诰诳诶诹诼诿谀谂谄谇谌谏谑谒谔谕谖谙谛谘谝谟谠谡谥谧谪谫谮谯谲谳谵谶卺陉陧邝邬邺郏郐郓郦刍奂劢巯垩圹坜垆垭垲埘埚埙芗苈苋苌苁苎茏茑茔茕荛荜荞荟荠荦荥荩荪荭莳莴莅莸莺萦蒇蒉蒌蓦蓠蓣蔹蔺蕲薮藓奁尴扪抟挢掴掼揿摅撄撷撸撺叽呒呓呖呗咛哒哓哔哕哙哜哝唛唠唢啧啭喽喾嗫嗳辔嘤噜囵帏帱帻帼岖岘岚峄峤峥崂崃嵘嵛嵝巅徕犷狯狲猃猡猕饧饨饩饪饫饬饴饷饽馀馄馊馍馐馑馔庑赓廪忏怃怄忾怅怆怿恸恹恻恺恽悭惬愠愦懔闩闫闱闳闵闶闼闾阃阄阆阈阊阌阍阏阒阕阖阗阙阚沣沩泷泸泺泾浃浈浍浏浒浔涞涠渎渑渖渌溆滟滠滢滗潆潇潋潴濑灏骞迩迳逦屦弪妩妪妫姗娅娆娈娲娴婵媪嫒嫔嫱嬷驵驷驸驺驿驽骀骁骅骈骊骐骒骓骖骘骛骜骝骟骠骢骣骥骧纡纣纥纨纩纭纰纾绀绁绂绉绋绌绗绛绠绡绨绫绮绯绲缍绶绺绻绾缁缂缃缇缈缋缌缏缑缒缗缙缜缛缟缡缢缣缤缥缦缧缪缫缬缭缯缱缲缳缵玑玮珏珑顼玺珲琏瑷璎璇瓒韪韫韬杩枥枨枞枭栉栊栌栀栎柽桠桡桢桤桦桧栾棂椟椠椤椁榄榇榈榉槟槠樯橥橹橼檐檩殁殇殒殓殚殡轫轭轲轳轵轶轸轹轺轼轾辁辂辄辇辋辍辎辏辘辚戋戗戬瓯昙晔晖暧贲贳贶贻贽"
   +"赀赅赆赈赉赇赕赙觇觊觋觌觎觏觐觑毵氇氩氲牍胧胪胫脍脶腌腽膑欤飑飒飓飕飙毂齑斓炀炜炖烨焖焘祢祯禅怼悫愍懑戆沓泶矶砀砗砺砻硖硗碛碜龛睐睑畲罴羁钆钇钋钊钌钍钏钐钔钗钕钛钣钤钫钪钭钬钯钰钲钴钶钸钹钺钼钽钿铄铈铉铊铋铌铍铎铐铑铒铕铖铗铙铛铟铠铢铤铥铧铨铪铩铫铮铯铳铴铵铷铹铼铽铿锂锆锇锉锊锒锓锔锕锖锛锞锟锢锩锬锱锲锴锶锷锸锼锾镂锵镆镉镌镏镒镓镔镖镗镘镙镛镞镟镝镡镤镦镧镨镪镫镬镯镱镳锺穑鸠鸢鸨鸩鸪鸫鸬鸲鸱鸶鸸鸷鸹鸺鸾鹁鹂鹄鹆鹇鹈鹉鹌鹎鹑鹕鹗鹞鹣鹦鹧鹨鹩鹪鹫鹬鹭鹳疖疠痨痫瘅瘗瘿瘾癞癫窦窭裆裢裣裥褛褴襁皲耧聍聩顸颀颃颉颌颏颔颚颛颞颟颡颢颦虬虮虿蚬蚝蛎蛏蛱蛲蛳蛴蝈蝾蝼罂笃笕笾筚筝箦箧箨箪箫篑簖籁舣舻袅羟糁絷麸趱酽酾鹾趸跄跖跞跷跸跹跻踬踯蹑蹒蹰躏躜觞觯靓雳霁霭龀龃龅龆龇龈龉龊龌黾鼋鼍隽雠銮錾鱿鲂鲅鲈稣鲋鲎鲐鲒鲔鲕鲚鲛鲞鲟鲠鲡鲢鲣鲥鲦鲧鲨鲩鲫鲭鲮鲰鲱鲲鲳鲵鲶鲷鲻鲽鳄鳅鳆鳇鳌鳍鳎鳏鳐鳓鳔鳕鳗鳜鳝鳟鳢鞑鞯鹘髅髋髌魇魉飨餍鬓黩黪鼹";

  private const string _tGB=@"?}?@?K?垡\?W?瘟T?[?☆C?k?O?徒??^?r??????篚U???^?N??v??P????砰]???H??q?p?索M?e?T?l?I?e?P??K?芾??K?g?N?a??⑿Q???M?K?N?n??}??????y?釉??v?较s???p?P?a?U???L?L???c?S?斥n???m??r?畏Q?驼\?G?V?t?Y?u?X??_?x?????P?I?I?h??N?z?r?A?τ|??鳢??J???N??b?o?~?n??[??????f?Z?e?_?зJ???梧??勰????Q?????觞h??n?v?u?\?ПI?羿???爝f???c?|??蔗??{??B???V??G?|????鲷Y?侏??x????嗑??蛾?????D?g?Z???Z?~??吼I????D?E?l?P?y?m?\?C?┕????L??w?u?U?M????^???S?S?麂h?L???T?p?S?P?w??彷o?x?}???D?`??}?w?众s??M??????V????R???w?t???o???m???^?????蛐M????礻P?^?^?T??V?矽?w???|?????F?]??L????^??n?h??u?Q?R?M?Z???t後?刈o????W?A????????g?h???Q?警???o?S?e?]?x?зV?x???Z?≈M?d?L???夥?@????C?e??E?I?u???O???D?姿E??????H?^?o?A?v?a?Z??r?{???O?怨{?g?D?}?O?z?A?|???旌????p?]???b?`?v??I????T?u?R???{?Y?????v?u?z?豺?????q?C?e?_??U?g?I?^?A?????Y?]?镁o?\?H??M?x?a?M?徘G?o?L?@??i?o?R?蒋d??Q?m???f?x?e?????※N??X?Q?E?^?x??E?_?P?w?ふn????煅??F?K?~???V??r??h?Q???U???D?R?碣??{??r?@?@?m??????[?欣|???E????疯D?绢??I?h?x?Y??Y?????畹[??r?`?z???B??z?i??磕???????Z??奢v????|??C?R??[?C?U?g??R?`?X?I?s????@?祷\??n?]???Шt?J?R?B?]?t"
   +"?稃u???T????H?武X?H?铱|?]?V?G?n???\??y?噍?????S?]??}?_???j???j?????a??R?R?豳I???u?~?}?m?z?U?M???^?T?Q?N?q?]?V?T?????i?糁i????缇d??R????}?Q???\???c?{?y?夏X?吏[?H??M??f?俞??B??m??????甯?Q?翕o?~??廪r???Z?W?t???I?a?P?????r???i?_?h?l??O?{?u???H?滗??阕V???D??R?T?M???????L?U?w??t?X?Q???\?l?q??????N?????@???S?N?[?`?J?H???p??A???c???F??^?|??x?E???s?o?_???_?@?犴g?J?x?s?q??J?c???⑺_?w?三??黍}????⒓??Y??h?W??????p???B?d?z?卦O????鹉I?B??K?俾}???{?裨???r?g???R??葸m?????郢F?休????H?傩g?湄Q????p?l???f?T?q?z???Z??A?b?\?K?V?C?m?S??q?O?p?S?s???i?H??B????c????T??U???C???d??v?`?R?}?w??l?N?F?d??N?~?y?^?d?D?T?F?j???r?W??E?D?m???愁B?f?W?f?`????H?S???尉??^?l?芈??y????Y?胛??u?C?P?桄u??@?_?o???]?F?照`?a?抟u???蚣??r??{?b?M?B???r?w?y?t??e?@?U?F?I?h?W?w???????l?????啼N??[??f??y?{?C???a?x?\??d?扮n?C??u??S?⒕w?m??疫x?_?k?W?煸??ゑZ???d?壶f??????????}?李???W????┲V?????P????W?B?蝇??u?蜻b?G?{?????I?~?t??U?z?x???|???x??h?x?g???[?a??y??[??膪????t????I?上??A?f???虬b?x?????n?]??q?T??~?O?逝c?Z?Z?n?R?z?u?A?S?x?Y?@?@?T?A??h??s?S??[??????y?螂E?\?N?j????s?妮d???嘿??E?v??????t?少\??"
   +"?????l?旁p?S???直K?剌????鹁`???q?べ~??w?U?H?N?@???稍\???瓯??b??????C???碳???S?觅|???R?K?N?[??a?S????E?i?T?D?T??谫A?T?B?v?4u?D??肚f?b?y??铄F????Y?著?崞??Y?n??C??v?u?{?M????d??????v?I?Q?T??q??????t?????z??????R?f??E???????L?e?Z?哮D?夹??C?L?A?V?????n?G?b?X?g?t?r?E?C??????????V?a?N?O??????~?r?R?G?o?]?@?I?X?O?B?J????q??k??v?P?S?H??d???????w??P??i?B?c?J?????????N?P??_?G??{?O??r?d?\?L?κ???w?C?j??钏|?p??P?n?W?~?L?M?r??V??y??`?A?I??\?Y?????????d?t?X?]?x?\?`???h???}?^??????????O?Z??K??D?????\??????????s??F????????V???p?偏E???s???M?J?h??q?????A?G?N?Q?t?x?}?~??T?s?[????Y???????Q?????a??C?|???V?Z??b?h?`?Y???b?????????H?D?I?R????{?o?T??????g?G??Z???^??c?O?s????]??u?t???z?|???q??????????????I??D?z????????唏z???|?A?w?~????P?U?S?K??s?\??t?~????K?J?u?q?v?w?k????C?X??U?E?I?W?{????c?_?p?i?E?R?^?J?U?l?~?|???D????P??N?b?d?c?r?O?V?_?~?z?w???i???`?R?Q?y?^?|?k????t?q?I?a???v???t?y?w?q?????n????d??f???E????u???????????????{???春??_?{?????????????V?_?T?W?F?]?U?Y?e?b?`?m??y?z?w??A?O???飚T?????豳S?B?L?O?"
   +"?D?W?B?c?l?g?y???J??]?D?M?P?U??????V?F???T?Z?e??e?R?S?Z?`?j??W????????F?c?[??U?????哌e?痛??X??Z?a????~???A?佘?`?b??????Q?A??{?O?S??k?j?[??^??Z??`?????X?f?g??p??C?B?G???I?D??s?B??e?t?K??z??b?A?f??x?|??P?C?|?|?@???n??H???~?S?s?Z?u?|?H??Q??K?d??U?O??|?J???}?D?U?I??k??y???\?S?M?N??O???C??h??|??j??Z?C?O?s?R?w?F?S?d?c?????R?z?|?????v?????[?P?Z?]?O???Y?^?g?l???????_?Y?W?p?w???????????X?X?O?A?B?D???`?a?]?d?]?M?d??c?M?@?h???e?????@?R?M?W?h???D????A?A?l?????|??u???X??N??V?a?e?`?~?j?D?X????f?[??A???u?R?{??????z?O???V?E??]?Q?W?U?b??X?k?g?x?z?n?Z?V?\?Z?e?_?f?b?l?r?p?}?w?x???h???Y???E?|?d?V?c?T?^?n?b?q?o??\??~????????a??N?O?E?H?K?F?T???l?{?q?v?m????????B?L??Z?X?V?k?^?d?X?t?y?x?|?u???W?t?o?B";

  public G2B5Filter(Stream sink)
  {
   //System.Web.HttpContext.Current.Response.ContentEncoding=Encoding.GetEncoding(950);
   _sink = sink;

  }

  // The following members of Stream must be overriden.
  public override bool CanRead
  {
   get { return true; }
  }

  public override bool CanSeek
  {
   get { return true; }
  }

  public override bool CanWrite
  {
   get { return true; }
  }

  public override long Length
  {
   get { return 0; }
  }

  public override long Position
  {
   get { return _position; }
   set { _position = value; }
  }

  public override long Seek(long offset, System.IO.SeekOrigin direction)
  {
   return _sink.Seek(offset, direction);
  }

  public override void SetLength(long length)
  {
   _sink.SetLength(length);
  }

  public override void Close()
  {
   _sink.Close();
  }

  public override void Flush()
  {
   _sink.Flush();
  }

  public override int Read(byte[] buffer, int offset, int count)
  {
   return _sink.Read(buffer, offset, count);
  }

  /// <summary>
  /// 实际处理字符替换的作业,将简体字和繁体字进行替换。
  /// </summary>
  /// <param name="buffer"> </param>
  /// <param name="offset"></param>
  /// <param name="count"></param>
  public override void Write(byte[] buffer, int offset, int count)
  {
   Encoding e=Encoding.GetEncoding(936);  //936是GB2312编码,950是Big5编码

   string str=e.GetString(buffer,offset,count);

   for(int i=0;i<str.Length;i++)
   {
    int j=_sGB.IndexOf(str[i]);
    if(j!=-1)str=str.Replace(_sGB[j],_tGB[j]);
   }
   e=Encoding.GetEncoding(System.Web.HttpContext.Current.Response.Charset);
   _sink.Write(e.GetBytes(str), 0, e.GetByteCount(str));

  }   

 }

 public class EncodingFilter
 {
  Page thisPage;

  public EncodingFilter(Page mPage)
  {
      thisPage=mPage;
  }

  /// <summary>
  /// 对输入的流对象进行编码,然后Write
  /// </summary>
  /// <param name="EncodingStyle"></param>
  public void G2BEncoding(string EncodingStyle)
  { 
   //======================== 公共函数 Start ============================= 
   thisPage.Response.ContentEncoding=Encoding.GetEncoding("GB2312");

   if(EncodingStyle=="BIG5")
   {    
    thisPage.Response.Filter = new G2B5Filter(thisPage.Response.Filter);
    thisPage.Response.Charset="BIG5";
   }

   if(EncodingStyle=="GB2312")
   {
    thisPage.Response.Charset="GB2312";
   }

   //======================== 公共函数 End ============================= 
  }

 }
  

上一篇:.Net项目在Docker容器中开发部署

栏    目:.NET代码

下一篇:ASP.NET Core管理应用程序状态

本文标题:ASP.NET中实现中文简/繁体自动转换的类

本文地址:http://www.codeinn.net/misctech/209134.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有