波兰月 "January" 的 COleDateTime::Format 变为 return "stycznia" 而不是 "styczeń"

Getting COleDateTime::Format to return "stycznia" instead of "styczeń" for Polish month "January"

我有这种方法可以将语言环境设置为波兰语:

void CMeetingScheduleAssistantApp::SetLocale()
{
    // https://www.microsoft.com/resources/msdn/goglobal/default.mspx#ISO2
    CString strLang[NUM_LANGUAGES] =
    {
        _T("plk")
        // Add more languages here
    };

    _tsetlocale(LC_ALL, strLang[m_eLanguage - LANGUAGE_ENGLISH]);
}

为简洁起见,我删除了其他语言。现在,当我格式化一个 COleDateTime 对象并显示月份时,比如一月,它显示为:

styczeń

但我想显示为:

stycznia

是否有区域设置可以调整 COleDateTime::Format 方法或区域返回的月份值?

否则我将不得不添加一些手动覆盖。

我想要返回的月份是:

  1. 斯蒂奇尼亚
  2. 琵琶行
  3. 马卡报
  4. 奎特尼亚
  5. 马哈
  6. 切尔瓦卡
  7. 利普卡
  8. 银杏
  9. 乌热尼亚
  10. października
  11. 列表
  12. 格鲁尼亚

更新

根据 here 它指出:

Some languages, such as Finnish, German, Polish, and Russian, have several noun forms. If you plan to use localized names provided by the system, have a linguist check to make sure you are using them in the right context. Windows carries both the nominative and genitive forms of Polish and Russian month names; the form changes depending on the month name's position in the string relative to the day name. Windows returns both forms in a single string separated by a null value. The system carries only one form of each month name or day name for all other languages.

现在,这就是我实际格式化日期字符串的方式(因为我支持 40 多种语言,所以有点棘手。所以,(对于英语)我从这个格式字符串开始:

%1 %2!d!-%3!d!
  1. %1
  2. %2!d! 第一天 值。
  3. %3!d! 第二天 值。

如果我的约会需要跨越两个月,我有:

%1 %2!d!–%3 %4!d!
  1. %1 值。
  2. %2!d! 第一天 值。
  3. %3 第二个月 的值。
  4. %3!d! 第二天 值。

上面是这样使用的:

if (datThisMonday.GetMonth() == datEndOfWeek.GetMonth())
{
    strDate.FormatMessage(IDS_STR_TPL_OCLM_WEEK,
            datThisMonday.Format(_T("%B")), datThisMonday.GetDay(), datEndOfWeek.GetDay());
}
else
{
    strDate.FormatMessage(IDS_STR_TPL_OCLM_WEEK2,
            datThisMonday.Format(_T("%B")), datThisMonday.GetDay(),
            datEndOfWeek.Format(_T("%B")), datEndOfWeek.GetDay());
}

对于波兰语,我各自的格式字符串是:

%2!d!-%3!d! %1
%2!d! %1–%4!d! %3

所以,我可以看到,因为我正在使用 FormatMessage 格式化日期字符串并且仅使用 COleDateTime::Format 方法来解析 month这可能是问题的原因。

因为我在日期字符串中有两个日期,所以我不能只使用一个日期格式 API 调用(因为我的日期字符串代表一周跨度)。

所以我检查了:

strDate = datThisMonday.Format(_T("%d %B"));

这没什么区别。所以我尝试了这个:

SYSTEMTIME sTime;
datThisMonday.GetAsSystemTime(sTime);
GetDateFormat(GetThreadLocale(), 
              DATE_LONGDATE, 
              &sTime, _T("d MMMM"), 
              strDate.GetBuffer(_MAX_PATH), _MAX_PATH);

没有区别。它仍然以与以前相同的方式显示日期。即使它确实正确显示了日期,它也不会考虑来自两个 COleDateTime 对象的日期范围

困惑。

更新 2:

也尝试过:

TCHAR szDate[100] = _T("");
GetDateFormatEx(_T("pl"), NULL, &sTime, _T("ddd MMMM"), szDate, 100, NULL);
AfxMessageBox(szDate);

只是不会显示变体。

更新 3

我让它显示正确日期的唯一方法是这样的:

GetDateFormatEx(_T("pl"), DATE_LONGDATE, &sTime, NULL, szDate, 100, NULL);

那么月份就对了。现在在我提到的文章中指出:

Windows returns both forms in a single string separated by a null value.

我什至不知道如何访问它。

我还遇到了其他问题,但它们与这个问题不同,所以我仍然会在这里提供我的答案。

首先,我找到了一个 document,其中指出您必须在日期中包含 d 才能显示正确的月份版本。

因为我想显示一个日期范围,所以我从这个开始:

波兰语

模板:%1-%2 日期 1:d 日期 2:d MMMM

然后我格式化日期:

SYSTEMTIME sysTime;
ENSURE(rDate.GetAsSystemTime(sysTime));
GetDateFormatEx(_T("pl"),
    NULL,
    &sysTime, 
    strDateFormat, 
    strDate.GetBuffer(_MAX_PATH), _MAX_PATH, nullptr);

显示正确。如果日期范围跨越两个月,我有:

模板:%1-%2 日期 1:d MMMM 日期 2:d MMMM

效果很好。

从Windows7开始,您还可以使用LOCALE_RETURN_GENITIVE_NAMES到return一个月的属名。 运行 这个 :

const wchar_t localeName[] = L"pl-PL";
for (LCTYPE m = LOCALE_SMONTHNAME1; m <= LOCALE_SMONTHNAME12; ++m) {
  wchar_t buf[1024];
  GetLocaleInfoEx(localeName, m | LOCALE_RETURN_GENITIVE_NAMES, buf, sizeof(buf) / sizeof(*buf));
  std::cout << to_utf8(buf) << '\n';
}

给出以下结果:

stycznia
lutego
marca
kwietnia
maja
czerwca
lipca
sierpnia
września
października
listopada
grudnia

这确实是波兰语月份的专有属名。