Text file
src/cmd/go/testdata/mod/rsc.io_sampler_v1.3.0.txt
1rsc.io/sampler@v1.3.0
2
3-- .mod --
4module "rsc.io/sampler"
5
6require "golang.org/x/text" v0.0.0-20170915032832-14c0d48ead0c
7-- .info --
8{"Version":"v1.3.0","Name":"0cc034b51e57ed7832d4c67d526f75a900996e5c","Short":"0cc034b51e57","Time":"2018-02-13T19:05:03Z"}
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
19package sampler
20
21var glass = newText(`
22
23English: en: I can eat glass and it doesn't hurt me.
24French: fr: Je peux manger du verre, ça ne me fait pas mal.
25Spanish: 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
33package sampler
34
35import (
36 "testing"
37
38 "golang.org/x/text/language"
39 _ "rsc.io/testonly"
40)
41
42var glassTests = []struct {
43 prefs []language.Tag
44 text string
45}{
46 {
47 []language.Tag{language.Make("en-US"), language.Make("fr")},
48 "I can eat glass and it doesn't hurt me.",
49 },
50 {
51 []language.Tag{language.Make("fr"), language.Make("en-US")},
52 "Je peux manger du verre, ça ne me fait pas mal.",
53 },
54}
55
56func TestGlass(t *testing.T) {
57 for _, tt := range glassTests {
58 text := Glass(tt.prefs...)
59 if text != tt.text {
60 t.Errorf("Glass(%v) = %q, want %q", tt.prefs, text, tt.text)
61 }
62 }
63}
64-- go.mod --
65module "rsc.io/sampler"
66
67require "golang.org/x/text" v0.0.0-20170915032832-14c0d48ead0c
68-- hello.go --
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.
72
73// Translations by Google Translate.
74
75package sampler
76
77var hello = newText(`
78
79English: en: Hello, world.
80French: fr: Bonjour le monde.
81Spanish: es: Hola Mundo.
82
83`)
84-- hello_test.go --
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.
88
89package sampler
90
91import (
92 "testing"
93
94 "golang.org/x/text/language"
95)
96
97var helloTests = []struct {
98 prefs []language.Tag
99 text string
100}{
101 {
102 []language.Tag{language.Make("en-US"), language.Make("fr")},
103 "Hello, world.",
104 },
105 {
106 []language.Tag{language.Make("fr"), language.Make("en-US")},
107 "Bonjour le monde.",
108 },
109}
110
111func TestHello(t *testing.T) {
112 for _, tt := range helloTests {
113 text := Hello(tt.prefs...)
114 if text != tt.text {
115 t.Errorf("Hello(%v) = %q, want %q", tt.prefs, text, tt.text)
116 }
117 }
118}
119-- sampler.go --
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.
123
124// Package sampler shows simple texts.
125package sampler // import "rsc.io/sampler"
126
127import (
128 "os"
129 "strings"
130
131 "golang.org/x/text/language"
132)
133
134// DefaultUserPrefs returns the default user language preferences.
135// It consults the $LC_ALL, $LC_MESSAGES, and $LANG environment
136// variables, in that order.
137func 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))
142 }
143 }
144 return prefs
145}
146
147// Hello returns a localized greeting.
148// If no prefs are given, Hello uses DefaultUserPrefs.
149func Hello(prefs ...language.Tag) string {
150 if len(prefs) == 0 {
151 prefs = DefaultUserPrefs()
152 }
153 return hello.find(prefs)
154}
155
156// Glass returns a localized silly phrase.
157// If no prefs are given, Glass uses DefaultUserPrefs.
158func Glass(prefs ...language.Tag) string {
159 if len(prefs) == 0 {
160 prefs = DefaultUserPrefs()
161 }
162 return glass.find(prefs)
163}
164
165// A text is a localized text.
166type text struct {
167 byTag map[string]string
168 matcher language.Matcher
169}
170
171// newText creates a new localized text, given a list of translations.
172func newText(s string) *text {
173 t := &text{
174 byTag: make(map[string]string),
175 }
176 var tags []language.Tag
177 for _, line := range strings.Split(s, "\n") {
178 line = strings.TrimSpace(line)
179 if line == "" {
180 continue
181 }
182 f := strings.Split(line, ": ")
183 if len(f) != 3 {
184 continue
185 }
186 tag := language.Make(f[1])
187 tags = append(tags, tag)
188 t.byTag[tag.String()] = f[2]
189 }
190 t.matcher = language.NewMatcher(tags)
191 return t
192}
193
194// find finds the text to use for the given language tag preferences.
195func (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"
200 }
201 return s
202}
View as plain text