Over a million developers have joined DZone.

WinRT 8.1: Regional Settings Still Misbehave

· Mobile Zone

The regional settings still misbehave in Windows 8.1 XAML apps. They misbehaved already in Windows 8 and they continue to misbehave in apps written with a newer WinRT. Some may say that they work as designed. To them I say the design is wrong.

The previous post about this subject contains more info, but here’s a short introduction to the problem.

Scenario: A Finnish users starts my Windows 8 XAML app. The app displays dates and times. The app isn’t localized to any language. I want the app to show the dates and times in Finnish format as that’s how she has configured her regional settings:

The problem: The app doesn’t respect her settings. Instead, all the dates and times are formatted using the US settings:

Capture

Other frameworks: Windows Forms, Console Applications, WPF, Silverlight and ASP.NET all handle this correctly: Dates and times are formatted using the regional settings:

image

The workaround: It’s possible to get the dates and times formatted correctly in WinRT apps. It’s just requires a lot of code. I call this a workaround until the WinRT XAML apps start behaving as they should:

var usersLanguage = Windows.Globalization.Language.CurrentInputMethodLanguageTag;
 
var dateFormatter = new DateTimeFormatter("shortdate", new[] { usersLanguage }).Patterns[0];
var timeFormatter = new DateTimeFormatter("shorttime", new[] { usersLanguage }).Patterns[0];
var fullFormatter = new DateTimeFormatter(dateFormatter + " " + timeFormatter);
 
this.MyDate.Text = fullFormatter.Format(DateTime.Now);

image

Update 4.7.2013:

It seems I can get the behavior similar to Silverlight and other frameworks by setting the Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride on application startup (App.xaml.cs). For example:

public App()
{
    Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = Windows.Globalization.Language.CurrentInputMethodLanguageTag;
    this.InitializeComponent();
    this.Suspending += OnSuspending;
}



Topics:

Published at DZone with permission of Mikael Koskinen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}