examples_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2014 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 display_test
  5. import (
  6. "fmt"
  7. "golang.org/x/text/language"
  8. "golang.org/x/text/language/display"
  9. "golang.org/x/text/message"
  10. )
  11. func ExampleFormatter() {
  12. message.SetString(language.Dutch, "In %v people speak %v.", "In %v spreekt men %v.")
  13. fr := language.French
  14. region, _ := fr.Region()
  15. for _, tag := range []string{"en", "nl"} {
  16. p := message.NewPrinter(language.Make(tag))
  17. p.Printf("In %v people speak %v.", display.Region(region), display.Language(fr))
  18. p.Println()
  19. }
  20. // Output:
  21. // In France people speak French.
  22. // In Frankrijk spreekt men Frans.
  23. }
  24. func ExampleNamer() {
  25. supported := []string{
  26. "en-US", "en-GB", "ja", "zh", "zh-Hans", "zh-Hant", "pt", "pt-PT", "ko", "ar", "el", "ru", "uk", "pa",
  27. }
  28. en := display.English.Languages()
  29. for _, s := range supported {
  30. t := language.MustParse(s)
  31. fmt.Printf("%-20s (%s)\n", en.Name(t), display.Self.Name(t))
  32. }
  33. // Output:
  34. // American English (American English)
  35. // British English (British English)
  36. // Japanese (日本語)
  37. // Chinese (中文)
  38. // Simplified Chinese (简体中文)
  39. // Traditional Chinese (繁體中文)
  40. // Portuguese (português)
  41. // European Portuguese (português europeu)
  42. // Korean (한국어)
  43. // Arabic (العربية)
  44. // Greek (Ελληνικά)
  45. // Russian (русский)
  46. // Ukrainian (українська)
  47. // Punjabi (ਪੰਜਾਬੀ)
  48. }
  49. func ExampleTags() {
  50. n := display.Tags(language.English)
  51. fmt.Println(n.Name(language.Make("nl")))
  52. fmt.Println(n.Name(language.Make("nl-BE")))
  53. fmt.Println(n.Name(language.Make("nl-CW")))
  54. fmt.Println(n.Name(language.Make("nl-Arab")))
  55. fmt.Println(n.Name(language.Make("nl-Cyrl-RU")))
  56. // Output:
  57. // Dutch
  58. // Flemish
  59. // Dutch (Curaçao)
  60. // Dutch (Arabic)
  61. // Dutch (Cyrillic, Russia)
  62. }
  63. // ExampleDictionary shows how to reduce the amount of data linked into your
  64. // binary by only using the predefined Dictionary variables of the languages you
  65. // wish to support.
  66. func ExampleDictionary() {
  67. tags := []language.Tag{
  68. language.English,
  69. language.German,
  70. language.Japanese,
  71. language.Russian,
  72. }
  73. dicts := []*display.Dictionary{
  74. display.English,
  75. display.German,
  76. display.Japanese,
  77. display.Russian,
  78. }
  79. m := language.NewMatcher(tags)
  80. getDict := func(t language.Tag) *display.Dictionary {
  81. _, i, confidence := m.Match(t)
  82. // Skip this check if you want to support a fall-back language, which
  83. // will be the first one passed to NewMatcher.
  84. if confidence == language.No {
  85. return nil
  86. }
  87. return dicts[i]
  88. }
  89. // The matcher will match Swiss German to German.
  90. n := getDict(language.Make("gsw")).Languages()
  91. fmt.Println(n.Name(language.German))
  92. fmt.Println(n.Name(language.Make("de-CH")))
  93. fmt.Println(n.Name(language.Make("gsw")))
  94. // Output:
  95. // Deutsch
  96. // Schweizer Hochdeutsch
  97. // Schweizerdeutsch
  98. }