C#笔记31:本地化或多语言支持
本章概要:
1:本地化解释
2:多语言支持
3:疑问的提出“非窗体,如控件的多语言支持”
本地化对我个人来说,更多的用处是开发多语言版本。更多的本地化内容,参考http://msdn.microsoft.com/zh-cn/library/h6270d0z.aspx。
多语言支持有很多种方法来做到,很主流的一种做法是提取应用程序中的全部语言,配置在XML文件中,如中文一个XML,英语一个XML,程序启动的时候再根据操作系统的语言环境进行加载。
但个人不喜欢这个做法,理由有为:、除非是需求非常明确的应用开发。也就是说,开发之前确定了大部分的语言要素,否则对于开发来讲,整理全部的语言要素在一个文件中,非常繁琐。维护起来也不见得方便。所以,本人支持每个页面创建资源文件来实现。
具体的做法如下(参考了http://www.cnblogs.com/greatverve/archive/2010/10/20/csharp-Multi-Language.html):
1、新建一个 Winform 应用程序, 新建一 Form ,名为 Form1;
2、 设置 Form1 的 Localizable 属性为 true, 设置该属性后,.net 将根据不同的语言,为应用程序生成不同的资源文件;
3、设置各个控件的文本(系统默认语言下);
4、更改 Form1 的 Language 属性为想要支持的另一种语言,此例中我们选用 English;
5、重新设置各个控件的文本
注:此时.net 将为 Form1 生成另一个资源文件,在本例中名为 Form1.en.resx,当你需要添加新的控件时,需要切换到default语言。
6、如果有其它的语言要设置,请重复第4,第5步;
7、编写代码 (需要消息框多语言支持的话,就用form做消息框吧。同时也做成多语言支持。)
备注:
1、对于非控件文本,也可在资源文件中定义相关的字段,然后调用字段解决。
2、对于应用程序整体的多语言支持,在应用程序入口处判断或者让用户自己选择,保存为全部变量,然后在页面基类中进行处理。
上文中只是简单的实现了多语言支持,在大型应用程序中,肯定会存在控件的开发或者中间件的开发,故,这里就提出一个问题:控件和中间件是没有Localizable属性,VS也不会自动为你生成资源文件。那么,这里的多语言支持该如何实现?
练习:
1.You need to generate a report that lists language codes and region codes. Which code segment should you
use?
A. foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{ // Output the culture information...}
B. CultureInfo culture = new CultureInfo(""); CultureTypes types = culture.CultureTypes;
// Output the culture information...
C. foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.NeutralCultures))
{ // Output the culture information...}
D. foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.ReplacementCultures))
{ // Output the culture information...}
Answer: A
2.You create an application that stores information about your customers who reside in various regions. You are
developing internal utilities for this application. You need to gather regional information about your customers
in Canada. Which code segment should you use?
A. foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{ // Output the region information...}
B. CultureInfo cultureInfo = new CultureInfo("CA"); // Output the region information...
C. RegionInfo regionInfo = new RegionInfo("CA"); // Output the region information...
D. RegionInfo regionInfo = new RegionInfo("");if (regionInfo.Name == "CA")
{ // Output the region information...}
Answer: C
3.You are developing a method that searches a string for a substring. The method will be localized to Italy.
Your method accepts the following parameters: The string to be searched, which is named searchListThe string for
which to search, which is named searchValue You need to write the code. Which code segment should you use?
A. return searchList.IndexOf(searchValue);
B. CompareInfo comparer = new CultureInfo("it-IT").CompareInfo;
return comparer.Compare(searchList, searchValue);
C. CultureInfo comparer = new CultureInfo("it-IT");
if (searchList.IndexOf(searchValue) > 0) {return true;}
else {return false;}
D. CompareInfo comparer = new CultureInfo("it-IT").CompareInfo;
if (comparer.IndexOf(searchList, searchValue) > 0) {return true;}
else { return false;}
Answer: D
4.You are developing an application for a client residing in Hong Kong. You need to display negative currency values by using a minus sign. Which code segment should you use?
A. NumberFormatInfo culture = new CultureInfo("zh-HK").NumberFormat;
culture.NumberNegativePattern = 1; return numberToPrint.ToString("C", culture);
B. NumberFormatInfo culture = new CultureInfo("zh-HK").NumberFormat;
culture.CurrencyNegativePattern = 1; return numberToPrint.ToString("C", culture);
C. CultureInfo culture =new Cult ureInfo("zh-HK"); return numberToPrint.ToString("-(0)", culture);
D. CultureInfo culture =new CultureInfo("zh-HK"); return numberToPrint.ToString("()", culture);
Answer: B
5.You are developing a fiscal report for a customer. Your customer has a main office in the United States and a
satellite office in Mexico. You need to ensure that when users in the satellite current date is displayed in Mexican Spanish format. Which code segment should you use?
A. DateTimeFormatInfo dtfi = new CultureInfo("es-MX", false).DateTimeFormat;
DateTime dt = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day);
string dateString = dt.ToString(dtfi.LongDatePattern);
B. Calendar cal = new CultureInfo("es-MX", false).Calendar;
DateTime dt = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day);
Strong dateString = dt.ToString();
C. string dateString = DateTimeFormatInfo.CurrentInfo GetMonthName(DateTime.Today.Month);
D. string dateString = DateTime.Today.Month.ToString("es-MX");
Answer: A