aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/testdata/mod/rsc.io_sampler_v1.2.1.txt
blob: 7982cccea100c0a523aa9e668ed45aa2d6dfada6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
generated by ./addmod.bash rsc.io/sampler@v1.2.1

-- .mod --
module "rsc.io/sampler"

require "golang.org/x/text" v0.0.0-20170915032832-14c0d48ead0c
-- .info --
{"Version":"v1.2.1","Name":"cac3af4f8a0ab40054fa6f8d423108a63a1255bb","Short":"cac3af4f8a0a","Time":"2018-02-13T18:16:22Z"}
-- hello.go --
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Translations by Google Translate.

package sampler

var hello = newText(`

English: en: Hello, world.
French: fr: Bonjour le monde.
Spanish: es: Hola Mundo.

`)
-- hello_test.go --
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package sampler

import (
	"testing"

	"golang.org/x/text/language"
)

var helloTests = []struct {
	prefs []language.Tag
	text  string
}{
	{
		[]language.Tag{language.Make("en-US"), language.Make("fr")},
		"Hello, world.",
	},
	{
		[]language.Tag{language.Make("fr"), language.Make("en-US")},
		"Bonjour le monde.",
	},
}

func TestHello(t *testing.T) {
	for _, tt := range helloTests {
		text := Hello(tt.prefs...)
		if text != tt.text {
			t.Errorf("Hello(%v) = %q, want %q", tt.prefs, text, tt.text)
		}
	}
}
-- sampler.go --
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package sampler shows simple texts.
package sampler // import "rsc.io/sampler"

import (
	"os"
	"strings"

	"golang.org/x/text/language"
)

// DefaultUserPrefs returns the default user language preferences.
// It consults the $LC_ALL, $LC_MESSAGES, and $LANG environment
// variables, in that order.
func DefaultUserPrefs() []language.Tag {
	var prefs []language.Tag
	for _, k := range []string{"LC_ALL", "LC_MESSAGES", "LANG"} {
		if env := os.Getenv(k); env != "" {
			prefs = append(prefs, language.Make(env))
		}
	}
	return prefs
}

// Hello returns a localized greeting.
// If no prefs are given, Hello uses DefaultUserPrefs.
func Hello(prefs ...language.Tag) string {
	if len(prefs) == 0 {
		prefs = DefaultUserPrefs()
	}
	return hello.find(prefs)
}

// A text is a localized text.
type text struct {
	byTag   map[string]string
	matcher language.Matcher
}

// newText creates a new localized text, given a list of translations.
func newText(s string) *text {
	t := &text{
		byTag: make(map[string]string),
	}
	var tags []language.Tag
	for _, line := range strings.Split(s, "\n") {
		line = strings.TrimSpace(line)
		if line == "" {
			continue
		}
		f := strings.Split(line, ": ")
		if len(f) != 3 {
			continue
		}
		tag := language.Make(f[1])
		tags = append(tags, tag)
		t.byTag[tag.String()] = f[2]
	}
	t.matcher = language.NewMatcher(tags)
	return t
}

// find finds the text to use for the given language tag preferences.
func (t *text) find(prefs []language.Tag) string {
	tag, _, _ := t.matcher.Match(prefs...)
	s := t.byTag[tag.String()]
	if strings.HasPrefix(s, "RTL ") {
		s = "\u200F" + strings.TrimPrefix(s, "RTL ") + "\u200E"
	}
	return s
}