Search Unity

Localization - FormattingException: Error parsing format string: Could not evaluate the selector

Discussion in 'Localization Tools' started by gokhancnr, Mar 14, 2022.

  1. gokhancnr

    gokhancnr

    Joined:
    Jul 25, 2020
    Posts:
    20
    Hi,

    I'm developing a PoC game. As I started to need localization I prefered to use Unity's official Localization Package. Everthing was fine with static texts and even assets so far. But when it comes to dynamic texts I did everything as it's written in documents but I'm always having the error below:

    FormattingException: Error parsing format string: Could not evaluate the selector "xxx" at ...
    2022-03-14_23-13-58.png


    Thanks in advance.
     
    Last edited: Mar 14, 2022
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    I cant see any reason it would produce this error. You do have a variable with the name "city" so it should be finding it. When does the error occur? When you enter play mode? Does it work if you press the Preview Button? Could it be coming from a different GameObject that does not have the configured variable for city?
     
  3. gokhancnr

    gokhancnr

    Joined:
    Jul 25, 2020
    Posts:
    20
    I don’t understand also and going crazy. It’s happening both in Edit Mode and Play Mode. It’s ok with Preview button.

    If i have only a string variable all alone it’s still producing the same error as well.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Are you able to share the project or an example project with the same problem?
     
  5. gokhancnr

    gokhancnr

    Joined:
    Jul 25, 2020
    Posts:
    20
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    I see the problem. You have setup 2 different systems to localize a GameObject. You are using a GameObjectLocalizer/Localized Property Variants but then also using a LocalizedStringEvent component. You dont need both, just the 1.
    The error comes from the GameObjectLocalizer which is not configured with the variables
    upload_2022-3-15_11-44-3.png

    So you can either
    1. Delete the GameObjectLocalizer
    2. Delete the LocalizeStringEvent and add the variables to the GameObjectLocalizer
     
    Rzeznik likes this.
  7. gokhancnr

    gokhancnr

    Joined:
    Jul 25, 2020
    Posts:
    20
    Thank you so much. You really helped me a lot.

    Thank you for taking your time
     
    karl_jones likes this.
  8. Rzeznik

    Rzeznik

    Joined:
    Apr 10, 2016
    Posts:
    27
    Made the same mistake, would never figure it out!
    Thank you!
     
    karl_jones likes this.
  9. Jontac

    Jontac

    Joined:
    Feb 6, 2019
    Posts:
    33
    I have a similar problem. I get the same error but I don't have a GameObjectLocalizer or anything similar. I created another project to isolate the problem more easily, and in that project it works just fine. I really don't get why I get this error.

    Thanks in advance!
     
  10. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    What's the full error message?
     
  11. Jontac

    Jontac

    Joined:
    Feb 6, 2019
    Posts:
    33
    FormattingException: Error parsing format string: Could not evaluate the selector "time_ready1" at 10
    Ready in {time_ready1} {time_unit1}
    ----------^
    UnityEngine.Localization.SmartFormat.SmartFormatter.FormatError (UnityEngine.Localization.SmartFormat.Core.Parsing.FormatItem errorItem, System.Exception innerException, System.Int32 startIndex, UnityEngine.Localization.SmartFormat.Core.Formatting.FormattingInfo formattingInfo) (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Smart Format/SmartFormatter.cs:347)
    UnityEngine.Localization.SmartFormat.SmartFormatter.Format (UnityEngine.Localization.SmartFormat.Core.Formatting.FormattingInfo formattingInfo) (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Smart Format/SmartFormatter.cs:319)
    UnityEngine.Localization.SmartFormat.SmartFormatter.Format (UnityEngine.Localization.SmartFormat.Core.Formatting.FormatDetails formatDetails, UnityEngine.Localization.SmartFormat.Core.Parsing.Format format, System.Object current) (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Smart Format/SmartFormatter.cs:286)
    UnityEngine.Localization.SmartFormat.SmartFormatter.FormatWithCache (UnityEngine.Localization.SmartFormat.Core.Formatting.FormatCache& cache, System.String format, System.IFormatProvider formatProvider, System.Collections.Generic.IList`1[T] args) (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Smart Format/SmartFormatter.cs:257)
    UnityEngine.Localization.Tables.StringTableEntry.GetLocalizedString (System.IFormatProvider formatProvider, System.Collections.Generic.IList`1[T] args, UnityEngine.Localization.Pseudo.PseudoLocale pseudoLocale) (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Tables/StringTable.cs:176)
    UnityEngine.Localization.Settings.LocalizedStringDatabase.GenerateLocalizedString (UnityEngine.Localization.Tables.StringTable table, UnityEngine.Localization.Tables.StringTableEntry entry, UnityEngine.Localization.Tables.TableReference tableReference, UnityEngine.Localization.Tables.TableEntryReference tableEntryReference, UnityEngine.Localization.Locale locale, System.Collections.Generic.IList`1[T] arguments) (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Settings/Database/LocalizedStringDatabase.cs:244)
    UnityEngine.Localization.LocalizedString.RefreshString () (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Localized Reference/LocalizedString.cs:206)
    UnityEngine.Localization.LocalizedString.AutomaticLoadingCompleted (UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle`1[TObject] loadOperation) (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Localized Reference/LocalizedString.cs:700)
    UnityEngine.Localization.LocalizedString.HandleLocaleChange (UnityEngine.Localization.Locale _) (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Localized Reference/LocalizedString.cs:690)
    UnityEngine.Localization.LocalizedString.ForceUpdate () (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Localized Reference/LocalizedString.cs:593)
    UnityEngine.Localization.LocalizedReference.SetReference (UnityEngine.Localization.Tables.TableReference table, UnityEngine.Localization.Tables.TableEntryReference entry) (at Library/PackageCache/com.unity.localization@1.0.3/Runtime/Localized Reference/LocalizedReference.cs:154)
    ManagerChangePopUpManager.AddAssignedPickerManager (PickerManager MANAGER) (at Assets/ManagerChangePopUpManager.cs:187)
    ManagerChangePopUpManager.BuyNewManager () (at Assets/ManagerChangePopUpManager.cs:60)
    UnityEngine.Events.InvokableCall.Invoke () (at <88f69663e9a64d00b2091dd8dfd4d38f>:0)
    UnityEngine.Events.UnityEvent.Invoke () (at <88f69663e9a64d00b2091dd8dfd4d38f>:0)
    UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:68)
    UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:110)
    UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:50)
    UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:262)
    UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:385)
     
  12. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Have you tried debugging Assets/ManagerChangePopUpManager.cs:60 to see if the arguments are being passed in?
    What is time_ready1?
     
  13. Jontac

    Jontac

    Joined:
    Feb 6, 2019
    Posts:
    33
    Yes, and I'm also using hardcoded values to be sure at the moment.

    I have the smart strings set up as following: "Ready in: {time_ready} {time_unit}" and I have another called "Effect: time_ready} {time_unit}". I added the "1" afterwards in case there was a problem having "{time_ready} {time_unit}" in 2 different smartstring. That doesn't seem to be the problem.

    As far as I can tell there doesn't seem to be a problem with adding the StringReference. The problem seem to come from the second I set the reference.

    This code works without problem:
    Code (CSharp):
    1.         LocalizeStringEvent[] LSEs = testGameObject.transform.GetComponentsInChildren<LocalizeStringEvent>();
    2.         testInt = new IntVariable { Value = 10 };
    3.         var testString = new LocalizedString();
    4.         testString.SetReference("test", "s_min");
    5.         LSEs[0].StringReference.SetReference("test", "s_ready_in");
    6.        
    7.        
    8.         LSEs[0].StringReference.Add("time_ready", testInt);
    9.         LSEs[0].StringReference.Add("time_unit", testString);
    This is the problem-code:
    Code (CSharp):
    1.         IntVariable testInt = new IntVariable { Value = 25 };
    2.         LocalizedString testLocalized = new LocalizedString();
    3.         testLocalized.SetReference("ManagerUI", "s_min");
    4.         alltextsLocalizeStringEvent[1].StringReference.SetReference("ManagerUI","s_Ready_in");
    5.         alltextsLocalizeStringEvent[1].StringReference.Add("time_ready1", testInt);
    6.         alltextsLocalizeStringEvent[1].StringReference.Add("time_unit1", testLocalized);
     
  14. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Oh I think I see the problem.
    You set the reference before you add the local variables. This causes it to trigger an update before it has the variables and will produce the error. Try adding the variables and then setting the reference.
     
  15. Jontac

    Jontac

    Joined:
    Feb 6, 2019
    Posts:
    33
    Yes, now it works! Thank you!

    2 questions:

    1) Why did the exact same code in my test project work with the code being in the wrong order? Shouldn't I have gotten similar results?

    2) Would that mean that if I change from
    "Ready in: {time_ready} {time_unit}" to
    "Effect: time_ready} {time_unit}"
    the string references I had added would remain? So if I'm using the same naming but different underlying data/variables I would first need to remove stringreferences? Or would it just overwrite existing string references with identical names?

    Thanks again!
     
    karl_jones likes this.
  16. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    I'm not sure, I would expect it to also happen in the new project.
    I'm not sure what you mean. If you change the localized string reference then it will use the same local variables. The variables are not tied to the reference but the localized string.
     
    Jontac likes this.
  17. Jontac

    Jontac

    Joined:
    Feb 6, 2019
    Posts:
    33
    What I meant was more or less: Is there a "SetKeyValue" for the smart strings?
    As it is now, I first need to remove the existing
    Code (CSharp):
    1. LocalizedStringEvent.StringReference.Remove(...)
    Then add a new
    Code (CSharp):
    1. LocalizedStringEvent.StringReference.Add(...)
    If I have already created or there already existed a Key with that name, regardless if I assign a new Value or not, I get an error as there already exist a Key with that name.
    If I know for a fact or if I check before hand and see that there is a Key but with a missing or incorret Value. Can I just call and assign that specific Key a new Value?
     
  18. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Oh I see. No at the moment you do need to remove the key but it makes sense to let you override the old value. I'll add a task to look into it.
    Thanks
     
  19. JBflowz

    JBflowz

    Joined:
    Feb 23, 2013
    Posts:
    8
    Hi, Im attempting to just use the sample code for localization and Im having issues by attempting the part of the dynamic text. Im using Unity 2020.3.47f1 with localization version 1.4.3. I've tried multiple times to make it work in even newer versions and the solutions presented here but I continue to have issues. Here is the error I keep getting.

    FormattingException: Error parsing format string: Could not evaluate the selector "appleCount" at 42
    {global.player-name} has {apple-component.appleCount:plural:an apple|{} apples}
    ------------------------------------------^
    UnityEngine.Localization.SmartFormat.SmartFormatter.FormatError (UnityEngine.Localization.SmartFormat.Core.Parsing.FormatItem errorItem, System.Exception innerException, System.Int32 startIndex, UnityEngine.Localization.SmartFormat.Core.Formatting.FormattingInfo formattingInfo) (at Library/PackageCache/com.unity.localization@1.4.3/Runtime/Smart Format/SmartFormatter.cs:347)
    UnityEngine.Localization.SmartFormat.SmartFormatter.Format (UnityEngine.Localization.SmartFormat.Core.Formatting.FormattingInfo formattingInfo) (at Library/PackageCache/com.unity.localization@1.4.3/Runtime/Smart Format/SmartFormatter.cs:319)
    UnityEngine.Localization.SmartFormat.SmartFormatter.Format (UnityEngine.Localization.SmartFormat.Core.Formatting.FormatDetails formatDetails, UnityEngine.Localization.SmartFormat.Core.Parsing.Format format, System.Object current) (at Library/PackageCache/com.unity.localization@1.4.3/Runtime/Smart Format/SmartFormatter.cs:286)
    UnityEngine.Localization.SmartFormat.SmartFormatter.FormatWithCache (UnityEngine.Localization.SmartFormat.Core.Formatting.FormatCache& cache, System.String format, System.IFormatProvider formatProvider, System.Collections.Generic.IList`1[T] args) (at Library/PackageCache/com.unity.localization@1.4.3/Runtime/Smart Format/SmartFormatter.cs:257)
    UnityEngine.Localization.Tables.StringTableEntry.GetLocalizedString (System.IFormatProvider formatProvider, System.Collections.Generic.IList`1[T] args, UnityEngine.Localization.Pseudo.PseudoLocale pseudoLocale) (at Library/PackageCache/com.unity.localization@1.4.3/Runtime/Tables/StringTable.cs:177)
    UnityEngine.Localization.Settings.LocalizedStringDatabase.GenerateLocalizedString (UnityEngine.Localization.Tables.StringTable table, UnityEngine.Localization.Tables.StringTableEntry entry, UnityEngine.Localization.Tables.TableReference tableReference, UnityEngine.Localization.Tables.TableEntryReference tableEntryReference, UnityEngine.Localization.Locale locale, System.Collections.Generic.IList`1[T] arguments) (at Library/PackageCache/com.unity.localization@1.4.3/Runtime/Settings/Database/LocalizedStringDatabase.cs:299)
    UnityEngine.Localization.LocalizedString.RefreshString () (at Library/PackageCache/com.unity.localization@1.4.3/Runtime/Localized Reference/LocalizedString.cs:230)
    UnityEngine.Localization.LocalizedString.AutomaticLoadingCompleted (UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle`1[TObject] loadOperation) (at Library/PackageCache/com.unity.localization@1.4.3/Runtime/Localized Reference/LocalizedString.cs:773)
    DelegateList`1[T].Invoke (T res) (at Library/PackageCache/com.unity.addressables@1.20.5/Runtime/ResourceManager/Util/DelegateList.cs:75)
    UnityEngine.Debug:LogException(Exception)
    DelegateList`1:Invoke(AsyncOperationHandle`1) (at Library/PackageCache/com.unity.addressables@1.20.5/Runtime/ResourceManager/Util/DelegateList.cs:79)
    UnityEngine.ResourceManagement.AsyncOperations.<>c__DisplayClass57_0:<add_CompletedTypeless>b__0(AsyncOperationHandle`1)
    DelegateList`1:Invoke(AsyncOperationHandle`1) (at Library/PackageCache/com.unity.addressables@1.20.5/Runtime/ResourceManager/Util/DelegateList.cs:75)
    UnityEngine.Localization.Operations.LoadTableOperation`2:TableLoaded(AsyncOperationHandle`1)
    DelegateList`1:Invoke(AsyncOperationHandle`1) (at Library/PackageCache/com.unity.addressables@1.20.5/Runtime/ResourceManager/Util/DelegateList.cs:75)
    UnityEngine.ResourceManagement.Util.DelayedActionManager:LateUpdate() (at Library/PackageCache/com.unity.addressables@1.20.5/Runtime/ResourceManager/Util/DelayedActionManager.cs:162)

    For some reason it has issue formatting with the variable appleCount and I can't seem to figure out why this is since I followed the quick start guide step by step. I appreciate your help with this issue.
     
  20. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Can you share a screenshot of your configured Text gameobject and the localized string event values?
     
  21. JBflowz

    JBflowz

    Joined:
    Feb 23, 2013
    Posts:
    8
    Ty so much for your quick response...
    Here is the screenshot,let me know if you need anything else from me.
    I appreciate your help.

    best regards
     

    Attached Files:

  22. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Can you also share the apple script? Nothing stands out in the screen shot
     
  23. JBflowz

    JBflowz

    Joined:
    Feb 23, 2013
    Posts:
    8
    certainly let me know if you find any issues with it :)
     

    Attached Files:

  24. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    I can't see anything wrong. The only other thing to check is that you dragged a reference to the monobehavior instance and not the actual script asset. If you click the apple-component object reference field what does it ping? If it's the Apple script in the project then that's the wrong thing, it should be the attached monobehavior
     
  25. JBflowz

    JBflowz

    Joined:
    Feb 23, 2013
    Posts:
    8
    Hi Karl, I was able to make it work with your help now Im having an issue finding out how to install Unity's csv extension plugin as stated in https://docs.unity3d.com/Packages/c...CSV.html#comma-separated-values-csv-extension and all of my search has been in vain. Not sure if this is an actual plugin or a window that we can get from UI. The actual article seems rather vague in the description of how to get there or plugin. Thanks for the help on advance.
     
  26. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Last edited: May 7, 2023
  27. JBflowz

    JBflowz

    Joined:
    Feb 23, 2013
    Posts:
    8
    Thanks for the quick answer, I just have one further question since the article is not very clear on how to make it work. I don't see any options on the settings of my localization table to bring this menu up, although I did see some scripts in Localization > Editor > Plugins > CSV. I'm just wondering if there are any steps to do with these in order ro make it work or is there just an option in the menu I'm not finding. I apologize if it's a dumb question I've been using Unity for years but I barely have time to keep up with all the changes and updates so your help is greatly appreciated.
     
  28. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    It says in the docs
    The results will depend on the window you click Export in:

     
  29. steikum

    steikum

    Joined:
    Mar 14, 2023
    Posts:
    4
    Hi, sorry if this is the wrong place to ask this question.

    I am suffering from the error below. What I am trying to do is to pass arguments to Arguments and change them dynamically, but it doesn't work with anything other than {0}, including global. Any help would be appreciated.


    FormattingException: Error parsing format string: Could not evaluate the selector "1" at 17
    Cooldown: {0}s ({1}/s)
    -----------------^
     

    Attached Files:

  30. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Can you give an example of the code and what you want the smart string to look like?
     
  31. steikum

    steikum

    Joined:
    Mar 14, 2023
    Posts:
    4
    Thanks for the quick response.
    I tried the code below first.
    Code (CSharp):
    1. public LocalizeStringEvent cooldownLocalizeString;
    Code (CSharp):
    1. cooldownLocalizeString.StringReference.Arguments = new object[]{ itemData.itemCooldown.ToString("N2"), (1/itemData.itemCooldown).ToString("N2"),};
    2.         cooldownLocalizeString.RefreshString();
    and second, I set the string as shown in the screenshot below, and script
    Code (CSharp):
    1.    
    2. public class ItemDescription : MonoBehaviour
    3. {
    4. public float cooldown;
    5.     public float per;}
    I got the following error
    FormattingException: Error parsing format string: Could not evaluate the selector "cooldown" at 18
    Cooldown: {script.cooldown}s ({script.per}/s)
     

    Attached Files:

  32. steikum

    steikum

    Joined:
    Mar 14, 2023
    Posts:
    4
    Sorry. I made a mistake in the reference. However, I still get the same error.
     

    Attached Files:

  33. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    It looks like you may have dragged a reference to the GameObject into the Object Reference for "script".
    You probably want to drag a reference to the MonoBehaviour. If it's on a different GameObject you can open 2 inspectors, lock one to the LocalizedString with the padlock symbol at the top, and then select the other GameObject. You can now drag the mono behaviour from one inspector to another.

    You shouldn't need to assign anything to Arguments for this.
    You will need to call
    cooldownLocalizeString.StringReference.RefreshString
    to force it to update though.
     
    Last edited: Jan 31, 2024
    steikum likes this.
  34. steikum

    steikum

    Joined:
    Mar 14, 2023
    Posts:
    4
    It worked! Sorry I overlooked about it.

    Thank you so much!
     
    karl_jones likes this.