2009年10月12日 星期一

原創-網站繁體轉簡體功能-推薦:Visual Studio International Pack

在MasterPage上的程式碼
using System.Globalization;
using System.IO;
using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter; using System.Threading;
using System.Configuration;
在Page_Load加上
        if (!IsPostBack)
{
if (Session["CultureName"] == null)
{
if (Response.Cookies["lang"].Value == null)
{
Session["CultureName"] = CultureInfo.CurrentCulture.Name;
}
else
{
Session["CultureName"] = Response.Cookies["lang"].Value;

}
}
}
繁體按鈕
    protected void LinkButton1_Click(object sender, EventArgs e)
{
Session["CultureName"] = "zh-TW";
Response.Cookies["lang"].Value = "zh-TW";
Response.Cookies["lang"].Expires = DateTime.Now.AddDays(60);
Page.Response.Redirect(Page.Request.Url.ToString(), true);
}
簡體按鈕
    protected void LinkButton2_Click(object sender, EventArgs e)
{
InitializeCulture();
Session["CultureName"] = "zh-CN";
Response.Cookies["lang"].Value = "zh-CN";
Response.Cookies["lang"].Expires = DateTime.Now.AddDays(60);
Page.Response.Redirect(Page.Request.Url.ToString(), true);
}
在網頁的Render事件
    protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
try
{
string lang = Session["CultureName"].ToString();
string sText;
StringWriter oStringWriter = new StringWriter();
HtmlTextWriter oHtmlWriter = new HtmlTextWriter(oStringWriter);
base.Render(oHtmlWriter);
oHtmlWriter.Flush();
oHtmlWriter.Close();
sText = oStringWriter.ToString();
if (lang == "zh-CN")
{
sText = translate.Big5ToGb(sText);
}
else if (lang == "zh-TW")
{
sText = translate.GbToBig5(sText);
}
Response.Write(sText);
}
catch
{
}
}
在translate.cs 理撰寫
    public static string Big5ToGb(string Text)
{
Text = UnicodeDictionary.StrToUnicode16(Text);
return ChineseConverter.Convert(Text, ChineseConversionDirection.TraditionalToSimplified);
}
public static string GbToBig5(string Text)
{
Text = UnicodeDictionary.StrToUnicode16(Text);
return ChineseConverter.Convert(Text, ChineseConversionDirection.SimplifiedToTraditional);
}
這樣就可以將整各網頁做繁簡轉換了

沒有留言:

張貼留言