Thursday, March 25, 2021

Getting Localized Day and Month Names

If you need to get a localized day or month name (such as "Thursday" or "March" in English, which are "Donnerstag" and "März" in German), there are Windows API calls that can help you but they can be complicated to use. This is especially true if you need the localized names in a language other than the one the user's machine is configured for. We have numerous customers that have their machine configured to use, for example, German, but want these names displayed in English.

wwDotNetBridge and .NET to the rescue. Pass this function a date or datetime value, the type of name wanted (see the comments in the code for the values; for example, use 2 for an abbreviated name such as "Thu" for "Thursday"), and a BCP-47 language code such as "en-US" for U.S. English or "de-DE" for Germany German. For example, to put a list of English month names into an array, use something like:

dimension laMonths[12]
for lnI = 1 to 12
laMonths[lnI] = GetDateName(date(2021, lnI, 1), 3, 'en-US')
next

Here's the code for the GetDateName function. It assumes the files for wwDotNetBridge are in place:

function GetDateName(tuDate, tnType, tcCulture)
local loBridge, lcFormat, loCulture, lcReturn
do wwDotNetBridge
loBridge = GetwwDotNetBridge()
do case
case tnType = 1
&& day
lcFormat = '{0:dddd}'
case tnType = 2
&& abbreviated day
lcFormat = '{0:ddd}'
case tnType = 3
&& month
lcFormat = '{0:MMMM}'
case tnType = 4
&& abbreviated month
lcFormat = '{0:MMM}'
endcase
loCulture = loBridge.CreateInstance('System.Globalization.CultureInfo', ;
tcCulture)
lcReturn = loBridge.InvokeStaticMethod('System.String', 'Format', loCulture, ;
lcFormat, tuDate)
return lcReturn

No comments: