I'm trying to understand pluralization in Go. The example from the docs https://godoc.org/golang.org/x/text/message/catalog#hdr-String_interpolation does not work. The method plural.Select
does not event exist. It should be plural.Selectf
. Note the f
at the end.
message.Set(language.English, "You are %d minute(s) late.",
catalog.Var("minutes", plural.Selectf(1, "one", "minute")),
catalog.String("You are %d ${minutes} late."))
p := message.NewPrinter(language.English)
p.Printf("You are %d minute(s) late.", 5)
I found another tutorial here https://phraseapp.com/blog/posts/internationalization-i18n-go/. This code works fine
message.Set(language.English, "You have %d problem",
catalog.Var("minutes", plural.Selectf(1, "%d", "one", "minute", "other", "minutes")),
catalog.String("You are %d ${minutes} late."))
printer := message.NewPrinter(language.English)
printer.Printf("You have %d problem", 1)
printer.Println()
printer.Printf("You have %d problem", 3)
printer.Println()
// result
// You are 1 minute late.
// You are 3 minutes late.
Both examples use the advanced string interpolation. Now I'm trying to understand plural.Selectf
. What is the first argument 1
doing? Why do I need the second argument %d
? I think I understand the rest
"one" : "minute"
"other": "minutes"
I also saw %[1]d
in catalog.String
. What does this do?
Thanks a lot! I'm super confused right now.