...
1
2
3
4
5 package message_test
6
7 import (
8 "fmt"
9 "net/http"
10
11 "golang.org/x/text/language"
12 "golang.org/x/text/message"
13 )
14
15 func Example_http() {
16
17 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
18 lang, _ := r.Cookie("lang")
19 accept := r.Header.Get("Accept-Language")
20 fallback := "en"
21 tag := message.MatchLanguage(lang.String(), accept, fallback)
22
23 p := message.NewPrinter(tag)
24
25 p.Fprintln(w, "User language is", tag)
26 })
27 }
28
29 func ExamplePrinter_numbers() {
30 for _, lang := range []string{"en", "de", "de-CH", "fr", "bn"} {
31 p := message.NewPrinter(language.Make(lang))
32 p.Printf("%-6s %g\n", lang, 123456.78)
33 }
34
35
36
37
38
39
40
41 }
42
43 func ExamplePrinter_mVerb() {
44 message.SetString(language.Dutch, "You have chosen to play %m.", "U heeft ervoor gekozen om %m te spelen.")
45 message.SetString(language.Dutch, "basketball", "basketbal")
46 message.SetString(language.Dutch, "hockey", "ijshockey")
47 message.SetString(language.Dutch, "soccer", "voetbal")
48 message.SetString(language.BritishEnglish, "soccer", "football")
49
50 for _, sport := range []string{"soccer", "basketball", "hockey"} {
51 for _, lang := range []string{"en", "en-GB", "nl"} {
52 p := message.NewPrinter(language.Make(lang))
53 fmt.Printf("%-6s %s\n", lang, p.Sprintf("You have chosen to play %m.", sport))
54 }
55 fmt.Println()
56 }
57
58
59
60
61
62
63
64
65
66
67
68
69
70 }
71
View as plain text