Search code examples
c#win-universal-appwindows-10-mobile

Regional format and language in Windows Phone 10 UWP app


I'm working on a localized Windows (Phone) 10 UWP app.

I have implemented support for 2 language: en-US and nl-NL (Dutch-Netherlands). This works fine: When the user has selected English in the phone settings the app starts in English and when the user has selected Dutch in the phone settings the app starts in Dutch.

To get this working I had to make some changes in the package.appxmanifest because my language resources are in a different DLL:

  <Resources>
    <Resource Language="nl-NL"/>
    <Resource Language="en-US"/>
  </Resources>

But I cannot find any way to get the regional format from the user for date, time and number formatting.

When the user has selected English as language but Dutch (Netherlands) for regional format, my app starts with both System.Globalization.CultureInfo.CurrentUICulture and System.Globalization.CultureInfo.CurrentCulture set to "en-US", where System.Globalization.CultureInfo.CurrentCulture should be "nl-NL".

I have been searching all of the documentation but I cannot find a way to retrieve the phone regional format (except Windows.System.UserProfile.GlobalizationPreferences.HomeGeographicRegion which is something different).

Is there a way to retrieve the phone regional format?


Solution

  • Peter Meinl's answer works, but is a little confusing because you do not need the RegionInfo.

    Pedro Lamas describes the hack using the ResolvedLanguage of DateTimeFormatter just using "US".

        DateTimeFormatter dtf = new DateTimeFormatter("longdate", new[] { "US" });
        string regionInfoName = dtf.ResolvedLanguage;
        CultureInfo regionFormatCultureInfo = new CultureInfo(regionInfoName);
    

    The ResolvedLanguage property of the DateTimeFormatter will contain the regional format id of the phone, in this case "nl-NL".

    Mind that you DO need a language in the constructor of the DateTimeFormatter, just new DateTimeFormatter("longdate") or DateTimeFormatter.LongDate won't work.