Line breaks in .xcstrings catalog
Asked Answered
F

1

10

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?

Fray answered 26/9, 2023 at 16:16 Comment(2)
Try keeping Option while entering "new line/enter". I guess it should work. Also, look at the .xcstrings file in Finder, and Preview (or open it with a text editor, it's a JSON file), and compare what you see with manual entering of "\n" and the "Option" key technique. You'll notice that it's \\n (for manual) or just \n (with the Option) explaining why it failed on your attempt.Linkous
Thanks! That did it. I tried both Shift+Enter and Ctrl+Enter, but not Option+Enter... and I really wanted to avoid always going back to editing the raw JSONFray
L
15
  • 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)

Linkous answered 26/9, 2023 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.