我试图理解 Go 中的复数形式。文档https://godoc.org/golang.org/x/text/message/catalog#hdr-String_interpolation中的示例不起作用。该方法plural.Select
不存在。应该是plural.Selectf
。注意f
最后。
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)
我在这里找到了另一个教程https://phraseapp.com/blog/posts/internationalization-i18n-go/。此代码工作正常
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.
这两个示例都使用了高级字符串插值。现在我试图理解plural.Selectf
。第一个参数1
在做什么?为什么我需要第二个参数%d
?我想我明白其余的
"one" : "minute"
"other": "minutes"
我也在%[1]d
. catalog.String
这是做什么的?
非常感谢!我现在超级迷茫。