ICU(International Components for Unicode,Unicode国际组件)是一套成熟,使用广泛的开源全球化API。ICU利用Unicode庞大的通用语言环境数据存储库(Common Locale Data Repository,CLDR)作为其数据库,为软件应用程序提供了全球化支持。ICU具有广泛的可移植性,可以在所有平台上为应用程序提供相同的结果。
ICU提供的全球化API服务的重点
- 代码页转换:将文本数据与Unicode以及几乎任何其他字符集或编码进行文本数据转换。ICU的转换表基于IBM数十年来收集的字符集数据,并且在任何地方都是最完整的。
- 排序规则:根据特定语言,地区或国家/地区的约定和标准比较字符串。ICU的归类基于Unicode归类算法以及CLDR中特定于语言环境的比较规则。
- 格式设置:根据所选区域设置的格式设置数字,日期,时间和货币金额。这包括将月份和日期名称翻译成所选语言,选择适当的缩写,正确地订购字段等。此数据也来自“通用语言环境”数据存储库。
- 时间计算:除了传统的公历之外,还提供多种类型的日历。提供了一套完整的时区计算API。
- Unicode支持:ICU密切跟踪Unicode标准,可轻松访问所有的许多Unicode字符属性,Unicode规范化,大小写转换和其他基本操作由指定的Unicode标准。
- 正则表达式:ICU的正则表达式完全支持Unicode,同时提供非常有竞争力的性能。
- Bidi:支持处理包含从左到右(英语)和从右到左(阿拉伯语或希伯来语)数据的混合文本。
有关更多信息,您可以访问ICU网站:http : //site.icu-project.org/
概述
在Windows 10 Creators Update中,ICU已集成到Windows中,从而使C API和数据可公开访问。
Windows中的ICU版本仅公开C API。它不公开任何C ++ API。不幸的是,由于C ++中缺少稳定的ABI,因此不可能公开C ++ API。
有关ICU C API的文档,请参阅此处的官方ICU文档页面:http : //icu-project.org/apiref/icu4c/index.html#Module
Windows中ICU库的更改历史记录
1703版(创建者更新)
ICU库最初是在此版本中添加到Windows 10操作系统的。它被添加为:
- 两个系统DLL:
- icuuc.dll(这是ICU“公共”库)
- icuin.dll(这是ICU“ i18n”库)
- Windows 10 SDK中的两个头文件:
- icucommon.h
- icui18n.h
- Windows 10 SDK中有两个导入库:
- icuuc.lib
- icuin.lib
1709版(秋季创作者更新)
添加了一个组合的头文件icu.h,其中包含上述两个头文件的内容(icucommon.h和icui18n.h),并且还将的类型更改UCHAR
为char16_t
。
1903版(2019年5月更新)
添加了一个新的组合DLL icu.dll,其中包含“ common”和“ i18n”库。此外,新的导入库已添加到Windows 10 SDK:icu.lib。
今后,不会将新的API添加到旧的标头(icucommon.h和icui18n.h)或旧的导入库(icuuc.lib和icuin.lib)中。新的API仅会添加到组合的标头(icu.h)和组合的导入库(icu.lib)中。
入门
遵循三个主要步骤:(Windows 10 Creators Update或更高版本)
- 您的应用程序需要以Windows 10版本1703(创建者更新)或更高版本为目标。
- 添加标题:
#include <icucommon.h>
#include <icui18n.h>
在Windows 10版本1709及更高版本上,您应改为包含组合标题:
#include <icu.h>
- 链接到两个库:
- icuuc.lib
- icuin.lib
在Windows 10版本1903和更高版本上,您应该改用合并的库:
- icu.lib
然后,您可以从所需的这些库中调用任何ICU C API。(没有公开的C ++ API。)
如果使用的是旧版导入库icuuc.lib和icuin.lib,请确保在“其他依赖项链接程序”设置中将它们列在伞形库(如onecoreuap.lib或WindowsApp.lib)之前(请参见下图)。否则,链接器将链接到icu.lib,这将导致尝试在运行时加载icu.dll。该DLL仅从1903版开始存在。因此,如果用户在1903版之前的Windows计算机上升级Windows 10 SDK,则该应用程序将无法加载和运行。有关Windows中ICU库的历史记录,请参阅Windows中ICU库的更改历史记录。
笔记
- 这是“所有平台”的配置。
- 为了使Win32应用程序使用ICU,他们需要首先调用CoInitializeEx。
- 并非所有ICU API返回的数据都将与Windows OS对齐,因为此对齐工作仍在进行中。
ICU示例应用程序
示例代码段
以下示例说明了如何在C ++ UWP应用程序中使用ICU API。(它不打算成为一个完整的独立应用程序,而只是一个调用ICU方法的示例。)
下面的小示例假定有ErrorMessage和OutputMessage方法以某种方式将字符串输出给用户。
句法复制
// On Windows 10 Creators Update, include the following two headers. With Windows 10 Fall Creators Update and later, you can just include the single header <icu.h>.
#include <icucommon.h>
#include <icui18n.h>
void FormatDateTimeICU()
{
UErrorCode status = U_ZERO_ERROR;
// Create a ICU date formatter, using only the 'short date' style format.
UDateFormat* dateFormatter = udat_open(UDAT_NONE, UDAT_SHORT, nullptr, nullptr, -1, nullptr, 0, &status);
if (U_FAILURE(status))
{
ErrorMessage(L"Failed to create date formatter.");
return;
}
// Get the current date and time.
UDate currentDateTime = ucal_getNow();
int32_t stringSize = 0;
// Determine how large the formatted string from ICU would be.
stringSize = udat_format(dateFormatter, currentDateTime, nullptr, 0, nullptr, &status);
if (status == U_BUFFER_OVERFLOW_ERROR)
{
status = U_ZERO_ERROR;
// Allocate space for the formatted string.
auto dateString = std::make_unique<UChar[]>(stringSize + 1);
// Format the date time into the string.
udat_format(dateFormatter, currentDateTime, dateString.get(), stringSize + 1, nullptr, &status);
if (U_FAILURE(status))
{
ErrorMessage(L"Failed to format the date time.");
return;
}
// Output the formatted date time.
OutputMessage(dateString.get());
}
else
{
ErrorMessage(L"An error occured while trying to determine the size of the formatted date time.");
return;
}
// We need to close the ICU date formatter.
udat_close(dateFormatter);
}
翻译:International Components for Unicode (ICU) - Win32 apps | Microsoft Learn