Search Unity

Nested LocalizedString's don't seem to work

Discussion in 'Localization Tools' started by Dizzy-Dalvin, Nov 27, 2021.

  1. Dizzy-Dalvin

    Dizzy-Dalvin

    Joined:
    Jul 4, 2013
    Posts:
    54
    Hi, I have two strings, both localized:
    New item unlocked: {0}

    Item name


    And I'm using the following script to combine them:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Localization;
    3. using UnityEngine.Localization.Components;
    4.  
    5. public class TestLocalized : MonoBehaviour
    6. {
    7.     public LocalizeStringEvent Localizer;
    8.     public LocalizedString NewItemUnlocked;
    9.     public LocalizedString ItemName;
    10.  
    11.     void Start()
    12.     {
    13.         NewItemUnlocked.Arguments = new object[] { ItemName };
    14.         Localizer.StringReference = NewItemUnlocked;
    15.     }
    16. }
    17.  
    When I run the game, I expect to see
    New item unlocked: Item name


    but instead I get this:
    New item unlocked: TableReference(3d667d0a-6a06-9394-4b73-627de8664d19 - Items)/TableEntryReference(69468413953 - New Entry 10)


    Looks like it doesn't support nested LocalizedString's passed as arguments or am I doing something wrong?

    Of course, I can do this instead:
    Code (CSharp):
    1. NewItemUnlocked.Arguments = new object[] { ItemName.GetLocalizedString() };
    but it won't work correctly when I change the language.

    Localization package version is 1.0.5.
     
    Last edited: Nov 27, 2021
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Hey.
    Nested localized strings need to be done through persistent variables, they are not supported through scripting arguments. You can get it working by making a few small changes.

    1. Do it through the inspector. and add the nested string directly to the LocalizeStringEvent.

    or

    2. To do it through script you can do something like this:
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.Localization;
    3. using UnityEngine.Localization.Components;
    4.  
    5. public class TestLocalized : MonoBehaviour
    6. {
    7.     public LocalizeStringEvent Localizer;
    8.     public LocalizedString NewItemUnlocked;
    9.     public LocalizedString ItemName;
    10.  
    11.     void Start()
    12.     {
    13.         NewItemUnlocked.Add("my-nested-string", ItemName);
    14.         Localizer.StringReference = NewItemUnlocked;
    15.         NewItemUnlocked.RefreshString();
    16.     }
    17. }
    Your string would look like:
    New item unlocked: {my-nested-string}
     
  3. Dizzy-Dalvin

    Dizzy-Dalvin

    Joined:
    Jul 4, 2013
    Posts:
    54
    Hey, Karl, thanks for the quick response!

    I see, so there's no way to do it without using string keys? I kind of wanted to avoid them.

    Also, it only works the first time. When I try to change the item name later, it throws
    ArgumentException: An item with the same key has already been added.
    . Which would make sense, if I called
    Add()
    , but even if I use an indexer:
    Code (CSharp):
    1. NewItemUnlocked["0"] = ItemName;
    it still throws the same exception.

    And I couldn't find another way to change an already set variable.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Ah that looks like a bug.
    For now you should be able to Remove and then Add.
    Ill create a bug report so we can get it fixed.
     
  5. Dizzy-Dalvin

    Dizzy-Dalvin

    Joined:
    Jul 4, 2013
    Posts:
    54
    Yes, removing and adding it back works, thank you.
     
    karl_jones likes this.