4 module "rsc.io/sampler"
6 require "golang.org/x/text" v0.0.0-20170915032832-14c0d48ead0c
8 {"Version":"v1.3.0","Name":"0cc034b51e57ed7832d4c67d526f75a900996e5c","Short":"0cc034b51e57","Time":"2018-02-13T19:05:03Z"}
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.
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
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.
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.
38 "golang.org/x/text/language"
42 var glassTests = []struct {
47 []language.Tag{language.Make("en-US"), language.Make("fr")},
48 "I can eat glass and it doesn't hurt me.",
51 []language.Tag{language.Make("fr"), language.Make("en-US")},
52 "Je peux manger du verre, ça ne me fait pas mal.",
56 func TestGlass(t *testing.T) {
57 for _, tt := range glassTests {
58 text := Glass(tt.prefs...)
60 t.Errorf("Glass(%v) = %q, want %q", tt.prefs, text, tt.text)
65 module "rsc.io/sampler"
67 require "golang.org/x/text" v0.0.0-20170915032832-14c0d48ead0c
69 // Copyright 2018 The Go Authors. All rights reserved.
70 // Use of this source code is governed by a BSD-style
71 // license that can be found in the LICENSE file.
73 // Translations by Google Translate.
79 English: en: Hello, world.
80 French: fr: Bonjour le monde.
81 Spanish: es: Hola Mundo.
85 // Copyright 2018 The Go Authors. All rights reserved.
86 // Use of this source code is governed by a BSD-style
87 // license that can be found in the LICENSE file.
94 "golang.org/x/text/language"
97 var helloTests = []struct {
102 []language.Tag{language.Make("en-US"), language.Make("fr")},
106 []language.Tag{language.Make("fr"), language.Make("en-US")},
111 func TestHello(t *testing.T) {
112 for _, tt := range helloTests {
113 text := Hello(tt.prefs...)
115 t.Errorf("Hello(%v) = %q, want %q", tt.prefs, text, tt.text)
120 // Copyright 2018 The Go Authors. All rights reserved.
121 // Use of this source code is governed by a BSD-style
122 // license that can be found in the LICENSE file.
124 // Package sampler shows simple texts.
125 package sampler // import "rsc.io/sampler"
131 "golang.org/x/text/language"
134 // DefaultUserPrefs returns the default user language preferences.
135 // It consults the $LC_ALL, $LC_MESSAGES, and $LANG environment
136 // variables, in that order.
137 func DefaultUserPrefs() []language.Tag {
138 var prefs []language.Tag
139 for _, k := range []string{"LC_ALL", "LC_MESSAGES", "LANG"} {
140 if env := os.Getenv(k); env != "" {
141 prefs = append(prefs, language.Make(env))
147 // Hello returns a localized greeting.
148 // If no prefs are given, Hello uses DefaultUserPrefs.
149 func Hello(prefs ...language.Tag) string {
151 prefs = DefaultUserPrefs()
153 return hello.find(prefs)
156 // Glass returns a localized silly phrase.
157 // If no prefs are given, Glass uses DefaultUserPrefs.
158 func Glass(prefs ...language.Tag) string {
160 prefs = DefaultUserPrefs()
162 return glass.find(prefs)
165 // A text is a localized text.
167 byTag map[string]string
168 matcher language.Matcher
171 // newText creates a new localized text, given a list of translations.
172 func newText(s string) *text {
174 byTag: make(map[string]string),
176 var tags []language.Tag
177 for _, line := range strings.Split(s, "\n") {
178 line = strings.TrimSpace(line)
182 f := strings.Split(line, ": ")
186 tag := language.Make(f[1])
187 tags = append(tags, tag)
188 t.byTag[tag.String()] = f[2]
190 t.matcher = language.NewMatcher(tags)
194 // find finds the text to use for the given language tag preferences.
195 func (t *text) find(prefs []language.Tag) string {
196 tag, _, _ := t.matcher.Match(prefs...)
197 s := t.byTag[tag.String()]
198 if strings.HasPrefix(s, "RTL ") {
199 s = "\u200F" + strings.TrimPrefix(s, "RTL ") + "\u200E"