【问题标题】:Change English numbers to Persian and vice versa in MVC (httpmodule)?在 MVC(httpmodule)中将英文数字更改为波斯语,反之亦然?
【发布时间】:2012-12-04 18:41:43
【问题描述】:

我想将所有英文数字更改为波斯语以显示给用户。并再次将它们更改为英文数字以提供所有请求(回发)

例如:
我们在IRQ170 中看到了类似的内容,我想向用户展示IRQ۱۷۰ 并从用户那里提供IRQ170。

我知道,我必须使用Httpmodule,但我不知道如何?
你能指导我吗?

编辑:

让我再描述一下:

我编写了以下 http 模块:

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 和脚本文件中的所有数字转换为波斯语并导致错误。

【问题讨论】:

  • 到目前为止您尝试过什么...您是否看过本地化?假设您也为它编写了 UI,本地化应该会为您提供正确的数字表示。
  • 在决定使用 httpmodule 之前,我在 MVC 中使用过滤器,它非常适合仅显示字符串,但我想在每个请求中将波斯数字更改为英文数字

标签: c# asp.net asp.net-mvc-3 httpmodule


【解决方案1】:

我相信在这里使用 HttpModules 是没有用的。 HttpModules 的一般用法可以在MSDN 找到。相反,建议您使用 全球化、国际化和本地化方法。解决问题会更加简单高效。请注意,您必须使用波斯编码字体才能显示预期结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 2020-01-18
    相关资源
    最近更新 更多