Given this view:
struct ContentView: View {
let count = 1
var body: some View {
Text("There are \(count) light(s)")
.padding()
}
}
And this 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>There are %ld light(s)</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@LIGHT_STRING@</string>
<key>LIGHT_STRING</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>ld</string>
<key>one</key>
<string>There is one light</string>
<key>other</key>
<string>There are %ld lights</string>
</dict>
</dict>
</dict>
</plist>
I would expect the view to say "There is one light". But instead, it says "There are 1 light(s)".
What am I missing?
If I add this function:
func tr(_ key: String, _ args: CVarArg...) -> String {
let format = NSLocalizedString(key, comment: "")
return String(format: format, locale: Locale.current, arguments: args)
}
And then use this syntax:
Text(tr("There are %ld light(s)", 1))
.padding()
Then I get the expected result. So I think the problem is something with the Text view not generating the correct key (as opposed to something wrong with my stringsdict file)
OK, got it working. I had to double check a few things:
po LocalizedStringKey("There are \(count) light(s)") in the debugger, which outputted the key About to Update %lld Contact(s) (I was using %ld).Localize… in the file inspectorLocalizable.stringsdictIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With