Welcome to mirror list, hosted at ThFree Co, Russian Federation.

example_test.go « i18n « v2 - github.com/gohugoio/go-i18n.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 149057c74543c8d776688e89b2948c8d2596503f (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
135
136
137
138
139
140
141
package i18n_test

import (
	"fmt"

	"github.com/BurntSushi/toml"
	"github.com/nicksnyder/test/v2/i18n"
	"golang.org/x/text/language"
)

func ExampleLocalizer_MustLocalize() {
	bundle := i18n.NewBundle(language.English)
	localizer := i18n.NewLocalizer(bundle, "en")
	fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{
		DefaultMessage: &i18n.Message{
			ID:    "HelloWorld",
			Other: "Hello World!",
		},
	}))
	// Output:
	// Hello World!
}

func ExampleLocalizer_MustLocalize_noDefaultMessage() {
	bundle := i18n.NewBundle(language.English)
	bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
	bundle.MustParseMessageFileBytes([]byte(`
HelloWorld = "Hello World!"
`), "en.toml")
	bundle.MustParseMessageFileBytes([]byte(`
HelloWorld = "Hola Mundo!"
`), "es.toml")

	{
		localizer := i18n.NewLocalizer(bundle, "en-US")
		fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "HelloWorld"}))
	}
	{
		localizer := i18n.NewLocalizer(bundle, "es-ES")
		fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "HelloWorld"}))
	}
	// Output:
	// Hello World!
	// Hola Mundo!
}

func ExampleLocalizer_MustLocalize_plural() {
	bundle := i18n.NewBundle(language.English)
	localizer := i18n.NewLocalizer(bundle, "en")
	catsMessage := &i18n.Message{
		ID:    "Cats",
		One:   "I have {{.PluralCount}} cat.",
		Other: "I have {{.PluralCount}} cats.",
	}
	fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{
		DefaultMessage: catsMessage,
		PluralCount:    1,
	}))
	fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{
		DefaultMessage: catsMessage,
		PluralCount:    2,
	}))
	fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{
		DefaultMessage: catsMessage,
		PluralCount:    "2.5",
	}))
	// Output:
	// I have 1 cat.
	// I have 2 cats.
	// I have 2.5 cats.
}

func ExampleLocalizer_MustLocalize_template() {
	bundle := i18n.NewBundle(language.English)
	localizer := i18n.NewLocalizer(bundle, "en")
	helloPersonMessage := &i18n.Message{
		ID:    "HelloPerson",
		Other: "Hello {{.Name}}!",
	}
	fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{
		DefaultMessage: helloPersonMessage,
		TemplateData:   map[string]string{"Name": "Nick"},
	}))
	// Output:
	// Hello Nick!
}

func ExampleLocalizer_MustLocalize_plural_template() {
	bundle := i18n.NewBundle(language.English)
	localizer := i18n.NewLocalizer(bundle, "en")
	personCatsMessage := &i18n.Message{
		ID:    "PersonCats",
		One:   "{{.Name}} has {{.Count}} cat.",
		Other: "{{.Name}} has {{.Count}} cats.",
	}
	fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{
		DefaultMessage: personCatsMessage,
		PluralCount:    1,
		TemplateData: map[string]interface{}{
			"Name":  "Nick",
			"Count": 1,
		},
	}))
	fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{
		DefaultMessage: personCatsMessage,
		PluralCount:    2,
		TemplateData: map[string]interface{}{
			"Name":  "Nick",
			"Count": 2,
		},
	}))
	fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{
		DefaultMessage: personCatsMessage,
		PluralCount:    "2.5",
		TemplateData: map[string]interface{}{
			"Name":  "Nick",
			"Count": "2.5",
		},
	}))
	// Output:
	// Nick has 1 cat.
	// Nick has 2 cats.
	// Nick has 2.5 cats.
}

func ExampleLocalizer_MustLocalize_customTemplateDelims() {
	bundle := i18n.NewBundle(language.English)
	localizer := i18n.NewLocalizer(bundle, "en")
	helloPersonMessage := &i18n.Message{
		ID:         "HelloPerson",
		Other:      "Hello <<.Name>>!",
		LeftDelim:  "<<",
		RightDelim: ">>",
	}
	fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{
		DefaultMessage: helloPersonMessage,
		TemplateData:   map[string]string{"Name": "Nick"},
	}))
	// Output:
	// Hello Nick!
}