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

前端 未结 4 754
-上瘾入骨i
-上瘾入骨i 2020-12-02 10:40

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 set

相关标签:
4条回答
  • 2020-12-02 11:17

    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.

    0 讨论(0)
  • 2020-12-02 11:20

    It is intentional. Microsoft is moving away from forcing applications to be in the language of the OS. Instead, each application uses information declared by the application (manifest languages, observable at Windows.Globalization.ApplicationLanguages.ManifestLanguages) and declared by the user (user languages, observable at Windows.System.UserProfile.GlobalizationPreferences.Languages) to determine how to display resources and globalized dates and times. This set of languages is called the application languages (observable at Windows.Globalization.ApplicationLanguages.Languages). The behavior you are seeing is because you are fiddling with the user languages and the manifest languages and you will get different application languages.

    0 讨论(0)
  • 2020-12-02 11:21

    This post still seems to be relevant even though it was asked two years ago. I just came across it as I was looking for an answer to about the same thing. I also wanted to display dates in the regional format in my WP8.1 WinRT app. The information posted here helps, but it was a bit hard to piece it together.

    This is what I came up with and it seems to work for me as the answer I needed:

    using Windows.Globalization;
    using Windows.Globalization.DateTimeFormatting;
    
    private string FormatDate(int year, int month, int day)
    {
        GeographicRegion userRegion = new GeographicRegion();
        string regionCode = userRegion.CodeTwoLetter;
        var formatter = new DateTimeFormatter("year month day", new[] { regionCode });
        DateTime dateToFormat = new DateTime(year, month, day);
        var formattedDate = formatter.Format(dateToFormat);
        return formattedDate;
    }
    
    0 讨论(0)
  • 2020-12-02 11:23

    Could it be we now need to query other classes? Like the example given here: http://code.msdn.microsoft.com/windowsapps/Globalization-preferences-6654eb36/sourcecode?fileId=52104&pathId=236099476

    0 讨论(0)
提交回复
热议问题