examples_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package cldr_test
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "golang.org/x/text/internal/gen"
  7. "golang.org/x/text/unicode/cldr"
  8. )
  9. func ExampleDecoder() {
  10. // Obtain the default CLDR reader (only for x/text packages).
  11. var d cldr.Decoder
  12. // Speed up decoding by setting filters for only what you need.
  13. d.SetDirFilter("main", "supplemental")
  14. d.SetSectionFilter("numbers", "plurals")
  15. cldr, err := d.DecodeZip(gen.OpenCLDRCoreZip())
  16. if err != nil {
  17. fmt.Println("ERROR", err)
  18. return
  19. }
  20. supplemental := cldr.Supplemental()
  21. fmt.Println(supplemental.MeasurementData.MeasurementSystem[0].Type)
  22. for _, lang := range cldr.Locales() {
  23. data := cldr.RawLDML(lang)
  24. fmt.Println(lang, data.Identity.Version.Number)
  25. }
  26. }
  27. func ExampleDecoder_DecodePath() {
  28. // This directory will exist if a go generate has been run in any of the
  29. // packages in x/text using the cldr package.
  30. path := filepath.FromSlash("../../DATA/cldr/" + cldr.Version)
  31. var d cldr.Decoder
  32. // Speed up decoding by setting filters for only what you need.
  33. d.SetDirFilter("main")
  34. d.SetSectionFilter("numbers")
  35. cldr, err := d.DecodePath(path)
  36. if err != nil {
  37. // handle error
  38. fmt.Println("ERROR", err)
  39. return
  40. }
  41. for _, lang := range cldr.Locales() {
  42. if numbers := cldr.RawLDML(lang).Numbers; numbers != nil {
  43. fmt.Println(lang, len(numbers.Symbols))
  44. }
  45. }
  46. }
  47. func ExampleDecoder_DecodeZip() {
  48. // This directory will exist if a go generate has been run in any of the
  49. // packages in x/text using the cldr package.
  50. path := filepath.FromSlash("../../DATA/cldr/" + cldr.Version)
  51. var d cldr.Decoder
  52. r, err := os.Open(filepath.Join(path, "core.zip"))
  53. if err != nil {
  54. fmt.Println("error:", err)
  55. return
  56. }
  57. // Only loading supplemental data can be done much faster using a dir
  58. // filter.
  59. d.SetDirFilter("supplemental")
  60. cldr, err := d.DecodeZip(r)
  61. if err != nil {
  62. fmt.Println("error:", err)
  63. return
  64. }
  65. fmt.Println(cldr.Supplemental().MeasurementData.MeasurementSystem[0].Type)
  66. }
  67. func ExampleSlice() {
  68. var dr *cldr.CLDR // assume this is initialized
  69. x, _ := dr.LDML("en")
  70. cs := x.Collations.Collation
  71. // remove all but the default
  72. cldr.MakeSlice(&cs).Filter(func(e cldr.Elem) bool {
  73. return e.GetCommon().Type != x.Collations.Default()
  74. })
  75. for i, c := range cs {
  76. fmt.Println(i, c.Type)
  77. }
  78. }