| 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 | |
| 5 | package languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 9 | "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/" |
| 10 | |
| 11 | "golang.org/x/text/internal/languagescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/" |
| 12 | ) |
| 13 | |
| 14 | // The Coverage interface is used to define the level of coverage of an |
| 15 | // internationalization service. Note that not all types are supported by all |
| 16 | // services. As lists may be generated on the fly, it is recommended that users |
| 17 | // of a Coverage cache the results. |
| 18 | type Coveragescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Coverage# interface { |
| 19 | // Tags returns the list of supported tags. |
| 20 | Tagsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Coverage#Tags.() []Tagscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Tag# |
| 21 | |
| 22 | // BaseLanguages returns the list of supported base languages. |
| 23 | BaseLanguagesscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Coverage#BaseLanguages.() []Basescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Base# |
| 24 | |
| 25 | // Scripts returns the list of supported scripts. |
| 26 | Scriptsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Coverage#Scripts.() []Scriptscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Script# |
| 27 | |
| 28 | // Regions returns the list of supported regions. |
| 29 | Regionsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Coverage#Regions.() []Regionscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Region# |
| 30 | } |
| 31 | |
| 32 | var ( |
| 33 | // Supported defines a Coverage that lists all supported subtags. Tags |
| 34 | // always returns nil. |
| 35 | Supportedscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Supported. Coveragescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Coverage# = allSubtagsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/allSubtags#{} |
| 36 | ) |
| 37 | |
| 38 | // TODO: |
| 39 | // - Support Variants, numbering systems. |
| 40 | // - CLDR coverage levels. |
| 41 | // - Set of common tags defined in this package. |
| 42 | |
| 43 | type allSubtagsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/allSubtags# struct{} |
| 44 | |
| 45 | // Regions returns the list of supported regions. As all regions are in a |
| 46 | // consecutive range, it simply returns a slice of numbers in increasing order. |
| 47 | // The "undefined" region is not returned. |
| 48 | func (s allSubtagsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/allSubtags#) Regionsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/allSubtags#Regions().() []Regionscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Region# { |
| 49 | reg := make([]Regionscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Region#, languagescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/.NumRegionsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/NumRegions.) |
| 50 | for i := range reg { |
| 51 | reg[i] = Regionscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Region#{languagescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/.Regionscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Region#(i + 1)} |
| 52 | } |
| 53 | return reg |
| 54 | } |
| 55 | |
| 56 | // Scripts returns the list of supported scripts. As all scripts are in a |
| 57 | // consecutive range, it simply returns a slice of numbers in increasing order. |
| 58 | // The "undefined" script is not returned. |
| 59 | func (s allSubtagsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/allSubtags#) Scriptsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/allSubtags#Scripts().() []Scriptscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Script# { |
| 60 | scr := make([]Scriptscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Script#, languagescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/.NumScriptsscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/NumScripts.) |
| 61 | for i := range scr { |
| 62 | scr[i] = Scriptscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Script#{languagescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/.Scriptscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Script#(i + 1)} |
| 63 | } |
| 64 | return scr |
| 65 | } |
| 66 | |
| 67 | // BaseLanguages returns the list of all supported base languages. It generates |
| 68 | // the list by traversing the internal structures. |
| 69 | func (s allSubtagsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/allSubtags#) BaseLanguagesscip-go gomod golang.org/x/text . `golang.org/x/text/language`/allSubtags#BaseLanguages().() []Basescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Base# { |
| 70 | bs := languagescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/.BaseLanguagesscip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/BaseLanguages().() |
| 71 | base := make([]Basescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Base#, len(bs)) |
| 72 | for i, b := range bs { |
| 73 | base[i] = Basescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Base#{b} |
| 74 | } |
| 75 | return base |
| 76 | } |
| 77 | |
| 78 | // Tags always returns nil. |
| 79 | func (s allSubtagsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/allSubtags#) Tagsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/allSubtags#Tags().() []Tagscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Tag# { |
| 80 | return nil |
| 81 | } |
| 82 | |
| 83 | // coverage is used by NewCoverage which is used as a convenient way for |
| 84 | // creating Coverage implementations for partially defined data. Very often a |
| 85 | // package will only need to define a subset of slices. coverage provides a |
| 86 | // convenient way to do this. Moreover, packages using NewCoverage, instead of |
| 87 | // their own implementation, will not break if later new slice types are added. |
| 88 | type coveragescip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage# struct { |
| 89 | tagsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#tags. func() []Tagscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Tag# |
| 90 | basesscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#bases. func() []Basescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Base# |
| 91 | scriptsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#scripts. func() []Scriptscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Script# |
| 92 | regionsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#regions. func() []Regionscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Region# |
| 93 | } |
| 94 | |
| 95 | func (s *coveragescip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#) Tagsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#Tags().() []Tagscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Tag# { |
| 96 | if s.tagsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#tags. == nil { |
| 97 | return nil |
| 98 | } |
| 99 | return s.tagsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#tags.() |
| 100 | } |
| 101 | |
| 102 | // bases implements sort.Interface and is used to sort base languages. |
| 103 | type basesscip-go gomod golang.org/x/text . `golang.org/x/text/language`/bases# []Basescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Base# |
| 104 | |
| 105 | func (b basesscip-go gomod golang.org/x/text . `golang.org/x/text/language`/bases#) Lenscip-go gomod golang.org/x/text . `golang.org/x/text/language`/bases#Len().() int { |
| 106 | return len(b) |
| 107 | } |
| 108 | |
| 109 | func (b basesscip-go gomod golang.org/x/text . `golang.org/x/text/language`/bases#) Swapscip-go gomod golang.org/x/text . `golang.org/x/text/language`/bases#Swap().(i, j int) { |
| 110 | b[i], b[j] = b[j], b[i] |
| 111 | } |
| 112 | |
| 113 | func (b basesscip-go gomod golang.org/x/text . `golang.org/x/text/language`/bases#) Lessscip-go gomod golang.org/x/text . `golang.org/x/text/language`/bases#Less().(i, j int) bool { |
| 114 | return b[i].langIDscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Base#langID. < b[j].langIDscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Base#langID. |
| 115 | } |
| 116 | |
| 117 | // BaseLanguages returns the result from calling s.bases if it is specified or |
| 118 | // otherwise derives the set of supported base languages from tags. |
| 119 | func (s *coveragescip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#) BaseLanguagesscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#BaseLanguages().() []Basescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Base# { |
| 120 | if s.basesscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#bases. == nil { |
| 121 | tags := s.Tagsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#Tags().() |
| 122 | if len(tags) == 0 { |
| 123 | return nil |
| 124 | } |
| 125 | a := make([]Basescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Base#, len(tags)) |
| 126 | for i, t := range tags { |
| 127 | a[i] = Basescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Base#{languagescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/.Languagescip-go gomod golang.org/x/text . `golang.org/x/text/internal/language`/Language#(t.langscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Tag#lang().())} |
| 128 | } |
| 129 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Sortscip-go gomod github.com/golang/go/src go1.25.0 sort/Sort().(basesscip-go gomod golang.org/x/text . `golang.org/x/text/language`/bases#(a)) |
| 130 | k := 0 |
| 131 | for i := 1; i < len(a); i++ { |
| 132 | if a[k] != a[i] { |
| 133 | k++ |
| 134 | a[k] = a[i] |
| 135 | } |
| 136 | } |
| 137 | return a[:k+1] |
| 138 | } |
| 139 | return s.basesscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#bases.() |
| 140 | } |
| 141 | |
| 142 | func (s *coveragescip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#) Scriptsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#Scripts().() []Scriptscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Script# { |
| 143 | if s.scriptsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#scripts. == nil { |
| 144 | return nil |
| 145 | } |
| 146 | return s.scriptsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#scripts.() |
| 147 | } |
| 148 | |
| 149 | func (s *coveragescip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#) Regionsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#Regions().() []Regionscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Region# { |
| 150 | if s.regionsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#regions. == nil { |
| 151 | return nil |
| 152 | } |
| 153 | return s.regionsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#regions.() |
| 154 | } |
| 155 | |
| 156 | // NewCoverage returns a Coverage for the given lists. It is typically used by |
| 157 | // packages providing internationalization services to define their level of |
| 158 | // coverage. A list may be of type []T or func() []T, where T is either Tag, |
| 159 | // Base, Script or Region. The returned Coverage derives the value for Bases |
| 160 | // from Tags if no func or slice for []Base is specified. For other unspecified |
| 161 | // types the returned Coverage will return nil for the respective methods. |
| 162 | func NewCoveragescip-go gomod golang.org/x/text . `golang.org/x/text/language`/NewCoverage().(list ...interface{}) Coveragescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Coverage# { |
| 163 | s := &coveragescip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#{} |
| 164 | for _, x := range list { |
| 165 | switch v := x.(type) { |
| 166 | case func() []Basescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Base#: |
| 167 | s.basesscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#bases. = v |
| 168 | case func() []Scriptscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Script#: |
| 169 | s.scriptsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#scripts. = v |
| 170 | case func() []Regionscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Region#: |
| 171 | s.regionsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#regions. = v |
| 172 | case func() []Tagscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Tag#: |
| 173 | s.tagsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#tags. = v |
| 174 | case []Basescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Base#: |
| 175 | s.basesscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#bases. = func() []Basescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Base# { return v } |
| 176 | case []Scriptscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Script#: |
| 177 | s.scriptsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#scripts. = func() []Scriptscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Script# { return v } |
| 178 | case []Regionscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Region#: |
| 179 | s.regionsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#regions. = func() []Regionscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Region# { return v } |
| 180 | case []Tagscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Tag#: |
| 181 | s.tagsscip-go gomod golang.org/x/text . `golang.org/x/text/language`/coverage#tags. = func() []Tagscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Tag# { return v } |
| 182 | default: |
| 183 | panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("language: unsupported set type %T", v)) |
| 184 | } |
| 185 | } |
| 186 | return s |
| 187 | } |