Search Unity

Bug Format string error with double curly braces in a nested placeholder

Discussion in 'Localization Tools' started by jake_card, Jun 6, 2022.

  1. jake_card

    jake_card

    Joined:
    Oct 2, 2014
    Posts:
    57
    I have a localized smart string that I'd like to write like this:

    Before placeholder {boolVal:cond:{numberVal:formatterOne()}|{numberVal:formatterTwo()}} after placeholder.


    When I write it as above, I get the following error:

    The format string has 1 issue:
    Format string is missing a closing brace
    In: "Before placeholder {boolVal:cond:{numberVal:formatterOne()}|{numberVal:formatterTwo()}} after placeholder."
    At: ----------------------------------------------------------------------------------------------------------^


    As far as I can tell, this error is caused by the two ending curly braces appearing right next to each other. If I slightly reformat the string so that the spaces are inside the conditional like this:

    Before placeholder {boolVal:cond:{numberVal:formatterOne()} |{numberVal:formatterTwo()} }after placeholder.


    I no longer get an error, since the two ending curly braces are now separated by a space.

    Moving these spaces inside the conditional is an okay workaround for now, but I'm pretty sure this is a bug in the smart string formatting, so I figured I'd report it in case there's a chance of it getting fixed in 1.4+.
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,297
    This is actually by design. By default we use string.format escaping, if you do }} in string.format it treats it as a literal }. We do have an alternative escaping mode which lets you do \} instead when you want a literal }. You can enable this mode in the Smart Format settings under Alternative Escaping.
    https://docs.unity3d.com/Packages/com.unity.localization@1.3/manual/LocalizationSettings.html

    If you enable this then you will be able to use double braces.
     
  3. jake_card

    jake_card

    Joined:
    Oct 2, 2014
    Posts:
    57
    Oh haha! Totally makes sense, I'm a fairly recent C# convert so I wasn't familiar with the string.format escaping rules. Everything works great now, thank you!
     
    karl_jones likes this.