前言:
好多年沒寫文章了,工作很忙,天天加班, 每天都 相信不用多久 , 就會升職加薪 , 當上總經(jīng)理 , 出任CEO , 迎娶白富美,走上人生巔峰,想想還有點小激動~~~~
直到后來發(fā)生了郵箱事件,我竟然忘了給郵箱密碼賦值,導致 遇到“郵箱不可用。 服務器響應為: 5.7.1 Unable to relay for”的問題,網(wǎng)上一查后, 讓Boss去設置 IIS里的SMTP。
結果Boss力證不用設置也可以發(fā),還給我發(fā)了N多Demo代碼,讓我蛋碎一地, 最后那點小激動,就在這小事件上栽沒了~~~
好了,不多扯了,回正文吧~~~
引子:
關于系統(tǒng)的多語言,我在之前的文章都寫過不少,包括秋色園QBlog的開源博客里,也有相應的實現(xiàn)方案,不過隨著項目環(huán)境的不同,往往實現(xiàn)的方案也不盡相同。
今天就來扯扯,ASP.NET MVC下的方案。
1:數(shù)據(jù)的多語言:
在QBlog里,數(shù)據(jù)的多語言,我是分成兩種方案一起處理:
A:多條數(shù)據(jù),文章數(shù)據(jù),用一個語言字段來標識該條數(shù)據(jù)為何種語言。
B:對于其它數(shù)據(jù),標題,公告等,用一個[#LangSplit]標識來分隔前后兩種語言。
不過現(xiàn)在的方案有點不同,看如下圖:
看到大量的Xml字段了吧,這就是上一個項目繼承而來的精華,在項目里動不了事實存在。
關于表名和字段命名方式,走的是國際范,大伙不要學。
針對Xml,需要有一小套處理方案:
< ML V ="1.0" >
< M L ="zh-cn" > 中文 </ M >
< M L ="en" > English </ M >
< M L =".." > 其它語言 </ M >
</ ML >
然后針對這種存檔,需要有相應的處理:
取值:字段名.value('(/ML/M[@L="zh-cn"])[1]','nvarchar(max)')
取節(jié)點:字段名.query('/ML/M[@L="en"]')
判斷:字段名.exists('/ML/M[@L="zh-cn"]')
排序:用取值后的字段名進行排序
處理流程大體如下:
2:UI多語言
2.1:MVC View的多語言流程:
經(jīng)過對MVC的源碼調(diào)試,發(fā)現(xiàn)在Control基類(自己定義)統(tǒng)一處理即可。
Demo代碼:
{
if (filterContext.Result is ViewResult)
{
string html=RenderViewToString( this ,((ViewResult)filterContext.Result).View);
html=LanguageMgr.Replace(html, " zh " );
Response.Clear();
Response.Write(html);
}
}
protected static string RenderViewToString(Controllercontroller,IViewview)
{
// IViewview=ViewEngines.Engines.FindView(controller.ControllerContext,viewName,masterName).View;
using (System.IO.StringWriterwriter= new System.IO.StringWriter())
{
ViewContextviewContext= new ViewContext(controller.ControllerContext,view,controller.ViewData,controller.TempData,writer);
viewContext.View.Render(viewContext,writer);
return writer.ToString();
}
}
一開始的想法是處理完后寫回去,后來調(diào)試了半天源碼發(fā)現(xiàn)找不到寫回去的,靈光一閃,發(fā)現(xiàn)數(shù)據(jù)在Response.OutPut流里,直接清空,輸出新的Html即可。
2.2:JS腳本的多語言流程:
具體的實現(xiàn),看下面的語法定義。
3: UI多語言的語法方案
配套的Demo實現(xiàn):
{
/// <summary>
/// 替換多語言。
/// </summary>
/// <paramname="html"></param>
/// <paramname="lang"></param>
/// <returns></returns>
public static string Replace( string html, string lang)
{
MatchCollectionmatchs=Regex.Matches(html, @" \[#([\S\s]*?)#\] " ,RegexOptions.Compiled|RegexOptions.IgnoreCase);
if (matchs!= null &&matchs.Count> 0 )
{
List< string >keys= new List< string >(matchs.Count); // 記錄已匹配過的
Dictionary< string , string >dic=GetLanguageDic(lang);
foreach (Matchmatch in matchs)
{
string text=match.Groups[ 0 ].Value;
string key=match.Groups[ 1 ].Value.Trim();
if (!keys.Contains(key))
{
keys.Add(key);
if (dic.ContainsKey(key))
{
html=html.Replace(text,dic[key]);
}
}
}
keys= null ;
matchs= null ;
}
return html;
}
internal static Dictionary< string , string >GetLanguageDic( string lang)
{
Dictionary< string , string >dic= new Dictionary< string , string >();
dic.Add( " aaa " , " 中文 " );
dic.Add( " bbb " , " 英文 " );
return dic;
}
}
4: JavaScript 多語言定義
var lang={loginID:”[#LoginID#]”,userName:”[#UserName#]”};
<script>
該View會在Controller端提前會替換成相應語言的文字。
總結:
以上的多語言方案,有特定的項目環(huán)境背景,僅供參考,討論,借鑒,反省,請勿輕易模仿。
謝謝觀賞。
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
