03interpolation.go 933 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package main
  2. import (
  3. "fmt"
  4. "golang.org/x/text/feature/plural"
  5. "golang.org/x/text/language"
  6. "golang.org/x/text/message"
  7. "golang.org/x/text/message/catalog"
  8. )
  9. const (
  10. Lateness = "You are %d minute(s) late\n"
  11. )
  12. func init() {
  13. message.Set(language.French, Lateness,
  14. catalog.Var("minutes", plural.Selectf(1, "%d",
  15. "=0", "minute",
  16. "=1", "minute",
  17. plural.Other, "minutes",
  18. )),
  19. catalog.String("Vous êtes en retard de %[1]d ${minutes}\n"),
  20. )
  21. message.Set(language.AmericanEnglish, Lateness,
  22. catalog.Var("minutes", plural.Selectf(1, "%d",
  23. "=1", "minute",
  24. plural.Other, "minutes",
  25. )),
  26. catalog.String("You are %[1]d ${minutes} late\n"),
  27. )
  28. }
  29. func main() {
  30. for _, lang := range []language.Tag{language.French, language.AmericanEnglish} {
  31. fmt.Printf("Language: %v\n", lang)
  32. p := message.NewPrinter(lang)
  33. for i := 0; i <= 2; i++ {
  34. fmt.Print("\t")
  35. p.Printf(Lateness, i)
  36. }
  37. fmt.Println()
  38. }
  39. }