nontomatic
nontomatic

Reputation: 2043

iOS: Localise multi-line string literals

Since a recent swift version, multi line string literals are available that allow to format the appearance of a multi line string easily.

I'm looking for a way, though, to localise such a string.

Here is an example of the text of a pre-configured mail, that users can send:

mailComposerVC.setMessageBody("""
                              Hi,

                               I would like to share the following feedback:

                              """,
                              isHTML: false)

It seems there is no way to properly transform this into the localisable .strings file.

As a workaround, I came up with the solution to individually localise each part of the message and use interpolation:

let localisedGreeting = NSLocalizedString("Hi", comment: "")
let localisedMessage = NSLocalizedString("I would like to share the following feedback: ", comment: "")
    mailComposerVC.setMessageBody("""
                                  \(localisedGreeting),

                                   \(localisedMessage)

                                  """,
                                  isHTML: false)

The .strings file looks like this:

"Hi" = "Hallo";
"I would like to share the following feedback: " = "ich möchte folgendes Feedback geben: ";

Is there a better/more concise way to do this?

Upvotes: 9

Views: 5532

Answers (3)

Hollycene
Hollycene

Reputation: 590

You can also create multiline strings directly in a single line using " " and these character literals:

  • Use \n to break the line.
  • Use \n\n to break the line and create a blank line.

A string in Swift file:

let longString: localizedStringKey = "This is the first line.\n\nThis is the second line after the blank line." 

Then the string created above can be used in Localizable.strings like this:

"This is the first line.\n\nThis is the second line." = "This is the first line.\n\nThis is the second line after the blank line.";

Tested on Xcode 15, Swift 5, SwiftUI on iOS 17

Upvotes: 0

mosariot
mosariot

Reputation: 201

For me this layout of text and quotes worked (Swift 5.3, Xcode 12)

Localizable.strings

"First Line
Second Line" = "Erste Linie
Zweite Linie";

Implementation

let lines = NSLocalizedString("""
            First Line
            Second Line
""", comment: "")

Upvotes: 1

Martin R
Martin R

Reputation: 539715

Both keys and values in the Localizable.strings file can be multi-line. With

"Hi,

 I would like to share the following feedback:
" = "Hallo,

 ich möchte folgendes Feedback geben:
";

the call

let localizedMessage = NSLocalizedString("""
                      Hi,

                       I would like to share the following feedback:

                      """, comment: "")

expands as intended.

This might however not work with localization tools, and also does not display with the correct syntax coloring in the Localizable.strings, which might be confusing.

I would use a short (single-line) key instead, and localize that for all languages (including the default language english).

Upvotes: 17

Related Questions