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 Conditional localization with Smart String?

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

  1. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,751
    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:
    7,056
  3. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,751
    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:
    7,056
    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.