Search Unity

Question Conditional localization with Smart String?

Discussion in 'Localization Tools' started by jGate99, Nov 29, 2022.

  1. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,943
    Hi there,

    Assuming I have a Text
    "Hello This is Text"

    Now assuming I get player name on runtime and either player provide it or not, above text will change into this

    "Hello PlayerName, This is Text"
    If playername is null
    "Hello This is Text"

    Notice with playername is not null then "," is also included this is where i need help

    "Hello {Condition:'${PlayerName},' : 'This'} is Text"

    Something like this?
    Please advise
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
  3. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,943
    Thanks, so it'd be like this?



    var playerName = randomBool ? "John" : null;

    Smart.Format("{0:choose(null|):You|{},}", playerName);
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Without the | after null

    Smart.Format("{0:choose(null):You|{},}", playerName);

    Also, I would not use Smart.Format, that's for direct access to the smart strings library. I would go through the localization system instead of handling it myself.

    Put the smart string in a string table and mark the entry as smart. Then pass the argument into a LocalizedString or use GetLocalizedString.
     
  5. maikkanerva

    maikkanerva

    Joined:
    Dec 14, 2018
    Posts:
    27
    Hey, I stumbled upon this thread as I'm trying to have "conditional" rows of text in a localized string, an entry would look like this:

    Code (CSharp):
    1.  
    2. You lost the battle.
    3. {0:choose(null): |<style="neg">{} was injured</style>}
    4. {1:choose(null): |<style="neg">{} was injured</style>}
    5. {2:choose(null): |<style="neg">{} was injured</style>}
    6.  
    I would like to have max. 3 rows (args 0-2) and only show a row if the argument is assigned. However, even if I assign an argument to this LocalizedString like this:
    Code (CSharp):
    1. description.Arguments = new[] { "Michael" };
    It will always just show the empty space (or if I put something else there like ...:empty|... then it would be that). Is there a better approach for something like this, meaning a listing of text where the amount of rows could change based on the arguments?

    EDIT: I had version 1.0.5 of localization, updated to 1.4.3, but that didn't help.
     
    Last edited: May 17, 2023
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Hi,

    I would use the ListFormatter

    Something like

    {0:list:<style="neg">{} was injured</style>\n|\n}


    Then pass in a List/Array of items

    Code (csharp):
    1. description.Arguments = new []{ new string[]{ "Michael" } };
     
    Last edited: May 19, 2023
    maikkanerva likes this.
  7. maikkanerva

    maikkanerva

    Joined:
    Dec 14, 2018
    Posts:
    27
    Hey, just to note that the correct way to format this is like this:
    {0:list:<style="neg">{} was injured</style>|\n}


    The example you gave had {}-brackets after list and stuff inside of the brackets, which caused some errors.
    Figured out from the documentation, thanks for the link on that!
     
  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Oh sorry about that. Yes I put too many brackets in, not sure what I was thinking. Glad the docs helped. Ill update my example so no future readers get confused.