Search code examples
mfclocalepolishcoledatetime

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


I have this method to set the locale to Polish:

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]);
}

I have cut the other languages out for brevity. Now, when I format a COleDateTime object and display the month, say January, it shows as:

styczeń

But i want to show it as:

stycznia

Is there a locale setting to adjust the month values returned by the COleDateTime::Format method or locale?

Otherwise I will have to add something manual to override.

The months I would like returned are:

  1. stycznia
  2. lutego
  3. marca
  4. kwietnia
  5. maja
  6. czerwca
  7. lipca
  8. sierpnia
  9. września
  10. października
  11. listopada
  12. grudnia

Update

According to here it states:

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.

Now, this is how I am actually formatting my date strings (since I am supporting over 40 languages it is a bit tricky. So, (for English) I start with this format string:

%1 %2!d!-%3!d!
  1. %1 is the month.
  2. %2!d! is the first day value.
  3. %3!d! is the second day value.

If my date needs to cater for crossing over two months, I have:

%1 %2!d!–%3 %4!d!
  1. %1 is the month value.
  2. %2!d! is the first day value.
  3. %3 is the second month value.
  4. %3!d! is the second day value.

The above is used like this:

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());
}

For Polish, my respective format strings are:

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

So, I can see that because I am formatting the the date string using FormatMessage and only using the COleDateTime::Format method to parse just the month that it is potentially the cause of the issue.

Since I have two dates in the date string I can't just use a single Date formatting API call (since my date string is representing a week span).

So I checked:

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

And it made no difference. So I tried this instead:

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

It made no difference. It still shows the date the same way as before. Even if it did display the date correctly it doesn't factor for a date range from two COleDateTime objects.

Confused.

Update 2:

Also tried:

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

Just will not show the variant.

Update 3

The only way I can get it to show the right date is like this:

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

Then the month is correct. Now in the article I referred to it states:

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

I can't even work out how to access that.


Solution

  • I have encountered other issues but they are distinct from this question, so I will still provide my answer here.

    Firstly, I found a document which states that you must have d in the date for it to show the correct version of the month.

    Since, I am wanting to display a date range, I start with this:

    Polish

    Template: %1-%2 Date 1: d Date 2: d MMMM

    Then I format the date:

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

    That displays it correctly. And if the date range spans two months I have:

    Template: %1-%2 Date 1: d MMMM Date 2: d MMMM

    It works well.