Localizing content of Text containing images and text

Viewed 93

SwiftUI Text is capable of displaying text inline with images via "+" operator or string interpolation

(Text("Hello, ") + Text(Image(systemName: "globe")) + Text("!")).font(.title)

or

Text("Hello, \(Image(systemName: "globe")) !").font(.title)

enter image description here

Has anyone ever tried to to localize something like this?

1 Answers

By calling Text("Hello, ") you're using this initialiser:

public init(_ key: LocalizedStringKey, tableName: String? = nil, bundle: Bundle? = nil, comment: StaticString? = nil)

Which means that SwiftUI will try to localise the "Hello, " string (which might not be what you expect).

I'd recommend splitting up text which needs to be localised and other characters/symbols/images:

// Localised part + non-localised part
Text("Hello") + Text(", \(Image(systemName: "globe"))!")

Now you can localise just the "Hello" string and it should work as expected.

Related