
1.建立工程,比如Document,配置webconfig
<appSettings>
<add key="DefaultCulture" value="zh-cn" />
<add key="CNCulture" value="zh-cn" />
<add key="ENCulture" value="en-us" />
</appSettings>
2.添加资源文件
右键添加新项目,选中Assembly Resource File,命名为strings.en-us.resx和strings.zh-cn.resx,然后配置如下
2.1 strings.en-us.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="LoginName">
<value>Username</value>
</data>
<data name="Password">
<value>Password</value>
</data>
</root>
2.2 strings.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="LoginName">
<value>用户名</value>
</data>
<data name="Password">
<value>密码</value>
</data>
</root>3.在Global.asax写如下代码
protected void Application_BeginRequest(Object sender, EventArgs e)

{
try

{
if(Request.Cookies["CultureResource"]!=null)
Thread.CurrentThread.CurrentCulture=new CultureInfo(Request.Cookies["CultureResource"].Value);
else
Thread.CurrentThread.CurrentCulture=new CultureInfo(ConfigurationSettings.AppSettings["DefaultCulture"].ToString());
Thread.CurrentThread.CurrentUICulture=Thread.CurrentThread.CurrentCulture;
}
catch(Exception)

{
Thread.CurrentThread.CurrentCulture=new CultureInfo(ConfigurationSettings.AppSettings["DefaultCulture"].ToString());
}
}4.添加测试页面
4.1 html
<HTML>
<HEAD>
<title>Login</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table align="center" cellSpacing="0" cellPadding="0" width="100%" border="0" height="100%">
<colgroup>
<col width="20%">
</col>
<col width="60%">
</col>
<col width="20%">
</col>
</colgroup>
<tr>
<td></td>
<td valign="middle">
<TABLE id="Table1" align="center" cellSpacing="0" cellPadding="0" width="100%" border="0">
<colgroup>
<col width="50%">
</col>
<col width="50%">
</col>
</colgroup>
<TR>
<TD align="right">语言选择</TD>
<TD>
<asp:Button id="Button1" runat="server" Text="中文"></asp:Button>
<asp:Button id="Button2" runat="server" Text="英文"></asp:Button></TD>
</TR>
<TR>
<TD align="right">
<asp:Label id="Label1" runat="server">Label</asp:Label></TD>
<TD>
<asp:TextBox id="txtLoginName" runat="server" Width="100%"></asp:TextBox></TD>
</TR>
<TR>
<TD align="right">
<asp:Label id="Label2" runat="server">Label</asp:Label></TD>
<TD>
<asp:TextBox id="txtPassword" runat="server" Width="100%"></asp:TextBox></TD>
</TR>
</TABLE>
</td>
<TD></TD>
</tr>
</table>
</form>
</body>
</HTML>4.2 cs代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Threading;
using System.Resources;
using System.Globalization;
using System.Diagnostics;
using System.Reflection;
namespace Document


