Search Unity

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.
     
    Thomas-Mountainborn likes this.
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    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:
    8,281
    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:
    8,281
    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:
    8,281
    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.
  10. Thomas-Mountainborn

    Thomas-Mountainborn

    Joined:
    Jun 11, 2015
    Posts:
    501
    Is this still not a thing? It seems to obvious to have this included. The use case is reusing the same translations, instead of having to put the conditional values within every translation.
     
  11. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
  12. Thomas-Mountainborn

    Thomas-Mountainborn

    Joined:
    Jun 11, 2015
    Posts:
    501
  13. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    The same way. You can add/remove and modify the variables via the LocalizedString API.

    E.G Add a nested localized string:

    Code (csharp):
    1. public LocalizedString withNestedTranslation = new LocalizedString("My String Table", "My Game Text")
    2. {
    3.     { "some-text", new StringVariable { Value = "Hello World" } },
    4.     { "nested", new LocalizedString("My String Table", "My Nested Text")
    5.       {
    6.           { "score", new IntVariable { Value = 100 } },
    7.       }}
    8. };
    or
    Code (csharp):
    1. localizedString.Add("nested", new LocalizedString("My String Table", "My Nested Text"));