Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 fileFinnish formatting

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

Resource file removedUS 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?

like image 483
Mikael Koskinen Avatar asked Sep 24 '12 07:09

Mikael Koskinen


People also ask

How do I change the regional format in Excel?

Click File > Options > Regional Format Settings. Click the regions drop-down, select a region, and then click Change.

How do I change regional settings?

Click the Start button, and then click Control Panel. Click Clock, Language, and Region, and then click Regional and Language Options. The Regional and Language Options dialog box appears. On the Formats tab, under Current format, click Customize this format.

How do I change regional settings in Windows 11?

How to change region settings on Windows 11. Open Settings. Click on Time & language. Click the Language & region page on the right side.


1 Answers

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:

  • date & time formatting sample: http://code.msdn.microsoft.com/windowsapps/Date-and-time-formatting-2361f348/sourcecode?fileId=52070&pathId=561085805
  • number formatting & parsing sample:
    http://code.msdn.microsoft.com/windowsapps/Number-formatting-and-bb10ba3d/sourcecode?fileId=52249&pathId=1462911094
  • there's also a nice article titled "How to use patterns to format dates and times" on msdn, but I can add only 2 links

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.

like image 60
Tom Avatar answered Nov 11 '22 06:11

Tom