1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 "golang.org/x/text/language"
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))
25 p("iw-Latn-fonipa-u-cu-usd")
27 // Default(en-Latn) -> en-Latn
28 // BCP47(en-Latn) -> en
29 // Macro(en-Latn) -> en-Latn
31 // Default(sh) -> sr-Latn
35 // Default(zh-cmn) -> cmn
36 // BCP47(zh-cmn) -> cmn
37 // Macro(zh-cmn) -> zh
39 // Default(bjd) -> drl
43 // Default(iw-Latn-fonipa-u-cu-usd) -> he-Latn-fonipa-u-cu-usd
44 // BCP47(iw-Latn-fonipa-u-cu-usd) -> he-Latn-fonipa-u-cu-usd
45 // Macro(iw-Latn-fonipa-u-cu-usd) -> iw-Latn-fonipa-u-cu-usd
46 // All(iw-Latn-fonipa-u-cu-usd) -> he-Latn-fonipa-u-cu-usd
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()) // Latin America
54 fmt.Println(language.Make("und-ZZ").Base())
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 // Was a script explicitly specified?
71 fmt.Println(c == language.Exact)
72 _, c = sr_Latn.Script()
73 fmt.Println(c == language.Exact)
81 func ExampleTag_Region() {
82 ru := language.Make("ru")
83 en := language.Make("en")
84 fmt.Println(ru.Region())
85 fmt.Println(en.Region())
91 func ExampleRegion_TLD() {
92 us := language.MustParseRegion("US")
93 gb := language.MustParseRegion("GB")
94 uk := language.MustParseRegion("UK")
95 bu := language.MustParseRegion("BU")
100 fmt.Println(bu.TLD())
102 fmt.Println(us.Canonicalize().TLD())
103 fmt.Println(gb.Canonicalize().TLD())
104 fmt.Println(uk.Canonicalize().TLD())
105 fmt.Println(bu.Canonicalize().TLD())
110 // ZZ language: region is not a valid ccTLD
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")
124 u, _ := language.ParseExtension("u-nu-arabic")
125 x, _ := language.ParseExtension("x-piglatin")
127 // Combine a base language and region.
128 fmt.Println(language.Compose(nl, us))
129 // Combine a base language and extension.
130 fmt.Println(language.Compose(nl, x))
131 // Replace the region.
132 fmt.Println(language.Compose(jp, us))
133 // Combine several tags.
134 fmt.Println(language.Compose(us, nl, u))
136 // Replace the base language of a tag.
137 fmt.Println(language.Compose(de, nl))
138 fmt.Println(language.Compose(de, nl, u))
139 // Remove the base language.
140 fmt.Println(language.Compose(de, language.Base{}))
141 // Remove all variants.
142 fmt.Println(language.Compose(de, []language.Variant{}))
143 // Remove all extensions.
144 fmt.Println(language.Compose(de, []language.Extension{}))
145 fmt.Println(language.Compose(fi, []language.Extension{}))
146 // Remove all variants and extensions.
147 fmt.Println(language.Compose(de.Raw()))
149 // An error is gobbled or returned if non-nil.
150 fmt.Println(language.Compose(language.ParseRegion("ZA")))
151 fmt.Println(language.Compose(language.ParseRegion("HH")))
153 // Compose uses the same Default canonicalization as Make.
154 fmt.Println(language.Compose(language.Raw.Parse("en-Latn-UK")))
156 // Call compose on a different CanonType for different results.
157 fmt.Println(language.All.Compose(language.Raw.Parse("en-Latn-UK")))
161 // nl-x-piglatin <nil>
163 // nl-US-u-nu-arabic <nil>
164 // nl-1901-u-co-phonebk <nil>
165 // nl-1901-u-nu-arabic <nil>
166 // und-1901-u-co-phonebk <nil>
167 // de-u-co-phonebk <nil>
172 // und language: subtag "HH" is well-formed but unknown
177 func ExampleParse_errors() {
178 for _, s := range []string{"Foo", "Bar", "Foobar"} {
179 _, err := language.Parse(s)
181 if inv, ok := err.(language.ValueError); ok {
182 fmt.Println(inv.Subtag())
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())
197 fmt.Printf("%s: ill-formed\n", s)
203 // aa-Uuuu: culprit "Uuuu"
208 func ExampleParent() {
209 p := func(tag string) {
210 fmt.Printf("parent(%v): %v\n", tag, language.Make(tag).Parent())
214 // Australian English inherits from World English.
217 // If the tag has a different maximized script from its parent, a tag with
218 // this maximized script is inserted. This allows different language tags
219 // which have the same base language and script in common to inherit from
220 // a common set of settings.
223 // If the maximized script of the parent is not identical, CLDR will skip
224 // inheriting from it, as it means there will not be many entries in common
225 // and inheriting from it is nonsensical.
228 // The parent of a tag with variants and extensions is the tag with all
229 // variants and extensions removed.
230 p("de-1994-u-co-phonebk")
232 // Remove default script.
237 // parent(en-AU): en-001
238 // parent(zh-HK): zh-Hant
239 // parent(zh-Hant): und
240 // parent(de-1994-u-co-phonebk): de
241 // parent(de-Latn-LU): de
244 // ExampleMatcher_bestMatch gives some examples of getting the best match of
245 // a set of tags to any of the tags of given set.
246 func ExampleMatcher() {
247 // This is the set of tags from which we want to pick the best match. These
248 // can be, for example, the supported languages for some package.
249 tags := []language.Tag{
251 language.BritishEnglish,
254 language.BrazilianPortuguese,
255 language.EuropeanPortuguese,
257 language.SimplifiedChinese,
258 language.Raw.Make("iw-IL"),
259 language.Raw.Make("iw"),
260 language.Raw.Make("he"),
262 m := language.NewMatcher(tags)
265 fmt.Println(m.Match(language.Make("fr")))
267 // Australian English is closer to British than American English.
268 fmt.Println(m.Match(language.Make("en-AU")))
270 // Default to the first tag passed to the Matcher if there is no match.
271 fmt.Println(m.Match(language.Make("ar")))
273 // Get the default tag.
274 fmt.Println(m.Match())
278 // Someone specifying sr-Latn is probably fine with getting Croatian.
279 fmt.Println(m.Match(language.Make("sr-Latn")))
281 // We match SimplifiedChinese, but with Low confidence.
282 fmt.Println(m.Match(language.TraditionalChinese))
284 // Serbian in Latin script is a closer match to Croatian than Traditional
285 // Chinese to Simplified Chinese.
286 fmt.Println(m.Match(language.TraditionalChinese, language.Make("sr-Latn")))
290 // In case a multiple variants of a language are available, the most spoken
291 // variant is typically returned.
292 fmt.Println(m.Match(language.Portuguese))
294 // Pick the first value passed to Match in case of a tie.
295 fmt.Println(m.Match(language.Dutch, language.Make("fr-BE"), language.Make("af-NA")))
296 fmt.Println(m.Match(language.Dutch, language.Make("af-NA"), language.Make("fr-BE")))
300 // If a Matcher is initialized with a language and it's deprecated version,
301 // it will distinguish between them.
302 fmt.Println(m.Match(language.Raw.Make("iw")))
304 // However, for non-exact matches, it will treat deprecated versions as
305 // equivalent and consider other factors first.
306 fmt.Println(m.Match(language.Raw.Make("he-IL")))
310 // User settings passed to the Unicode extension are ignored for matching
311 // and preserved in the returned tag.
312 fmt.Println(m.Match(language.Make("de-u-co-phonebk"), language.Make("fr-u-cu-frf")))
314 // Even if the matching language is different.
315 fmt.Println(m.Match(language.Make("de-u-co-phonebk"), language.Make("br-u-cu-frf")))
317 // If there is no matching language, the options of the first preferred tag are used.
318 fmt.Println(m.Match(language.Make("de-u-co-phonebk")))
337 // fr-u-cu-frf 2 Exact
338 // fr-u-cu-frf 2 High
339 // en-u-co-phonebk 0 No
341 // TODO: "he" should be "he-u-rg-IL High"
344 func ExampleMatchStrings() {
345 // languages supported by this service:
346 matcher := language.NewMatcher([]language.Tag{
347 language.English, language.Dutch, language.German,
350 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
351 lang, _ := r.Cookie("lang")
352 tag, _ := language.MatchStrings(matcher, lang.String(), r.Header.Get("Accept-Language"))
354 fmt.Println("User language:", tag)
358 func ExampleComprehends() {
359 // Various levels of comprehensibility.
360 fmt.Println(language.Comprehends(language.English, language.English))
361 fmt.Println(language.Comprehends(language.AmericanEnglish, language.BritishEnglish))
363 // An explicit Und results in no match.
364 fmt.Println(language.Comprehends(language.English, language.Und))
368 // There is usually no mutual comprehensibility between different scripts.
369 fmt.Println(language.Comprehends(language.Make("en-Dsrt"), language.English))
371 // One exception is for Traditional versus Simplified Chinese, albeit with
373 fmt.Println(language.Comprehends(language.TraditionalChinese, language.SimplifiedChinese))
377 // A Swiss German speaker will often understand High German.
378 fmt.Println(language.Comprehends(language.Make("gsw"), language.Make("de")))
380 // The converse is not generally the case.
381 fmt.Println(language.Comprehends(language.Make("de"), language.Make("gsw")))
395 func ExampleTag_values() {
396 us := language.MustParseRegion("US")
397 en := language.MustParseBase("en")
399 lang, _, region := language.AmericanEnglish.Raw()
400 fmt.Println(lang == en, region == us)
402 lang, _, region = language.BritishEnglish.Raw()
403 fmt.Println(lang == en, region == us)
405 // Tags can be compared for exact equivalence using '=='.
406 en_us, _ := language.Compose(en, us)
407 fmt.Println(en_us == language.AmericanEnglish)