开发全球通用应用程序的最佳做法Best practices for developing world-ready applications

本节描述在开发全球通用的应用程序时应遵循的最佳做法。This section describes the best practices to follow when developing world-ready applications.

全球化最佳做法Globalization best practices

  1. 在内部使应用程序代码成为 Unicode。Make your application Unicode internally.

  2. 使用 System.Globalization 命名空间提供的区域性识别类来操作和格式化数据。Use the culture-aware classes provided by the System.Globalization namespace to manipulate and format data.

  3. 在适当的情况下,使用 System.Globalization.CultureInfo 类提供的区域性属性设置。Use the culture property settings provided by the System.Globalization.CultureInfo class in the appropriate situations. 使用 CultureInfo.CurrentCulture 属性来执行格式化任务,如日期和时间或数字的格式化。Use the CultureInfo.CurrentCulture property for formatting tasks, such as date and time or numeric formatting. 使用 CultureInfo.CurrentUICulture 属性来检索资源。Use the CultureInfo.CurrentUICulture property to retrieve resources. 请注意,CurrentCultureCurrentUICulture 属性可以通过线程进行设置。Note that the CurrentCulture and CurrentUICulture properties can be set per thread.

  4. 通过使用 System.Text 命名空间中的编码类,使应用程序能够与各种编码相互进行数据读写。Enable your application to read and write data to and from a variety of encodings by using the encoding classes in the System.Text namespace. 不要采用 ASCII 数据。Do not assume ASCII data. 假定在用户可以输入文本的任何位置都将提供国际字符。Assume that international characters will be supplied anywhere a user can enter text. 例如,应用程序应接受服务器名、目录、文件名、用户名和 URL 中的国际字符。For example, the application should accept international characters in server names, directories, file names, user names, and URLs.

  5. 使用 UTF8Encoding 类时,出于安全原因,应使用此类提供的错误检测功能。When using the UTF8Encoding class, for security reasons, use the error detection feature offered by this class. 为了打开错误检测功能,请使用具有一个 throwOnInvalidBytes 参数的构造函数来创建该类的一个实例,并将该参数的值设置为 trueTo turn on the error detection feature, create an instance of the class using the constructor that takes a throwOnInvalidBytes parameter and set the value of this parameter to true.

  6. 尽可能将字符串按整个字符串处理,而不是按一系列个别字符处理。Whenever possible, handle strings as entire strings instead of as a series of individual characters. 这在排序或搜索子字符串时尤为重要。This is especially important when sorting or searching for substrings. 这可以防止与分析组合字符有关的问题。This will prevent problems associated with parsing combined characters. 还可以通过 System.Globalization.StringInfo 类使用文本单元而不是单个字符。You can also work with units of text rather than single characters by using the System.Globalization.StringInfo class.

  7. 使用 System.Drawing 命名空间提供的类来显示文本。Display text using the classes provided by the System.Drawing namespace.

  8. 为保持操作系统间的一致性,不要允许用户设置重写 CultureInfoFor consistency across operating systems, do not allow user settings to override CultureInfo. 使用接受 CultureInfo 参数的 useUserOverride 构造函数并将此参数设置为 falseUse the CultureInfo constructor that accepts a useUserOverride parameter and set it to false.

  9. 在国际操作系统版本上使用国际数据来测试应用程序功能。Test your application functionality on international operating system versions, using international data.

  10. 如果安全决策基于字符串比较或大小写更改操作的结果,请使用不区分区域性的字符串操作。If a security decision is based on the result of a string comparison or case change operation, use a culture-insensitive string operation. 这种做法可确保结果不会受 CultureInfo.CurrentCulture 值的影响。This practice ensures that the result is not affected by the value of CultureInfo.CurrentCulture. 有关展示了区分区域性字符串比较如何产生不一致结果的示例,请参阅字符串使用最佳做法“使用当前区域性的字符串比较”部分。See the "String Comparisons that Use the Current Culture" section of Best Practices for Using Strings for an example that demonstrates how culture-sensitive string comparisons can produce inconsistent results.

