Search Unity

Resolved Updating LocalizeStringEvent to get specific key

Discussion in 'Localization Tools' started by MaximilianPs, Aug 12, 2022.

  1. MaximilianPs

    MaximilianPs

    Joined:
    Nov 7, 2011
    Posts:
    322
    Please bear with me but I'm still noobs, and also English is not my mother tongue :oops:
    I need to get a specific key at runtime, and to do that, I try to get LocalizedStringEvent Component and set the StringReference so it will update my TextMeshPro component and I will be able to test the text in Game Mode.

    Is that the correct way to do things?
    Code (CSharp):
    1. private string LocalizeName()
    2. {
    3.      localizedString = GetComponent<LocalizeStringEvent>();
    4.      TableReference tableReference;
    5.      tableReference = item.GetCategory();
    6.  
    7.      return localizedString.StringReference.GetLocalizedString(tableReference, item.GetName());
    8. }
    9.  
    At the moment I'm using this method, which is not working because:

    ArgumentException: Empty Table Reference. Must contain a Guid or Table Collection Name

    but I'm trying to pass the correct name.:rolleyes:
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,297
    Hi.
    Yes that's the correct way to do things.
    It looks like the table reference value is empty. Check that item.GetCategory() is returning a non empty/null string.
    Try doing a debug.log of tableReference to see it's value.
     
  3. MaximilianPs

    MaximilianPs

    Joined:
    Nov 7, 2011
    Posts:
    322
    In Debug.Log I've printed out the item.GetName and item.GetCategory
    but I still getting the error:

    ArgumentException: Empty Table Reference. Must contain a Guid or Table Collection Name
    UnityEngine.Localization.Tables.TableReference.Validate () (at Library/PackageCache/com.unity.localization@1.3.2/Runtime/Tables/TableReference.cs:167)


    upload_2022-8-13_10-4-16.png
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,297
    Could you print out the tableReference after you have assigned the category to it?
    Can you share the project so I can take a look?
     
  5. MaximilianPs

    MaximilianPs

    Joined:
    Nov 7, 2011
    Posts:
    322
    I was just working on it with VS debugging tool
    in Immediate windows I get
    Code (CSharp):
    1. tableReference.TableCollectionName
    2. "Weapons"
    Also the data are there, so why I can't extract it? ¯\_(ツ)_/¯

    upload_2022-8-13_10-24-54.png
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,297
    That looks fine.
    What's the full callstack of the error?
    Could this be coming from another script?
     
  7. MaximilianPs

    MaximilianPs

    Joined:
    Nov 7, 2011
    Posts:
    322
    upload_2022-8-13_10-29-13.png

    Ok definitely isn't working
     
  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,297
    That error is from immediate mode, not localization.
    It can't find the method.
    Do it without the arguments. ()
     
  9. MaximilianPs

    MaximilianPs

    Joined:
    Nov 7, 2011
    Posts:
    322
    And we are going back to

    localizedString.StringReference.GetLocalizedString("Weapons");
    Failed to find a match for GetLocalizedString(string)


    Code (CSharp):
    1. tableReference
    2. "TableReference(Weapons)"
    3.     ReferenceType: Name
    4.     SharedTableData: "Weapons(Shared Table Data)"
    5.     TableCollectionName: "Weapons"
    6.     TableCollectionNameGuid: "00000000-0000-0000-0000-000000000000"
    7.     m_TableCollectionName: "Weapons"
    8.     m_Valid: false
    Why its not valid?
     m_Valid: false
     
  10. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,297
    Oooohhhhhhhh! I see the problem.

    Localizedstring does not have a version of GetLocalizedString that takes a table reference and table entry. That data is already stored in the localizedstring. You can use SetReference to update it.
    What's happening here is you are invoking the version that takes a params object[] for formatting and passing in the table reference and entry as arguments for the formatting.
    If you want to update the reference then use SetReference and call GetLocalizedString () with no arguments.

    Alternatively use LocalizationSettings.StringDatabase.GetLocalizedString and pass the references into that

    https://docs.unity3d.com/Packages/c...UnityEngine.Localization.LocalizedString.html
     
  11. MaximilianPs

    MaximilianPs

    Joined:
    Nov 7, 2011
    Posts:
    322
    Sure this is another way to achive it,

    Code (CSharp):
    1. private string LocalizeName()
    2.         {
    3.             Debug.Log(item.GetName() + " " + item.GetCategory());
    4.  
    5.             LocalizeStringEvent localizedString = GetComponent<LocalizeStringEvent>();
    6.             LocalizedString myString = new LocalizedString { TableReference = item.GetCategory(), TableEntryReference = item.GetName() };
    7.  
    8.             return myString.GetLocalizedString();
    9.         }    

    but I don't get how I set the StringReference in the Localize String Event component :D
    upload_2022-8-13_11-0-19.png
     
  12. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,297
  13. MaximilianPs

    MaximilianPs

    Joined:
    Nov 7, 2011
    Posts:
    322
    Yes, I read it and watched a couple of tutorials, but what I'm doing here is to fill a dynamic table with dynamic rows, so I need to fill the String Reference at runtime.
    But if I made it with the script above I can't test the translation with this menu, and that's the reason of this thread o_O:D
    upload_2022-8-13_11-41-28.png

    Anyway thank you for you infinite patience :D

    Oh but at this point couldn't be more simple than that xD
    Code (CSharp):
    1. private string LocalizeName()
    2.         {
    3.             Debug.Log(item.GetName() + " " + item.GetCategory());
    4.  
    5.             LocalizeStringEvent localizedString = GetComponent<LocalizeStringEvent>();
    6.             LocalizedString myString = new LocalizedString { TableReference = item.GetCategory(), TableEntryReference = item.GetName() };
    7.            
    8.            // Update the String Reference into the Component =)
    9.             localizedString.StringReference = myString;
    10.  
    11.             return myString.GetLocalizedString();
    12.         }
     
    Last edited: Aug 13, 2022
  14. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,297
    Yes if you want to get the translated values through script then you can bypass using a LocalizedString and just do:

    Code (csharp):
    1. private string LocalizeName()
    2. {
    3.     return LocalizationSettings.StringDatabase.GetLocalizedString(item.GetCategory(), myString);
    4. }
    You don't need the LocalizedStringEvent component at all for this.

    The advantage of using the LocalizedString is that it can automatically update when the language changes. You will need to use the LocalizationSettings.SelectedLocaleChanged event instead.
     
    MaximilianPs likes this.
  15. MaximilianPs

    MaximilianPs

    Joined:
    Nov 7, 2011
    Posts:
    322
    Got it, thank you again :D

     
    karl_jones likes this.