程序的多语言支持解决方案(WinForm+WebForm)

主要代码不多:

None.gif Thread.CurrentThread.CurrentCulture  =   new  CultureInfo(lang); 
None.gifThread.CurrentThread.CurrentUICulture 
=  Thread.CurrentThread.CurrentCulture; 
None.gifdot.gifdot.gifdot.gifdot.gifdot.gifdot.gifdot.gifdot.gifdot.gifdot.gif 
None.gif
public   static  System.Resources.ResourceManager rm  =   null
None.gifrm 
=   new  System.Resources.ResourceManager( " WindowAppTest.Languages.Language " ,Assembly.GetExecutingAssembly()); 


------------------------------------------------------------------------------------
详细代码(Web程序)
------------------------------------------------------------------------------------
Global.asax:
 
 

None.gif   protected   void  Application_BeginRequest(Object sender, EventArgs e) 
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif
InBlock.gif   
if(Request.Cookies["resource"!= null && Request.Cookies["resource"].Value.Trim() != ""
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    Thread.CurrentThread.CurrentCulture 
= CultureInfo.CreateSpecificCulture(Request.Cookies["resource"].Value.Trim()); 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    Thread.CurrentThread.CurrentCulture 
= new CultureInfo(System.Configuration.ConfigurationSettings.AppSettings["DefaultLanguage"]); 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   Thread.CurrentThread.CurrentUICulture 
= Thread.CurrentThread.CurrentCulture; 
ExpandedBlockEnd.gif  }
 
None.gif

/*------------------------------------------------------------------------------------
使用的时候:
   System.Resources.ResourceManager rm = new System.Resources.ResourceManager("TestWebApp.Languages.Language",typeof(WebForm1).Assembly);
   Response.Write(rm.GetString("Message"));
------------------------------------------------------------------------------------
详细代码(widows程序):
------------------------------------------------------------------------------------*/

None.gif using  System; 
None.gif
using  System.Drawing; 
None.gif
using  System.Collections; 
None.gif
using  System.ComponentModel; 
None.gif
using  System.Windows.Forms; 
None.gif
using  System.Data; 
None.gif
using  System.IO; 
None.gif
using  System.Threading; 
None.gif
using  System.Globalization; 
None.gif
using  System.Reflection; 
None.gif
namespace  WindowAppTest 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//// <summary> 
InBlock.gif 
/// Form1 的摘要说明。 
ExpandedSubBlockEnd.gif 
/// </summary> 

InBlock.gif public class Form1 : System.Windows.Forms.Form 
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif
InBlock.gif  
private System.Windows.Forms.ComboBox comboBox1; 
InBlock.gif  
private System.Windows.Forms.Label lbMsg; 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 必需的设计器变量。 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  private System.ComponentModel.Container components = null
InBlock.gif  
public Form1() 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   InitializeComponent(); 
InBlock.gif   
this.LoadResource(); 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif  [STAThread] 
InBlock.gif  
static void Main(string[] args) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
bool isFirst; 
InBlock.gif    
InBlock.gif   System.Threading.Mutex mutex
=new System.Threading.Mutex(true,"WindowAppTest",out isFirst); 
InBlock.gif   
//这里的myApp是程序的标识,建议换成你的程序的物理路径,这样的话如果在一个操作系统中这个标志不会和其它程序冲突     
InBlock.gif
   if(!isFirst) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    MessageBox.Show(
"Exist"); 
InBlock.gif    Environment.Exit(
1);//实例已经存在,退出程序 
ExpandedSubBlockEnd.gif
   }
 
InBlock.gif   
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    LoadLang(
"zh-cn"); 
InBlock.gif    Application.Run(
new Form1()); 
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif  
public static System.Resources.ResourceManager rm = null;
InBlock.gif  
private void Form1_Load(object sender, System.EventArgs e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
this.comboBox1.Items.Add("zh-cn"); 
InBlock.gif   
this.comboBox1.Items.Add("en-us"); 
InBlock.gif   
this.comboBox1.SelectedIndex = 0
ExpandedSubBlockEnd.gif  }
 
InBlock.gif  
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   LoadLang(
this.comboBox1.SelectedItem.ToString()); 
InBlock.gif   LoadResource(); 
InBlock.gif   
this.lbMsg.Text = rm.GetString("Message"); 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif  
private static void LoadLang(string lang) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   Thread.CurrentThread.CurrentCulture 
= new CultureInfo(lang); 
InBlock.gif   Thread.CurrentThread.CurrentUICulture 
= Thread.CurrentThread.CurrentCulture; 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif  
private void LoadResource() 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
if(rm != null
InBlock.gif    rm.ReleaseAllResources(); 
InBlock.gif   rm 
= new System.Resources.ResourceManager("WindowAppTest.Languages.Language",Assembly.GetExecutingAssembly()); 
ExpandedSubBlockEnd.gif  }
 
ExpandedSubBlockEnd.gif }
 
ExpandedBlockEnd.gif}

 
----------------------------------------------------------------------------
Language.zh-cn.resx:
----------------------------------------------------------------------------

