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