Update: sandord, in the comments, has pointed out that you can achieve the same result with a simple configuration setting in Web.config:
<globalization culture="auto" uiculture="auto" enableClientBasedCulture="”true”" />
My web application TardisBank (a pocket money bank account for kids) needs to show users the currency symbol for their location. So UK users should see ‘£’, but US users should see ‘$’. I’d thought that I would have to ask the user to choose a currency symbol when they signed up, but Dylan Beattie had a much better idea:
A quick Google turned up this excellent article by Rick Strahl, ‘detecting and setting the current locale on the current asp.net web request’.
Using Rick’s code I quickly created an ASP.NET module:
using System; using System.Globalization; using System.Threading; using System.Web; namespace Suteki.TardisBank.Mvc { public class UserLocaleModule : IHttpModule { public void Init(HttpApplication httpApplication) { httpApplication.BeginRequest += (sender, eventArgs) => { var app = sender as HttpApplication; if (app == null) { throw new ApplicationException("Sender is null or not an HttpApplication"); } var request = app.Context.Request; if (request.UserLanguages == null || request.UserLanguages.Length == 0) return; var language = request.UserLanguages[0]; if (language == null) return; try { Thread.CurrentThread.CurrentCulture = new CultureInfo(language); } catch {} }; } public void Dispose() { } } }
Which I configured in TardisBank’s web.config:
<modules runAllManagedModulesForAllRequests="true"> <add name="UserLocale" type="Suteki.TardisBank.Mvc.UserLocaleModule, Suteki.TardisBank" /> </modules>
And now I’ve got currency symbols :)
And if I change my browser’s language settings:
I now get the $ symbol instead:
Thanks Dylan & Rick! It’s a very neat solution.
I don't think you need this bit...
ReplyDeletelanguage = language + "-" + language.ToUpper();
You can create a CultureInfo just fine from a two letter language code e.g.
new CultureInfo("fr").DisplayName.Dump();
Plus you may run into an issue where using the language code as the region code results in an invalid culture.
Thanks Derek, I did some more tests and you are right. I wonder why Rick had this in his original code?
ReplyDeleteAnyway I've updated the post.
Don;t you need to convert amounts to the new currency too?
ReplyDeleteDimitris
How is this any different from adding this to the web.config file?
ReplyDeleteSorry, the XML didn't show up. Here it is:
ReplyDelete<globalization culture="auto" uiCulture="auto"/>
I'm with Dimitris. The currency symbol is useless as you need to convert the value. Currency type and ammount have a relationship as a pound/dollar/euro are not the same.
ReplyDeleteHi Anonymous, Dimitris,
ReplyDeleteYes, you are right _if_ I was transferring money from one user's account to another. I'm ignoring the case where parents and children could be in different countries of course.
Of course if you are writing a serious financial application, then you need a better way to record and convert currencies.
Hi sandord,
ReplyDeleteThanks for pointing that out to me, I had no idea that that setting even existed :p
Yes, it does look like that would do exactly the same thing.
I'm updating the post to point this out.
Another minor optimization is use the creator method CultureInfo.GetCultureInfo() to construct the culture, instead of creating a new one. The former gets the value from the pre-initialized tables.
ReplyDeleteCheck this helpful link too...
ReplyDeleteDisplaying User Culture Language in ASP.Net
Thanks!!