Search Unity

Can I check if table entry exists with the table name and string key?

Discussion in 'Localization Tools' started by unace, May 16, 2020.

  1. unace

    unace

    Joined:
    Oct 13, 2014
    Posts:
    23
    Hi,
    question again :)
    I am implementing a message box dialog. i want to show localized string if the input string is a valid string key in a specific table, otherwise, show input string as it is. For example, script will be like below

    Code (CSharp):
    1.  
    2. bool validLocalizedString  = LocaliseTableUtility.Exists("UIText", input.message);
    3. if (validLocalizedString)
    4. {
    5.     messageLocaliseString.StringReference.SetReference("UIText", input.message);
    6.     messageLocaliseString.enabled = true;
    7. }
    8. else
    9. {
    10.     messageLocaliseString.enabled = false;
    11.     messageText.text = input.message;
    12. }
    13.  
    Is there some way to check if table entry exists with the table name and string key?
    Thanks!
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Yes.
    The simplest thing to do now would be to get the table and then try to get the table entry if one does not exist then you will get a null entry or the entry will contain an empty string.

    Code (csharp):
    1. var sd = LocalizationSettings.StringDatabase;
    2. var table = sd.GetTableAsync("UIText");
    3. yield return table;
    4.  
    5. var entry = table.Result.GetEntry(input.message);
    6. if (entry == null ||  string.IsNullOrEmpty(entry.GetLocalizedString())
    7. {
    8.  
    9. }
     
    unace likes this.
  3. unace

    unace

    Joined:
    Oct 13, 2014
    Posts:
    23
    Wow, this is exactly what I want! thank you very much!
     
    karl_jones likes this.
  4. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    Sorry to necro, but I was looking for a solution to this and found this, and I thought I would give the updated solution here to help anyone with this issue out, because the above solution probably works, but throws an error. So, if you don't want errors, here's how you can do it now,
    Code (CSharp):
    1. if(table.GetEntry(input.message) != null)
    2. {
    3.  
    4. }
    You can just check while calling it. Not sure why, but it gives errors if your key doesn't exist and you try to call GetEntry() instead of just returning null.