Local Resource应用概述

本文介绍了一种在 ASP.NET 应用中实现多语言支持的方法。通过在 BasePage 类中设置用户界面文化和当前文化,根据用户的语言偏好来动态切换应用的语言显示。此外,还提供了一个简单的语言选择界面,用户可以选择自己偏好的语言。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考资料: http://hi.baidu.com/lsheshang/blog/item/c63a7889a81399b20e24445e.html

BasePage.cs
using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Runtime.Remoting.Contexts;
using  System.Threading;
using  System.Globalization;

/**/ /// <summary>
/// Summary description for BasePage
/// </summary>

public   class  BasePage : System.Web.UI.Page
{
    
public BasePage()
    
{
        
//
        
// TODO: Add constructor logic here
        
//
    }


    
protected override void InitializeCulture()
    
{
        
string s = (string)Context.Profile.GetPropertyValue("LanguagePreference");
        
if (!string.IsNullOrEmpty(s) && (s != "Auto"))
        
{
            
//UICulture - 决定了采用哪一种本地化资源,也就是使用哪种语言
            
//Culture - 决定各种数据类型是如何组织,如数字与日期
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(s);
            Thread.CurrentThread.CurrentCulture 
=
 CultureInfo.CreateSpecificCulture(s);
        }

    }

}

MasterPage.master.cs

using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Runtime.Remoting.Contexts;

public   partial   class  MasterPage : System.Web.UI.MasterPage
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    
{
        
string language = RadioButtonList1.SelectedValue.Trim();
        Profile.LanguagePreference 
= language;
        Response.Redirect(Request.Url.AbsolutePath);
    }

}

Default.cs
public   partial   class  _Default : BasePage
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
    }

}

Web.config
< configuration >
    
< system.web >
        
< anonymousIdentification  enabled ="true" />
        
< profile >
            
< properties >
                
< add  name ="LanguagePreference"  type ="string"
                     defaultValue
="Auto"  allowAnonymous ="true"   />
            
</ properties >
        
</ profile >
    
</ system.web >
</ configuration >

转载于:https://www.cnblogs.com/syringa-flz/archive/2008/01/28/1056217.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值