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

主要代码不多:

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


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

 protected void Application_BeginRequest(Object sender, EventArgs e) 
  

   
if(Request.Cookies["resource"!= null && Request.Cookies["resource"].Value.Trim() != ""
   

    Thread.CurrentThread.CurrentCulture 
= CultureInfo.CreateSpecificCulture(Request.Cookies["resource"].Value.Trim()); 
   }
 
   
else 
   

    Thread.CurrentThread.CurrentCulture 
= new CultureInfo(System.Configuration.ConfigurationSettings.AppSettings["DefaultLanguage"]); 
   }
 
   Thread.CurrentThread.CurrentUICulture 
= Thread.CurrentThread.CurrentCulture; 
  }
 

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

using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 
using System.IO; 
using System.Threading; 
using System.Globalization; 
using System.Reflection; 
namespace WindowAppTest 

 
/// <summary> 
 
/// Form1 的摘要说明。 
 
/// </summary> 

 public class Form1 : System.Windows.Forms.Form 
 

  
private System.Windows.Forms.ComboBox comboBox1; 
  
private System.Windows.Forms.Label lbMsg; 
  
/// <summary> 
  
/// 必需的设计器变量。 
  
/// </summary> 

  private System.ComponentModel.Container components = null
  
public Form1() 
  

   InitializeComponent(); 
   
this.LoadResource(); 
  }
 
  [STAThread] 
  
static void Main(string[] args) 
  

   
bool isFirst; 
    
   System.Threading.Mutex mutex
=new System.Threading.Mutex(true,"WindowAppTest",out isFirst); 
   
//这里的myApp是程序的标识,建议换成你的程序的物理路径,这样的话如果在一个操作系统中这个标志不会和其它程序冲突     
   if(!isFirst) 
   

    MessageBox.Show(
"Exist"); 
    Environment.Exit(
1);//实例已经存在,退出程序 
   }
 
   
else 
   

    LoadLang(
"zh-cn"); 
    Application.Run(
new Form1()); 
   }
 
  }
 
  
public static System.Resources.ResourceManager rm = null;
  
private void Form1_Load(object sender, System.EventArgs e) 
  

   
this.comboBox1.Items.Add("zh-cn"); 
   
this.comboBox1.Items.Add("en-us"); 
   
this.comboBox1.SelectedIndex = 0
  }
 
  
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e) 
  

   LoadLang(
this.comboBox1.SelectedItem.ToString()); 
   LoadResource(); 
   
this.lbMsg.Text = rm.GetString("Message"); 
  }
 
  
private static void LoadLang(string lang) 
  

   Thread.CurrentThread.CurrentCulture 
= new CultureInfo(lang); 
   Thread.CurrentThread.CurrentUICulture 
= Thread.CurrentThread.CurrentCulture; 
  }
 
  
private void LoadResource() 
  

   
if(rm != null
    rm.ReleaseAllResources(); 
   rm 
= new System.Resources.ResourceManager("WindowAppTest.Languages.Language",Assembly.GetExecutingAssembly()); 
  }
 
 }
 
}

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

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

 

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

namespace Utility 

 
using System; 
 
using System.Threading; 
 
using System.Globalization; 
 
using System.Reflection; 
 
/// <summary> 
 
/// 语言管理类 
 
/// </summary> 

 public class LanguageMgr 
 

  System.Resources.ResourceManager rm 
= null
  
/// <summary> 
  
/// 构造函数 
  
/// </summary> 
  
/// <param name="filename">语言文件的根名称</param> 
  
/// <param name="ass">主Assembly</param> 

  public LanguageMgr(string filename,Assembly ass) 
  

   rm 
= new System.Resources.ResourceManager(filename,ass); 
  }
 
  
/// <summary> 
  
/// 获得已经指定的资源包中key对应的内容 
  
/// </summary> 

  public string this[string key] 
  

   
get 
   

    
string temp = ""
    
try 
    


     temp 
= rm.GetString(key); 
    }
 
    
catch 
    
{} 
    
if(temp != null && temp.Trim() != ""
    

     
return temp; 
    }
 
    
else 
    

     
return "we can't find the string \"" + key + "\",please check your code!"
    }
 
   }
 
  }
 

  
/// <summary> 
  
/// 获得指定字符串 
  
/// </summary> 
  
/// <example> 
  
///  <code> 
  
///   String str = Utility.LanguageMgr.LocalStrings["Message"]; 
  
///  </code> 
  
/// </example> 

  public static LanguageMgr LocalStrings 
  

   
get 
   

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

  public static void LoadLanguage(string filename,string langName,Assembly ass) 
  

   Thread.CurrentThread.CurrentCulture 
= new CultureInfo(langName); 
   Thread.CurrentThread.CurrentUICulture 
= Thread.CurrentThread.CurrentCulture; 
   localString 
= new LanguageMgr(filename,ass); 
  }
 
 }
 
}
 

posted @ 2005-10-22 12:59  伊飏  阅读(1502)  评论(1编辑  收藏  举报