更改英文数字波斯语,反之亦然在MVC(HTTP模块)? [英] Change English numbers to Persian and vice versa in MVC (httpmodule)?

查看:156
本文介绍了更改英文数字波斯语,反之亦然在MVC(HTTP模块)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变这一切的英语数字波斯用于显示给用户。并再次将其更改为英语号码给所有的请求(回发)搜索结果
例如:结果
我们有鉴于这样的事情 IRQ170 ,我想告诉 IRQ170 来的用户,并给 IRQ170 从用户。

I wanna change all English numbers to Persian for showing to users. and change them to English numbers again for giving all requests (Postbacks)

e.g:
we have something like this in view IRQ170, I wanna show IRQ۱۷۰ to users and give IRQ170 from users.

,我必须使用的HttpModule ,但我不知道怎么样?结果
能否请您指导我?

I know, I have to use Httpmodule, But I don't know how ?
Could you please guide me?

编辑:

让我描述更多:结果

我已经写了下面的HTTP模块:

I've written the following http module :

using System;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using Smartiz.Common;

namespace Smartiz.UI.Classes
{
    public class PersianNumberModule : IHttpModule
    {
        private StreamWatcher _watcher;

        #region Implementation of IHttpModule

        /// <summary>
        /// Initializes a module and prepares it to handle requests.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application </param>
        public void Init(HttpApplication context)
        {
            context.BeginRequest += ContextBeginRequest;
            context.EndRequest += ContextEndRequest;
        }

        /// <summary>
        /// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
        /// </summary>
        public void Dispose()
        {
        }

        #endregion

        private void ContextBeginRequest(object sender, EventArgs e)
        {
            HttpApplication context = sender as HttpApplication;
            if (context == null) return;
            _watcher = new StreamWatcher(context.Response.Filter);
            context.Response.Filter = _watcher;
        }

        private void ContextEndRequest(object sender, EventArgs e)
        {
            HttpApplication context = sender as HttpApplication;
            if (context == null) return;
            _watcher = new StreamWatcher(context.Response.Filter);
            context.Response.Filter = _watcher;
        }

    }

    public class StreamWatcher : Stream
    {
        private readonly Stream _stream;
        private readonly MemoryStream _memoryStream = new MemoryStream();

        public StreamWatcher(Stream stream)
        {
            _stream = stream;
        }

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

        public override int Read(byte[] buffer, int offset, int count)
        {
            int bytesRead = _stream.Read(buffer, offset, count);

            string orgContent = Encoding.UTF8.GetString(buffer, offset, bytesRead);
            string newContent = orgContent.ToEnglishNumber();
            int newByteCountLength = Encoding.UTF8.GetByteCount(newContent);
            Encoding.UTF8.GetBytes(newContent, 0, Encoding.UTF8.GetByteCount(newContent), buffer, 0);

            return newByteCountLength;
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            string strBuffer = Encoding.UTF8.GetString(buffer, offset, count);

            MatchCollection htmlAttributes = Regex.Matches(strBuffer, @"(\S+)=[""']?((?:.(?![""']?\s+(?:\S+)=|[>""']))+.)[""']?", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            foreach (Match match in htmlAttributes)
            {
                strBuffer = strBuffer.Replace(match.Value, match.Value.ToEnglishNumber());
            }

            MatchCollection scripts = Regex.Matches(strBuffer, "<script[^>]*>(.*?)</script>", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
            foreach (Match match in scripts)
            {
                MatchCollection values = Regex.Matches(match.Value, @"([""'])(?:(?=(\\?))\2.)*?\1", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
                foreach (Match stringValue in values)
                {
                    strBuffer = strBuffer.Replace(stringValue.Value, stringValue.Value.ToEnglishNumber());
                }
            }

            MatchCollection styles = Regex.Matches(strBuffer, "<style[^>]*>(.*?)</style>", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
            foreach (Match match in styles)
            {
                strBuffer = strBuffer.Replace(match.Value, match.Value.ToEnglishNumber());
            }

            byte[] data = Encoding.UTF8.GetBytes(strBuffer);

            _memoryStream.Write(data, offset, count);
            _stream.Write(data, offset, count);
        }

        public override string ToString()
        {
            return Encoding.UTF8.GetString(_memoryStream.ToArray());
        }

        #region Rest of the overrides
        public override bool CanRead
        {
            get { throw new NotImplementedException(); }
        }

        public override bool CanSeek
        {
            get { throw new NotImplementedException(); }
        }

        public override bool CanWrite
        {
            get { throw new NotImplementedException(); }
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotImplementedException();
        }

        public override void SetLength(long value)
        {
            throw new NotImplementedException();
        }

        public override long Length
        {
            get { throw new NotImplementedException(); }
        }

        public override long Position
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        #endregion
    }
}

它运作良好,但它在CSS和脚本文件中的所有数字转换为波斯语,这会导致错误。

It works well, but It converts all numbers in css and scripts files to Persian and it causes error.

推荐答案

我相信它不是有用雇用的HttpModules在这里。的HttpModules的一般用法可以在 MSDN 。相反,建议您使用
全球化,国际化和本地化的方法。这将是更简单和高效的解决问题。请注意,您必须使用波斯EN codeD字体显示预期的结果。

I believe that its not useful to employ HttpModules here. General usage of HttpModules can be found at MSDN. Instead, Suggest you to use Globalization, Internationalization and Localization methods. it will be more simpler and efficient to solve the problem. Note that you must use Persian encoded fonts to show expected result.

这篇关于更改英文数字波斯语,反之亦然在MVC(HTTP模块)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