None.gif <? xml version="1.0" encoding="utf-8"  ?>  
None.gif
< root >  
None.gif 
< xsd:schema  id ="root"  xmlns =""  xmlns:xsd ="http://www.w3.org/2001/XMLSchema"  xmlns:msdata ="urn:schemas-microsoft-com:xml-msdata" >  
None.gif  
< xsd:element  name ="root"  msdata:IsDataSet ="true" >  
None.gif   
< xsd:complexType >  
None.gif    
< xsd:choice  maxOccurs ="unbounded" >  
None.gif     
< xsd:element  name ="data" >  
None.gif      
< xsd:complexType >  
None.gif       
< xsd:sequence >  
None.gif        
< xsd:element  name ="value"  type ="xsd:string"  minOccurs ="0"  msdata:Ordinal ="1"   />  
None.gif        
< xsd:element  name ="comment"  type ="xsd:string"  minOccurs ="0"  msdata:Ordinal ="2"   />  
None.gif       
</ xsd:sequence >  
None.gif       
< xsd:attribute  name ="name"  type ="xsd:string"   />  
None.gif       
< xsd:attribute  name ="type"  type ="xsd:string"   />  
None.gif       
< xsd:attribute  name ="mimetype"  type ="xsd:string"   />  
None.gif      
</ xsd:complexType >  
None.gif     
</ xsd:element >  
None.gif     
< xsd:element  name ="resheader" >  
None.gif      
< xsd:complexType >  
None.gif       
< xsd:sequence >  
None.gif        
< xsd:element  name ="value"  type ="xsd:string"  minOccurs ="0"  msdata:Ordinal ="1"   />  
None.gif       
</ xsd:sequence >  
None.gif       
< xsd:attribute  name ="name"  type ="xsd:string"  use ="required"   />  
None.gif      
</ xsd:complexType >  
None.gif     
</ xsd:element >  
None.gif    
</ xsd:choice >  
None.gif   
</ xsd:complexType >  
None.gif  
</ xsd:element >  
None.gif 
</ xsd:schema >  
None.gif 
< resheader  name ="ResMimeType" >  
None.gif  
< value > text/microsoft-resx </ value >  
None.gif 
</ resheader >  
None.gif 
< resheader  name ="Version" >  
None.gif  
< value > 1.0.0.0 </ value >  
None.gif 
</ resheader >  
None.gif 
< resheader  name ="Reader" >  
None.gif  
< value > System.Resources.ResXResourceReader, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 </ value >  
None.gif 
</ resheader >  
None.gif 
< resheader  name ="Writer" >  
None.gif  
< value > System.Resources.ResXResourceWriter, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 </ value >  
None.gif 
</ resheader >  
None.gif 
< data  name ="Message" >  
None.gif  
< value > 你好 </ value >  
None.gif 
</ data >  
None.gif
</ root >  

 

--------------------------------------------------------
整理了一个简单的语言管理类 
 

None.gif namespace  Utility 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif 
using System; 
InBlock.gif 
using System.Threading; 
InBlock.gif 
using System.Globalization; 
InBlock.gif 
using System.Reflection; 
ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//// <summary> 
InBlock.gif 
/// 语言管理类 
ExpandedSubBlockEnd.gif 
/// </summary> 

InBlock.gif public class LanguageMgr 
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif
InBlock.gif  System.Resources.ResourceManager rm 
= null
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 构造函数 
InBlock.gif  
/// </summary> 
InBlock.gif  
/// <param name="filename">语言文件的根名称</param> 
ExpandedSubBlockEnd.gif  
/// <param name="ass">主Assembly</param> 

InBlock.gif  public LanguageMgr(string filename,Assembly ass) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   rm 
= new System.Resources.ResourceManager(filename,ass); 
ExpandedSubBlockEnd.gif  }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 获得已经指定的资源包中key对应的内容 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  public string this[string key] 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
string temp = ""
InBlock.gif    
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif
InBlock.gif     temp 
= rm.GetString(key); 
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{} 
InBlock.gif    
if(temp != null && temp.Trim() != ""
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif     
return temp; 
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif     
return "we can't find the string \"" + key + "\",please check your code!"
ExpandedSubBlockEnd.gif    }
 
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 获得指定字符串 
InBlock.gif  
/// </summary> 
InBlock.gif  
/// <example> 
InBlock.gif  
///  <code> 
InBlock.gif  
///   String str = Utility.LanguageMgr.LocalStrings["Message"]; 
InBlock.gif  
///  </code> 
ExpandedSubBlockEnd.gif  
/// </example> 

InBlock.gif  public static LanguageMgr LocalStrings 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
if(localString == null
InBlock.gif     
throw new Exception("please call \"LoadLanguage(dot.gif)\" at first!"); 
InBlock.gif    
else 
InBlock.gif     
return localString; 
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif  
static LanguageMgr localString = null
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 初始化资源 
InBlock.gif  
/// </summary> 
InBlock.gif  
/// <example> 
InBlock.gif  
///  <code> 
InBlock.gif  
///   LanguageMgr.LoadLanguage("TestWebApp.Languages.Language",System.Configuration.ConfigurationSettings.AppSettings["DefaultLanguage"],System.Reflection.Assembly.GetExecutingAssembly()); 
InBlock.gif  
///  </code> 
ExpandedSubBlockEnd.gif  
/// </example> 

InBlock.gif  public static void LoadLanguage(string filename,string langName,Assembly ass) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   Thread.CurrentThread.CurrentCulture 
= new CultureInfo(langName); 
InBlock.gif   Thread.CurrentThread.CurrentUICulture 
= Thread.CurrentThread.CurrentCulture; 
InBlock.gif   localString 
= new LanguageMgr(filename,ass); 
ExpandedSubBlockEnd.gif  }
 
ExpandedSubBlockEnd.gif }
 
ExpandedBlockEnd.gif}
 
None.gif
None.gif

转载于:https://www.cnblogs.com/iyond/archive/2005/10/22/259806.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值