WinRT apps and Regional settings. The correct way to format dates and numbers based on the user's regional settings?
Asked Answered
O

4

21

I'm having some problems in Windows 8 Metro apps (XAML & C#) regarding the user's regional settings. It seems that the apps won't respect user's regional settings, so even if your Windows 8 is set to display dates and times in Finnish format, the apps will still display them using US-formatting. But this is such a big problem that there must be something I'm missing?

To test this I started by creating a WPF-application. The application just prints out the CurrentCulture and the formatted DateTime.Now:

    private void Culture_Loaded_1(object sender, RoutedEventArgs e)
    {
        this.Culture.Text = System.Globalization.CultureInfo.CurrentCulture.DisplayName;
    }

    private void Date_Loaded_1(object sender, RoutedEventArgs e)
    {
        this.Date.Text = DateTime.Now.ToString();
    }

Here's my default regional settings: Regional settings

When run, the app displayed the date in Finnish format:

Finnish formatting

Then I changed the regional settings to US: US Regional settings

And when the app was run again, the culture and formatting changed: US wpf formatting

This is as I expected everything to work and this is also how I expected WinRT apps to work.

So as a next step, I created a WinRT (XAML & C#) app with the same code and reverted the regional settings back to Finnish. The problem:

Winrt formatting

Even when I've defined through regional settings that the formatting should be "Finnish", the WinRT app displays the datetime with US-formatting. I then modified the app's project file and made fi-FI the default language:

Default language

This change also modified the app's culture:

Finnish winrt formatting

Strange. I changed the Default Language back to its default value and the formatting was restored to US. I then created folders "Strings - fi-FI" inside the project and added an empty "Resources.resw" to the project. This empty file seems to be enough, as I was now getting the Finnish formatting:

Resource file Finnish formatting

As soon as I remove the empty resource file, the formattings reverts back to US:

Resource file removed US formatting winrt

Very strange.

This leads to few questions, but the main one I think is: Is it intentional that the WinRT-apps don't follow the user's regional settings like the WPF apps do?

Orelle answered 24/9, 2012 at 7:14 Comment(0)
S
15

It's been a while, but the question is not fully answered, so let me share my little research. Depechie is mostly right, but he provided only a link and wasn't really sure.

Yes, this unexpected change is intentional. We shouldn't use CultureInfo anymore as it contains legacy codes and Microsoft want us to use Windows.Globalization APIs instead.

To obtain current region we can use:

GeographicRegion userRegion = new GeographicRegion();
string regionCode = userRegion.CodeTwoLetter;

But as I noticed it contains only region information, there's no language code. To obtain language we can use:

string langRegionCode = Windows.Globalization.Language.CurrentInputMethodLanguageTag; // depends on keyboard settings
List<string> langs = Windows.System.UserProfile.GlobalizationPreferences.Languages; // all user  languages, like in languages control panel
List<string> applicationlangs = Windows.Globalization.ApplicationLanguages.Languages; // application languages (user languages resolved against languages declared as supported by application)

They return BCP47 language tags in format language-REGION like "en-US" if language has dialects or just language like "pl" if the language doesn't have major dialects.

We can also set one primary language which will override all the rest:

Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "en-US";

(This is a persisted setting and is supposed to be used at user request)

There is also new API for date, time and numbers:

Windows.Globalization.DateTimeFormatting.DateTimeFormatter dtf = new DateTimeFormatter("longdate", new[] { "en-US" }, "US", CalendarIdentifiers.Gregorian, ClockIdentifiers.TwentyFourHour);
string longDate = dtf.Format(DateTime.Now);

Windows.Globalization.NumberFormatting.DecimalFormatter deciamlFormatter = new DecimalFormatter(new string[] { "PL" }, "PL");
double d1 = (double)deciamlFormatter.ParseDouble("2,5"); // ParseDouble returns double?, not double

There's really a lot more in Windows.Globalization APIs, but I think that this gives us the general idea. For further reading:

You can also find some topics about the issue on windows 8 dev center forum with some Microsoft employee answers, but they mainly send you to the documentation.

Sulphur answered 29/12, 2012 at 23:2 Comment(3)
So, acutally how the get / respect the Users Regional Format settings? I started the windows phone 8.1 emulator with the following configuration for testing purpose: UI Language: German, Region: US, Format Settings: Irish I have found no way to get the Information, that the user has set his format settings to irish. And my only wish is to format a DateTime like the user wants to see it. And Regional Format does not automaticly mean its region i set the device to or the UI Langauge i chose. On my Devices i set the UI to english but want to see all TimeStamps and Dates in the German format...Bb
thanks, the following was great, because I needed ISO 3166-1 alpha2 en.wikipedia.org/wiki/ISO_3166-1_alpha-2 GeographicRegion userRegion = new GeographicRegion(); string regionCode = userRegion.CodeTwoLetter;Oma
This has worked for me but now I need big numbers and the CurrencyFormatter does not support decimal so I need a workaround...Endomorphic
F
4

It is intentional. Microsoft is moving away from forcing applications to be in the language of the OS. Instead, each application uses information declared by the application (manifest languages, observable at Windows.Globalization.ApplicationLanguages.ManifestLanguages) and declared by the user (user languages, observable at Windows.System.UserProfile.GlobalizationPreferences.Languages) to determine how to display resources and globalized dates and times. This set of languages is called the application languages (observable at Windows.Globalization.ApplicationLanguages.Languages). The behavior you are seeing is because you are fiddling with the user languages and the manifest languages and you will get different application languages.

Fletcherfletcherism answered 7/3, 2013 at 21:43 Comment(1)
Interesting approach, though as a Portuguese living in the UK (and currently working for Microsoft) I couldn't disagree more of it! Here's my view: My Win10 phone has language set to "English US", country to "United Kingdom" and regional format "Portuguese (Portugal)". Outlook shows dates in correct pt-PT format, but all other apps insist in using en-US which IMHO is wrong and not what I expected!!Forum
W
2

Could it be we now need to query other classes? Like the example given here: http://code.msdn.microsoft.com/windowsapps/Globalization-preferences-6654eb36/sourcecode?fileId=52104&pathId=236099476

Warsle answered 25/9, 2012 at 17:13 Comment(0)
A
0

This post still seems to be relevant even though it was asked two years ago. I just came across it as I was looking for an answer to about the same thing. I also wanted to display dates in the regional format in my WP8.1 WinRT app. The information posted here helps, but it was a bit hard to piece it together.

This is what I came up with and it seems to work for me as the answer I needed:

using Windows.Globalization;
using Windows.Globalization.DateTimeFormatting;

private string FormatDate(int year, int month, int day)
{
    GeographicRegion userRegion = new GeographicRegion();
    string regionCode = userRegion.CodeTwoLetter;
    var formatter = new DateTimeFormatter("year month day", new[] { regionCode });
    DateTime dateToFormat = new DateTime(year, month, day);
    var formattedDate = formatter.Format(dateToFormat);
    return formattedDate;
}
Altamirano answered 7/7, 2015 at 19:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.