62,041
社区成员
发帖
与我相关
我的任务
分享
Button1.Text
Button1.BackColor
Label1.Text
<asp:Button ID="Button1" runat="server" Text="DefaultText"
meta:resourcekey="Button1" />
/*Author:yfqvip */
/*日期:2007-6-27*/
/*功能:实现不同语言切换*/
using System;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using AspxControls.BasicControls.Tab;
namespace LanguageSwitch
{
/// <summary>
///调用方法: LanguageSwitch.TravelsalControl.Travelsal(this);
/// </summary>
public class TravelsalControl
{
public TravelsalControl()
{
}
#region 遍历指定用户控件上所有的控件,public static void Travelsal()
/// <summary>
/// 遍历指定用户控件上所有的控件,包括Label,TextBox,LinkButton,RadioButton,Button,ImageButton
/// HyperLink,CheckBox,Image,DropDownList
/// </summary>
/// <param name="page"> 指定的页面</param>
public static void TravelsalUserControl(System.Web.UI.UserControl page,string filePath)
{
string strFileName = page.ToString();
strFileName = strFileName.Substring(4,strFileName.IndexOf("_aspx")-4);
int iPageControls = page.Controls.Count;
for (int i = 0; i < iPageControls; i++)
{
foreach (System.Web.UI.Control control in page.Controls)
{
if (control is Label)
(control as Label).Text=LanguageSwitch((control as Label).ID,strFileName,filePath,(control as Label).Text);
if(control is TextBox)
(control as TextBox).Text=LanguageSwitch((control as TextBox).ID,strFileName,filePath,(control as TextBox).Text);
if (control is Button)
(control as Button).Text=LanguageSwitch((control as Button).ID,strFileName,filePath,(control as Button).Text);
if (control is LinkButton)
(control as LinkButton).Text=LanguageSwitch((control as LinkButton).ID,strFileName,filePath,(control as LinkButton).Text);
if(control is ImageButton)
(control as ImageButton).ImageUrl=LanguageSwitch((control as ImageButton).ID,strFileName,filePath,(control as ImageButton).ImageUrl);
if(control is HyperLink)
(control as HyperLink).Text=LanguageSwitch((control as HyperLink).ID,strFileName,filePath,(control as HyperLink).Text);
if (control is CheckBox)
(control as CheckBox).Text=LanguageSwitch((control as CheckBox).ID,strFileName,filePath,(control as CheckBox).Text);
if (control is RadioButton)
(control as RadioButton).Text=LanguageSwitch((control as RadioButton).ID,strFileName,filePath,(control as RadioButton).Text);
if(control is Image)
(control as Image).ImageUrl=LanguageSwitch((control as Image).ID,strFileName,filePath,(control as Image).ImageUrl);
if(control is DropDownList)
{
string [] strItems = (LanguageSwitch((control as DropDownList).ID,strFileName,filePath,(control as DropDownList).SelectedItem.Text)).Split('|');
DropDownList ddl = control as DropDownList;
for(int j=0;j<strItems.Length;j++)
{
ddl.Items.Add(strItems[j]);
}
}
}
}
}
#endregion
protected void Application_Start(Object sender, EventArgs e)
{
Hashtable CultHashTable = new Hashtable();
// CultHashTable.Add("en-US","en-US");
// CultHashTable.Add("ko-KR","ko-KR");
CultHashTable.Add("zh-CN","zh-CN");
CultHashTable.Add("zh-TW","zh-TW");
Application["CultHashTable"] = CultHashTable;
Application["rm"]=new System.Resources.ResourceManager("Artdio.Web.strings",System.Reflection.Assembly.Load("Artdio.Web"));
}
protected void Application_BeginRequest(Object sender, EventArgs e)
{
try
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);
}
catch
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN");
}
if (((Hashtable)Application["CultHashTable"]).Contains(Thread.CurrentThread.CurrentCulture.Name.ToString()))
{
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
}
else
{
Application["img"] = "zh-CN";
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("zh-CN");
}
}
#region 读资源文件
public ResourceManager RM
{
get
{
try
{
if(Application["rm"]==null)
{
rm = new ResourceManager("Artdio.Web.strings",Assembly.Load("Artdio.Web"));
}
else
{
rm=(ResourceManager)Application["rm"];
}
}
catch
{
rm = new ResourceManager("Artdio.Web.strings",Assembly.Load("Artdio.Web"));
}
return rm;
}
}
#endregion
string resourceValue = null;//读到的值
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CurrentCulture;
string str="....";//路径
System.Resources.ResourceManager rm = new System.Resources.ResourceManager(str, System.Reflection.Assembly.GetExecutingAssembly());
resourceValue = rm.GetString(key,ci);//KEY是键