Question:
What is the difference between CurrentCulture and CurrentUICulture and when do I use which one in dot.net?Should I use CurrentCulture or CurrentUICulture for localization (translation)?
Which culture should I use to parse dates or numbers in c#?
Answer:
First of all, both properties are properties of the current Thread.CurrentCulture
- CurrentCulture is the cultural context for formatting and parsing (for ex. to parse dates or numbers) A date is often written like '10/30/2000' by Americans, but '30/10/2000' by Irish people, and 30.10.2000 by Germans etc.
- CurrentCulture does not support neutral cultures (i.e. "en", "fr" ...). It is always a specific culture (i.e. "en-GB", "de-AT", "pt-BR")
CurrentUICulture
- CurrentUICulture provides the cultural context for localization, i.e. translation of resources.
- CurrentUICulture supports neutral cultures (i.e. "en", "fr" ...)
Get resources sample in asp.net
using System.Threading; using System.Globalization; string culture = "en"; Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture); string hello = (string)HttpContext.GetGlobalResourceObject("myClassKey", "myResourceKey"); // and now French string culture = "fr"; Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture); string hello = (string)HttpContext.GetGlobalResourceObject("myClassKey", "myResourceKey"); ...
Parse date sample in asp.net
using System.Threading; using System.Globalization; DateTime dt; // this is a British English date format string dateStr = "31/10/2001"; // parse it with British English culture Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"); bool parseOk = DateTime.TryParse(dateStr, out dt); // parseOk: true -- dt: {31/10/2001 00:00:00} Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); parseOk = DateTime.TryParse(dateStr, out dt); // parseOk: false-- dt: {1/1/0001 12:00:00 AM} ...
well, you get the idea.
No comments:
Post a Comment