本地化最佳做法Localization best practices

  1. 将所有可本地化的资源移动到单独的纯资源 DLL 中。Move all localizable resources to separate resource-only DLLs. 可本地化的资源包括用户界面元素,如字符串、错误消息、对话框、菜单以及嵌入的对象资源。Localizable resources include user interface elements, such as strings, error messages, dialog boxes, menus, and embedded object resources.

  2. 不要对字符串或用户界面资源进行硬编码。Do not hardcode strings or user interface resources.

  3. 不要将不可本地化的资源放在纯资源 DLL 中。Do not put nonlocalizable resources into the resource-only DLLs. 否则会使翻译人员产生困惑。This causes confusion for translators.

  4. 不要使用在运行时从串联词组生成的复合字符串。Do not use composite strings that are built at run time from concatenated phrases. 复合字符串难以本地化,因为它们往往采用英语语法顺序,而此顺序并不适用于所有语言。Composite strings are difficult to localize because they often assume an English grammatical order that does not apply to all languages.

  5. 避免不明确的结构,如“Empty Folder”,因为根据字符串组成部分的语法规则,这些字符串可能产生不同的翻译。Avoid ambiguous constructs such as "Empty Folder" where the strings can be translated differently depending on the grammatical roles of the string components. 例如,“empty”既可以是一个动词,也可以是一个形容词,因此在诸如意大利语或法语等语言中就可能导致不同的翻译。For example, "empty" can be either a verb or an adjective, which can lead to different translations in languages such as Italian or French.

  6. 避免在应用程序中使用包含文本的图像和图标。Avoid using images and icons that contain text in your application. 本地化这些图像和图标的成本是很大的。They are expensive to localize.

  7. 允许在用户界面中为字符串长度的扩展保留足够的空间。Allow plenty of room for the length of strings to expand in the user interface. 在某些语言中,词组所需的空间可能比在其他语言中多 50-75%。In some languages, phrases can require 50-75 percent more space than they need in other languages.

  8. 使用 System.Resources.ResourceManager 类来根据区域性检索资源。Use the System.Resources.ResourceManager class to retrieve resources based on culture.

  9. 使用 Visual Studio 创建 Windows 窗体对话框,以便可以使用 Windows 窗体资源编辑器 (Winres.exe) 对它们进行本地化。Use Visual Studio to create Windows Forms dialog boxes so they can be localized using the Windows Forms Resource Editor (Winres.exe). 不要对 Windows 窗体对话框进行手动编码。Do not code Windows Forms dialog boxes by hand.

  10. 安排进行专业本地化工作(翻译)。Arrange for professional localization (translation).

  11. 有关创建并本地化资源的完整说明,请参阅应用中的资源For a complete description of creating and localizing resources, see Resources in Applications.

ASP.NET 应用程序的全球化最佳做法Globalization best practices for ASP.NET applications

  1. 在应用程序中显式设置 CurrentUICultureCurrentCulture 属性。Explicitly set the CurrentUICulture and CurrentCulture properties in your application. 不要依赖于默认设置。Do not rely on defaults.

  2. 请注意,ASP.NET 应用程序是托管应用程序,因此可以使用与其他托管应用程序相同的类,以根据区域性检索、显示和操作信息。Note that ASP.NET applications are managed applications and therefore can use the same classes as other managed applications for retrieving, displaying, and manipulating information based on culture.

  3. 注意在 ASP.NET 中可以指定以下三种编码类型:Be aware that you can specify the following three types of encodings in ASP.NET:

    • requestEncoding 指定从客户端浏览器接收的编码。requestEncoding specifies the encoding received from the client's browser.

    • responseEncoding 指定要发送到客户端浏览器的编码。responseEncoding specifies the encoding to send to the client browser. 在大多数情形下,此编码应该与为 requestEncoding 指定的编码相同。In most situations, this encoding should be the same as that specified for requestEncoding.

    • fileEncoding 指定用于 .aspx、.asmx 和 .asax 文件分析的默认编码。fileEncoding specifies the default encoding for .aspx, .asmx, and .asax file parsing.

  4. 在 ASP.NET 应用程序中的以下三个位置指定 requestEncoding、responseEncoding、fileEncoding、culture 和 uiCulture 特性的值:Specify the values for the requestEncoding, responseEncoding, fileEncoding, culture, and uiCulture attributes in the following three places in an ASP.NET application:

    • 在 Web.config 文件的全球化一节中。In the globalization section of a Web.config file. 此文件是 ASP.NET 应用程序的外部文件。This file is external to the ASP.NET application. 有关详细信息,请参阅 <globalization> 元素For more information, see <globalization> Element.

    • 在页面指令中。In a page directive. 请注意,当应用程序在页面中时,文件已经被读取。Note that, when an application is in a page, the file has already been read. 因此,指定 fileEncoding 和 requestEncoding 为时已晚。Therefore, it is too late to specify fileEncoding and requestEncoding. 只有 uiCulture、Culture 和 responseEncoding 可以在页面指令中指定。Only uiCulture, Culture, and responseEncoding can be specified in a page directive.

    • 在应用程序代码中以编程方式指定。Programmatically in application code. 该设置可能随请求的不同而不同。This setting can vary per request. 同页面指令一样,到打开应用程序代码时,指定 fileEncoding 和 requestEncoding 为时已晚。As with a page directive, by the time the application's code is reached it is too late to specify fileEncoding and requestEncoding. 只有 uiCulture、Culture 和 responseEncoding 可以在应用程序代码中指定。Only uiCulture, Culture, and responseEncoding can be specified in application code.

  5. 请注意,uiCulture 值可以设置为浏览器接受的语言。Note that the uiCulture value can be set to the browser accept language.

请参阅See also