zoukankan      html  css  js  c++  java
  • net2.0下的简繁转换

    规则:
     以URL地址来决定简繁的显示,zh-cn/index.htm为简体,zh-tw/index.htm为繁体。

    思路很简单,以IHttpHandler接口为基类,写一个类,用来处理HttpHandler,用ProcessRequest方法来处理客户端的请求。在ProcessRequest方法中获取url值,有zh-tw就用繁体,反之用简体。

    using System;
    using System.IO;
    using System.Data;
    using System.Configuration;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text;
    using System.Text.RegularExpressions;
    using Microsoft.VisualBasic;

    导入以上命名空间,注意Microsoft.VisualBasic,要先引用一下Microsoft.VisualBasic。

    public class HtmlHttpHandler : IHttpHandler
    {
       //这里是定义的一个结构 ,与简繁转换无关,主要设定url重写的规则。
        private List<RegexInfo> _regex_list = new List<RegexInfo>();

        public HtmlHttpHandler()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(ConfigurationManager.AppSettings["RegexsXml"]);
            foreach (DataRow r in ds.Tables["regex"].Rows)
                _regex_list.Add(new RegexInfo(((string)r["b"]).Trim(), ((string)r["a"]).Trim()));
        }

        //主方法

        public void ProcessRequest(HttpContext context)
        {
            string path = context.Request.Path;
            //foreach (RegexInfo r in _regex_list)
                //path = Regex.Replace(path, r._before, r._after);   //url重写
           //开始判断并转换
            if (path.IndexOf("zh-tw") != -1)
            {
                path = path.Replace("zh-tw", "zh-cn");
                 //以自定义方式过滤
                context.Response.Filter = new CnToTwStream(context.Response.Filter, context.Response.ContentEncoding);
            }
            context.Server.Transfer(path);
        }

        // Override the IsReusable property.
        public bool IsReusable
        {
            get { return true; }
        }
    }

    CnToTwStream类实现简繁的转换

    class CnToTwStream : Stream
    {
        private Stream _sink;
        private MemoryStream _ms;
        private Encoding _encoding;

        public CnToTwStream(Stream sink, Encoding encoding)
        {
            _sink = sink;
            _ms = new MemoryStream();
            _encoding = encoding;
        }

        public override bool CanRead
        {
            get { return false; }
        }

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

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

        public override long Length
        {
            get { return _ms.Length; }
        }

        public override long Position
        {
            get { return _ms.Position; }
            set { throw new NotSupportedException(); }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException();
        }

        public override long Seek(long offset, System.IO.SeekOrigin direction)
        {
            throw new NotSupportedException();
        }

        public override void SetLength(long length)
        {
            throw new NotSupportedException();
        }

        public override void Close()
        {
            _ms.Close();
            byte[] buffer_cn = _ms.GetBuffer();
            string str_cn = _encoding.GetString( buffer_cn );
            //用Strings类的StrConv方法,其中TraditionalChinese是VisualBasic中的一个枚举
            string str_tw = Strings.StrConv(str_cn, VbStrConv.TraditionalChinese, 0);
            str_tw = str_tw.Replace("__zh-cn__", "__zh-tw__");
            byte[] buffer_tw = _encoding.GetBytes(str_tw);
            using (_sink)
            {
                _sink.Write(buffer_tw, 0, buffer_tw.Length);
            }
        }

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

        public override void Write(byte[] buffer, int offset, int count)
        {
            _ms.Write(buffer, offset, count);
        }
    }

  • 相关阅读:
    warning MSB3245: 未能解析此引用。未能找到程序集“CemeteryBLL”。请检查磁盘上是否存在该程序集。 如果您的代码需要此引用,则可能出现编译错误。
    C#MVC中创建多模块web应用程序
    Visual Studio提示“无法启动IIS Express Web服务器”的解决方法
    未能找到 CodeDom 提供程序类型“Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=31bf385
    WebHost failed to process a request.Memory gates checking failed because the free memory (140656640 bytes) is less than 5% of total memory
    wamp安装后打开默认网页显示dir,图标红点
    数字转为大写,钱转为大写格式
    SpringCloud
    Spring Boot
    双重校验锁 --使用volatile和两次判空校验
  • 原文地址:https://www.cnblogs.com/scgw/p/1303662.html
Copyright © 2011-2022 走看看