Search Unity

Question Get Key from KeyId from a StringTable / SharedTableData

Discussion in 'Localization Tools' started by Hypnotoad0, Jan 31, 2024.

  1. Hypnotoad0

    Hypnotoad0

    Joined:
    Jun 22, 2013
    Posts:
    47
    Hello. I'm writing an editor script to generate voice audio clips for a string table. I got it to work some time ago, but a new string table I just added is causing some issues. In order to name the generated audio files properly, I need the 'key' from a table entry, but the key is always null. I tried multiple methods:

    This is what worked previously:

    Code (CSharp):
    1.  
    2. var stringTableEntry = LocalizationSettings.StringDatabase.GetTableEntry(bank.sourceStringTable.TableReference, entry.KeyId);
    3. var stringTableKey = stringTableEntry.Entry.SharedEntry.Key;
    4.  
    But the shared entry is null, so this throws an Exception. It works for other string tables though.

    I also tried to get the shared table and get the key from that, like this:

    Code (CSharp):
    1.  
    2. Debug.Log($"[TextToSpeech] Key id {entry.KeyId} converted to key {sharedData.Entries.FirstOrDefault(e => e.Id == entry.KeyId)?.Key}");
    3. Debug.Log($"[TextToSpeech] Key id {entry.KeyId} converted to source table entry {sourceTable.GetEntry(entry.KeyId)}");
    4. Debug.Log($"[TextToSpeech] Key id {entry.KeyId} converted to shared entry {sharedData.GetEntry(entry.KeyId)}");
    But the key is always null, despite definitely existing in the localization string table view:

    upload_2024-1-31_16-50-49.png

    Please help me, I'm losing so much time on this.

    I tried updating the localization package to 1.5.0-pre.6, but this doesn't change anything in this case.
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    I cant see any reason why it would be null, are you sure it's not the Entry that is null?
    If this is an editor script then I would use our Editor api instead.
    LocalizationEditorSettings can do most of what you need.

    Code (csharp):
    1. var collection = LocalizationEditorSettings.GetStringTableCollection("my collection");
    2. var key = collection.SharedData.GetKey(entry.KeyId);
     
  3. Hypnotoad0

    Hypnotoad0

    Joined:
    Jun 22, 2013
    Posts:
    47
    I printed all SharedTableEntries from SharedTableData, they contain keys and look fine.

    upload_2024-1-31_17-9-0.png

    The issue is that their KeyIds differ from the string table ones, that's why it's not matching properly.

    For example, R_Alex_9 (last line in the screenshot) is 1482006498979858 in the shared table, but 1454216161959944 in the string table.

    upload_2024-1-31_17-10-47.png

    I tried the code you've posted, and the result is null.

    Code (CSharp):
    1. var stringTableCollection = LocalizationEditorSettings.GetStringTableCollection(sharedTableName);
    2. var key = stringTableCollection.SharedData.GetKey(entry.KeyId);
    3. Debug.Log(key);
    I guess I can change my approach and iterate all shared table data entries and get string table entries based on them, not the other way around.
     
  4. Hypnotoad0

    Hypnotoad0

    Joined:
    Jun 22, 2013
    Posts:
    47
    Ok, for anyone dealing with something similar: the new approach seems to work. I iterate all shared table data entries and use their keys to get string table entries

    Code (CSharp):
    1. // iterate keys in table
    2. var sourceTable = bank.sourceStringTable.GetTable();
    3. var sharedTableName = sourceTable.SharedData.TableCollectionName;
    4.                  
    5. var sourceTableValues = sourceTable.Values;
    6. Debug.Log($"[TextToSpeechDatabase] Indexing {sourceTableValues.Count} entries from bank {bank.sourceStringTable.TableReference.TableCollectionName}");
    7.                  
    8. var sharedData = sourceTable.SharedData;
    9. Debug.Log($"[TextToSpeechDatabase] Shared table: {sharedData.Entries.Count}");
    10.  
    11. foreach (var sharedTableEntry in sharedData.Entries)
    12. {
    13.     Debug.Log($"[TextToSpeechDatabase] Shared table entry: {sharedTableEntry}, matching entry: {sourceTable.GetEntry(sharedTableEntry.Key)}");
    14. }
     
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    That doesn't sound right. If you have string table entries with Key id values that are not in the shared table data then something has gone wrong and those entries will likely be lost at some point. They also won't have Key names because that's stored in the shared table data.
    Did you delete the shared data entries but not the string table ones at some point?
     
  6. Hypnotoad0

    Hypnotoad0

    Joined:
    Jun 22, 2013
    Posts:
    47
    Hey. No, I didn't delete any string tables or anything of this sort, I'm really confused by this. Getting keys using the following method has worked fine before:

    Code (CSharp):
    1. var stringTableEntry = LocalizationSettings.StringDatabase.GetTableEntry(bank.sourceStringTable.TableReference, entry.KeyId);
    2. var stringTableKey = stringTableEntry.Entry.SharedEntry.Key;
    But in this case, the shared entry is null, which I don't think should ever happen. I guess this particular string table got corrupted somehow, or I ran into some kind of bug.

    The values and amount of entries seem to match between the shared data and string table values, but the IDs are different.

    Using a key from shared table data to get an entry from a string table works as expected, which is even more weird, but I'm glad it worked out and I can move on :D
     
  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Do you have the same entries in multiple string table collections? Maybe you are using the wrong shared table data?
     
  8. Hypnotoad0

    Hypnotoad0

    Joined:
    Jun 22, 2013
    Posts:
    47
    I got the shared table data from the string table, like this

    Code (CSharp):
    1. var sourceTable = bank.sourceStringTable.GetTable();
    2. var sharedData = sourceTable.SharedData;
    This is a string table with 2 locales. I only had an issue with this particular one. The 10 other ones didn't cause any issues of this kind.