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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Any way to expand LocalizedStrings in LocalizedString arguments?

Discussion in 'Localization Tools' started by tomtanak, Feb 19, 2021.

  1. tomtanak

    tomtanak

    Joined:
    Mar 24, 2020
    Posts:
    30
    Hello.
    Is there any simple way to use LocalizedStrings as another LocalizedString's arguments?
    When I merely added a LocalizedString to Arguments, it seemed to be expanded by ToString.
    Thank you.
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,846
    Hi,
    This is not supported at the moment but is on the roadmap.
     
  3. tomtanak

    tomtanak

    Joined:
    Mar 24, 2020
    Posts:
    30
    Thank you for the quick reply!
    Okay, I'll look forward to it.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,846
    Do you have any examples/use cases of how you would use this?
    So I can make sure we cover your use case.
     
  5. tomtanak

    tomtanak

    Joined:
    Mar 24, 2020
    Posts:
    30
    Messages like "Enabled: {0:Yes|No}".
    I also want to localize "Yes" and "No" not only "Enabled: {0}".

    For present I can manage on my own.
     
    Last edited: Feb 19, 2021
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,846
    For your example you would set the yes no in each locales table entry. You don't need any nested translations for this.

    E.g
    Spanish
    "Habilitar: {0:Si|No}".
     
  7. tomtanak

    tomtanak

    Joined:
    Mar 24, 2020
    Posts:
    30
    Ah, that was a blind spot.
    That certainly seems to deal with a lot of cases!

    However, although I gave a simple example earlier, I have some cases where I need to display a text like "The following message could not be sent: {0}" and its argument may be a LocalizedString.
     
  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,846
    Ah ok I see. As this example requires a dynamic message you could do this now in script like so:

    With a coroutine
    Code (csharp):
    1. IEnumerator LoadStringWithCoroutine()
    2. {
    3.     var errorMessage = LocalizationSettings.StringDatabase.GetLocalizedStringAsync("Error Messages Table", "My Error Message");
    4.     yield return errorMessage;
    5.  
    6.     var fullMessage = LocalizationSettings.StringDatabase.GetLocalizedStringAsync("Error Messages Table", "My Error Message", arguments: errorMessage.Result);
    7.     yield return fullMessage;
    8.  
    9.     Debug.Log(fullMessage.Result);
    10. }
    To do it with a LocalizedString
    Code (csharp):
    1. public class LocalizedStringWithChangeHandlerExample : MonoBehaviour
    2. {
    3.     public LocalizedString fullMessage = new LocalizedString();
    4.     public LocalizedString error = new LocalizedString();
    5.  
    6.     // Call this to set the error message
    7.     public void SetErrorMessage(string errorKey)
    8.     {
    9.         // This will trigger an update
    10.         error.TableEntryReference = errorKey;
    11.  
    12.         error.StringChanged -= UpdateMessageArguments;
    13.         error.StringChanged += UpdateMessageArguments;
    14.     }
    15.  
    16.     void UpdateMessageArguments(string message)
    17.     {
    18.         fullMessage.Arguments = new object[] { message };
    19.         fullMessage.StringChanged -= UpdateString;
    20.         fullMessage.StringChanged += UpdateString;
    21.     }
    22.  
    23.     void UpdateString(string translatedValue)
    24.     {
    25.         Debug.Log("Full Error Message is " + translatedValue);
    26.     }
    27. }
    So I guess what you are looking for here is the ability to pass a LocalizedString as an argument rather than setting it in the Smart String field?
     
  9. tomtanak

    tomtanak

    Joined:
    Mar 24, 2020
    Posts:
    30
    Yes, sorry for the confusion.
    I wrote a class more general but similar to your LocalizedStringWithChangeHandlerExample.
    Thanks anyway!
     
    karl_jones likes this.