gen_index.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2015 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. //go:build ignore
  5. // +build ignore
  6. package main
  7. // This file generates derivative tables based on the language package itself.
  8. import (
  9. "fmt"
  10. "log"
  11. "sort"
  12. "strings"
  13. "golang.org/x/text/internal/language"
  14. )
  15. // Compact indices:
  16. // Note -va-X variants only apply to localization variants.
  17. // BCP variants only ever apply to language.
  18. // The only ambiguity between tags is with regions.
  19. func (b *builder) writeCompactIndex() {
  20. // Collect all language tags for which we have any data in CLDR.
  21. m := map[language.Tag]bool{}
  22. for _, lang := range b.data.Locales() {
  23. // We include all locales unconditionally to be consistent with en_US.
  24. // We want en_US, even though it has no data associated with it.
  25. // TODO: put any of the languages for which no data exists at the end
  26. // of the index. This allows all components based on ICU to use that
  27. // as the cutoff point.
  28. // if x := data.RawLDML(lang); false ||
  29. // x.LocaleDisplayNames != nil ||
  30. // x.Characters != nil ||
  31. // x.Delimiters != nil ||
  32. // x.Measurement != nil ||
  33. // x.Dates != nil ||
  34. // x.Numbers != nil ||
  35. // x.Units != nil ||
  36. // x.ListPatterns != nil ||
  37. // x.Collations != nil ||
  38. // x.Segmentations != nil ||
  39. // x.Rbnf != nil ||
  40. // x.Annotations != nil ||
  41. // x.Metadata != nil {
  42. // TODO: support POSIX natively, albeit non-standard.
  43. tag := language.Make(strings.Replace(lang, "_POSIX", "-u-va-posix", 1))
  44. m[tag] = true
  45. // }
  46. }
  47. // TODO: plural rules are also defined for the deprecated tags:
  48. // iw mo sh tl
  49. // Consider removing these as compact tags.
  50. // Include locales for plural rules, which uses a different structure.
  51. for _, plurals := range b.supp.Plurals {
  52. for _, rules := range plurals.PluralRules {
  53. for _, lang := range strings.Split(rules.Locales, " ") {
  54. m[language.Make(lang)] = true
  55. }
  56. }
  57. }
  58. var coreTags []language.CompactCoreInfo
  59. var special []string
  60. for t := range m {
  61. if x := t.Extensions(); len(x) != 0 && fmt.Sprint(x) != "[u-va-posix]" {
  62. log.Fatalf("Unexpected extension %v in %v", x, t)
  63. }
  64. if len(t.Variants()) == 0 && len(t.Extensions()) == 0 {
  65. cci, ok := language.GetCompactCore(t)
  66. if !ok {
  67. log.Fatalf("Locale for non-basic language %q", t)
  68. }
  69. coreTags = append(coreTags, cci)
  70. } else {
  71. special = append(special, t.String())
  72. }
  73. }
  74. w := b.w
  75. sort.Slice(coreTags, func(i, j int) bool { return coreTags[i] < coreTags[j] })
  76. sort.Strings(special)
  77. w.WriteComment(`
  78. NumCompactTags is the number of common tags. The maximum tag is
  79. NumCompactTags-1.`)
  80. w.WriteConst("NumCompactTags", len(m))
  81. fmt.Fprintln(w, "const (")
  82. for i, t := range coreTags {
  83. fmt.Fprintf(w, "%s ID = %d\n", ident(t.Tag().String()), i)
  84. }
  85. for i, t := range special {
  86. fmt.Fprintf(w, "%s ID = %d\n", ident(t), i+len(coreTags))
  87. }
  88. fmt.Fprintln(w, ")")
  89. w.WriteVar("coreTags", coreTags)
  90. w.WriteConst("specialTagsStr", strings.Join(special, " "))
  91. }
  92. func ident(s string) string {
  93. return strings.Replace(s, "-", "", -1) + "Index"
  94. }