I am trying to retrieve a Smart String Table Entry in a Code File, so far I’ve been scrolling through the docs and various sites for Information, but I couldn’t find anything that worked.
I’m at the Point, where I am able to get the Entry, but it is constantly throwing an Error since it is unable to retrieve the Variable i am trying to pass in, no matter which Datatype I am passing in.
Here is my current Code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.Localization.Tables;
namespace Beta.Localization {
public class LocalizeTest : MonoBehaviour {
// Get a reference to the localized string table
[SerializeField] private LocalizedStringTable _localizedStringTable;
// The Data i am trying to pass in
private Dictionary<string, object> _passedData = new Dictionary<string, object> { ["Count"] = 3 };
private IEnumerator Start() {
//Wait until the Table has been Loaded
StringTable tableLoading = _localizedStringTable.GetTable();
yield return tableLoading;
// Creating a Formatter, seems to be necessary
IFormatProvider formatter = LocalizationSettings.SelectedLocale.Formatter;
// Testing with passing it in as an Array
Dictionary<string, object>[] dataAsArray = { _passedData };
// Testing with passing it in as a Class
PassTest dataAsClass = new PassTest(3);
string str = tableLoading["FirstTest"].GetLocalizedString(formatter, _passedData);
Debug.Log(str);
}
// Text that i want to Evaluate:
// "Es {Count:plural:wurde|wurden} {Count} {Count:plural:Apfel:Äpfel} übergeben"
// Dict with formatter: Error parsing format string: Could not evaluate the selector "Count" at 4
// Dict without formatter: Error parsing format string: No suitable Formatter could be found at 53
// Array, with and without formatter: Error parsing format string: No suitable Formatter could be found at 53
// Class with formatter: Error parsing format string: Could not evaluate the selector "Count" at 4
// Class without formatter: Error parsing format string: No suitable Formatter could be found at 53
}
public class PassTest {
public int Count;
public PassTest(int count) {
Count = count;
}
}
}
First of all Thanks for the super quick response,
sadly this still throws the Exception for “No suitable Formatter could be found”, here is the entire Error, maybe that can help.
FormattingException: Error parsing format string: No suitable Formatter could be found at 53
Es {Count:plural:wurde|wurden} {Count} {Count:plural:Apfel:Äpfel} übergeben
-----------------------------------------------------^
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.0-pre.9/Runtime/Smart Format/SmartFormatter.cs:329)
UnityEngine.Localization.SmartFormat.SmartFormatter.Format (UnityEngine.Localization.SmartFormat.Core.Formatting.FormattingInfo formattingInfo) (at Library/PackageCache/com.unity.localization@1.0.0-pre.9/Runtime/Smart Format/SmartFormatter.cs:313)
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.0-pre.9/Runtime/Smart Format/SmartFormatter.cs:268)
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.0-pre.9/Runtime/Smart Format/SmartFormatter.cs:239)
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.0-pre.9/Runtime/Tables/StringTable.cs:147)
UnityEngine.Localization.Tables.StringTableEntry.GetLocalizedString (System.Object[] args) (at Library/PackageCache/com.unity.localization@1.0.0-pre.9/Runtime/Tables/StringTable.cs:105)
Beta.Localization.LocalizeTest+<Start>d__2.MoveNext () (at Assets/Scripts/Localization/LocalizeTest.cs:29)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <ef667cdc82bf4a4e911d1ddea9ff581d>:0)
As u can see i have enabled smart string but when trying to fetch from code
MaxCountModel maxCountModel = new MaxCountModel { Count = 2, MaxCount = 20 };
var myLocalizedString = new LocalizedString(“LocalizedTable”, samplesDisplayCount);
Debug.Log(myLocalizedString.GetLocalizedString(new object[ ] { maxCountModel }));
getting error as
OperationException : Input string was not in the correct format for String.Format. Ensure that the string is marked as Smart if you intended to use Smart Format. {Count} of {MaxCount} search results are displayed
Also tried with code
var myLocalizedString = new LocalizedString(“LocalizedTable”, “Sample”);
myLocalizedString.Arguments = new object[ ] { maxCountModel };
Debug.Log(myLocalizedString.GetLocalizedString());
That error is when the string is not marked as smart. Are you using the correct table and entry names? Maybe its trying to read a different one?
Try toggling the smart flag off and on, if that fixes it then its a bug.
If that doesn’t solve it can you share the project or an example one with the same issue?
Table and entry names are correct as i could get data for other entries…but as u can see in above picture to toggle on and off smart string. im using 2022.3.36 editor and 1.5.2 localization