Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Solved] Culture problematic when using 'fr-FR' under Android

Discussion in 'Unity IAP' started by MoinTom, Oct 17, 2016.

Thread Status:
Not open for further replies.
  1. MoinTom

    MoinTom

    Joined:
    May 8, 2015
    Posts:
    15
    Hey guys,
    we currently encounter an Exception ("FormatException: Invalid character at position 2") during UnityPurchasing.Initialize. This happens when the Thread.CurrentThread.CurrentCulture is set to CultureInfo.CreateSpecificCulture("fr-FR") under Android. We already had the same issue using Unibill, but we were able to write an own fix since we had the Code.

    StackTrace:
    10-11 12:46:14.341: I/Unity(27045): FormatException: Invalid character at position 2
    10-11 12:46:14.341: I/Unity(27045): at System.Decimal.ThrowAtPos (Int32 pos) [0x00000] in <filename unknown>:0
    10-11 12:46:14.341: I/Unity(27045): at System.Decimal.stripStyles (System.String s, NumberStyles style, System.Globalization.NumberFormatInfo nfi, System.Int32& decPos, System.Boolean& isNegative, System.Boolean& expFlag, System.Int32& exp, Boolean throwex) [0x00000] in <filename unknown>:0
    10-11 12:46:14.341: I/Unity(27045): at System.Decimal.PerformParse (System.String s, NumberStyles style, IFormatProvider provider, System.Decimal& res, Boolean throwex) [0x00000] in <filename unknown>:0
    10-11 12:46:14.341: I/Unity(27045): at System.Decimal.Parse (System.String s, NumberStyles style, IFormatProvider provider) [0x00000] in <filename unknown>:0
    10-11 12:46:14.341: I/Unity(27045): at System.Decimal.Parse (System.String s) [0x00000] in <filename unknown>:0
    10-11 12:46:14.341: I/Unity(27045): at UnityEngine.Purchasing.JSONSerializer.DeserializeMetadata (System.Collections.Generic.Dictionary`2 data) [0x00000] in <filename unknown>:0
    10-11 12:46:14.341: I/Unity(27045): at UnityEngine.Purchasing.JSONSerializer.DeserializeProductDescriptions (System.String json) [0x00000]​


    Our "fix" for Unibill was something similar to this: (Util.cs / DeserialiseProductList)

    decimal priceDecimal;
    if (!decimal.TryParse(details.getString("priceDecimal"), out priceDecimal)) {
    priceDecimal = decimal.Parse(details.getString("priceDecimal"), NumberStyles.Any, CultureInfo.CreateSpecificCulture("en-GB"));​
    }​

    Maybe it's also applicable for the current UnityIAP implementation since it based on Unbill.

    LG
     
  2. ap-unity

    ap-unity

    Unity Technologies

    Joined:
    Aug 3, 2016
    Posts:
    1,519
    Hi @MoinTom,

    We are taking a look at this error. Thanks for alerting us to this issue.
     
  3. WillemKokke

    WillemKokke

    Joined:
    Sep 6, 2014
    Posts:
    31
  4. MoinTom

    MoinTom

    Joined:
    May 8, 2015
    Posts:
    15
    @ap-unity Thanks :)

    @WillemKokke Indeed there is a workaround. Just initialize the app and payment with 'en-US' (or a other working one) and switch to the right after the payment is fully initialized.

    Thread.CurrentThread.Culture = CultureInfo.CreateSpecificCulture("en-US");
    ...
    UnityPurchasing.Initialize(<your IStoreListener>, <your ConfigurationBuilder>);
    ...

    In your IStoreListener:
    public void OnInitialized(IStoreController controller, IExtensionProvider extensions) {
    ...
    Thread.CurrentThread.Culture = CultureInfo.CreateSpecificCulture("<the user culture>");​
    }
     
    WillemKokke and ap-unity like this.
  5. WillemKokke

    WillemKokke

    Joined:
    Sep 6, 2014
    Posts:
    31
    Life saver, thanks!
     
  6. WillemKokke

    WillemKokke

    Joined:
    Sep 6, 2014
    Posts:
    31
    Hi @MoinTom

    I can't get your workaround to work I'm afraid.

    I think the deserialisation and parsing of the product descriptions probably happens on a thread other than the unity main thread making the modifications I make pointless.

    Am I doing something wrong?

    I'm using the following code

    Code (CSharp):
    1.     public void Start()
    2.     {
    3.         var module = StandardPurchasingModule.Instance();
    4.         var builder = ConfigurationBuilder.Instance(module);
    5.  
    6.         // setup builder here //
    7.  
    8.         Debug.Log("[CultureInfo] CultureInfo.CurrentCulture.Name: " + CultureInfo.CurrentCulture.Name);
    9.         Debug.Log("[CultureInfo] CultureInfo.CurrentCulture.DisplayName: " + CultureInfo.CurrentCulture.DisplayName);
    10.         Debug.Log("[CultureInfo] Thread.CurrentThread.CurrentCulture.Name: " + Thread.CurrentThread.CurrentCulture.Name);
    11.         Debug.Log("[CultureInfo] Thread.CurrentThread.CurrentCulture.DisplayName: " + Thread.CurrentThread.CurrentCulture.DisplayName);
    12.  
    13.         savedCulture = CultureInfo.CurrentCulture;
    14.         Debug.Log("[CultureInfo] Saved Culture: " + savedCulture.DisplayName);
    15.         Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
    16.         Debug.Log("[CultureInfo] Switched to Culture: " + Thread.CurrentThread.CurrentCulture.DisplayName);
    17.  
    18.         // Now we're ready to initialize Unity IAP.
    19.         UnityPurchasing.Initialize(this, builder);
    20.     }
    21.  
    22.  
    23.     public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    24.     {
    25.         Thread.CurrentThread.CurrentCulture = savedCulture;
    26.         Debug.Log("[CultureInfo] Restored culture to Culture: " + Thread.CurrentThread.CurrentCulture.Name);
    27.     }
    This is the cleaned up log (OnInitialized is never called)

     
  7. WillemKokke

    WillemKokke

    Joined:
    Sep 6, 2014
    Posts:
    31
  8. ap-unity

    ap-unity

    Unity Technologies

    Joined:
    Aug 3, 2016
    Posts:
    1,519
    @MoinTom,

    I'm glad you found a workaround. We are going to have this issue fixed in one of the next releases of the IAP plugin.
     
Thread Status:
Not open for further replies.