github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/text/language/examples_test.go (about) 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. 4 5 package language_test 6 7 import ( 8 "fmt" 9 10 "golang.org/x/text/language" 11 ) 12 13 func ExampleCanonType() { 14 p := func(id string) { 15 fmt.Printf("Default(%s) -> %s\n", id, language.Make(id)) 16 fmt.Printf("BCP47(%s) -> %s\n", id, language.BCP47.Make(id)) 17 fmt.Printf("Macro(%s) -> %s\n", id, language.Macro.Make(id)) 18 fmt.Printf("All(%s) -> %s\n", id, language.All.Make(id)) 19 } 20 p("en-Latn") 21 p("sh") 22 p("zh-cmn") 23 p("bjd") 24 p("iw-Latn-fonipa-u-cu-usd") 25 // Output: 26 // Default(en-Latn) -> en-Latn 27 // BCP47(en-Latn) -> en 28 // Macro(en-Latn) -> en-Latn 29 // All(en-Latn) -> en 30 // Default(sh) -> sr-Latn 31 // BCP47(sh) -> sh 32 // Macro(sh) -> sh 33 // All(sh) -> sr-Latn 34 // Default(zh-cmn) -> cmn 35 // BCP47(zh-cmn) -> cmn 36 // Macro(zh-cmn) -> zh 37 // All(zh-cmn) -> zh 38 // Default(bjd) -> drl 39 // BCP47(bjd) -> drl 40 // Macro(bjd) -> bjd 41 // All(bjd) -> drl 42 // Default(iw-Latn-fonipa-u-cu-usd) -> he-Latn-fonipa-u-cu-usd 43 // BCP47(iw-Latn-fonipa-u-cu-usd) -> he-Latn-fonipa-u-cu-usd 44 // Macro(iw-Latn-fonipa-u-cu-usd) -> iw-Latn-fonipa-u-cu-usd 45 // All(iw-Latn-fonipa-u-cu-usd) -> he-Latn-fonipa-u-cu-usd 46 } 47 48 func ExampleTag_Base() { 49 fmt.Println(language.Make("und").Base()) 50 fmt.Println(language.Make("und-US").Base()) 51 fmt.Println(language.Make("und-NL").Base()) 52 fmt.Println(language.Make("und-419").Base()) // Latin America 53 fmt.Println(language.Make("und-ZZ").Base()) 54 // Output: 55 // en Low 56 // en High 57 // nl High 58 // es Low 59 // en Low 60 } 61 62 func ExampleTag_Script() { 63 en := language.Make("en") 64 sr := language.Make("sr") 65 sr_Latn := language.Make("sr_Latn") 66 fmt.Println(en.Script()) 67 fmt.Println(sr.Script()) 68 // Was a script explicitly specified? 69 _, c := sr.Script() 70 fmt.Println(c == language.Exact) 71 _, c = sr_Latn.Script() 72 fmt.Println(c == language.Exact) 73 // Output: 74 // Latn High 75 // Cyrl Low 76 // false 77 // true 78 } 79 80 func ExampleTag_Region() { 81 ru := language.Make("ru") 82 en := language.Make("en") 83 fmt.Println(ru.Region()) 84 fmt.Println(en.Region()) 85 // Output: 86 // RU Low 87 // US Low 88 } 89 90 func ExampleRegion_TLD() { 91 us := language.MustParseRegion("US") 92 gb := language.MustParseRegion("GB") 93 uk := language.MustParseRegion("UK") 94 bu := language.MustParseRegion("BU") 95 96 fmt.Println(us.TLD()) 97 fmt.Println(gb.TLD()) 98 fmt.Println(uk.TLD()) 99 fmt.Println(bu.TLD()) 100 101 fmt.Println(us.Canonicalize().TLD()) 102 fmt.Println(gb.Canonicalize().TLD()) 103 fmt.Println(uk.Canonicalize().TLD()) 104 fmt.Println(bu.Canonicalize().TLD()) 105 // Output: 106 // US <nil> 107 // UK <nil> 108 // UK <nil> 109 // ZZ language: region is not a valid ccTLD 110 // US <nil> 111 // UK <nil> 112 // UK <nil> 113 // MM <nil> 114 } 115 116 func ExampleCompose() { 117 nl, _ := language.ParseBase("nl") 118 us, _ := language.ParseRegion("US") 119 de := language.Make("de-1901-u-co-phonebk") 120 jp := language.Make("ja-JP") 121 fi := language.Make("fi-x-ing") 122 123 u, _ := language.ParseExtension("u-nu-arabic") 124 x, _ := language.ParseExtension("x-piglatin") 125 126 // Combine a base language and region. 127 fmt.Println(language.Compose(nl, us)) 128 // Combine a base language and extension. 129 fmt.Println(language.Compose(nl, x)) 130 // Replace the region. 131 fmt.Println(language.Compose(jp, us)) 132 // Combine several tags. 133 fmt.Println(language.Compose(us, nl, u)) 134 135 // Replace the base language of a tag. 136 fmt.Println(language.Compose(de, nl)) 137 fmt.Println(language.Compose(de, nl, u)) 138 // Remove the base language. 139 fmt.Println(language.Compose(de, language.Base{})) 140 // Remove all variants. 141 fmt.Println(language.Compose(de, []language.Variant{})) 142 // Remove all extensions. 143 fmt.Println(language.Compose(de, []language.Extension{})) 144 fmt.Println(language.Compose(fi, []language.Extension{})) 145 // Remove all variants and extensions. 146 fmt.Println(language.Compose(de.Raw())) 147 148 // An error is gobbled or returned if non-nil. 149 fmt.Println(language.Compose(language.ParseRegion("ZA"))) 150 fmt.Println(language.Compose(language.ParseRegion("HH"))) 151 152 // Compose uses the same Default canonicalization as Make. 153 fmt.Println(language.Compose(language.Raw.Parse("en-Latn-UK"))) 154 155 // Call compose on a different CanonType for different results. 156 fmt.Println(language.All.Compose(language.Raw.Parse("en-Latn-UK"))) 157 158 // Output: 159 // nl-US <nil> 160 // nl-x-piglatin <nil> 161 // ja-US <nil> 162 // nl-US-u-nu-arabic <nil> 163 // nl-1901-u-co-phonebk <nil> 164 // nl-1901-u-nu-arabic <nil> 165 // und-1901-u-co-phonebk <nil> 166 // de-u-co-phonebk <nil> 167 // de-1901 <nil> 168 // fi <nil> 169 // de <nil> 170 // und-ZA <nil> 171 // und language: subtag "HH" is well-formed but unknown 172 // en-Latn-GB <nil> 173 // en-GB <nil> 174 } 175 176 func ExampleParse_errors() { 177 for _, s := range []string{"Foo", "Bar", "Foobar"} { 178 _, err := language.Parse(s) 179 if err != nil { 180 if inv, ok := err.(language.ValueError); ok { 181 fmt.Println(inv.Subtag()) 182 } else { 183 fmt.Println(s) 184 } 185 } 186 } 187 for _, s := range []string{"en", "aa-Uuuu", "AC", "ac-u"} { 188 _, err := language.Parse(s) 189 switch e := err.(type) { 190 case language.ValueError: 191 fmt.Printf("%s: culprit %q\n", s, e.Subtag()) 192 case nil: 193 // No error. 194 default: 195 // A syntax error. 196 fmt.Printf("%s: ill-formed\n", s) 197 } 198 } 199 // Output: 200 // foo 201 // Foobar 202 // aa-Uuuu: culprit "Uuuu" 203 // AC: culprit "ac" 204 // ac-u: ill-formed 205 } 206 207 func ExampleParent() { 208 p := func(tag string) { 209 fmt.Printf("parent(%v): %v\n", tag, language.Make(tag).Parent()) 210 } 211 p("zh-CN") 212 213 // Australian English inherits from World English. 214 p("en-AU") 215 216 // If the tag has a different maximized script from its parent, a tag with 217 // this maximized script is inserted. This allows different language tags 218 // which have the same base language and script in common to inherit from 219 // a common set of settings. 220 p("zh-HK") 221 222 // If the maximized script of the parent is not identical, CLDR will skip 223 // inheriting from it, as it means there will not be many entries in common 224 // and inheriting from it is nonsensical. 225 p("zh-Hant") 226 227 // The parent of a tag with variants and extensions is the tag with all 228 // variants and extensions removed. 229 p("de-1994-u-co-phonebk") 230 231 // Remove default script. 232 p("de-Latn-LU") 233 234 // Output: 235 // parent(zh-CN): zh 236 // parent(en-AU): en-001 237 // parent(zh-HK): zh-Hant 238 // parent(zh-Hant): und 239 // parent(de-1994-u-co-phonebk): de 240 // parent(de-Latn-LU): de 241 } 242 243 // ExampleMatcher_bestMatch gives some examples of getting the best match of 244 // a set of tags to any of the tags of given set. 245 func ExampleMatcher() { 246 // This is the set of tags from which we want to pick the best match. These 247 // can be, for example, the supported languages for some package. 248 tags := []language.Tag{ 249 language.English, 250 language.BritishEnglish, 251 language.French, 252 language.Afrikaans, 253 language.BrazilianPortuguese, 254 language.EuropeanPortuguese, 255 language.Croatian, 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 // A simple match. 264 fmt.Println(m.Match(language.Make("fr"))) 265 266 // Australian English is closer to British than American English. 267 fmt.Println(m.Match(language.Make("en-AU"))) 268 269 // Default to the first tag passed to the Matcher if there is no match. 270 fmt.Println(m.Match(language.Make("ar"))) 271 272 // Get the default tag. 273 fmt.Println(m.Match()) 274 275 fmt.Println("----") 276 277 // Croatian speakers will likely understand Serbian written in Latin script. 278 fmt.Println(m.Match(language.Make("sr-Latn"))) 279 280 // We match SimplifiedChinese, but with Low confidence. 281 fmt.Println(m.Match(language.TraditionalChinese)) 282 283 // Serbian in Latin script is a closer match to Croatian than Traditional 284 // Chinese to Simplified Chinese. 285 fmt.Println(m.Match(language.TraditionalChinese, language.Make("sr-Latn"))) 286 287 fmt.Println("----") 288 289 // In case a multiple variants of a language are available, the most spoken 290 // variant is typically returned. 291 fmt.Println(m.Match(language.Portuguese)) 292 293 // Pick the first value passed to Match in case of a tie. 294 fmt.Println(m.Match(language.Dutch, language.Make("fr-BE"), language.Make("af-NA"))) 295 fmt.Println(m.Match(language.Dutch, language.Make("af-NA"), language.Make("fr-BE"))) 296 297 fmt.Println("----") 298 299 // If a Matcher is initialized with a language and it's deprecated version, 300 // it will distinguish between them. 301 fmt.Println(m.Match(language.Raw.Make("iw"))) 302 303 // However, for non-exact matches, it will treat deprecated versions as 304 // equivalent and consider other factors first. 305 fmt.Println(m.Match(language.Raw.Make("he-IL"))) 306 307 fmt.Println("----") 308 309 // User settings passed to the Unicode extension are ignored for matching 310 // and preserved in the returned tag. 311 fmt.Println(m.Match(language.Make("de-u-co-phonebk"), language.Make("fr-u-cu-frf"))) 312 313 // Even if the matching language is different. 314 fmt.Println(m.Match(language.Make("de-u-co-phonebk"), language.Make("br-u-cu-frf"))) 315 316 // If there is no matching language, the options of the first preferred tag are used. 317 fmt.Println(m.Match(language.Make("de-u-co-phonebk"))) 318 319 // Output: 320 // fr 2 Exact 321 // en-GB 1 High 322 // en 0 No 323 // en 0 No 324 // ---- 325 // hr 6 High 326 // zh-Hans 7 Low 327 // hr 6 High 328 // ---- 329 // pt-BR 4 High 330 // fr 2 High 331 // af 3 High 332 // ---- 333 // iw 9 Exact 334 // iw-IL 8 Exact 335 // ---- 336 // fr-u-cu-frf 2 Exact 337 // fr-u-cu-frf 2 High 338 // en-u-co-phonebk 0 No 339 } 340 341 func ExampleComprehends() { 342 // Various levels of comprehensibility. 343 fmt.Println(language.Comprehends(language.English, language.English)) 344 fmt.Println(language.Comprehends(language.AmericanEnglish, language.BritishEnglish)) 345 346 // An explicit Und results in no match. 347 fmt.Println(language.Comprehends(language.English, language.Und)) 348 349 fmt.Println("----") 350 351 // There is usually no mutual comprehensibility between different scripts. 352 fmt.Println(language.Comprehends(language.Make("en-Dsrt"), language.English)) 353 354 // One exception is for Traditional versus Simplified Chinese, albeit with 355 // a low confidence. 356 fmt.Println(language.Comprehends(language.TraditionalChinese, language.SimplifiedChinese)) 357 358 fmt.Println("----") 359 360 // A Swiss German speaker will often understand High German. 361 fmt.Println(language.Comprehends(language.Make("gsw"), language.Make("de"))) 362 363 // The converse is not generally the case. 364 fmt.Println(language.Comprehends(language.Make("de"), language.Make("gsw"))) 365 366 // Output: 367 // Exact 368 // High 369 // No 370 // ---- 371 // No 372 // Low 373 // ---- 374 // High 375 // No 376 } 377 378 func ExampleParseAcceptLanguage() { 379 // Tags are reordered based on their q rating. A missing q value means 1.0. 380 fmt.Println(language.ParseAcceptLanguage(" nn;q=0.3, en-gb;q=0.8, en,")) 381 382 m := language.NewMatcher([]language.Tag{language.Norwegian, language.Make("en-AU")}) 383 384 t, _, _ := language.ParseAcceptLanguage("da, en-gb;q=0.8, en;q=0.7") 385 fmt.Println(m.Match(t...)) 386 387 // Danish is pretty close to Norwegian. 388 t, _, _ = language.ParseAcceptLanguage(" da, nl") 389 fmt.Println(m.Match(t...)) 390 391 // Output: 392 // [en en-GB nn] [1 0.8 0.3] <nil> 393 // en-AU 1 High 394 // no 0 High 395 } 396 397 func ExampleTag_values() { 398 us := language.MustParseRegion("US") 399 en := language.MustParseBase("en") 400 401 lang, _, region := language.AmericanEnglish.Raw() 402 fmt.Println(lang == en, region == us) 403 404 lang, _, region = language.BritishEnglish.Raw() 405 fmt.Println(lang == en, region == us) 406 407 // Tags can be compared for exact equivalence using '=='. 408 en_us, _ := language.Compose(en, us) 409 fmt.Println(en_us == language.AmericanEnglish) 410 411 // Output: 412 // true true 413 // true false 414 // true 415 }