Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 470
nontomatic Avatar asked Jul 23 '18 12:07

nontomatic


People also ask

How is multi line string literal defined in Python?

A multiline string in Python begins and ends with either three single quotes or three double quotes. Any quotes, tabs, or newlines in between the “triple quotes” are considered part of the string. Python's indentation rules for blocks do not apply to lines inside a multiline string.

How do you localize a string in Swift?

string file, select it and on the File Inspector (right menu) select “Localize”. Select your the new language you added and click on “Finish”. If you select the file again you should see something similar to the first image below (be sure to select both the supported languages).

What are localization strings?

There are two categories of localized strings: the strings included in the installation package's UI, common to every MSI file. the strings included in your project, that are particular to the current project: the name of your application, file names, registry values, properties etc.


1 Answers

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

like image 129
Martin R Avatar answered Sep 29 '22 11:09

Martin R