The sample code:
struct ContentView: View {
var count: Int = 0
var body: some View {
VStack {
Text("Order \(count) Tickets")
}
}
}
Apparently don't use the Localizable.stringsdict
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Order %d Tickets</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@Tickets@</string>
<key>Tickets</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>zero</key>
<string>zero</string>
<key>one</key>
<string>one</string>
<key>other</key>
<string>other</string>
</dict>
</dict>
</dict>
</plist>
What what am I doing wrong?
What do you expect ? What do you get ?
Your use of stringsDict is not correct.
I changed the code and the stringsDict:
struct ContentView: View {
@State var count: Int = 0
var body: some View {
Spacer()
VStack {
Text("Order \(count)")
}
Spacer()
HStack(spacing: 30) {
Button(action: {
if count > 0 { count -= 1 }
}) {
Image(systemName: "minus")
}
Button(action: {
count += 1
}) {
Image(systemName: "plus")
}
}
}
}
- Essentially, Text must not contain Tickets. That will be added from the stringsDict
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Order %lld</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@Ticket@</string>
<key>Ticket</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>lld</string>
<key>zero</key>
<string>Order %lld Ticket</string>
<key>one</key>
<string>Order %lld Ticket</string>
<key>other</key>
<string>Order %lld Tickets</string>
</dict>
</dict>
</dict>
</plist>
Note: this key entry:
<key>zero</key>
<string>Order %lld Ticket</string>
could be replaced by
<key>zero</key>
<string>No Ticket</string>
- Easier to read as plist:
- In the dict, the key is Order %lld, without Ticket
Results:
See details in this tutorial: https://phrase.com/blog/posts/swiftui-tutorial-localization/