【从CSLA中学C#】1 多语言的微软默认解决方案:ResourceManager

来源:互联网 发布:仿真类软件 编辑:程序博客网 时间:2024/04/29 20:52

CSLA框架时,看到了下图的内容;经过仔细跟踪,找到了ResourceManager这个好东西。

 

右键ResourceManager,在其中截了一段关键的内容

// 摘要:                                                              

// Provides convenient access to culture-specific resources at run time.

[Serializable]                                                       

[ComVisible(true)]                                                    

public class ResourceManager                                          

//

// 摘要:

//     Gets the value of the System.Object resource localized for the specified culture.

// 参数:

//   name:

//     The name of the resource to get.

//   culture:

//     The System.Globalization.CultureInfo object that represents the culture for  which the resource is localized. Note that if the resource is not localized for this culture, the lookup will fall back using the culture's System.Globalization.CultureInfo.Parent property, stopping after checking in the neutral culture.  If this value  is null, the System.Globalization.CultureInfo is obtained using the culture's   System.Globalization.CultureInfo.CurrentUICulture property.

// 返回结果:

//     The value of the resource, localized for the specified culture. If a "best match" is not possible, null is returned.

// 异常:

//   System.ArgumentNullException:

//     The name parameter is null.

//   System.Resources.MissingManifestResourceException:

//     No usable set of resources have been found, and there are no neutral culture resources.

public virtual object GetObject(string name, CultureInfo culture);

 

根据后来从网上的资源查询,我发现特别是.net开发的web系统,有很多都是用这个作为多语言的解决方案;另一种常用方式是使用xml文件保存多语言内容,使用自定义的方法解析到界面层的方案。

本文主要讨论直接使用微软提供的这个类来实现。

为了配合这一节的讲解,我完成了一个小例子WinResourceManagerUsing,并总结了它的几个使用关键点。

1Resources.***.resx 中的***必须使用图表 1‑3中规定的内容;

2.定义CultureInfo时使用***即可;

3.如果没有定义指定的资源,则会遵循默认的Resources.resx中的定义;

如果Resources.resx中依然没有定义,则会返回空字符串;

4.不要尝试使用值去翻译,比如c.Text = cm.GetString(c.Text, cin);
5
.必须使用一个固定不变的内容去多语言,否则不定因素太大,非常不利于正常开发

 

 

图表 11

 

图表 12

 

 

图表 13

总结:使用这个功能很简单,不过如果要使用好也必须有个很好的前期规划。由于真正使用的期间,通常是按钮、菜单、tabpage标题、窗口标题等属性的翻译,所以必须把可能翻译的内容规定好统一的命名,否则系统将无法查询到这些元素,也就无法为其赋值。


样例代码下载

 

原创粉丝点击