Yes, you can use nesting of localized strings but you should be careful about it.
The structure may need to change when dealing with languages such as Spanish where the gender of the object can change the other words. For example, see here Persistent Variables source | Localization | 1.3.2
The gender example is available through the Samples which can be found in the package manager.
You should be able to do something similiar. If you dont have a lot of objects and states then it may be worth just having each sentence as a separate table entry, this will be much easier for a translator to work with.
public LocalizedString withNestedTranslation = new LocalizedString("My String Table", "My Game Text")
{
{ "some-text", new StringVariable { Value = "Hello World" } },
{ "nested", new LocalizedString("My String Table", "My Nested Text")
{
{ "score", new IntVariable { Value = 100 } },
}}
};
You can also call Add on an existing LocalizedString.
myString.Add("nested-string", new LocalizedString("My String Table", "My Nested Text");
So, supposing I want to continue my above implementation and supposing I have the following table where ItemText is the SmartString to setup:
As you said, I can add persistent serialized arguments by using Add. I should to it in lines 11 and 12. What I don’t understand is the values to assign. If there are multiple values (item = {pen, backpack} - color = {red, blue, etc…}, how can I add the right value?
// Add LocalizeStringEvent component
targetGameObject.AddComponent<LocalizeStringEvent>();
// Assign text to update
TextMeshProUGUI textToUpdate = data.text;
// Setup LocalizeString.
LocalizedString localizeString = new LocalizedString("table_name", "entry_name");
// Setup smart fields
localizeString.Add("item", new StringVariable { Value = ?? });
localizeString.Add("color", new StringVariable { Value = ?? });
// Assign localized string and setup update
targetGameObject.GetComponent<LocalizeStringEvent>().StringReference = localizeString;
targetGameObject.GetComponent<LocalizeStringEvent>().OnUpdateString.AddListener(x => textToUpdate.text = x);
targetGameObject.GetComponent<LocalizeStringEvent>().StringReference.RefreshString();
using System;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Components;
public class SetLocalVariable : MonoBehaviour
{
public LocalizeStringEvent localizedString;
public void SetNestedStringEntry(string variableAndEntry)
{
var args = variableAndEntry.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (args.Length != 2)
return;
if (localizedString.StringReference[args[0]] is LocalizedString nested)
{
nested.TableEntryReference = args[1];
}
}
}