4 module "rsc.io/sampler"
6 require "golang.org/x/text" v0.0.0-20170915032832-14c0d48ead0c
8 {"Version":"v1.3.1","Name":"f545d0289d06e2add4556ea6a15fc4938014bf87","Short":"f545d0289d06","Time":"2018-02-14T16:34:12Z"}
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"
41 var glassTests = []struct {
46 []language.Tag{language.Make("en-US"), language.Make("fr")},
47 "I can eat glass and it doesn't hurt me.",
50 []language.Tag{language.Make("fr"), language.Make("en-US")},
51 "Je peux manger du verre, ça ne me fait pas mal.",
55 func TestGlass(t *testing.T) {
56 for _, tt := range glassTests {
57 text := Glass(tt.prefs...)
59 t.Errorf("Glass(%v) = %q, want %q", tt.prefs, text, tt.text)
64 module "rsc.io/sampler"
66 require "golang.org/x/text" v0.0.0-20170915032832-14c0d48ead0c
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.
72 // Translations by Google Translate.
78 English: en: Hello, world.
79 French: fr: Bonjour le monde.
80 Spanish: es: Hola Mundo.
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.
93 "golang.org/x/text/language"
96 var helloTests = []struct {
101 []language.Tag{language.Make("en-US"), language.Make("fr")},
105 []language.Tag{language.Make("fr"), language.Make("en-US")},
110 func TestHello(t *testing.T) {
111 for _, tt := range helloTests {
112 text := Hello(tt.prefs...)
114 t.Errorf("Hello(%v) = %q, want %q", tt.prefs, text, tt.text)
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.
123 // Package sampler shows simple texts in a variety of languages.
124 package sampler // import "rsc.io/sampler"
130 "golang.org/x/text/language"
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))
146 // Hello returns a localized greeting.
147 // If no prefs are given, Hello uses DefaultUserPrefs.
148 func Hello(prefs ...language.Tag) string {
150 prefs = DefaultUserPrefs()
152 return hello.find(prefs)
155 // Glass returns a localized silly phrase.
156 // If no prefs are given, Glass uses DefaultUserPrefs.
157 func Glass(prefs ...language.Tag) string {
159 prefs = DefaultUserPrefs()
161 return glass.find(prefs)
164 // A text is a localized text.
166 byTag map[string]string
167 matcher language.Matcher
170 // newText creates a new localized text, given a list of translations.
171 func newText(s string) *text {
173 byTag: make(map[string]string),
175 var tags []language.Tag
176 for _, line := range strings.Split(s, "\n") {
177 line = strings.TrimSpace(line)
181 f := strings.Split(line, ": ")
185 tag := language.Make(f[1])
186 tags = append(tags, tag)
187 t.byTag[tag.String()] = f[2]
189 t.matcher = language.NewMatcher(tags)
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"