Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / IIS

Override Date, Time, Currency and Number Formats in ASP.NET Applications

5.00/5 (1 vote)
18 Jan 2024CPOL2 min read 5.5K   39  
Step-by-step guide to enable an ASP.NET web application to override date, time, currency and number formats.
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:

XML
<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:

XML
<!-- Required -->
<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.:

XML
<!-- Force a specific culture (optional) -->
<add key="culture.cultureName" value="en-FR" />
    
<!-- Global override (optional, but if specified, all cultures will use these values) -->
<add key="culture..NumberDecimalSeparator" value="!" />
<add key="culture..NumberGroupSizes" value="2,3,2" />
<add key="culture..CurrencySymbol" value="Z" />

<!-- Override culture-specific settings (optional, case-insensitive) -->
<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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)