Reputation: 93
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).
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.
Upvotes: 3
Views: 3475
Reputation: 12906
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");
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:
And then if I restart the phone, the selected region has been added as the second language:
And now the Windows.System.UserProfile.GlobalizationPreferences.Languages contains the Region format I selected.
Upvotes: 0
Reputation: 5085
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);
Upvotes: 1