Reputation: 2856
I am trying to localize my app using this method: How to implement localization in Swift UI
In general it works. One problem I found is with text concatenation. Example: I have a translation for the text "bookings". To make it work I need to separate my previous code:
Text("bookings: 40")
to be:
Text("bookings")
.fontWeight(.bold)
+ Text(": 40")
.fontWeight(.bold)
Translation still works. The problem is that right now I need to have text formatting twice (in this example: fontWeight, but sometimes it's more complex).
I have tried to make it more simple like this:
Text("bookings" + ": 40)
.fontWeight(.bold)
This code works in English, but is not being translated now to another languages. How should I change my code to make it work and keep it simple?
Upvotes: 3
Views: 2868
Reputation: 539775
Text
localization works with string interpolation, see for example the WWDC 2019: What's new in Swift session video, or Localization in SwiftUI, or this answer.
However, you have to use the correct format specifier. For strings, it is %@
, for integers it is %lld
. Example:
let value = 40
struct ContentView: View {
var body: some View {
Text("bookings: \(value)")
.fontWeight(.bold)
}
}
with the localization entry
"bookings: %lld" = "Buchungen: %lld";
in the Localizable.strings file results in the text "Buchungen: 40" to be displayed in a bold font:
Upvotes: 6