2012-12-04 28 views
0

我想將所有英文數字都改爲波斯文,以便向用戶展示。並再次將其更改爲英語的數字給了所有請求(回傳)

e.g:
我們有這樣的事情,鑑於IRQ170,我想告訴IRQ۱۷۰給用戶,並從用戶提供IRQ170在MVC(httpmodule)中將英文數字更改爲波斯語,反之亦然?

我知道,我不得不使用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和腳本文件到波斯,它會導致錯誤。

+1

你到目前爲止嘗試過什麼......並且你看過本地化嗎?本地化應該給你你正確的號碼呈現,假設你編碼你的用戶界面。 –

+0

在我決定使用httpmodule之前,我在MVC中使用過濾器,它僅適用於顯示字符串,但我想在每個請求中將波斯語數字更改爲英文數字 –

回答

1

我相信它在這裏沒有用HttpModules。 HttpModules的一般用法可以在MSDN找到。相反,建議您使用全球化,國際化和本地化方法。解決問題將會更加簡單高效。請注意,您必須使用波斯語編碼字體來顯示預期結果。

相關問題