Get LocalizedString not working

Hi, I am using the Localization package to translate some text in my UI and because I want the text to change if the player is selecting the button the text is in, i made the following code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.Localization;
public class ChangeButtonText : MonoBehaviour
{
    public LocalizedString textString;
    private TMP_Text thisText;

    void Start ()
    {
        thisText = GetComponent<TMP_Text>();
    }

    public void ChangeText()
    {
        thisText.text = "< " + textString.ToString() + " >";
    }

    public void ResetText()
    {
        thisText.text = textString.ToString();
    }
}

But when I select the button, the text changes to this (example for the play button):
TableReference(293e6970-785b-ac84-e85e-5cf9acb9ee0d - UI Text)/TableEntryReference(31665307648 - Play)

I don’t think this is how you get the string out out a LocalizedString.

Pretty sure you need to use the .GetLocalizedString() method…

Ok, while looking how to use the GetLocalizedString() method, I found another way:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.Localization;
using UnityEngine.Localization.Tables;
public class ChangeButtonText : MonoBehaviour
{
    [SerializeField]
    private LocalizedStringTable localizedStringTable;

    private StringTable currentStringTable;
    public string stringToGet;
    private TMP_Text thisText;

    void Start ()
    {
        thisText = GetComponent<TMP_Text>();
    }

    public void ChangeText()
    {
        StartCoroutine(ChangeTextButton());
       
    }

    public void ResetText()
    {
        StartCoroutine(ResetButtonText());
    }

    IEnumerator ChangeTextButton()
    {
        var tableLoading = localizedStringTable.GetTable();
        yield return tableLoading;

        currentStringTable = tableLoading.Result;

        var str = currentStringTable[stringToGet].LocalizedValue;

        thisText.text = "< " + str + " >";
    }

    IEnumerator ResetButtonText()
    {
        var tableLoading = localizedStringTable.GetTable();
        yield return tableLoading;

        currentStringTable = tableLoading.Result;

        var str = currentStringTable[stringToGet].LocalizedValue;

        thisText.text = str;
    }
}

First of all, could this be right. And secondly, on the 37 and 49 I get the followinf error:
Assets\Scripts\UI\ChangeButtonText.cs(37,43): error CS1061: ‘StringTable’ does not contain a definition for ‘Result’ and no accessible extension method ‘Result’ accepting a first argument of type ‘StringTable’ could be found (are you missing a using directive or an assembly reference?)

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

You can remove the .Result part and it should work fine. Result is a member of the Async version, GetTableAsync but the GetTable version just returns the table without the AsyncOperationHandle wrapper.
Use the tools you have to hand, visual studio and other ide’s should tell you the type being returned and the members it has.
Also if you copied this from the docs make sure you are using a version of the docs for the same version of the package. Some API including this changed during early development. I know Google likes to send people to 0.4 docs which are nearly 4 years out of date.

1 Like