locked
Equivalent for CultureInfo.CurrentCulture.UserDefaultCulture in Windows Store App?

    Question

  • Hello!

    CultureInfo.CurrentCulture.UserDefaultCulture is internal and is not accessible.

    How to get its value in  Windows Store App?

    Friday, February 21, 2014 8:39 AM

All replies

  • Please see http://social.msdn.microsoft.com/Forums/windowsapps/en-US/ad6ff685-1cc5-41d1-a7f1-aacaec73f2d5/cultureinfo-class?forum=winappswithcsharp

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, February 21, 2014 8:45 PM
    Moderator
  • Hello Matt,

    Unfortunately this article does not help.

    I need to convert ListingInformation.FormattedPrice to decimal for non-US Market.

    Given that user looks UK market and its Region Format setting is 'Russia' and user set '@' symbol as currency decimal symbol:



    When Windows Store shows the price (and return the same in FormattedPrice property) it combines

    Russian currency pattern with UK currency symbol:

    So I need following code to correct parse formattedPrice string:
    Decimal value = Decimal.Parse(formattedPrice.Replace((new RegionInfo(market).CurrencySymbol), CultureInfo.UserDefaultCulture.NumberFormat.CurrencySymbol), NumberStyles.Currency, CultureInfo.UserDefaultCulture);

    But CultureInfo.UserDefaultCulture is internal property and I can't use it in my Metro App (and can't use reflection as well). So I need an alternative.

    Now I have to workaround to use C++ component and GetUserDefaultLocaleName(); to get culture name "ru-RU" and then get CultureInfo.UserDefaultCulture ( as new CultureInfo("ru-RU)).

    But how to get this user setting from C# metro code?


    Monday, February 24, 2014 3:51 PM
  • I have to say that I am really unfamiliar with this area of development. That said, could you use a custom formatter?

    http://msdn.microsoft.com/en-us/library/windows/apps/jj673581.aspx

    If this is unhelpful, can you explain it to me as though I were 5 years old? :-)


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, February 24, 2014 7:07 PM
    Moderator
  • Hi Matt,

    I've prepared a little snippet to show why I can't use Windows.Globalization.NumberFormatting.CurrencyFormatter.

    It looks like Windows Store uses CultureInfo.UserDefaultCulture (in my case it is CultureInfo("ru-RU")).

    To parse it back to number we need to know CultureInfo.UserDefaultCulture (at least it's culture name string "ru-RU").

    Given that user looks UK market and its Region Format setting is 'Russia' and user set '@' symbol as currency decimal symbol (see screenshots in my previous post).

    UK market show price: "4@99 £" and we can use ListingInformation.FormattedPrice Windows APP API to get it. We know that ListingInformation.CurrentMarket is "GB" and know its currency symbol (new RegionInfo("GB").CurrencySymbol)

    To parse the price string we have to 1) replace GB currency symbol with UserDefaultCulture currency symbol and 2) use UserDefaultCulture in Decimal.Parce()

    UserDefaultCulture is CultureInfo internal property and we can't use in in Windows Store App. In C++ code we can get culture name string ("ru-RU") by GetUserDefaultLocaleName() (it is available API for Windows Store App) and then use new CultureInfo("ru-RU") instead of CultureInfo.UserDefaultCulture. But how to get user default culture name string ("ru-RU") in C# I can't find yet.

    With Windows.Globalization.NumberFormatting we can't do same formatting as Windows App Store does and can't parse it back accordingly

                foreach (string appLanguage in ApplicationLanguages.Languages)
                    Debug.WriteLine("appLanguage: " + appLanguage);
                Debug.WriteLine("ApplicationLanguages.PrimaryLanguageOverride = [" + ApplicationLanguages.PrimaryLanguageOverride + "]");
    
                foreach (string userLanguage in GlobalizationPreferences.Languages)
                {
                    Debug.WriteLine("userLanguage: " + userLanguage);
                }
    
                foreach (string userCurrency in GlobalizationPreferences.Currencies)
                {
                    Debug.WriteLine("userCurrency: " + userCurrency);
                }
    
    
                Debug.WriteLine("\n1. Parse Windows App Store formatted price to number");
                string formattedPrice = "4@99 £"; //ListingInformation.FormattedPrice
                string market = "GB"; //ListingInformation.CurrentMarket
    
    
                Decimal price = Decimal.Parse(
                    formattedPrice.Replace(new RegionInfo(market).CurrencySymbol, new CultureInfo("ru-RU").NumberFormat.CurrencySymbol),
                    NumberStyles.Currency, 
                    new CultureInfo("ru-RU"));
                Debug.WriteLine("unFormattedPrice = " + price);
    
                Debug.WriteLine("\n2. Format number to Windows App Store formatted price");
                double priceNumber = 4.99;
                string ruFormattedPrice = string.Format(new CultureInfo("ru-RU"), "{0:c}", priceNumber);
                Debug.WriteLine("ruFormattedPrice = " + ruFormattedPrice);
                string storeFormattedPrice = ruFormattedPrice.Replace(new CultureInfo("ru-RU").NumberFormat.CurrencySymbol, new RegionInfo(market).CurrencySymbol);
                Debug.WriteLine("storeFormattedPrice = " + storeFormattedPrice);
    
                Debug.WriteLine("\n3. we can't use Windows.Globalization.NumberFormatting.CurrencyFormatter to do the same");
                var ruCurrencyFormat = new Windows.Globalization.NumberFormatting.CurrencyFormatter("RUB");
                Debug.WriteLine(ruCurrencyFormat.Format(priceNumber));
                var userCurrencyFormat = new Windows.Globalization.NumberFormatting.CurrencyFormatter(GlobalizationPreferences.Currencies[0]);
                Debug.WriteLine(userCurrencyFormat.Format(priceNumber));

    output is:

    appLanguage: en-US
    ApplicationLanguages.PrimaryLanguageOverride = []
    userLanguage: en-US
    userCurrency: GBP

    1. Parse Windows App Store formatted price to number
    unFormattedPrice = 4.99

    2. Format number to Windows App Store formatted price
    ruFormattedPrice = 4@99 р.
    storeFormattedPrice = 4@99 £

    3. we can't use Windows.Globalization.NumberFormatting.CurrencyFormatter to do the same
    р.4.99
    £4.99


    Unfortunately Windows App Store does not use Windows.Globalization.NumberFormatting.CurrencyFormatter and display the price as 4@99 £, NOT £4.99


    Tuesday, February 25, 2014 9:18 AM