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 Get string from Localize table by script

Discussion in 'Localization Tools' started by miniil, Aug 31, 2023.

  1. miniil

    miniil

    Joined:
    Jul 13, 2022
    Posts:
    10
    Hello,

    I'm trying to write a script that get the localized string.

    I'm trying to use
    UnityEngine.Localization.Settings.LocalizationSettings.StringDatabase.GetLocalizedStringAsync(table, key)

    but I don't know how to wait response and return the string.

    Could you help me?
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,820
  3. miniil

    miniil

    Joined:
    Jul 13, 2022
    Posts:
    10
    Yes,

    I tried this code

    Code (CSharp):
    1. var stringOperation = LocalizationSettings.StringDatabase.GetLocalizedStringAsync("My String", new object[] { 123 });
    2.     stringOperation.Completed += s =>
    3.     {
    4.         // Example output: "The value is 123"
    5.         Debug.Log(s.Result);
    6.     };
    7.  
    But s.Result is always empty string.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,820
    Try passing in the table name before the entry name.

    You are using the version that uses the default table so unless you have also configured a default table it wont work.
    If its still not working then check that you have a localized value for the current language.
     
    Last edited: Aug 31, 2023
  5. miniil

    miniil

    Joined:
    Jul 13, 2022
    Posts:
    10
    Thanks for your help but nothing works for me.
    I'm going crazy :)

    I think there is a problem with the table loading and also because have to table.

    I'll try to explain.

    If the gameobject where I want to load my translations is active when I start the "game" I don't have any value. I got empty string.

    If the gameobject is active later after starting the game it's ok for my first table named "Localization Table". But if I tried to get a value into my second table named "StrumPatternTable" I got no translation found for ... in StrumPatternTable.

    So I think I have two problems :
    1. My Localization tables are not loaded yet when I asked
    2. My second table is not recognized

    Here is my entire code where I tried to load a collection within a dropdown :
    I'm really new with Unity :)

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using System.Text;
    6. using System.Threading.Tasks;
    7. using UnityEngine;
    8. using UnityEngine.Android;
    9. using UnityEngine.Localization;
    10. using UnityEngine.Localization.Settings;
    11. using UnityEngine.Localization.Tables;
    12. using UnityEngine.ResourceManagement.AsyncOperations;
    13. using UnityEngine.UI;
    14.  
    15. public class StrummingPaternHandler : MonoBehaviour
    16. {
    17.  
    18.     public GameObject ChoiceContainer;
    19.     public GameObject StrummingContainer;
    20.     public Dropdown dropdown;
    21.     public Button GoStrummingButton;
    22.  
    23.  
    24.     private Strum strumSelected;
    25.     private Dictionary<int, Strum> strumsPatternsInDropDown = new Dictionary<int, Strum>();
    26.     private string m;
    27.  
    28.     string getLocalString(string table, string key)
    29.     {
    30.         // Sometimes the localized value may not be immediately available.
    31.         // The Localization system may not have been initialized yet or the String Table may need loading.
    32.         // The AsyncOperation wraps this loading operation. We can yield on it in a coroutine,
    33.         // use its various Completed Events or await its Task if using async and await.
    34.         var stringOperation = LocalizationSettings.StringDatabase.GetLocalizedStringAsync(table, key);
    35.         if (stringOperation.IsDone)
    36.         {
    37.             SetString(stringOperation);
    38.             return m;
    39.         }
    40.  
    41.         StartCoroutine(LoadStringWithCoroutine(stringOperation));
    42.         return m;
    43.     }
    44.  
    45.     IEnumerator LoadStringWithCoroutine(AsyncOperationHandle<string> stringOperation)
    46.     {
    47.         yield return stringOperation;
    48.         SetString(stringOperation);
    49.     }
    50.  
    51.     void SetString(AsyncOperationHandle<string> stringOperation)
    52.     {
    53.         // Its possible that something may have gone wrong during loading. We can handle this locally
    54.         // or ignore all errors as they will still be captured and reported by the Localization system.
    55.         if (stringOperation.Status == AsyncOperationStatus.Failed)
    56.             m = "Failed to load string";
    57.         else
    58.             m = stringOperation.Result;
    59.     }
    60.  
    61.     string GetLocalizedString(StringTable table, string entryName)
    62.     {
    63.         // Get the table entry. The entry contains the localized string and Metadata
    64.         var entry = table.GetEntry(entryName);
    65.         return entry.GetLocalizedString(); // We can pass in optional arguments for Smart Format or String.Format here
    66.     }
    67.  
    68.  
    69.     private void Start()
    70.     {
    71.         dropdown.options.Clear();
    72.  
    73.         StrumCollection strumCollection = new StrumCollection();
    74.         Dictionary<string, Strum> strumsPatterns = strumCollection.getStrums();
    75.  
    76.         int i = 0;
    77.  
    78.         foreach (KeyValuePair<string, Strum> s in strumsPatterns)
    79.         {
    80.             Strum strum = s.Value;
    81.             //string value = LocalizationUtils.getTranslationMessage(strum.levelKey, "StrumPatternTable");
    82.             string value = getLocalString("LocalizationTable", "go");
    83.             value += " - ";          
    84.             value += getLocalString("StrumPatternTable", strum.strokes);
    85.  
    86.             dropdown.options.Add(new Dropdown.OptionData()
    87.             {
    88.                 text = value
    89.             });
    90.            
    91.             strumsPatternsInDropDown.Add(i, strum);
    92.             i++;
    93.         }
    94.  
    95.         dropdown.RefreshShownValue();
    96.  
    97.         DropdownItemSelected(dropdown);
    98.  
    99.         dropdown.onValueChanged.AddListener(delegate { DropdownItemSelected(dropdown); });
    100.         GoStrummingButton.onClick.AddListener(delegate { GoToStrumming(); });
    101.     }
    102.  
    103.     void DropdownItemSelected(Dropdown dropdown)
    104.     {
    105.         int index = dropdown.value;
    106.  
    107.         strumSelected = strumsPatternsInDropDown[index];
    108.  
    109.         CurrentStrum.strum = strumSelected;
    110.     }
    111.  
    112.     void GoToStrumming()
    113.     {
    114.         Strum strum = strumSelected;
    115.        
    116.         StrummingContainer.SetActive(true);
    117.         ChoiceContainer.SetActive(false);
    118.     }
    119.  
    120.     public void GoBack()
    121.     {
    122.         StrummingContainer.SetActive(false);
    123.         ChoiceContainer.SetActive(true);
    124.     }
    125.  
    126.     private void Update()
    127.     {
    128.        
    129.     }
    130. }
    131.  
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,820
  7. miniil

    miniil

    Joined:
    Jul 13, 2022
    Posts:
    10
    Thanks I'll tried.

    But first, is there something to call to be sure Localization tables are loaded at start?
     
  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,820
  9. miniil

    miniil

    Joined:
    Jul 13, 2022
    Posts:
    10
    Yes I see this documentation on preloading but I don't have this option.

    I have version 1.3.2 of Localization. I think this option have to be there but I don't have the right part of the windows.
     
  10. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,820
    You need to click the metadata icon next to the language name in the table editor window
     
  11. miniil

    miniil

    Joined:
    Jul 13, 2022
    Posts:
    10
    OK thanks, it seems to work now.
    I don't know what happens before because I do that at first.

    Now, I have to reload those values when users change language from the UI. I don't know how :)
     
  12. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,820
    If you use the LocalizedString.StringChanged event then it will call this when the language changes.
    You can also use LocalizationSettings.SelectedLocaleChanged.
     
  13. miniil

    miniil

    Joined:
    Jul 13, 2022
    Posts:
    10
    oH yes it works :)
    Thank you!

    I can now publish my app in english too :)
     
    karl_jones likes this.