8

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?

nontomatic
  • 2,003
  • 2
  • 24
  • 38
  • 1
    The *value* in Localizable.strings could always be multiline (https://stackoverflow.com/a/2968662/1187415) – why do you want the *key* to be multiline? – Martin R Jul 23 '18 at 12:06
  • Thanks, @MartinR, that's interesting. I always thought the exact string would be looked up. I've used the base language (English) string as a key always. If I changed it to work as you suggested I'd need to set up a dedicated .strings file for English as well, correct? – nontomatic Jul 23 '18 at 12:11
  • That is correct. – Martin R Jul 23 '18 at 12:11
  • 1
    @nontomatic i think if it's a localized 1 string then it won't make difference if it's \\n or written multiline – Shehata Gamal Jul 23 '18 at 12:17
  • Yes, but I was trying to give a max stripped down example, but I was looking for a solution that would work comfortably with any multi line string literal and also with custom indentation. – nontomatic Jul 23 '18 at 12:20
  • Aside bar note: I would suggest to use a short text for keys instead of the fully translation for the value, it would be tedious for when using it... – Ahmad F Jul 23 '18 at 12:48

2 Answers2

17

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).

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
0

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: "")
mosariot
  • 171
  • 1
  • 6