Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Pluralization Perplexity

Discussion in 'Localization Tools' started by egbokalaka, Jul 23, 2023.

  1. egbokalaka

    egbokalaka

    Joined:
    Jun 12, 2023
    Posts:
    13
    I keep getting a ": No suitable Formatter could be found" exception when trying to update a localized string with plurals. I've tried with positional parameters, but discovered I could only use strings with the example codes given. So I tried with local variables with the same result. Here's the error:


    FormattingException: Error parsing format string: No suitable Formatter could be found at 32
    Player {nPlayer} roll {nDice:p:{} die|{} dice}


    And here's the relevant portion of the TMP_TXT object:


    And here is the script driving it:

    Code (CSharp):
    1.  
    2. using TMPro;
    3. using UnityEngine;
    4. using UnityEngine.Localization;
    5.  
    6. public class InstructLabel : MonoBehaviour
    7. {
    8.     public LocalizedString localStringInstruct;
    9.     public TMP_Text textComp;
    10.  
    11.     public int nPlayer;
    12.     public int nDice;
    13.     public void SetText(int player,int dice)
    14.     {
    15.         Debug.Log("InstructLabel.SetText(): player = "+player+"; nDice = "+dice);
    16.  
    17.         nPlayer = player;
    18.         nDice = dice;
    19.         localStringInstruct.RefreshString();
    20.      
    21.     }
    22.     private void OnEnable()
    23.     {
    24.         localStringInstruct.StringChanged += UpdateText;
    25.     }
    26.     private void OnDisable()
    27.     {
    28.         localStringInstruct.StringChanged -= UpdateText;
    29.     }
    30.  
    31.     public void UpdateText(string value)
    32.     {
    33.         textComp.text = value;
    34.     }
    35. }
    36.  
    I haven't used the reflection feature before so I suspect I'm making some sort of bonehead error with it. Please help!
     
    Last edited: Jul 23, 2023
  2. egbokalaka

    egbokalaka

    Joined:
    Jun 12, 2023
    Posts:
    13
    Here's the URL to the screenshot. Somehow it shows up in preview but not in the post:

    https://egbok.com/plural.png
     
  3. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,820
    That URL doesnt seem to work, I get a 404.
    Looks like you may need to add a reference to the object that contains `nDice`. Add an Object Reference variable to the localized string, give it a name (e.g myReference) then drag the component into it. You will need to change the smart string to to `myReference.nDice`
     
  4. egbokalaka

    egbokalaka

    Joined:
    Jun 12, 2023
    Posts:
    13
    There was something amiss at one of my web hosts. I updated the parent post with a working URL.

    I updated the smart string with the object reference. Now I get:

    FormattingException: Error parsing format string: Could not evaluate the selector "InstructLabel" at 8
    Player {InstructLabel.nDice} roll {InstructLabel.nDice:p:{} die|{} dice}
     
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,820
    Ah the image seems to be working now. Looks like you added the reference under both nPlayer and nDice. You only need the 1 reference to The component.
    Drag the InstructLabel component from the gameobject into the Object reference variable.
    Name the variable, e.g "InstructLabel"
     
  6. egbokalaka

    egbokalaka

    Joined:
    Jun 12, 2023
    Posts:
    13
    Now I get :
    [

    FormattingException: Error parsing format string: Could not evaluate the selector "nPlayer" at 22
    Player {InstructLabel.nPlayer} roll {InstructLabel.nDice:p:{} die|{} dice}
    ----------------------^



    And, for completeness sake,

    Code (CSharp):
    1. using TMPro;
    2. using UnityEngine;
    3. using UnityEngine.Localization;
    4.  
    5. public class InstructLabel : MonoBehaviour
    6. {
    7.     public LocalizedString localStringInstruct;
    8.     public TMP_Text textComp;
    9.  
    10.     public int nPlayer;
    11.     public int nDice;
    12.     public void SetText(int player,int dice)
    13.     {
    14.         Debug.Log("InstructLabel.SetText(): player = "+player+"; nDice = "+dice);
    15.  
    16.         nPlayer = player;
    17.         nDice = dice;
    18.         localStringInstruct.RefreshString();
    19.    
    20.     }
    21.     private void OnEnable()
    22.     {
    23.         localStringInstruct.StringChanged += UpdateText;
    24.     }
    25.     private void OnDisable()
    26.     {
    27.         localStringInstruct.StringChanged -= UpdateText;
    28.     }
    29.  
    30.     public void UpdateText(string value)
    31.     {
    32.         textComp.text = value;
    33.     }
    34. }
     
  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,820
    Make sure that the reference is to the component and not the Gameobject. If it's on a different gameobject you can open 2 inspectors, lock one with the padlock icon and drag the component across to the other inspector reference field.
     
  8. egbokalaka

    egbokalaka

    Joined:
    Jun 12, 2023
    Posts:
    13
    I used the script component the current inspector was showing and it worked. It seems a bit self-referential, but I can't argue with success. Thanks for your help! (On a Sunday. Again.)
     
    karl_jones likes this.