Multi-line strings in objective-c localized strings file

13,863

Just use the new lines directly.

"email" = "Hello %@,

Check out %@.

Sincerely,

%@";
Share:
13,863
Christopher Pickslay
Author by

Christopher Pickslay

Agile iOS and web application developer in the SF Bay Area, creator of iKu, a social haiku app (http://iku-app.com). I also built Verbally, an assistive iPad app for people who've lost the ability to speak (http://verballyapp.com). Follow me on twitter at @cpickslay.

Updated on July 10, 2022

Comments

  • Christopher Pickslay
    Christopher Pickslay almost 2 years

    I have a template for an email that I've put in a localized strings file, and I'm loading the string with the NSLocalizedString macro.

    I'd rather not make each line its own string with a unique key. In Objective-C, I can create a human-readable multiline string like so:

    NSString *email = @"Hello %@,\n"
        "\n"
        "Check out %@.\n"
        "\n"
        "Sincerely,\n"
        "\n"
        "%@";
    

    I tried to put that in a .strings file with:

    "email" = "Hello %@,\n"
        "\n"
        "Check out %@.\n"
        "\n"
        "Sincerely,\n"
        "\n"
        "%@";
    

    But I get the following error at build time:

    CFPropertyListCreateFromXMLData(): Old-style plist parser: missing semicolon in dictionary.
    email-template.strings: Unexpected character " at line 1
    Command /Developer/Library/Xcode/Plug-ins/CoreBuildTasks.xcplugin/Contents/Resources/copystrings failed with exit code 1
    

    I can concatenate it all together like this:

    "email" = "Hello %@,\n\nCheck out %@.\n\nSincerely,\n\n%@";
    

    But that will be a mess to maintain, particularly as the email gets longer.

    Is there a way to do this in a localized strings file? I've already tried adding backslashes at the end of each line, to no avail.