【发布时间】:2018-12-25 23:40:13
【问题描述】:
我试图理解 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"
我还在catalog.String 中看到了%[1]d。这是做什么的?
非常感谢!我现在超级迷茫。
【问题讨论】: