iosswiftinternationalization

iOS internationalization: What's the meaning of the placeholder?


Within "Localizable.xcstrings" string-catalog, for example "%lld click" or even more cryptic "%1$@-%2$@"?

What's there meaning? How have it to read and understood?


Solution

  • It's just an option to format your String with input parameters, for example String(format:arguments:).

    And in this situation, they are:

    let clicks = 10
    let result = String(format: "%lld click(s)", clicks)
    //"10 click(s)"
    
    let string1 = "Hello"
    let string2 = "World"
    let result = String(format: "%1$@-%2$@", string2, string1)
    //"World-Hello"
    

    Notice: an improper format will result in a crash. e.g.

    let clicks = 10
    let result = String(format: "%1$@-%2$@", clicks, clicks) //<- 💥 runtime crash