主要代码不多:
Thread.CurrentThread.CurrentCulture
=
new
CultureInfo(lang);
Thread.CurrentThread.CurrentUICulture
=
Thread.CurrentThread.CurrentCulture;
![None.gif](/Images/OutliningIndicators/None.gif)
![dot.gif](/Images/dot.gif)
![dot.gif](/Images/dot.gif)
![dot.gif](/Images/dot.gif)
![dot.gif](/Images/dot.gif)
![dot.gif](/Images/dot.gif)
![dot.gif](/Images/dot.gif)
![dot.gif](/Images/dot.gif)
![dot.gif](/Images/dot.gif)
![dot.gif](/Images/dot.gif)
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)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if(Request.Cookies["resource"] != null && Request.Cookies["resource"].Value.Trim() != "")
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.Cookies["resource"].Value.Trim());
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Label lbMsg;
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitializeComponent();
this.LoadResource();
}
[STAThread]
static void Main(string[] args)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bool isFirst;
System.Threading.Mutex mutex=new System.Threading.Mutex(true,"WindowAppTest",out isFirst);
//这里的myApp是程序的标识,建议换成你的程序的物理路径,这样的话如果在一个操作系统中这个标志不会和其它程序冲突
if(!isFirst)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("Exist");
Environment.Exit(1);//实例已经存在,退出程序
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
LoadLang("zh-cn");
Application.Run(new Form1());
}
}
public static System.Resources.ResourceManager rm = null;
private void Form1_Load(object sender, System.EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
LoadLang(this.comboBox1.SelectedItem.ToString());
LoadResource();
this.lbMsg.Text = rm.GetString("Message");
}
private static void LoadLang(string lang)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(lang);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
}
private void LoadResource()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
using System;
using System.Threading;
using System.Globalization;
using System.Reflection;
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 语言管理类
/// </summary>
public class LanguageMgr
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Resources.ResourceManager rm = null;
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 构造函数
/// </summary>
/// <param name="filename">语言文件的根名称</param>
/// <param name="ass">主Assembly</param>
public LanguageMgr(string filename,Assembly ass)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
rm = new System.Resources.ResourceManager(filename,ass);
}
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获得已经指定的资源包中key对应的内容
/// </summary>
public string this[string key]
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string temp = "";
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
temp = rm.GetString(key);
}
catch
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{}
if(temp != null && temp.Trim() != "")
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return temp;
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "we can't find the string \"" + key + "\",please check your code!";
}
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获得指定字符串
/// </summary>
/// <example>
/// <code>
/// String str = Utility.LanguageMgr.LocalStrings["Message"];
/// </code>
/// </example>
public static LanguageMgr LocalStrings
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(localString == null)
throw new Exception("please call \"LoadLanguage(
)\" at first!");
else
return localString;
}
}
static LanguageMgr localString = null;
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <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)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(langName);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
localString = new LanguageMgr(filename,ass);
}
}
}
![None.gif](/Images/OutliningIndicators/None.gif)