Accessing String Tables from Code

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;
        }
    }
}

Try

tableLoading["FirstTest"].GetLocalizedString(new object []{ _passedData });

You don’t need to pass a formatter.
The arguments are expected to be a list or array of items so the dictionary needs to be item 0 in the collection.

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)

I am extremly stupid, i found my mistake, I used ‘:’ instead of ‘|’ for the plural expression, thank you a lot, it is working now.

1 Like

Oh no it is looking for a Formatter called Apfel, looks like you smart string should be
{Count:plural:Apfel|Äpfel}
Using a | instead of :

We both posted at the same time :wink:

I see, again thanks a lot, i was really struggeling with understanding, how i was supposed to pass in the data ^^

1 Like

@karl_jones
Hi I’m trying get the localized string from script below is my code.


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());

but still getting same error.

Thanks in advance

9934836--1438071--upload_2024-7-11_15-59-14.png

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

I can’t think of any other reason it would happen. Are you able to share the project?

Project ill not be able to share right now…but is there any way to check if smart string is enabled from script and enable is not enabled already