...

Text file src/cmd/go/testdata/mod/rsc.io_sampler_v1.99.99.txt

Documentation: cmd/go/testdata/mod

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

View as plain text