How would you translate a enum-like ScriptableObject

Hi!

I’m currently enjoying working with the Localisation Tools. Good start so far, kudos to the team!

Now, for simplicity imagine the following setup:

public class TypeSO : ScriptableObject {
  public string Name;
}

public class DefinitionSO : ScriptableObject {
  public string Name;
  public TypeSO Type;
}

That’s basically an enum, where the game designer can create value by creating more instances of TypeSO in the editor.

Now, I want to display a text, like:

public class UIDisplay : MonoBehaviour {
  [SerializeField]
  private DefinitionSO Definition;

  [SerializeField]
  private TextMeshProUGUI MyText;

  private void OnEnable() {
    MyText.text = $"{Definition.Name} - ${Definition.Type.Name}";
  }
}

The Definition.Name part is a fixed string, that does not need to be translated. However, the Definition.Type.Name needs to be translated.

The user can change the language at runtime, so it needs to react to language changes.

I’m currently not sure, what’s the best approach for this.

Any idea is welcome.

Thanks!

Ok, one moment after I wrote my issue here, I found the solution. :slight_smile:

First, I changed my SO to:

public class TypeSO : ScriptableObject {
  public string Name;

  public LocalizedString Translation;
}

Then, in the MonoBehavior I do:

public class UIDisplay : MonoBehaviour {
  [SerializeField]
  private DefinitionSO Definition;
  [SerializeField]
  private TextMeshProUGUI MyText;
  private void OnEnable() {
    Definition.Type.Translation.StringChanged += UpdateLabel;
  }

  private void OnDisable() {
    Definition.Type.Trnslation.StringChanged -= UpdateLabel;
  }

  private void UpdateLabel(string translation) {
        MyText.text = $"{Definition.Name} - ${translation}";
  }
}

Super easy to do, because StringChanged actually sends you the data directly, if it is available and it would send it again, when the language is changed at runtime.

It let this post here, if someone comes across the same issue.

1 Like

Ah great. I was going to suggest something but looks you found a better solution :slight_smile:

I’m still interested in your idea. I’m pretty new to the Unity Localization Tools and it’s always good to have more tools in the toolbelt, depending on the use case, right? :slight_smile:

I thought you were dealing with the actual enum type. For that you could use a choose formatter, for example:

public enum MyEnum
{
  A,
  B,
  C
}

{0:choose(A|B|C):translated A|translated B|translated C}

1 Like

Yes, with normal enums it’s easier that way. :slight_smile:

Thanks!

1 Like