.NET程序实现多语言
2024-07-10 12:58:52
供稿:网友
首先建立.net资源文件.resx
langs.en-us.resx
<?xml version="1.0" encoding="gb2312" ?>
<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="frm-caption">
<value>english</value>
</data>
</root>
langs.zh-chs.resx
<?xml version="1.0" encoding="gb2312" ?>
<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="frm-caption">
<value>中文</value>
</data>
</root>
运行.net资源文件生成器
resgen langs.en-us.resx
resgen langs.zh-chs.resx
生成lang.en-us.resources和lang.zh-chs.resources
将2个文件分别复制到bin/debug/en-us和bin/debug/zh-chs目录里
代码写:
代码写:
public class main
{
[stathread]
static void main() {
application.run(new langs.langsfrm("en-us"));
}
}
窗体程序中:
窗体程序中:
public langsfrm(string culture){
rm=resourcemanager.createfilebasedresourcemanager("langs."+culture,[email protected]"/"+culture,null);
initializecomponent();
this.text=rm.getstring("frm-caption");
}