golang.org/x/tools@v0.21.1-0.20240520172518-788d39e776b1/internal/imports/testdata/mod/rsc.io_sampler_v1.3.1.txt (about) 1 rsc.io/sampler@v1.3.1 2 3 -- .mod -- 4 module "rsc.io/sampler" 5 6 require "golang.org/x/text" v0.0.0-20170915032832-14c0d48ead0c 7 -- .info -- 8 {"Version":"v1.3.1","Name":"f545d0289d06e2add4556ea6a15fc4938014bf87","Short":"f545d0289d06","Time":"2018-02-14T16:34:12Z"} 9 -- glass.go -- 10 // Copyright 2018 The Go Authors. All rights reserved. 11 // Use of this source code is governed by a BSD-style 12 // license that can be found in the LICENSE file. 13 14 // Translations from Frank da Cruz, Ethan Mollick, and many others. 15 // See http://kermitproject.org/utf8.html. 16 // http://www.oocities.org/nodotus/hbglass.html 17 // https://en.wikipedia.org/wiki/I_Can_Eat_Glass 18 19 package sampler 20 21 var glass = newText(` 22 23 English: en: I can eat glass and it doesn't hurt me. 24 French: fr: Je peux manger du verre, ça ne me fait pas mal. 25 Spanish: es: Puedo comer vidrio, no me hace daño. 26 27 `) 28 -- glass_test.go -- 29 // Copyright 2018 The Go Authors. All rights reserved. 30 // Use of this source code is governed by a BSD-style 31 // license that can be found in the LICENSE file. 32 33 package sampler 34 35 import ( 36 "testing" 37 38 "golang.org/x/text/language" 39 ) 40 41 var glassTests = []struct { 42 prefs []language.Tag 43 text string 44 }{ 45 { 46 []language.Tag{language.Make("en-US"), language.Make("fr")}, 47 "I can eat glass and it doesn't hurt me.", 48 }, 49 { 50 []language.Tag{language.Make("fr"), language.Make("en-US")}, 51 "Je peux manger du verre, ça ne me fait pas mal.", 52 }, 53 } 54 55 func TestGlass(t *testing.T) { 56 for _, tt := range glassTests { 57 text := Glass(tt.prefs...) 58 if text != tt.text { 59 t.Errorf("Glass(%v) = %q, want %q", tt.prefs, text, tt.text) 60 } 61 } 62 } 63 -- go.mod -- 64 module "rsc.io/sampler" 65 66 require "golang.org/x/text" v0.0.0-20170915032832-14c0d48ead0c 67 -- hello.go -- 68 // Copyright 2018 The Go Authors. All rights reserved. 69 // Use of this source code is governed by a BSD-style 70 // license that can be found in the LICENSE file. 71 72 // Translations by Google Translate. 73 74 package sampler 75 76 var hello = newText(` 77 78 English: en: Hello, world. 79 French: fr: Bonjour le monde. 80 Spanish: es: Hola Mundo. 81 82 `) 83 -- hello_test.go -- 84 // Copyright 2018 The Go Authors. All rights reserved. 85 // Use of this source code is governed by a BSD-style 86 // license that can be found in the LICENSE file. 87 88 package sampler 89 90 import ( 91 "testing" 92 93 "golang.org/x/text/language" 94 ) 95 96 var helloTests = []struct { 97 prefs []language.Tag 98 text string 99 }{ 100 { 101 []language.Tag{language.Make("en-US"), language.Make("fr")}, 102 "Hello, world.", 103 }, 104 { 105 []language.Tag{language.Make("fr"), language.Make("en-US")}, 106 "Bonjour le monde.", 107 }, 108 } 109 110 func TestHello(t *testing.T) { 111 for _, tt := range helloTests { 112 text := Hello(tt.prefs...) 113 if text != tt.text { 114 t.Errorf("Hello(%v) = %q, want %q", tt.prefs, text, tt.text) 115 } 116 } 117 } 118 -- sampler.go -- 119 // Copyright 2018 The Go Authors. All rights reserved. 120 // Use of this source code is governed by a BSD-style 121 // license that can be found in the LICENSE file. 122 123 // Package sampler shows simple texts in a variety of languages. 124 package sampler // import "rsc.io/sampler" 125 126 import ( 127 "os" 128 "strings" 129 130 "golang.org/x/text/language" 131 ) 132 133 // DefaultUserPrefs returns the default user language preferences. 134 // It consults the $LC_ALL, $LC_MESSAGES, and $LANG environment 135 // variables, in that order. 136 func DefaultUserPrefs() []language.Tag { 137 var prefs []language.Tag 138 for _, k := range []string{"LC_ALL", "LC_MESSAGES", "LANG"} { 139 if env := os.Getenv(k); env != "" { 140 prefs = append(prefs, language.Make(env)) 141 } 142 } 143 return prefs 144 } 145 146 // Hello returns a localized greeting. 147 // If no prefs are given, Hello uses DefaultUserPrefs. 148 func Hello(prefs ...language.Tag) string { 149 if len(prefs) == 0 { 150 prefs = DefaultUserPrefs() 151 } 152 return hello.find(prefs) 153 } 154 155 // Glass returns a localized silly phrase. 156 // If no prefs are given, Glass uses DefaultUserPrefs. 157 func Glass(prefs ...language.Tag) string { 158 if len(prefs) == 0 { 159 prefs = DefaultUserPrefs() 160 } 161 return glass.find(prefs) 162 } 163 164 // A text is a localized text. 165 type text struct { 166 byTag map[string]string 167 matcher language.Matcher 168 } 169 170 // newText creates a new localized text, given a list of translations. 171 func newText(s string) *text { 172 t := &text{ 173 byTag: make(map[string]string), 174 } 175 var tags []language.Tag 176 for _, line := range strings.Split(s, "\n") { 177 line = strings.TrimSpace(line) 178 if line == "" { 179 continue 180 } 181 f := strings.Split(line, ": ") 182 if len(f) != 3 { 183 continue 184 } 185 tag := language.Make(f[1]) 186 tags = append(tags, tag) 187 t.byTag[tag.String()] = f[2] 188 } 189 t.matcher = language.NewMatcher(tags) 190 return t 191 } 192 193 // find finds the text to use for the given language tag preferences. 194 func (t *text) find(prefs []language.Tag) string { 195 tag, _, _ := t.matcher.Match(prefs...) 196 s := t.byTag[tag.String()] 197 if strings.HasPrefix(s, "RTL ") { 198 s = "\u200F" + strings.TrimPrefix(s, "RTL ") + "\u200E" 199 } 200 return s 201 }