1
2
3
4
5 package language_test
6
7 import (
8 "fmt"
9 "net/http"
10
11 "golang.org/x/text/language"
12 )
13
14 func ExampleCanonType() {
15 p := func(id string) {
16 fmt.Printf("Default(%s) -> %s\n", id, language.Make(id))
17 fmt.Printf("BCP47(%s) -> %s\n", id, language.BCP47.Make(id))
18 fmt.Printf("Macro(%s) -> %s\n", id, language.Macro.Make(id))
19 fmt.Printf("All(%s) -> %s\n", id, language.All.Make(id))
20 }
21 p("en-Latn")
22 p("sh")
23 p("zh-cmn")
24 p("bjd")
25 p("iw-Latn-fonipa-u-cu-usd")
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 }
48
49 func ExampleTag_Base() {
50 fmt.Println(language.Make("und").Base())
51 fmt.Println(language.Make("und-US").Base())
52 fmt.Println(language.Make("und-NL").Base())
53 fmt.Println(language.Make("und-419").Base())
54 fmt.Println(language.Make("und-ZZ").Base())
55
56
57
58
59
60
61 }
62
63 func ExampleTag_Script() {
64 en := language.Make("en")
65 sr := language.Make("sr")
66 sr_Latn := language.Make("sr_Latn")
67 fmt.Println(en.Script())
68 fmt.Println(sr.Script())
69
70 _, c := sr.Script()
71 fmt.Println(c == language.Exact)
72 _, c = sr_Latn.Script()
73 fmt.Println(c == language.Exact)
74
75
76
77
78
79 }
80
81 func ExampleTag_Region() {
82 ru := language.Make("ru")
83 en := language.Make("en")
84 fmt.Println(ru.Region())
85 fmt.Println(en.Region())
86
87
88
89 }
90
91 func ExampleRegion_TLD() {
92 us := language.MustParseRegion("US")
93 gb := language.MustParseRegion("GB")
94 uk := language.MustParseRegion("UK")
95 bu := language.MustParseRegion("BU")
96
97 fmt.Println(us.TLD())
98 fmt.Println(gb.TLD())
99 fmt.Println(uk.TLD())
100 fmt.Println(bu.TLD())
101
102 fmt.Println(us.Canonicalize().TLD())
103 fmt.Println(gb.Canonicalize().TLD())
104 fmt.Println(uk.Canonicalize().TLD())
105 fmt.Println(bu.Canonicalize().TLD())
106
107
108
109
110
111
112
113
114
115 }
116
117 func ExampleCompose() {
118 nl, _ := language.ParseBase("nl")
119 us, _ := language.ParseRegion("US")
120 de := language.Make("de-1901-u-co-phonebk")
121 jp := language.Make("ja-JP")
122 fi := language.Make("fi-x-ing")
123
124 u, _ := language.ParseExtension("u-nu-arabic")
125 x, _ := language.ParseExtension("x-piglatin")
126
127
128 fmt.Println(language.Compose(nl, us))
129
130 fmt.Println(language.Compose(nl, x))
131
132 fmt.Println(language.Compose(jp, us))
133
134 fmt.Println(language.Compose(us, nl, u))
135
136
137 fmt.Println(language.Compose(de, nl))
138 fmt.Println(language.Compose(de, nl, u))
139
140 fmt.Println(language.Compose(de, language.Base{}))
141
142 fmt.Println(language.Compose(de, []language.Variant{}))
143
144 fmt.Println(language.Compose(de, []language.Extension{}))
145 fmt.Println(language.Compose(fi, []language.Extension{}))
146
147 fmt.Println(language.Compose(de.Raw()))
148
149
150 fmt.Println(language.Compose(language.ParseRegion("ZA")))
151 fmt.Println(language.Compose(language.ParseRegion("HH")))
152
153
154 fmt.Println(language.Compose(language.Raw.Parse("en-Latn-UK")))
155
156
157 fmt.Println(language.All.Compose(language.Raw.Parse("en-Latn-UK")))
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 }
176
177 func ExampleParse_errors() {
178 for _, s := range []string{"Foo", "Bar", "Foobar"} {
179 _, err := language.Parse(s)
180 if err != nil {
181 if inv, ok := err.(language.ValueError); ok {
182 fmt.Println(inv.Subtag())
183 } else {
184 fmt.Println(s)
185 }
186 }
187 }
188 for _, s := range []string{"en", "aa-Uuuu", "AC", "ac-u"} {
189 _, err := language.Parse(s)
190 switch e := err.(type) {
191 case language.ValueError:
192 fmt.Printf("%s: culprit %q\n", s, e.Subtag())
193 case nil:
194
195 default:
196
197 fmt.Printf("%s: ill-formed\n", s)
198 }
199 }
200
201
202
203
204
205
206 }
207
208 func ExampleTag_Parent() {
209 p := func(tag string) {
210 fmt.Printf("parent(%v): %v\n", tag, language.Make(tag).Parent())
211 }
212 p("zh-CN")
213
214
215 p("en-AU")
216
217
218
219
220
221 p("zh-HK")
222
223
224
225
226 p("zh-Hant")
227
228
229
230 p("de-1994-u-co-phonebk")
231
232
233 p("de-Latn-LU")
234
235
236
237
238
239
240
241
242 }
243
244
245
246 func ExampleMatcher() {
247
248
249 tags := []language.Tag{
250 language.English,
251 language.BritishEnglish,
252 language.French,
253 language.Afrikaans,
254 language.BrazilianPortuguese,
255 language.EuropeanPortuguese,
256 language.SimplifiedChinese,
257 language.Raw.Make("iw-IL"),
258 language.Raw.Make("iw"),
259 language.Raw.Make("he"),
260 }
261 m := language.NewMatcher(tags)
262
263
264 fmt.Println(m.Match(language.Make("fr")))
265
266
267
268
269
270
271
272 fmt.Println(m.Match(language.Make("en-AU")))
273
274
275 fmt.Println(m.Match(language.Make("ar")))
276
277
278 fmt.Println(m.Match())
279
280 fmt.Println("----")
281
282
283 fmt.Println(m.Match(language.TraditionalChinese))
284
285
286
287 fmt.Println(m.Match(language.TraditionalChinese, language.Make("en-AU")))
288
289 fmt.Println("----")
290
291
292
293 fmt.Println(m.Match(language.Portuguese))
294
295
296 fmt.Println(m.Match(language.Dutch, language.Make("fr-BE"), language.Make("af-NA")))
297 fmt.Println(m.Match(language.Dutch, language.Make("af-NA"), language.Make("fr-BE")))
298
299 fmt.Println("----")
300
301
302
303 fmt.Println(m.Match(language.Raw.Make("iw")))
304
305
306
307 fmt.Println(m.Match(language.Raw.Make("he-IL")))
308
309 fmt.Println("----")
310
311
312
313 fmt.Println(m.Match(language.Make("de-u-co-phonebk"), language.Make("fr-u-cu-frf")))
314
315
316 fmt.Println(m.Match(language.Make("de-u-co-phonebk"), language.Make("br-u-cu-frf")))
317
318
319 fmt.Println(m.Match(language.Make("de-u-co-phonebk")))
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340 }
341
342 func ExampleMatchStrings() {
343
344 matcher := language.NewMatcher([]language.Tag{
345 language.English, language.Dutch, language.German,
346 })
347
348 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
349 lang, _ := r.Cookie("lang")
350 tag, _ := language.MatchStrings(matcher, lang.String(), r.Header.Get("Accept-Language"))
351
352 fmt.Println("User language:", tag)
353 })
354 }
355
356 func ExampleComprehends() {
357
358 fmt.Println(language.Comprehends(language.English, language.English))
359 fmt.Println(language.Comprehends(language.AmericanEnglish, language.BritishEnglish))
360
361
362 fmt.Println(language.Comprehends(language.English, language.Und))
363
364 fmt.Println("----")
365
366
367 fmt.Println(language.Comprehends(language.Make("en-Dsrt"), language.English))
368
369
370
371 fmt.Println(language.Comprehends(language.TraditionalChinese, language.SimplifiedChinese))
372
373 fmt.Println("----")
374
375
376 fmt.Println(language.Comprehends(language.Make("gsw"), language.Make("de")))
377
378
379 fmt.Println(language.Comprehends(language.Make("de"), language.Make("gsw")))
380
381
382
383
384
385
386
387
388
389
390
391 }
392
393 func ExampleTag_values() {
394 us := language.MustParseRegion("US")
395 en := language.MustParseBase("en")
396
397 lang, _, region := language.AmericanEnglish.Raw()
398 fmt.Println(lang == en, region == us)
399
400 lang, _, region = language.BritishEnglish.Raw()
401 fmt.Println(lang == en, region == us)
402
403
404 en_us, _ := language.Compose(en, us)
405 fmt.Println(en_us == language.AmericanEnglish)
406
407
408
409
410
411 }
412
View as plain text