I’m trying to set up my game’s dialogue system to be completely controllable inside my Google Sheets document so translators don’t have to open Unity. Please consider the following situation and offer any suggestions about how to best handle it with the Localization system:
I want to allow for index-based shortcuts for writing placeholders. For example, assume Ashley is a ScriptableObject that I have assigned as a Global Variable named “Ashley”. If a translator typed “{1.name}” in the below example, that would be equivalent to typing “{Ashley.name}”. This would have the same effect as assigning the Ashley object as a local variable named “1” to the LocalizedString in Unity. In my case, it seems more convenient to have these frequently-used objects globally accessible rather than hunting down the relevant LocalizedStrings to assign them as local variables. And it makes it more translator-friendly.
I defined a metadata column “Tags” that assigns global variables to indices. This column would be interpreted when the key is pulled. If I ever wanted to change which characters were involved in this quest, I could just change the Tags metadata rather than editing the placeholders in every column’s translation of this key.
This is a contrived example, but I hope I explained it well.
tl;dr I’m asking how I could intercept and change the contents of placeholders before the LocalizeStringEvent component evaluates the LocalizedString.
Hi,
Interesting use case.
What about assigning these tags to global variables?
Let the system do the swapping for you.
The variables could point to a class/asset that could be returned.
Alternatively, I would not use the LocalizedStringEvent class and instead create my own that can handle what you need.
Something that uses GetTableEntry:
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
public class Example : MonoBehaviour
{
public LocalizedString localizedString = new LocalizedString("My Table", "My Entry");
private void OnEnable()
{
LocalizationSettings.SelectedLocaleChanged += OnSelectedLocaleChanged;
LoadText();
}
void OnSelectedLocaleChanged(Locale locale)
{
LoadText();
}
void LoadText()
{
var result = LocalizationSettings.StringDatabase.GetTableEntry(localizedString.TableReference, localizedString.TableEntryReference);
// Store the original string
var originalString = result.Entry.Value;
// Modify the string - e.g
result.Entry.Value = result.Entry.Value.Replace("{1", "{Ashely");
// Use the normal LocalizedString methods to get the string. This lets us continue to use local variables etc
var translated = result.Entry.GetLocalizedString();
// Restore the original string
result.Entry.Value = originalString;
UpdateText(translated);
}
void UpdateText(string value)
{
// Do something with the translated string
}
}
I have added a task for looking into how we can better support this in the future.