3

I'm developing a Windows Phone 8.1 app on C#/XAML and I need to format a list of time values according to the user's regional format preference. For example, for the settings on this picture, I'd have to format the time values using the formats for Portuguese (Brazil). Windows Phone region settings

Problem is, the Date/Time APIs format times and dates according to the application's language/region. Because my app has a "en-US" resource file and the system language is set to English, the APIs pick up "en-US" as my app's language and format everything according to this culture.

Is there any way to override this behavior and use the regional format as defined by the user in the system settings?

The closest thing I could find is Windows.System.UserProfile.GlobalizationPreferences.Clocks, which is a list of the clocks preferred by the user. But it only returns a string with the value "24HourClock", no format patterns or anything.

gabspeck
  • 93
  • 2
  • 8

2 Answers2

1

Try this:

CultureInfo ci = new CultureInfo("pt-BR");
DateTime dt = DateTime.Now;
String strDate = dt.ToString("D", ci);
String strTime = dt.ToString("t", ci);
String strCurr = ci.NumberFormat.CurrencySymbol;

To get a CultureInfo for the current culture try:

CultureInfo ci = CultureInfo.CurrentUICulture;

To get a CultureInfo for the current region try:

CultureInfo ci = new CultureInfo(Globalization.RegionInfo.CurrentRegion.Name);
Jim Rhodes
  • 5,021
  • 4
  • 25
  • 38
  • Your example has Portuguese hardcoded, but I mentioned it just as an example. I want to get whichever regional format setting the user has defined in the settings. – gabspeck Feb 22 '15 at 15:58
  • I get it, but do you know a way to retrieve the appropriate string for the Regional format defined in the system settings? – gabspeck Feb 22 '15 at 16:18
  • nope, still en-US. However, I inspected the properties of the `CultureInfo` class on the debugger, and there's a non-public member called `UserDefaultCulture` which returns "pt-BR", apparently what I need! It's not a public property though, so I can't access it from my app :( – gabspeck Feb 22 '15 at 16:43
  • the `Thread` class is not supported on the Store apps API (but I suspect it would return en-US anyway, because that *is* my current culture - just not my regional format setting) – gabspeck Feb 22 '15 at 17:27
  • Still no dice. I did some tests and it seems Nextgen Reader has found a way to do it. I'll try and get in touch with the developer to see how he did it. I'll post here if I get back from him. – gabspeck Feb 22 '15 at 18:05
  • @gabspeck Check out System.Globalization.RegionInfo.CurrentRegion – Jim Rhodes Feb 22 '15 at 18:11
0

The regional formatting behaves correctly (IMHO) in the Windows Phone Silverlight stack but misbehaves (still IMHO) in the WinRT stack. It has been misbehaving since the Windows 8.0 and there's lengthy discussion about the issue here: WinRT apps and Regional settings. The correct way to format dates and numbers based on the user's regional settings?

Also I did a little more recent blog post about the issue here: http://mikaelkoskinen.net/winrt-xaml-misbehave-still-in-windows-81/

The blog post also contains a workaround which seems to work OK: You can set the Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride to the user's regional format to make things work better.

So, hopefully the following code in your App.xaml.cs's constructor proves useful:

    public App()
    {
        Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = Windows.System.UserProfile.GlobalizationPreferences.HomeGeographicRegion;
        this.InitializeComponent();
        this.Suspending += this.OnSuspending;
    }

Updated with alternative version

Another alternative is to use GlobalizationPreferences.Languages-array. For example:

        Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = Windows.System.UserProfile.GlobalizationPreferences.Languages[1];

Just make sure to check that there actually is more than 1 language in the array.

This is how it looks in the app after setting the PrimaryLanguageOverride:

            this.DateText.Text = DateTime.Now.ToString("D");

Output with PrimaryLanguageOverride

Updated with screenshots

In Windows Phone 8.1, if you change the Regional format through Settings, the selected Regional format will be added as the the second language. At least this how the 8.1 emulator works. So, when I start the emulator from scratch and change the regional setting:

WP 8.1 regional setting

And then if I restart the phone, the selected region has been added as the second language:

Second language WP 8.1

And now the Windows.System.UserProfile.GlobalizationPreferences.Languages contains the Region format I selected.

Community
  • 1
  • 1
Mikael Koskinen
  • 12,306
  • 5
  • 48
  • 63
  • Hi, thanks for answering! Unfortunately your code didn't help... `HomeGeographicRegion` returns "US" (i.e. Country/Region) and the date/time is formatted the American way: http://1drv.ms/1zeQYxd. Why Microsoft thought it would be a good idea to let users choose a regional format different from their country/region's default and make app developers figure out by themselves how to use it on their projects is beyond me. – gabspeck Feb 25 '15 at 14:35
  • I've added an alternative option, hope it helps. – Mikael Koskinen Feb 25 '15 at 16:50
  • That new code formats the date according to the phone's secondary language, which is a different thing. What I want is to format the date according to the *Regional format*, which you set on Settings > Region. It's unrelated to the phone's languages. – gabspeck Feb 25 '15 at 17:20
  • Thanks for the comment. I've added some more info. – Mikael Koskinen Feb 25 '15 at 17:42
  • Oh, I didn't realize the OS did that! Thanks for clarifying. This method didn't work for me because I added German as a language some time ago and it shows up as the second language. So it's not a reliable method to determine the regional format setting... Anyway, it's a minor issue for my app. Thanks for the help! – gabspeck Feb 25 '15 at 18:04
  • @gabspeck: If there's any damning evidence that this is a WinRT 8.1 limitation, it is this: [even the WP8.1 Calendar app is affected](http://answers.microsoft.com/en-us/mobiledevices/forum/mdlumia-mdtips/windows-phone-81-calendar-app-does-not-follow/ed287e76-06c3-49e7-a90e-c28b35a84dd7). I was able to confirm this on my WP8.1 device, with phone language set to en-US (for Cortana) and regional format en-SG (where I live). One can only hope and pray that any of this is addressed at all on Windows 10... – BoltClock Oct 10 '15 at 05:07