{

/**//// <summary>
/// Summary description for Login.
/// </summary>
public class Login : System.Web.UI.Page

{
protected System.Web.UI.WebControls.TextBox txtLoginName;
protected System.Web.UI.WebControls.TextBox txtPassword;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
private void Page_Load(object sender, System.EventArgs e)

{
Label1.Text = Resource("LoginName");
Label2.Text = Resource("Password");
}


Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)

{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()

{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion


Resource#region Resource
public string Resource(string key)

{
string resourceValue = null;

CultureInfo ci = CultureInfo.CurrentCulture;
ResourceManager rm = new ResourceManager("Document.strings", Assembly.GetExecutingAssembly());
resourceValue = rm.GetString(key,ci);
return resourceValue;
}
#endregion

private void Button2_Click(object sender, System.EventArgs e)

{
this.UpdateCultureCookie(ConfigurationSettings.AppSettings["ENCulture"].ToString());
System.Web.UI.Page currentPage= (System.Web.UI.Page)this;
Response.Redirect(currentPage.Request.Url.ToString());
}

private void Button1_Click(object sender, System.EventArgs e)

{
this.UpdateCultureCookie(ConfigurationSettings.AppSettings["CNCulture"].ToString());
System.Web.UI.Page currentPage= (System.Web.UI.Page)this;
Response.Redirect(currentPage.Request.Url.ToString());

}
private void UpdateCultureCookie(string culture)

{
if(Request.Cookies["CultureResource"] != null)

{
Response.Cookies["CultureResource"].Value = culture;
Response.Cookies["CultureResource"].Expires = System.DateTime.Now.AddDays(30);
}
else

{
HttpCookie cultureCookie = new HttpCookie("CultureResource");
cultureCookie.Value = culture;
cultureCookie.Expires = System.DateTime.Now.AddDays(30);
Response.Cookies.Add(cultureCookie);
}
}
}
}5.源代码下载
/Files/singlepine/Resource.rar 6.资源文件配置小工具
使用XML读写删除功能来实现资源文件配置
Feedback
1) 对于windows应用程序如何实现多语言呢?
2) 是在程序编写代码的同时写资源文件还是等到程序全部开发完成以后再设置资 源文件?
3) <value>Password</value>按照您的这个写法,要人工将显示的信息翻译出来并写到资源文件里.我公司现在要开发一个winForm的单机版的应用程序,要在英国,西班牙,法国,德国,韩国,日本等国家的客户中使用,那么请问您我在写资源文件的时候是不是也要把各个国家的语言文字都翻译出来啊?
4) 请问您如何解决界面控件的长度与显示文字的长度之间的关系呢?如果翻译出来的某个国家的语言文字很长,那么显示在控件或菜单时会不会产生截断现象?如何避免?
5) 如何实现不同国家的货币单位,度量单位,时间日期等显示问题?国家不同表示的形式也不同,或者说对时间日期的表示形式也不同,请问该如何解决?
回复
-->对于windows我到没试过,不是很清楚,不过做法都是查不多。
2) 是在程序编写代码的同时写资源文件还是等到程序全部开发完成以后再设置资
源文件?
-->我个人认为人应该先着重于完成功能,在功能完成的情况下再做多语言设置。
3) <value>Password</value>按照您的这个写法,要人工将显示的信息翻译出来并
写到资源文件里.我公司现在要开发一个winForm的单机版的应用程序,要在英国,西
班牙,法国,德国,韩国,日本等国家的客户中使用,那么请问您我在写资源文件的时
候是不是也要把各个国家的语言文字都翻译出来啊?
-->没错,所有<value>Password</value>都需要一一对应
4) 请问您如何解决界面控件的长度与显示文字的长度之间的关系呢?如果翻译出来
的某个国家的语言文字很长,那么显示在控件或菜单时会不会产生截断现象?如何避
免?
-->这到是一个问题,在我的设计中没有严格去限制,象label等的长度都设置成了width=100%,至于其他语言下好不好看,没有去做研究,估计有这个问题。
5) 如何实现不同国家的货币单位,度量单位,时间日期等显示问题?国家不同表示的
形式也不同,或者说对时间日期的表示形式也不同,请问该如何解决?
-->这些信息可以保存到数据库,根据国家的不同动态显示 回复
2.请教一下,怎么编程动态修改资源文件呢?是不是就是修改XML的方法?对于资源文件的编程修改有没有他自己封装好的类可以直接调用的? 回复
WebCoremax.en-us.resx
WebCoremax.zh-cn.resx
在我编译后bin目录下出现两个文件夹en-us、zh-cn,里有都有WebCoremax.resources.dll
在测试页中这样使用:
public string Resource(string key)
{
string resourceValue = null;
CultureInfo ci = CultureInfo.CurrentCulture;
ResourceManager rm = new ResourceManager("WebCoremax", Assembly.GetExecutingAssembly());
resourceValue = rm.GetString(key,ci);
return resourceValue;
}
但报错为:
未能在给定的程序集中找到任何适合于指定的区域性(或非特定区域性)的资源。请确保已将“WebCoremax.resources”正确嵌入或链接到程序集“WebCoremax”。 baseName: WebCoremax locationInfo: <null> resource file name: WebCoremax.resources assembly: WebCoremax, Version=1.0.2164.30583, Culture=neutral, PublicKeyToken=null
有谁知道,
ResourceManager rm = new ResourceManager("WebCoremax", Assembly.GetExecutingAssembly()); 中的第一参数究竟应该写什么?
回复
--用 "Web工程名称.WebCoremax"就可以了 回复
{
try
{
if(Request.Cookies["CultureResource"]!=null)
Thread.CurrentThread.CurrentCulture=new CultureInfo(Request.Cookies["CultureResource"].Value);
else
Thread.CurrentThread.CurrentCulture=new CultureInfo(ConfigurationSettings.AppSettings["DefaultCulture"].ToString());
Thread.CurrentThread.CurrentUICulture=Thread.CurrentThread.CurrentCulture;
}
catch(Exception)
{
Thread.CurrentThread.CurrentCulture=new CultureInfo(ConfigurationSettings.AppSettings["DefaultCulture"].ToString());
}
}
中Thread運行時報錯,我添加了引用envdte,提示“d:\inetpub\wwwroot\Document\Global.asax.cs(39): 找不到类型或命名空间名称“Thread”(是否缺少 using 指令或程序集引用?)
” 回复
using System.Globalization;
using System.Threading;
回复
可在global.asax的Application_BeginRequest事件中.
通过检查HTTP标头的Content-Language字段
(该字段可通过UserLanguages集合访问到)来确定用户的语言. 回复