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

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 …

Thanks in advance.

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?

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.

Are you able to share the project or an example project with the same problem?

Of course:

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

So you can either

  1. Delete the GameObjectLocalizer
  2. Delete the LocalizeStringEvent and add the variables to the GameObjectLocalizer
1 Like

Thank you so much. You really helped me a lot.

Thank you for taking your time

1 Like

Made the same mistake, would never figure it out!
Thank you!

1 Like

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!

What’s the full error message?

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.IList1[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.IList1[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.IList1[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.AsyncOperationHandle1[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)

Have you tried debugging Assets/ManagerChangePopUpManager.cs:60 to see if the arguments are being passed in?
What is time_ready1?

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:

        LocalizeStringEvent[] LSEs = testGameObject.transform.GetComponentsInChildren<LocalizeStringEvent>();
        testInt = new IntVariable { Value = 10 };
        var testString = new LocalizedString();
        testString.SetReference("test", "s_min");
        LSEs[0].StringReference.SetReference("test", "s_ready_in");
       
       
        LSEs[0].StringReference.Add("time_ready", testInt);
        LSEs[0].StringReference.Add("time_unit", testString);

This is the problem-code:

        IntVariable testInt = new IntVariable { Value = 25 };
        LocalizedString testLocalized = new LocalizedString();
        testLocalized.SetReference("ManagerUI", "s_min");
        alltextsLocalizeStringEvent[1].StringReference.SetReference("ManagerUI","s_Ready_in");
        alltextsLocalizeStringEvent[1].StringReference.Add("time_ready1", testInt);
        alltextsLocalizeStringEvent[1].StringReference.Add("time_unit1", testLocalized);

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.

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!

1 Like

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.

1 Like

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

LocalizedStringEvent.StringReference.Remove(...)

Then add a new

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?

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

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.IList1[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.IList1[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.IList1[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.AsyncOperationHandle1[TObject] loadOperation) (at Library/PackageCache/com.unity.localization@1.4.3/Runtime/Localized Reference/LocalizedString.cs:773)
DelegateList1[T].Invoke (T res) (at Library/PackageCache/com.unity.addressables@1.20.5/Runtime/ResourceManager/Util/DelegateList.cs:75) UnityEngine.Debug:LogException(Exception) DelegateList1:Invoke(AsyncOperationHandle1) (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(AsyncOperationHandle1)
DelegateList1:Invoke(AsyncOperationHandle1) (at Library/PackageCache/com.unity.addressables@1.20.5/Runtime/ResourceManager/Util/DelegateList.cs:75)
UnityEngine.Localization.Operations.LoadTableOperation2:TableLoaded(AsyncOperationHandle1)
DelegateList1:Invoke(AsyncOperationHandle1) (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.

Can you share a screenshot of your configured Text gameobject and the localized string event values?