【从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,并总结了它的几个使用关键点。
1.Resources.***.resx 中的***必须使用图表 1‑3中规定的内容;
2.定义CultureInfo时使用***即可;
3.如果没有定义指定的资源,则会遵循默认的Resources.resx中的定义;
如果Resources.resx中依然没有定义,则会返回空字符串;
4.不要尝试使用值去翻译,比如c.Text = cm.GetString(c.Text, cin);
5.必须使用一个固定不变的内容去多语言,否则不定因素太大,非常不利于正常开发
图表 1‑1
图表 1‑2
图表 1‑3
总结:使用这个功能很简单,不过如果要使用好也必须有个很好的前期规划。由于真正使用的期间,通常是按钮、菜单、tabpage标题、窗口标题等属性的翻译,所以必须把可能翻译的内容规定好统一的命名,否则系统将无法查询到这些元素,也就无法为其赋值。
样例代码下载
- 【从CSLA中学C#】1 多语言的微软默认解决方案:ResourceManager
- 【从CSLA中学C#】2 Attribute的使用
- 【从CSLA中学C#】3 序列化的使用
- 【从CSLA中学C#】4 泛型的使用
- 从CSLA中学C#系列_序
- 从微软发明c语言想到的
- C语言中%*[^\n]的重要用途(从JM8.6解码器中学到的)
- 从masonry中学到的c++and关键字
- 从生活中学到的
- 从dota中学到的
- 从面试中学到的
- C语言 return多值的解决方案
- 某中学断网的解决方案
- 转:从《The C Programming Language》中学到的那些编程风格和设计思想
- 转:从《The C Programming Language》中学到的那些编程风格和设计思想
- 从《循环的代价》中学到的
- 从两个团队中学到的
- [Java]从一个Updater中学到的
- x264_scan8 的存储方式
- 获得枚举中的数值
- 如何在Win7/Vista下聆听最佳质量的音乐?(数字输出 by WASAPI)
- /bin/sh: can't access tty; job control turned off
- asp.net结合ajaxfileupload控件上传文件一直提示undefined问题
- 【从CSLA中学C#】1 多语言的微软默认解决方案:ResourceManager
- m)->p_fref[0]指向是参考帧的Y
- HDU1176 免费馅饼
- Lazarus 使用KbmMemTable控件
- .NET中的Drag and Drop操作(一)
- Delphi五种设计模式
- VC++多线程编程
- 关于Indy控件内存泄露的问题
- C#中类和结构的区别