github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/testdata/mod/rsc.io_sampler_v1.2.1.txt (about) 1 generated by ./addmod.bash rsc.io/sampler@v1.2.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.2.1","Name":"cac3af4f8a0ab40054fa6f8d423108a63a1255bb","Short":"cac3af4f8a0a","Time":"2018-02-13T18:16:22Z"}EOF 9 -- hello.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 by Google Translate. 15 16 package sampler 17 18 var hello = newText(` 19 20 English: en: Hello, world. 21 French: fr: Bonjour le monde. 22 Spanish: es: Hola Mundo. 23 24 `) 25 -- hello_test.go -- 26 // Copyright 2018 The Go Authors. All rights reserved. 27 // Use of this source code is governed by a BSD-style 28 // license that can be found in the LICENSE file. 29 30 package sampler 31 32 import ( 33 "testing" 34 35 "golang.org/x/text/language" 36 ) 37 38 var helloTests = []struct { 39 prefs []language.Tag 40 text string 41 }{ 42 { 43 []language.Tag{language.Make("en-US"), language.Make("fr")}, 44 "Hello, world.", 45 }, 46 { 47 []language.Tag{language.Make("fr"), language.Make("en-US")}, 48 "Bonjour le monde.", 49 }, 50 } 51 52 func TestHello(t *testing.T) { 53 for _, tt := range helloTests { 54 text := Hello(tt.prefs...) 55 if text != tt.text { 56 t.Errorf("Hello(%v) = %q, want %q", tt.prefs, text, tt.text) 57 } 58 } 59 } 60 -- sampler.go -- 61 // Copyright 2018 The Go Authors. All rights reserved. 62 // Use of this source code is governed by a BSD-style 63 // license that can be found in the LICENSE file. 64 65 // Package sampler shows simple texts. 66 package sampler // import "rsc.io/sampler" 67 68 import ( 69 "os" 70 "strings" 71 72 "golang.org/x/text/language" 73 ) 74 75 // DefaultUserPrefs returns the default user language preferences. 76 // It consults the $LC_ALL, $LC_MESSAGES, and $LANG environment 77 // variables, in that order. 78 func DefaultUserPrefs() []language.Tag { 79 var prefs []language.Tag 80 for _, k := range []string{"LC_ALL", "LC_MESSAGES", "LANG"} { 81 if env := os.Getenv(k); env != "" { 82 prefs = append(prefs, language.Make(env)) 83 } 84 } 85 return prefs 86 } 87 88 // Hello returns a localized greeting. 89 // If no prefs are given, Hello uses DefaultUserPrefs. 90 func Hello(prefs ...language.Tag) string { 91 if len(prefs) == 0 { 92 prefs = DefaultUserPrefs() 93 } 94 return hello.find(prefs) 95 } 96 97 // A text is a localized text. 98 type text struct { 99 byTag map[string]string 100 matcher language.Matcher 101 } 102 103 // newText creates a new localized text, given a list of translations. 104 func newText(s string) *text { 105 t := &text{ 106 byTag: make(map[string]string), 107 } 108 var tags []language.Tag 109 for _, line := range strings.Split(s, "\n") { 110 line = strings.TrimSpace(line) 111 if line == "" { 112 continue 113 } 114 f := strings.Split(line, ": ") 115 if len(f) != 3 { 116 continue 117 } 118 tag := language.Make(f[1]) 119 tags = append(tags, tag) 120 t.byTag[tag.String()] = f[2] 121 } 122 t.matcher = language.NewMatcher(tags) 123 return t 124 } 125 126 // find finds the text to use for the given language tag preferences. 127 func (t *text) find(prefs []language.Tag) string { 128 tag, _, _ := t.matcher.Match(prefs...) 129 s := t.byTag[tag.String()] 130 if strings.HasPrefix(s, "RTL ") { 131 s = "\u200F" + strings.TrimPrefix(s, "RTL ") + "\u200E" 132 } 133 return s 134 }