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.

Question Change text from Localization String Tables

Discussion in 'Localization Tools' started by marianaloa, Oct 31, 2022.

  1. marianaloa

    marianaloa

    Joined:
    Feb 17, 2021
    Posts:
    15
    Hello i wanna change the text from a key in a localized string table, i want to change this "<icono>" to another thing i want, i already did it in the console but i cant get to change it in the actual table, sorry still new to the localization package in unity
     

    Attached Files:

  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,056
  3. marianaloa

    marianaloa

    Joined:
    Feb 17, 2021
    Posts:
    15

    Attached Files:

  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,056
    Remove the '.Key' part and you will have the entry. You can then set the Localized field.
    Make sure to also mark the table as dirty using
    EditorUtility.SetDirty.
     
  5. marianaloa

    marianaloa

    Joined:
    Feb 17, 2021
    Posts:
    15
    Now i have another doubt, can i check if a key has the "<icono>" thing and then change the text ui instead of the string key? Cause right know i access the key checks that it has the "<icono>" then change it in the console but i dont know if it's better to change it in the text or in the key itself Captura de Pantalla 2022-10-31 a la(s) 18.45.05.png

    Captura de Pantalla 2022-10-31 a la(s) 18.39.46.png

    Code (CSharp):
    1. public class LocalizationIconData : ScriptableObject
    2.     {
    3.         public IconFlow[] Icons;
    4.  
    5.         int _index = 1;
    6.  
    7.         public string spriteSheetName;
    8.         public string actionName;
    9.  
    10.         private void OnValidate()
    11.         {
    12.             GetTableKey();
    13.             LocalizeText();
    14.         }
    15.  
    16.         public string LocalizeText()
    17.         {
    18.             string text = Icons[0].StringReference.GetLocalizedString();
    19.  
    20.             //string actionName = "EmojiOne";
    21.             //string spriteSheet = "Smiling face with smiling eyes";
    22.  
    23.             Debug.Log(text);
    24.  
    25.             if (text.Contains("<icono>"))
    26.             {
    27.                 Debug.Log("it has it");
    28.              
    29.                 text = text.Replace("icono", $"sprite=\"{spriteSheetName}\" name=\"{actionName}\"");
    30.                 Debug.Log(text);
    31.  
    32.             }
    33.             else
    34.             {
    35.                 Debug.Log("no lo tiene");
    36.             }
    37.  
    38.             return text;
    39.         }
    40.  
    41.         private void GetTableKey()
    42.         {
    43.             var collection = UnityEditor.Localization.LocalizationEditorSettings.GetStringTableCollection(Icons[_index].StringReference.TableReference);
    44.  
    45.             if (collection != null)
    46.             {
    47.                 var keyName = collection.SharedData.GetEntryFromReference(Icons[_index].StringReference.TableEntryReference);
    48.                 actionName = keyName.ToString();
    49.             }
    50.  
    51.         }
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,056
    I would use a smart string.

    instead of

    "New Game <icono>"

    do

    "New Game <sprite="{spriteSheetName}" name="{actionName}">";

    Now pass a reference to your instance as an argument to GetLocalizedString.

    Code (csharp):
    1. public class LocalizationIconData : ScriptableObject
    2. {
    3.     public IconFlow[] Icons;
    4.  
    5.     int _index = 1;
    6.  
    7.     public string spriteSheetName;
    8.     public string actionName;
    9.  
    10.     Dictionary<string, string> m_SpriteArguments = new Dictionary<string, string>();
    11.  
    12.     private void OnValidate()
    13.     {
    14.         LocalizeText();
    15.     }
    16.  
    17.     public string LocalizeText()
    18.     {
    19.         string text = Icons[0].StringReference.GetLocalizedString(this);
    20.         return text;
    21.     }
    22. }
    It will now extract the fields spriteSheetName and actionName from the argument using the Reflection Source, if the smart string is not using those arguments then it will just do nothing. This way you keep the same code for strings with and without a sprite.
     
  7. marianaloa

    marianaloa

    Joined:
    Feb 17, 2021
    Posts:
    15
    Okay, i'll try using the smart string and let you know if i find the way to make it work for me
     
    karl_jones likes this.
  8. marianaloa

    marianaloa

    Joined:
    Feb 17, 2021
    Posts:
    15
    Is there a way that i change the action name via script or when i choose the enum i want it changes the action name and then changes the locale variable?
    Captura de Pantalla 2022-11-01 a la(s) 11.57.41.png
    like right now my local variables are set at spriteSheetName = SpritesPS and actionName = north but i want it to be south because that's what i chose in the inspector but it's still north
     
  9. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,056
    Are you using a custom inspector? What does your code look like? Its not saving the changes via the inspector?
     
  10. marianaloa

    marianaloa

    Joined:
    Feb 17, 2021
    Posts:
    15
    Yeah its not, let me show you this is my inspector
    Captura de Pantalla 2022-11-01 a la(s) 17.34.10.png

    And this is the two scripts im using, one for the icons items and the other for whatever it's going on outside. My plan is using the enum of icons to change the action name in the smart string, cause the sprite sheet name is actually gonna be the same all the time. I don't know if maybe im not doing in it in the right script and maybe that's why im not getting what i want

    Script #1

    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using TMPro;
    7. using UnityEngine.Localization;
    8. using UnityEngine.Localization.SmartFormat.PersistentVariables;
    9.  
    10.  
    11.     [CreateAssetMenu(menuName = "LocalizedIconData", fileName = "LocalizedIconData")]
    12.     public class LocalizationIconData : ScriptableObject
    13.     {
    14.         public IconFlow[] Icons;
    15.  
    16.         int _index = 1;
    17.  
    18.         Dictionary<string, string> m_SpriteArguments = new Dictionary<string, string>();
    19.  
    20.         public string spriteSheetName;
    21.        // public string actionName;
    22.  
    23.  
    24.         private void OnValidate()
    25.         {
    26.             //GetTableKey();
    27.             LocalizeText();
    28.         }
    29.  
    30.         public string LocalizeText()
    31.         {
    32.             string text = Icons[0].StringReference.GetLocalizedString(this);
    33.  
    34.             (Icons[0].StringReference["actionName"] as StringVariable).Value = "south";
    35.  
    36.             return text;
    37.  
    38.             /* string text = Icons[0].StringReference.GetLocalizedString();
    39.  
    40.              //string actionName = "EmojiOne";
    41.              //string spriteSheet = "Smiling face with smiling eyes";
    42.  
    43.              Debug.Log(text);
    44.  
    45.              if (text.Contains("<icono>"))
    46.              {
    47.                  Debug.Log("it has it");
    48.  
    49.                  text = text.Replace("icono", $"sprite=\"{spriteSheetName}\" name=\"{actionName}\"");
    50.                  Debug.Log(text);
    51.  
    52.              }
    53.              else
    54.              {
    55.                  Debug.Log("no lo tiene");
    56.              }
    57.  
    58.              return text;*/
    59.  
    60.  
    61.         }
    62.  
    63.         private void GetTableKey()
    64.         {
    65.             var collection = UnityEditor.Localization.LocalizationEditorSettings.GetStringTableCollection(Icons[_index].StringReference.TableReference);
    66.  
    67.             if (collection != null)
    68.             {
    69.                 var keyName = collection.SharedData.GetEntryFromReference(Icons[_index].StringReference.TableEntryReference);
    70.                 actionName = keyName.ToString();
    71.             }
    72.  
    73.         }
    74.  
    75.     }
    76.  
    77.  
    78.  
    and this is script #2

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using TMPro;
    4. using System.Collections.Generic;
    5. using UnityEngine.Localization;
    6.  
    7.  
    8.  
    9.  
    10.     public enum IconType
    11.     {
    12.         north = 0,
    13.         south = 1,
    14.     }
    15.  
    16.     [Serializable]
    17.     public class IconFlow
    18.  
    19.     {
    20.         public LocalizedString StringReference;
    21.         public IconType icons;
    22.         public string actionName;
    23.  
    24.         public void GetActionName(IconType icon)
    25.         {
    26.             if(icon == IconType.north)
    27.             {
    28.                 string enumAsString = nameof(IconType.north);
    29.                 actionName = enumAsString;
    30.             }
    31.             else if(icon == IconType.south)
    32.             {
    33.                 string enumAsString = nameof(IconType.south);
    34.                 actionName = enumAsString;
    35.             }
    36.          
    37.         }
    38.  
    39.     }
    40.  
    41.     public class LocalizedIcons : MonoBehaviour
    42.     {
    43.      
    44.     }
    45.  
    46.  
     
    Last edited: Nov 1, 2022
  11. marianaloa

    marianaloa

    Joined:
    Feb 17, 2021
    Posts:
    15
    Okay i made it work but now i realized im not using smart strings, so im back at the beginning with trying to change the content of a key, i have the selected string reference and what i want to have, i just need to change it in the string table entry with whatever i have in my custom inspector, that is a string that i choose
    Captura de Pantalla 2022-11-02 a la(s) 11.22.22.png
     
  12. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,056
    Are you using the Editor settings class. That will let you change the values
     
  13. marianaloa

    marianaloa

    Joined:
    Feb 17, 2021
    Posts:
    15
    Im sorry to ask but how do you use the editor settings class?
     
  14. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,056