全球化 API 在 Windows 上使用 ICU 库Globalization APIs use ICU libraries on Windows

当在 Windows 10 2019 年 5 月更新或更高版本上运行时,.NET 5.0 及更高版本使用 Unicode 国际组件 (ICU) 库来实现全球化功能。.NET 5.0 and later versions use International Components for Unicode (ICU) libraries for globalization functionality when running on Windows 10 May 2019 Update or later.

更改描述Change description

在 .NET Core 1.0 - 3.1 和 .NET Framework 4 及更高版本中,.NET 库使用本地语言支持 (NLS) API 来实现 Windows 上的全球化功能。In .NET Core 1.0 - 3.1 and .NET Framework 4 and later, .NET libraries use National Language Support (NLS) APIs for globalization functionality on Windows. 例如,NLS 函数用于比较字符串,获取区域性信息,并在适当的区域中执行字符串大小写。For example, NLS functions were used to compare strings, get culture information, and perform string casing in the appropriate culture.

默认情况下,从 .NET 5.0 开始,如果应用在 Windows 10 2019 年 5 月更新或更高版本上运行,.NET 库将使用 ICU 全球化 API。Starting in .NET 5.0, if an app is running on Windows 10 May 2019 Update or later, .NET libraries use ICU globalization APIs, by default.

备注

Windows 10 2019 年 5 月更新及更高版本随 ICU 本机库一起提供。Windows 10 May 2019 Update and later versions ship with the ICU native library. 如果 .NET 运行时无法加载 ICU,它将改用 NLS。If the .NET runtime can't load ICU, it uses NLS instead.

行为差异Behavioral differences

即使你不知道正在使用全球化设施,你也可能会在应用中看到更改。You might see changes in your app even if you don't realize you're using globalization facilities. 本部分列出了你可能会看到的一些行为更改,但还有其他一些行为更改。This section lists a couple of the behavioral changes you might see, but there are others too.

String.IndexOfString.IndexOf

请考虑使用以下代码,它调用 String.IndexOf(String) 来查找字符串中的换行符索引。Consider the following code that calls String.IndexOf(String) to find the index of the newline character in a string.

string s = "Hello\r\nworld!";
int idx = s.IndexOf("\n");
Console.WriteLine(idx);
  • 在 Windows 上的早期版本的 .NET 中,代码片段打印 6In previous versions of .NET on Windows, the snippet prints 6.
  • 在 Windows 19H1 和更高版本上的 .NET 5.0 及更高版本中,代码片段打印 -1In .NET 5.0 and later versions on Windows 19H1 and later versions, the snippet prints -1.

若要通过执行序号搜索而不是区分区域性的搜索来修复此代码,请调用 IndexOf(String, StringComparison) 重载,并传入 StringComparison.Ordinal 作为参数。To fix this code by conducting an ordinal search instead of a culture-sensitive search, call the IndexOf(String, StringComparison) overload and pass in StringComparison.Ordinal as an argument.

可以运行代码分析规则 CA1307:为了清晰起见,请指定 StringComparisonCA1309:使用序号 StringComparison 在代码中查找这些调用站点。You can run code analysis rules CA1307: Specify StringComparison for clarity and CA1309: Use ordinal StringComparison to find these call sites in your code.

有关详细信息,请参阅在 .NET 5 及更高版本中比较字符串时的行为更改For more information, see Behavior changes when comparing strings on .NET 5+.

货币符号Currency symbol

请考虑使用以下代码,它使用货币格式说明符 C 设置字符串格式。Consider the following code that formats a string using the currency format specifier C. 当前线程的区域性设置为仅包括语言(而非国家/地区)的区域性。The current thread's culture is set to a culture that includes only the language and not the country.

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de");
string text = string.Format("{0:C}", 100);
  • 在 Windows 上的早期版本的 .NET 中,文本值为 "100,00 €"In previous versions of .NET on Windows, the value of text is "100,00 €".
  • 在 Windows 19H1 和更高版本上的 .NET 5.0 及更高版本中,文本值为 "100,00 ¤",它使用国际货币符号而不是欧元。In .NET 5.0 and later versions on Windows 19H1 and later versions, the value of text is "100,00 ¤", which uses the international currency symbol instead of the euro. 在 ICU 中,这种设计是指,货币是国家或地区的属性,而不是语言。In ICU, the design is that a currency is a property of a country or region, not a language.

更改原因Reason for change

引入此更改是为了统一所有支持的操作系统上的 .NET 的全球化行为。This change was introduced to unify .NET's globalization behavior across all supported operating systems. 它还能让应用程序捆绑自己的全球化库,而不是依赖于操作系统的内置库。It also provides the ability for applications to bundle their own globalization libraries rather than depend on the operating system's built-in libraries.

引入的版本Version introduced

.NET 5.0.NET 5.0

开发人员一方不需要执行任何操作。No action is required on the part of the developer. 但是,如果你想要继续使用 NLS 全球化 API,则可以将运行时开关设置为还原到该行为。However, if you wish to continue using NLS globalization APIs, you can set a run-time switch to revert to that behavior. 有关可用开关的详细信息,请参阅 .NET 全球化和 ICU 一文。For more information about the available switches, see the .NET globalization and ICU article.

受影响的 APIAffected APIs