How to change CultureInfo.CurrentCulture in a C#/Windows Store app - c#

I want string.Format() and .ToString() calls to use invariant culture, not the system culture.
System.Globalisation.CultureInfo.CurrentCulture property is read-only in Windows Store apps, so it cannot be changed directly, unlike in .NET 4.6 and later.
I tried using System.Globalization.CultureInfo.DefaultThreadCurrentCulture (which should work according to documentation), but it does not change anything:
System.Globalization.CultureInfo.DefaultThreadCurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = System.Globalization.CultureInfo.InvariantCulture;
var c1 = System.Globalization.CultureInfo.CurrentCulture;
Task.Run(delegate
{
var c2 = System.Globalization.CultureInfo.CurrentCulture;
});
After the above code executes, c1 and c2 still contain my local system culture, not the invariant culture. string.Format still produces strings using my local culture settings.
Surely there must be a way to change culture of a thread?

I have added following code to my App.Xaml.cs constructor. (Just after this.InitializeComponent();)
It worked for me.
var culture = CultureInfo.InvariantCulture;
ApplicationLanguages.PrimaryLanguageOverride = culture.Name;
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;

Related

managing MultiLanguage resources

I have created two resource files in my project GlobalRes.ge.resx and GlobalResources.en.resx
I receive language as an input parameters . I want to know how can I read my values based on language. for example if string lang = "en" then by globalres.welcome I should see WOLCOME but if I choose lang = "ge" then globalres.welcome should be willkommen
(I have already created the welcome line in both files)
The Resource Designer will load the appropriate text based on the CurrentUICulture
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
bCancel.Text = Resource.Cancel;
In .NET 4.5 and later you can use the following properties to set the DefaultThreadCurrentCulture & DefaultThreadCurrentUICulture culture.
CultureInfo.DefaultThreadCurrentCulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
CultureInfo.DefaultThreadCurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");

IIS use wrong pt-PT

In .net 4.0, web forms and IIS 8
I have in web.config this:
<globalization culture="pt-PT" uiCulture="pt-PT" />
When I do in C# this:
ltNumber.Text = (12345.12).ToString("N");
I get this: 12 345,12
But the output must be 12.345,12
This began to look bad on windows 10. windows 7 everything was fine. What can be wrong?
The numeric ("N") format specifier uses your CurrentCulture settings.
That means your CurrentCulture has white space as a NumberGroupSeparator and , as a NumberDecimalSeparator.
As a solution, you can Clone your CurrentCulture, set these properties what ever you want, and use that culture as a second parameter in your ToString method. Like;
var clone = (CultureInfo) CultureInfo.CurrentCulture.Clone();
clone.NumberFormat.NumberDecimalSeparator = ",";
clone.NumberFormat.NumberGroupSeparator = ".";
ltNumber.Text = (12345.12).ToString("N", clone); // 12.345,12
I don't think these properties are changed based on operating system versions. It all about which culture settings you use.

WPF Default DateTime format

