example_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package plural_test
  5. import (
  6. "golang.org/x/text/feature/plural"
  7. "golang.org/x/text/language"
  8. "golang.org/x/text/message"
  9. )
  10. func ExampleSelect() {
  11. // Manually set some translations. This is typically done programmatically.
  12. message.Set(language.English, "%d files remaining",
  13. plural.Selectf(1, "%d",
  14. "=0", "done!",
  15. plural.One, "one file remaining",
  16. plural.Other, "%[1]d files remaining",
  17. ))
  18. message.Set(language.Dutch, "%d files remaining",
  19. plural.Selectf(1, "%d",
  20. "=0", "klaar!",
  21. // One can also use a string instead of a Kind
  22. "one", "nog één bestand te gaan",
  23. "other", "nog %[1]d bestanden te gaan",
  24. ))
  25. p := message.NewPrinter(language.English)
  26. p.Printf("%d files remaining", 5)
  27. p.Println()
  28. p.Printf("%d files remaining", 1)
  29. p.Println()
  30. p = message.NewPrinter(language.Dutch)
  31. p.Printf("%d files remaining", 1)
  32. p.Println()
  33. p.Printf("%d files remaining", 0)
  34. p.Println()
  35. // Output:
  36. // 5 files remaining
  37. // one file remaining
  38. // nog één bestand te gaan
  39. // klaar!
  40. }