I need to set my application's culture through an App.Config file, so that "pt-BR" is used automatically for parsing dates without the need to manually inform the culture for each operation.
As far as I know, there's a globalization
section that can be defined inside the system.web
section in a Web.Config file, but I'm running a console application and I can't figure this out.
Any idea?
Explicitly Setting the Current UI Culture NET Framework 4.6, you can change the current UI culture by assigning a CultureInfo object that represents the new culture to the CultureInfo. CurrentUICulture property.
How to CultureInfo in VB.NET. The CultureInfo class specifies a unique name for each culture, based on RFC 4646. The name is a combination of an ISO 639 two-letter lowercase culture code associated with a language and an ISO 3166 two-letter uppercase subculture code associated with a country or region.
CurrentCulture = New CultureInfo("th-TH", False) Console. WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name) ' Display the name of the current UI culture. Console. WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name) ' Change the current UI culture to ja-JP.
I don't know a built-in way to set it from App.config, but you could just define a key in your App.config like this
<configuration> <appSettings> <add key="DefaultCulture" value="pt-BR" /> </appSettings> </configuration>
and in your application read that value and set the culture
CultureInfo culture = new CultureInfo(ConfigurationManager.AppSettings["DefaultCulture"]); Thread.CurrentThread.CurrentCulture = culture; Thread.CurrentThread.CurrentUICulture = culture;
Also, as @Ilya has mentioned, since .NET 4.5 you can set the default culture once, rather than per-thread:
CultureInfo.DefaultThreadCurrentCulture = culture CultureInfo.DefaultThreadCurrentUICulture = culture
Starting form .Net 4.5 it's possible to set the default thread culture so there is no need to fix it per thread:
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("pt-BR"); CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("pt-BR");
I haven't yet found a configuration that matches web.config
globalization
section unfortunately.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With