Working on a large WPF application and having some issues with the DateTime format, it's a huge codebase and many places do not specify the culture.
When the user opens the application on Windows 7 or 8 the datetime format is different (Win 7 uses slashes and Win 8 uses dashes).
I tried setting the Culture to "en-US" in the Application Startup (see below link) but it doesn't seem work?
Setting Culture (en-IN) Globally in WPF App
How do I set my WPF application to not use the culture of the user machine?
Here is what I use in OnStartup. It's different than the SO question you linked to as I use DefaultThreadCurrentCulture as it sets the default for the entire process and any threads launched.
DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture are in .NET 4.5 and later...
var newCulture = new CultureInfo(displayCulture);
// Set desired date format here
newCulture.DateTimeFormat.ShortDatePattern = "dd MMM yyyy";
// You cannot use DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture if
// you dont have .NET 4.5 or later. Just remove these two lines and pass the culture
// to any new threads you create and set Thread.CurrentThread...
CultureInfo.DefaultThreadCurrentCulture = newCulture;
CultureInfo.DefaultThreadCurrentUICulture = newCulture;
Thread.CurrentThread.CurrentCulture = newCulture;
Thread.CurrentThread.CurrentUICulture = newCulture;
FrameworkElement.LanguageProperty.OverrideMetadata(
typeof(FrameworkElement),
new FrameworkPropertyMetadata(
System.Windows.Markup.XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
This will provide you with what you are looking for, or you can use regex...
DateTime dateTime;
bool validDate = DateTime.TryParseExact(
"04/22/2015",
"MM/dd/yyyy", // or you can use MM.dd.yyyy
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dateTime);

Windows Form Culture not setting up

I am trying to get right string values from culture resource file but it's not working, always returning english resources,
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
but
Resources.Resource1.myResource;
still getting english resources, I have two files Resource1.resx and Resource1.fr-FR.resx
I think that you need
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR")
Thread.CurrentThread.CurrentUICulture
Gets or sets the current culture used by the Resource Manager to look
up culture-specific resources at run time.

Use the Culture of the OS, but not the language

I've encountered several times the same problem in applications we develop:
We want to allow the user to edit/display it's data in his format (date, currency, ...), but we want to display the application in English only (for several reasons, it's a pro, international application, in a domain in which we communicate mostly in English).
There is no problem when we manage the whole application, but most of third-party pro frameworks that I used (Telerik, DevExpress) are using the CurrentCulture to display my data in the correct format AND in the corresponding language.
So, even if I have my computer in English, I have my regional settings set to fr-CH, I will have all third party user controls in French.
I cannot set the CurrentCulture to a specific culture and set the format of my user controls to something else (I would loose my default format) and I can't let the CurrentCulture to be the default one because I would have my third party components in another language.
I tried to build my own culture (CultureAndRegionInfoBuilder), with no success. When I change the language, I still have my application in the user-specific language.
Concrete problem
I'm using a date editor(basic, it has one text input and can popup a calendar). I want to have the date displayed in my OS locale(ch-FR, so 15 january 2013 would be "15.01.2013"), but I don't want that when I display the calendar month/day name appears in french.
What is the correct approach with this?
Store the original CultureInfo for your purposes and try editing CurrentCulture and CurrentUICulture properties of the CurrentThread property in System.Threading.Thread, maybe this will solve your problem.
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
I resolved my problem by having a custom culture info:
private static void UpdateCultureInfoWithoutLangage()
{
//We initialize a en-US cultureInfo and change all formats + number infor related
CultureInfo cultureInfoEn = new CultureInfo("en-US");
CultureInfo cultureInfoEnClone = (CultureInfo)cultureInfoEn.Clone();
//Setting DateTimeFormat(Without changing translations)
cultureInfoEnClone.DateTimeFormat.FirstDayOfWeek = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
cultureInfoEnClone.DateTimeFormat.FullDateTimePattern = CultureInfo.CurrentCulture.DateTimeFormat.FullDateTimePattern;
cultureInfoEnClone.DateTimeFormat.LongDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern;
cultureInfoEnClone.DateTimeFormat.LongTimePattern = CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern;
cultureInfoEnClone.DateTimeFormat.MonthDayPattern = CultureInfo.CurrentCulture.DateTimeFormat.MonthDayPattern;
cultureInfoEnClone.DateTimeFormat.ShortDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
cultureInfoEnClone.DateTimeFormat.ShortTimePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern;
cultureInfoEnClone.DateTimeFormat.TimeSeparator = CultureInfo.CurrentCulture.DateTimeFormat.TimeSeparator;
cultureInfoEnClone.DateTimeFormat.YearMonthPattern = CultureInfo.CurrentCulture.DateTimeFormat.YearMonthPattern;
cultureInfoEnClone.NumberFormat = CultureInfo.CurrentCulture.NumberFormat;
Thread.CurrentThread.CurrentCulture = cultureInfoEnClone;
Thread.CurrentThread.CurrentUICulture = cultureInfoEnClone;
Application.CurrentCulture = cultureInfoEnClone;
}

Categories