六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 776|回复: 0

ASP.NET 多语言之模板机制原理及应用

[复制链接]
 楼主| 发表于 2013-11-24 21:10:40 | 显示全部楼层 |阅读模式
 多语言实现方法有许多种,  1.利用VS2005自带的资源文件去实现
  2.重写控件,给多语言赋值<myuiable key="language" runat=server />;
  3.利用模板,内文格式如<input type="button" value="{search}" />
  。。。
  以下利用模板机制实现多语言:
  原理是用正则式把所有的{XXX}读取出来,然后替换,再写回到页面。
  需要用到的命名空间:
  using System.Text.RegularExpressions;
  using System.Text;
  using System.IO;
  using System.Diagnostics;
  具体代码如下:
  1. CODE:
  2.   
  3.   protected override void Render(HtmlTextWriter writer)
  4.     {
  5.       Stopwatch stopwatch = new Stopwatch();
  6.       stopwatch.Reset();
  7.       stopwatch.Start();
  8.       try
  9.       {
  10.         StringBuilder sb = new StringBuilder();
  11.         StringWriter sw = new StringWriter(sb);
  12.         HtmlTextWriter htw = new HtmlTextWriter(sw);
  13.         base.Render(htw);
  14.         string content = sb.ToString();
  15.         //模板标签{yes},{no},{search}
  16.         Regex re = new Regex(@"{S+}", RegexOptions.Multiline);
  17.         MatchCollection mc = re.Matches(content);
  18.         //利用Hashtable来筛选所有的多语言标签,然后做替换
  19.         Hashtable ht = new Hashtable();
  20.         //多语言的资源文件读取到一个Hashtable里,可以保存到缓存里,
  21.         Hashtable ResurceHt = new Hashtable();
  22.         for (int i = 0; i < mc.Count; i++)
  23.         {
  24.           if (!ht.ContainsKey(mc.Value))
  25.           {
  26.             ht.Add(mc.Value, null);
  27.             //进行替换
  28.             content = content.Replace(mc.Value, (string)ResurceHt[mc.Value.TrimStart('{').TrimEnd('}')]);
  29.           }         
  30.         }
  31.         ht.Clear();
  32.         //重新写入页面
  33.         writer.Write(content);
  34.       }
  35.       catch (Exception ex)
  36.       {
  37.         Response.Write(ex.ToString());
  38.         Response.End();
  39.       }
  40.       finally {
  41.         stopwatch.Stop();
  42.         Response.Write("runtime:" + stopwatch.ElapsedMilliseconds.ToString() + "ms");
  43.       }
  44.     }
复制代码
扩展:  如果有些多语言标签不需要替换,则可以用<NOREPLACE>{xxx},hello ,一舟 </NOREPLACE>包起来,
  然后用正则式把content分割开,然后分别替换,再把分割开的字符串连接起来。
  代码如下:
  1. CODE:
  2.   
  3.    //需要替换的标签
  4.     static readonly Regex re = new Regex(@"{S+}", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
  5.     //不需要替换的标签
  6.     static readonly Regex re_nr = new Regex(@"<NOREPLACE>[wW]*?</NOREPLACE>", RegexOptions.Multiline | RegexOptions.IgnoreCase);
  7.     //多语言的资源文件读取到一个Hashtable里,可以保存到缓存里,
  8.     Hashtable ResurceHt = new Hashtable();
  9.     private string ReplaceTag(ref string content)
  10.     {
  11.       //模板标签{yes},{no},{search}
  12.       MatchCollection mc = re.Matches(content);
  13.       //利用Hashtable来筛选所有的多语言标签,然后做替换
  14.       Hashtable ht = new Hashtable();
  15.       for (int i = 0; i < mc.Count; i++)
  16.       {
  17.         if (!ht.ContainsKey(mc.Value))
  18.         {
  19.           ht.Add(mc.Value, null);
  20.           //进行替换
  21.           content = content.Replace(mc.Value, mc.Value.TrimStart('{').TrimEnd('}'));
  22.         }
  23.       }
  24.       ht.Clear();
  25.       return content;
  26.     }
  27.     protected override void Render(HtmlTextWriter writer)
  28.     {
  29.       Stopwatch stopwatch = new Stopwatch();
  30.       stopwatch.Reset();
  31.       stopwatch.Start();
  32.       try
  33.       {
  34.         StringBuilder sb = new StringBuilder();
  35.         StringWriter sw = new StringWriter(sb);
  36.         HtmlTextWriter htw = new HtmlTextWriter(sw);
  37.         base.Render(htw);
  38.         string content = sb.ToString();
  39.         if (re_nr.IsMatch(content))//不需要替换标签
  40.         {
  41.           string text = re_nr.Match(content).Groups[0].Value;
  42.           string[] textArray = re_nr.Split(content);
  43.           textArray[0] = ReplaceTag(ref textArray[0]);
  44.           textArray[1] = ReplaceTag(ref textArray[1]);
  45.           content = textArray[0] + text + textArray[1];
  46.         }
  47.         else {
  48.           content = ReplaceTag(ref content);
  49.         }   
  50.         //重新写入页面
  51.         writer.Write(content);
  52.       }
  53.       catch (Exception ex)
  54.       {
  55.         Response.Write(ex.ToString());
  56.         Response.End();
  57.       }
  58.       finally {
  59.         stopwatch.Stop();
  60.         Response.Write("runtime:" + stopwatch.ElapsedMilliseconds.ToString() + "ms");
  61.       }
  62.     }
复制代码
本文摘自:http://tech.ddvip.com/2008-10/122423178179092.html


该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表