asp.net国际化,页面选择语言

方案一


 

国际化(internationalization)是让我们所写的程序,有多语言显示,有点象见什么人说什么话的味

道,呵呵!
下面是一个例子:
第一步:
新建一个Website,在这个工程中添加一个类,继承 System.Web.UI.Page,主要代码如下:

  protected override void InitializeCulture()
    {
        if (Request.Cookies["Language"] != null && Request.Cookies["Language"].Value !=

null && Request.Cookies["Language"].Value != "")
        {
          setCulture();
        }
        else
        { //设置Cookie
          Response.Cookies["Language"].Value =
            (Request.Headers["accept-language"].Split(",".ToCharArray())[0]);
          Response.Cookies["Language"].Expires = DateTime.Now.AddDays(1000);
          setCulture();
        }
        base.InitializeCulture();
    }
    private void setCulture()
   {
       try
       {
         String selectedLanguage = Request.Cookies["Language"].Value;
         UICulture = selectedLanguage;
         Culture = selectedLanguage;

         System.Threading.Thread.CurrentThread.CurrentCulture =
           System.Globalization.CultureInfo.CreateSpecificCulture(selectedLanguage);
         System.Threading.Thread.CurrentThread.CurrentUICulture = new
           System.Globalization.CultureInfo(selectedLanguage);
       }
       catch
       {
         Response.Cookies.Clear();
       }
   }

第二步:添加一个WebPage 主要代码如下:

protected void Page_Load(object sender, EventArgs e)
   {  
       Response.Cookies["Language"].Value = Request["Language"];
       Response.Cookies["Language"].Expires = DateTime.Now.AddDays(1000);  
       Response.Redirect(Server.UrlDecode(Request["url"]));
   }
第三步:
添加要显示国际化的页面:
  注意:这个页面要继承我们第一步写的那个类
主要代码:

<asp: Label ID="Label1" runat="server" Text="<%$ Resources:ResourceGlobal,test%>"></asp:

Label>
另外我们还要在此页做国际化测试,所以还要添加一个DropdownList,做为选择显示语言

<asp: DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"

OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp: ListItem Text="English Version" Value="en-US"></asp: ListItem>
<asp: ListItem Text="中 文 版" Value="zh-CN"></asp: ListItem>
</asp: DropDownList>
同时在代码文件添加如下代码:

protected void Page_Load(object sender, EventArgs e)
   {

       if (!Page.IsPostBack)
       {
         if (Request.Cookies["Language"] == null || Request.Cookies["Language"].Value ==

"")
         {
           foreach (ListItem li in DropDownList1.Items)
           {
               if (li.Value == "")
                 li.Selected = true;
           }
         }
         else
         {
           foreach (ListItem li in DropDownList1.Items)
           {
               if (li.Value.ToLower() == Request.Cookies["Language"].Value.ToLower())
                 li.Selected = true;
           }
         }
       }

   }
   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       Response.Redirect("~/ChangeLanguage.aspx?Language=" + DropDownList1.SelectedValue +

"&url=" + Server.UrlEncode(Request.Url.PathAndQuery), true);

   }
第四步:添加资源文件ResourceGlobal.resx和ResourceGlobal.en.resx呵呵,这里,只做中英文测试,

所以只做2个了,如果显示其它语言,请自行添加。
在资源文件里一定要添加一个test键值,这是个测试数据啊。
呵呵OK了,现在可以浏览测试了。

 

方案二


解决方案主要包含几个部分组成:
1:客户设置语言的控件,一般会包含在每个页面,使得用户可以随时切换区域
控件ascx代码

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="English Version" Value="en-US"></asp:ListItem>
<asp:ListItem Text="中 文 版" Value="zh-CN"></asp:ListItem>
</asp:DropDownList>


控件CS事件代码

protected void Page_Load(object sender, EventArgs e)
        
{
            
if (!Page.IsPostBack){
                
if (Request.Cookies[Settings.Default.Cookies_Language] == null || Request.Cookies[Settings.Default.Cookies_Language].Value == "")
                
{
                    
foreach (ListItem li in DropDownList1.Items)
                    
{
                        
if (li.Value == "")
                            li.Selected 
= true;
                    }

                }

                
else
                
{
                    
foreach (ListItem li in DropDownList1.Items)
                    
{
                        
if (li.Value.ToLower() == Request.Cookies[Settings.Default.Cookies_Language].Value.ToLower())
                            li.Selected 
= true;
                    }

                }

            }

        }


        
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        
{
            Response.Redirect(
"~/ChangeLanguage.aspx?Language=" + DropDownList1.SelectedValue + "&url=" + Server.UrlEncode(Request.Url.PathAndQuery) ,true);
        }

ChangeLanguage中间页面代码:

public partial class ChangeLanguage : System.Web.UI.Page
    
{
        
protected void Page_Load(object sender, EventArgs e)
        
{
            Response.Cookies[
"Language"].Value = Request["Language"];
            Response.Cookies[
"Language"].Expires = DateTime.Now.AddDays(1000);
           

            Response.Redirect(Server.UrlDecode(Request[
"url"]));
        }

    }


2:在每页加载时会判断当前用户选择的区域,一般在页面的基类中实现比如类名:BasePage。
当用户初次进入网站识别客户端浏览器,
当用户曾经选择了区域,加载选择区域

/// <summary>
        
/// 本地化
        
/// </summary>

        protected override void InitializeCulture()
        
{
            
if (Request.Cookies[Settings.Default.Cookies_Language] != null && Request.Cookies[Settings.Default.Cookies_Language].Value != null && Request.Cookies[Settings.Default.Cookies_Language].Value != "")
            
{
                setCulture();
            }

            
else
            
{//设置Cookie
                Response.Cookies[Settings.Default.Cookies_Language].Value =
                    (Request.Headers[
"accept-language"].Split(",".ToCharArray())[0]);
                Response.Cookies[Settings.Default.Cookies_Language].Expires 
= DateTime.Now.AddDays(1000);
                setCulture();
            }

            
base.InitializeCulture();
        }



        
private void setCulture()
        
{

            
try
            
{
                String selectedLanguage 
= Request.Cookies[Settings.Default.Cookies_Language].Value;
                UICulture 
= selectedLanguage;
                Culture 
= selectedLanguage;

                System.Threading.Thread.CurrentThread.CurrentCulture 
=
                    System.Globalization.CultureInfo.CreateSpecificCulture(selectedLanguage);
                System.Threading.Thread.CurrentThread.CurrentUICulture 
= new
                    System.Globalization.CultureInfo(selectedLanguage);
            }

            
catch //( Exception ex )
            {
                Response.Cookies.Clear();
// [Settings.Default.Cookies_Language].d
                
//throw;
            }

        }


3:当然是为每个页面准备资源文件。
这个是个体力活,
ASPNET.ascx.en.resx 表示英文的
ASPNET.ascx.zh.resx 这个表示中文的,当然还可以分为更加细的区域,比如zh-CN/zh-HK/zh-TW

4:当然要记得,把你的页面都要继承你的基类
   public partial class Index : PageBase

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