B/S或者C/S软件中都会涉及多语言版本的问题,那么如何在编程过程中尽量减少因为多语言而带来的程序复杂性呢?下面是我想到的一个解决方案,希望广大网友们各抒己见,我用asp.net(C#)语言抛砖引玉了.

软件语言版本就是软件界面上的文字.我将界面上的文字分为两种类型:1 直接放在界面上的文字(在asp.net中大部分这些文字都是控件上一个属性的值) 2 由程序控制动态生成的文字(大部分是生成的控件上一个属性的值) ,所以所谓多语言版本的对这些文字的控制.

下面是我的解决方案:

一 对直接放在界面上的文字的处理
主要是使用资源文件来处理:(方法引用自:http://singlepine.cnblogs.com/articles/253309.html)
界面事例:

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);
}
}
}
}

二 由程序控制动态生成的文字

1 数据库设计

动态生成的文字必须在数据库中有记录,比如中文 英文 和日文,那么存放记录的字段名分别为:name ,name_en,name_jp._en和_jp别人表示英文和日文的意思,那么在整个软件的数据库设计中凡是涉及存放文字的地方都满足这种规律.

2 语言类

该类的作用统一处理涉及语言文字的字段,在页面程序中,将存放中文的的字段名(默认语言)传递到这个类中,这个类根据当前所使用的语言在语言类中给中文字段后面加上_en ,_jp等等,如果是默认语言就不要加了.

3 其他类中的使用
主要在SQL语句中使用:
language lang=new language;//定义语言类的对象实例
.....
string Lname=lang("name");//用语言类来处理name字段,这样根据当前的语言的类型,name就变成name_en或者name_jp
string sqlStr="select id,"+Lname+" from user";
.....

4 总结

如果现在要加一种新的语言版本,
只要
1 一个页面加一个资源文件(工作量会大点)
2 Global.asax文件做一些修改
3 数据库中加一个新语言的存放字段
4 修改一下语言类
一切OK了,主要的程序类库完全不需要修改.

转载于:https://www.cnblogs.com/yzxchoice/archive/2006/10/01/519754.html

asp.net(C#)软件中多语言解决方案相关推荐

  1. 基于c语言的组态软件,工业组态软件中CFC语言的设计实现及语言转换的研究-计算机应用技术专业论文.docx...

    工业组态软件中CFC语言的设计实现及语言转换的研究-计算机应用技术专业论文 摘 要 近年来随着 IEC61131-3 国际标准的正式公布与推广,基于此标准的工业自动 化领域的组态软件,经过不断完善已日 ...

  2. 开发直播app软件中关于语言国际化适配

    很多直播app软件如果需要在海外运营的话,就需要适配当地语言系统,google提供了一套很好地解决方案,下面以开发直播app软件为例简单的介绍下国际化语言方案. 1.文字资源的抽取 任何需要体现到UI ...

  3. ASP.NET 2.0中CSS失效解决方案

    经常有人遇到ASP.NET 2.0(ASP.NET 1.x中可能是有效的)中CSS失效的问题,现将主要原因和解决方法罗列如下: 1.CSS文件路径不正确 这个问题属于Web开发中的基础问题,一般采用相 ...

  4. 组态中常用c语言代码,工业组态软件中CFC语言的设计实现及语言转换的研究

    摘要: 近年来随着IEC61131-3国际标准的正式公布与推广,基于此标准的工业自动化领域的组态软件,经过不断完善已日趋成熟并获得了广泛的应用组态软件作为DCS PLC SCADA等控制系统的上位机软 ...

  5. keil c语言循环嵌套,keil软件中C语言嵌套汇编

    一.讲解背景 在单片机学习的过程中,掌握一点汇编语言是非常有必有的,作为低级语言汇编语言在单片机开发中有它不可取代的作用,比如每条指令可以精确的确定延时时间,便于理解非常适合硬件工程师学习.但是要提高 ...

  6. iar软件中C语言跳出for循环,关于 IAR一些C语言扩展

    今天在阅读RF_Example_Code_v1.0中头文件cc430x613x.h时发现了几部分的疑问. 首先来看一下cc430x613x.h 中的3个#define的例子: #define DEFC ...

  7. 唯众中职软件与信息服务专业解决方案

    一.专业背景 软件技术专业是围绕新一代信息技术专业群的智能软件人才培养,它对接新型软件和新型信息技术服务,实现软件高端化.智能化及分布式化.计算机软件在现代社会经济生活中占有极其重要的地位,在各个领域 ...

  8. 在ASP.Net 2.0中实现多语言界面的方法

    1. 跟以前一样做界面,只是注意,把所有需要有多语言界面的文字都用label来做 2. 做完以后,在Solution Explorer里选中这个文件,选Tools->Generate Local ...

  9. 犀牛建模软件的英文语言包_使用tidytext和textmineR软件包在R中进行主题建模(

    犀牛建模软件的英文语言包 In this article, we will learn to do Topic Model using tidytext and textmineR packages ...

  10. 在python语言中可作为源文件后缀名的是_在计价软件中可以新建下列哪些类型的项目( )...

    [多选题]在2013清单计价中,其他项目界面,包括( ) [单选题]锂离子电池最常用的负极材料是碳材料,其理论嵌锂容量是() [单选题]某企业为增值税一般纳税人,购买原材料取得增值税专用发票上注明的价 ...

最新文章

  1. 【C++ 语言】面向对象 ( 类定义 | 限制头文件引用次数 | 构造方法 | 析构方法 )
  2. 指针的引用做函数的参数
  3. C#验证 中国 身份证 代码
  4. Water-Net:水下图像增强基准数据集(UIEB Dataset)2019年TIP顶刊论文
  5. java设计把两个字符串的值交换 而不使用中间变量
  6. java多线程初识4
  7. 随想录(smp的一些注意事项)
  8. 金融市场中的NLP——情感分析
  9. 【转】WebService 的创建,部署和使用
  10. KITTI数据集介绍
  11. ajaxsubmit php上传文件,使用ajaxSubmit方法实现多文件上传(异步)
  12. 简易聊天软件开发(python+socket)
  13. 没什么流量的产品该怎么引流呢?淘宝上新没有流量怎么办?
  14. 用python实现一个简单的语音录入转换文字的程序
  15. 工业互联网与高端装备健康管理解决方案
  16. Android10源码下载与编译(Mac移动硬盘)
  17. LASSO回归与L1正则化 西瓜书
  18. 中国网络优化行业发展前景预测分析及投资风险评估报告2021-2027年
  19. JAVA判断键盘录取的加减乘除_java从键盘输入3个数(其中一个数用来代表加减乘除的符号,其余两个数用来计算),用来进行加减乘除...
  20. 边缘计算架构、分层及典型组网拓扑

热门文章

  1. Light OJ 1011
  2. 关于codeMirror插件使用的一个坑
  3. 关于eclipse中maven项目的问题
  4. U3D关于message的使用
  5. 微软欲对Silverlight进行部分开源(转载)
  6. 哎,老了之display-box
  7. 2019 ,我的新年Flag
  8. 1.2.4 List.contains方法——判断列表中是否包含指定元素
  9. JetS3t使用说明
  10. :将照片处理成绘画风格