Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Using LocalizationString[i].ToString(); with switch

Discussion in 'Localization Tools' started by ratshayuu, Mar 3, 2023.

  1. ratshayuu

    ratshayuu

    Joined:
    Aug 20, 2022
    Posts:
    13
    Hi I've made a list of Localization Strings to use with global variable.

    Here's the screenshot of the Local Variables,
    and here's the screenshot of the time manager script I've set up.

    I was trying to use the Weather global variable as int. Whenever the randomNumber int is between 0 and 4, the textMeshProUGUI shows one of the five random kinds of weather according to the current language.

    This is the script I have:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Localization;
    6. using UnityEngine.Localization.Settings;
    7. using UnityEngine.Localization.SmartFormat.PersistentVariables;
    8. using UnityEngine.Localization.SmartFormat.Extensions;
    9. using TMPro;
    10.  
    11. public class TimeManager : MonoBehaviour
    12. {
    13.     public IntVariable weather;
    14.     private int randomNumber;
    15.     [SerializeField] private List<LocalizedString> weatherStrings;
    16.     [SerializeField] private TextMeshProUGUI weatherText;
    17.  
    18.     public void Start()
    19.     {
    20.         var source = LocalizationSettings.StringDatabase.SmartFormatter.GetSourceExtension<PersistentVariablesSource>();
    21.         weather = source["global"]["weather"] as IntVariable;
    22.         RandomWeather();
    23.     }
    24.  
    25.     public void RandomWeather()
    26.     {
    27.         randomNumber = Random.Range(0, 4);
    28.         switch (randomNumber)
    29.         {
    30.             case 0:
    31.                 weatherText.text = weatherStrings[0].ToString();
    32.                 break;
    33.             case 1:
    34.                 weatherText.text = weatherStrings[1].ToString();
    35.                 break;
    36.             case 2:
    37.                 weatherText.text = weatherStrings[2].ToString();
    38.                 break;
    39.             case 3:
    40.                 weatherText.text = weatherStrings[3].ToString();
    41.                 break;
    42.             case 4:
    43.                 weatherText.text = weatherStrings[4].ToString();
    44.                 break;
    45.         }
    46.     }
    47. }
    and here's the error I'm getting. It gives me the table reference, the number, the system and everything.
    I want it to show the Localized Entry Text only, but have no idea how. I tried using 'weather' instead of 'randomNumber' but it told me it can't change IntVariable to int.
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,227
    You need to call GetLocalizedString. ToString will just return a string representation of the object instance
    So it should be
     weatherStrings[0].GetLocalizedString()
    .
    For this example it you may find it easier to use a choose formatter to select the weather instead of having a different string for each.
    E.G

    "The weather is {weather:choose(0|1|2|3|4):Cloudy|Raining|Red Mist|Eclipse}"
     
  3. ratshayuu

    ratshayuu

    Joined:
    Aug 20, 2022
    Posts:
    13
    It works perfectly! Thank you!! :)
     
    karl_jones likes this.