UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Ultimate Portfolio App - Localization of Plurals

Forums > Articles

In the article about the plurals to a localization (and any other documentation on this) give one var in the string eg

Text("\(project.projectItems.count) items")

then in stringdict file

<dict>
    <key>%lld items</key>
    <dict>
        <key>NSStringLocalizedFormatKey</key>
        <string>%#@VARIABLE@</string>
        <key>VARIABLE</key>
        <dict>
            <key>NSStringFormatSpecTypeKey</key>
            <string>NSStringPluralRuleType</string>
            <key>NSStringFormatValueTypeKey</key>
            <string>lld</string>
            <key>zero</key>
            <string>Empty</string>
            <key>one</key>
            <string>1 item</string>
            <key>other</key>
            <string>%lld items</string>
        </dict>
    </dict>
</dict>

however what do you do if you have two variables in the string eg

Text("Completed \(completed) of \(project.projectItems.count) items")

and stringdict

<dict>
    <key>Completed %lld of %lld items</key>
    <dict>
        <key>NSStringLocalizedFormatKey</key>
        <string>%#@VARIABLE@</string>
        <key>VARIABLE</key>
        <dict>
            <key>NSStringFormatSpecTypeKey</key>
            <string>NSStringPluralRuleType</string>
            <key>NSStringFormatValueTypeKey</key>
            <string>lld</string>
            <key>zero</key>
            <string>Empty</string>
            <key>one</key>
            <string>Completed %lld of 1 item</string>
            <key>other</key>
            <string>Completed %lld of %lld items</string>
        </dict>
    </dict>
</dict>

because this do not work properly. Can any help me to understand what to do?

5      

You need to:

  1. rename VARIABLE to something more useful
  2. add more variable names for every thing you want to replace in the string
  3. add a new key and dict for each new variable in your string

Here's an article showing what I mean: Step-by-step guide for localizing plurals in iOS

PS. Sorry I didn't include any code to illustrate. I'm typing this on my phone.

5      

Ok, in the fresh light of day and on a Mac instead of a phone...

  1. change your Text to this: Text("Completed \(completed) of \(project.projectItems.count)")
  2. put this in your Localizable.stringsdict file:
<dict>
    <key>Completed %lld of %lld</key>
    <dict>
        <key>NSStringLocalizedFormatKey</key>
        <string>Completed %d of %#@projectItemsCount@</string>
        <key>projectItemsCount</key>
        <dict>
            <key>NSStringFormatSpecTypeKey</key>
            <string>NSStringPluralRuleType</string>
            <key>NSStringFormatValueTypeKey</key>
            <string>d</string>
            <key>one</key>
            <string>%d item</string>
            <key>other</key>
            <string>%d items</string>
        </dict>
    </dict>
</dict>

5      

Cheers @roosterboy, I am getting tthe hang of it and got it to work, however I have something like this

struct ContentView: View {
    var number = 1
    var total = 1

    var open = true
    @State var message = ""

    var body: some View {
        VStack {
            Text(message)
        }
        .onAppear {
            if open {
                message = "Completed \(number) of \(total) items"
            } else {
                message = "Closed"
            }
        }
    }
}

and put this in the Localizable.stringsdict

<dict>
    <key>Completed %lld of %lld items</key>
    <dict>
        <key>NSStringLocalizedFormatKey</key>
        <string>Completed %lld of %#@total@</string>
        <key>total</key>
        <dict>
            <key>NSStringFormatSpecTypeKey</key>
            <string>NSStringPluralRuleType</string>
            <key>NSStringFormatValueTypeKey</key>
            <string>d</string>
            <key>zero</key>
            <string>Empty</string>
            <key>one</key>
            <string>%d item</string>
            <key>other</key>
            <string>%d items</string>
        </dict>
    </dict>
</dict>

I think because put Text(message) the stringdict do not work but if Text("Completed \(number) of \(total) items") works.

Thank you for your help.

PS. Did do a workaround that would let me add more change of a langange requires

if open {
  switch total {
  case 0:
      message = "Empty"
  case 1:
      message = "Completed \(number) of \(total) item"
  default:
      message = "Completed \(number) of \(total) items"
    }
} else {
  message = "Closed"
}

5      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.