Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Line breaks in .xcstrings catalog

I’m trying to add a line break to a string in the new Xcode 15 strings catalog, aka xcstrings. Thinking of nothing, I added the usual \n to the place where I want the line break.

But in my SwiftUI view I get to see the actual escaped \n and the text does not break lines. So the text "lorem ipsum\ndolor" gets presented to the user as "lorem ipsum\ndolor".

How can I create a text with intentional line breaks without using multiple independent strings in the catalog?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

  • Solution 1:
    Keep Option when adding a new line with Enter.

  • Solution 2:
    Edit manually the xcstrings (which is a JSON file).

If you open the xcstrings file with a text editor, or right click on it, Open as/Source Code, you’ll see this:

{
  "sourceLanguage" : "en",
  "strings" : {
    "key1" : {
      "extractionState" : "manual",
      "localizations" : {
        "en" : {
          "stringUnit" : {
            "state" : "translated",
            "value" : "key1\n2"
          }
        }
      }
    },
    "key2" : {
      "extractionState" : "manual",
      "localizations" : {
        "en" : {
          "stringUnit" : {
            "state" : "translated",
            "value" : "key2\\n2"
          }
        }
      }
    }
  },
  "version" : "1.0"
}

On key2, I write explicitly in the interface "\n" as you tried, and on key1, I kept Option while typing new line.
Seeing in the source \\n explain why it’s rendered once in the UI as visually \n and not a new line, since the backslash before the "n" is backslashed (ie escaped)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading