You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

114 lines
3.1 KiB

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