Domanda

I have an application that needs to detect the user's country so I can adjust dates to that expectation for that country. I do have JS that is working already in detecting the locale. Now, I understand that JS is browser side and my CS file is server side. Is there a way to either: 1) Link that JS's answer up to my CS or 2) Detect the locale within the CS file?

Thank you for your time

Kevin

È stato utile?

Soluzione

Well, detecting the user's country isn't really the issue. If you need to format dates, you need to use something like this to detect the current culture rather than the country:

CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;

You wouldn't want to detect the country as there's plenty of users in a given country that don't use the program in that country's culture. For example, say someone has their system set to Spanish, but they live in the USA. You'd force them to use English, but using this method, you can take it directly from the system setting instead. Being dynamic is good!

That way, when you output a date, you can do something like this:

// Displays dt, formatted using the ShortDatePattern
// and the CurrentThread.CurrentCulture.
Console.WriteLine(dt.ToString("d"));

Reference: http://msdn.microsoft.com/en-us/library/5hh873ya(v=vs.90).aspx

See also: http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.currentculture.aspx

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top