stringsdict doesn't works

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?

Answered by Claude31 in 740510022

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/

Accepted Answer

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/

Thank you Claude31 for your detailed explanation.

You let my understand that the error in my simple example (from "Streamline your localised strings" in WWDC21) is the use of %d, which refers to 32bit integer, instead of %ld, which refers to 64bit integer.

While I corrected the mistake, I realised that a widely portable stringsdict should have all theoretically possible values, i.e. "Order %d" (int, or 32-bit integer), "Order %ld" (long int) and "Order %lld" (long long int).

Thanks for the feedback. I must admit that stringsDict is not so easy to understand and use. Good continuation and don't forget to close the thread.

stringsdict doesn't works
 
 
Q