Modify EntryName of GameObjectLocalizer from code

Hi,

I can’t find a way to modify EntryName of tracked property from code. Is there any way to do that?

We have unified system for generating string key and I want to use that key here:

I’m mostly interesed in changing this field for localized TextMeshProUGUI components.

Hey,

Yeah you can do it through the SharedTableData - RenameKey.

var stringTableCollection = LocalizationEditorSettings.GetStringTableCollection("My Game Text");
stringTableCollection.SharedData..RenameKey("Old name", "New Name");

// Mark the asset dirty so that Unity saves the changes
EditorUtility.SetDirty(stringTableCollection.SharedData);

I’m aware of RenameKey method, but how can I get “Old name” from the GameObjectLocalizer component?

I think I’ve got it:

public static void AssignNewTerm(GameObjectLocalizer localize, string newTermID, StringTable stringTable) {
            var tmp = localize.GetComponent<TextMeshProUGUI>();
            if (tmp != null) {
                var tableCollection = LocalizationEditorSettings.GetStringTableCollection(stringTable.TableCollectionName);
                if (stringTable.GetEntry(newTermID) == null) {
                    var trackedText = localize.GetTrackedObject<TrackedUGuiGraphic>(tmp);
                    var textVariant = trackedText.GetTrackedProperty<LocalizedStringProperty>("m_text");
                    string oldKey = textVariant.LocalizedString.TableEntryReference.Key;
                    tableCollection.SharedData.RenameKey(oldKey, newTermID);
                } else {
                    stringTable.AddEntry(newTermID, tmp.text);
                }
                EditorUtility.SetDirty(stringTable);
                EditorUtility.SetDirty(tableCollection);
            }
        }

Ah. Something like this:

var text = go.GetComponent<TMPro.TMP_Text>();
var localizer = go.GetComponent<GameObjectLocalizer>();
var trackedObject = localizer.GetTrackedObject(text);
foreach(var prop in trackedObject.TrackedProperties)
{
    if (prop is LocalizedStringProperty stringProp)
    {
        var collection = LocalizationEditorSettings.GetStringTableCollection(stringProp.LocalizedString.TableReference);
        var entry = collection.SharedData.GetEntryFromReference(stringProp.LocalizedString.TableEntryReference);
        Debug.Log("old name " + entry.Key);

        collection.SharedData.RenameKey(entry.Id, "New Name");
        EditorUtility.SetDirty(collection.SharedData);
    }
}

More examples are here https://docs.unity3d.com/Packages/com.unity.localization@1.0/api/UnityEngine.Localization.PropertyVariants.GameObjectLocalizer.html?q=gameobjectlocalizer

1 Like