Search Unity

Resolved Modify EntryName of GameObjectLocalizer from code

Discussion in 'Localization Tools' started by Piotr_Wicher, Oct 25, 2021.

  1. Piotr_Wicher

    Piotr_Wicher

    Joined:
    Mar 31, 2020
    Posts:
    12
    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:
    upload_2021-10-25_15-50-58.png

    I'm mostly interesed in changing this field for localized TextMeshProUGUI components.
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,292
    Hey,

    Yeah you can do it through the SharedTableData - RenameKey.

    Code (csharp):
    1. var stringTableCollection = LocalizationEditorSettings.GetStringTableCollection("My Game Text");
    2. stringTableCollection.SharedData..RenameKey("Old name", "New Name");
    3.  
    4. // Mark the asset dirty so that Unity saves the changes
    5. EditorUtility.SetDirty(stringTableCollection.SharedData);
     
  3. Piotr_Wicher

    Piotr_Wicher

    Joined:
    Mar 31, 2020
    Posts:
    12
    I'm aware of RenameKey method, but how can I get "Old name" from the GameObjectLocalizer component?
     
  4. Piotr_Wicher

    Piotr_Wicher

    Joined:
    Mar 31, 2020
    Posts:
    12
    I think I've got it:

    Code (CSharp):
    1. public static void AssignNewTerm(GameObjectLocalizer localize, string newTermID, StringTable stringTable) {
    2.             var tmp = localize.GetComponent<TextMeshProUGUI>();
    3.             if (tmp != null) {
    4.                 var tableCollection = LocalizationEditorSettings.GetStringTableCollection(stringTable.TableCollectionName);
    5.                 if (stringTable.GetEntry(newTermID) == null) {
    6.                     var trackedText = localize.GetTrackedObject<TrackedUGuiGraphic>(tmp);
    7.                     var textVariant = trackedText.GetTrackedProperty<LocalizedStringProperty>("m_text");
    8.                     string oldKey = textVariant.LocalizedString.TableEntryReference.Key;
    9.                     tableCollection.SharedData.RenameKey(oldKey, newTermID);
    10.                 } else {
    11.                     stringTable.AddEntry(newTermID, tmp.text);
    12.                 }
    13.                 EditorUtility.SetDirty(stringTable);
    14.                 EditorUtility.SetDirty(tableCollection);
    15.             }
    16.         }
     
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,292
    Ah. Something like this:

    Code (csharp):
    1. var text = go.GetComponent<TMPro.TMP_Text>();
    2. var localizer = go.GetComponent<GameObjectLocalizer>();
    3. var trackedObject = localizer.GetTrackedObject(text);
    4. foreach(var prop in trackedObject.TrackedProperties)
    5. {
    6.     if (prop is LocalizedStringProperty stringProp)
    7.     {
    8.         var collection = LocalizationEditorSettings.GetStringTableCollection(stringProp.LocalizedString.TableReference);
    9.         var entry = collection.SharedData.GetEntryFromReference(stringProp.LocalizedString.TableEntryReference);
    10.         Debug.Log("old name " + entry.Key);
    11.  
    12.         collection.SharedData.RenameKey(entry.Id, "New Name");
    13.         EditorUtility.SetDirty(collection.SharedData);
    14.     }
    15. }
    16.  
    More examples are here https://docs.unity3d.com/Packages/c...ameObjectLocalizer.html?q=gameobjectlocalizer
     
    Last edited: Oct 25, 2021
    Piotr_Wicher likes this.