Use an IIS module to override the server date, time, currency and number formats (dictated by the regional settings) without editing application code.
Introduction
ASP.NET supports globalization in the web.config file, forcing the application to a specific culture, i.e., en-US
, en-FR
, en-ZA
, etc.
For example:
<system.web>
<globalization culture="en-ZA"/>
</system.web>
The application then uses the regional settings of the host for displaying currency, number and date time information.
Issues can arise with how currency, number and date/time information is handled if these regional settings differ between hosts or operating systems (e.g.: migrating sites/services from one server to another).
It is sometimes not possible to change the hosts regional settings without affecting other sites and services running on the server.
Background
Recently, I was involved in a project that included migrating ASP.NET sites and services from a server that had reached end of life and was being decommissioned. It seemed fairly straight-forward until the testers reported that the number formats were incorrect for our specific locale (en-ZA).
We were unable to change the number formats on the server without affecting other sites and services so I developed and implemented an IIS module to intercept the request and set the locale and specific formats via the config file.
Features
- Granular control of currency, number and date/time formats for multiple cultures.
- Global override (apply specific format regardless of culture).
- Multi-process support (worker threads also honour specified formats).
- Override values of all
public
properties of the NumberFormatInfo and DateTimeFormatInfo objects. - No code changes required - all done via the web.config file.
Using the Code
Download Localize.zip (or clone source code from here).
Add the Localize.dll file to the bin directory of your ASP.NET web application or web service.
Next, register the module with IIS by adding the module to the system.webServer
section of the web.config file:
<system.webServer>
<modules>
<add name="Localize" type="Localize.CultureHelper" />
</modules>
</system.webServer>
Add the settings/properties you wish to override in the appSettings
section of the web.config file, e.g.:
<add key="culture.cultureName" value="en-FR" />
<add key="culture..NumberDecimalSeparator" value="!" />
<add key="culture..NumberGroupSizes" value="2,3,2" />
<add key="culture..CurrencySymbol" value="Z" />
<add key="culture.en-za.NumberDecimalSeparator" value="." />
<add key="culture.en-za.NumberDecimalDigits" value="9" />
<add key="culture.en-za.currencySymbol" value="#" />
<add key="culture.en-za.ShortDatePattern" value="MM-dd-yyyy" />
<add key="culture.en-za.ShortTimePattern" value="HHmm" />
<add key="culture.en-za.MonthDayPattern" value="MMM d" />
<add key="culture.en-US.currencySymbol" value="#" />
<add key="culture.en-fr.currencySymbol" value="@" />
The module uses reflection to match the properties to override. The property names are case-insensitive.
Note: In the config example above, I've used (mostly) ridiculous values to test with, so please change/remove these to suit your needs.
Exceptions are handled gracefully and logged to the Windows Event Log.
Points of Interest
Conclusion
Thanks for reading - I hope this saves somebody some time and frustration.
History
- 17th January, 2024: Initial version
I'm a software developer that enjoys a great many things. Developing and integrating with API's is one of them. I live on a smallholding in the beautiful KZN midlands in South Africa with my lovely wife, 2 dogs, some chickens, a few sheep, a cat with half a tail, and two feral children.
I may be the world's shoddiest example of an "IT guy": I love being outdoors, exercising, gardening, "farming" (I really cannot call myself a farmer - keeping some animals alive does not make one a farmer), riding motorbikes and generally doing things that I am far too old for.