| 1 | // Copyright 2014 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 display_testscip-go gomod golang.org/x/text . `golang.org/x/text/language/display_test`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 9 | |
| 10 | "golang.org/x/text/languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/" |
| 11 | "golang.org/x/text/language/displayscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/" |
| 12 | "golang.org/x/text/messagescip-go gomod golang.org/x/text . `golang.org/x/text/message`/" |
| 13 | ) |
| 14 | |
| 15 | func ExampleFormatterscip-go gomod golang.org/x/text . `golang.org/x/text/language/display_test`/ExampleFormatter().() { |
| 16 | messagescip-go gomod golang.org/x/text . `golang.org/x/text/message`/.SetStringscip-go gomod golang.org/x/text . `golang.org/x/text/message`/SetString().(languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Dutchscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Dutch., "In %v people speak %v.", "In %v spreekt men %v.") |
| 17 | |
| 18 | fr := languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Frenchscip-go gomod golang.org/x/text . `golang.org/x/text/language`/French. |
| 19 | region, _ := fr.Regionscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Tag#Region().() |
| 20 | for _, tag := range []string{"en", "nl"} { |
| 21 | p := messagescip-go gomod golang.org/x/text . `golang.org/x/text/message`/.NewPrinterscip-go gomod golang.org/x/text . `golang.org/x/text/message`/NewPrinter().(languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Makescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Make().(tag)) |
| 22 | |
| 23 | p.Printfscip-go gomod golang.org/x/text . `golang.org/x/text/message`/Printer#Printf().("In %v people speak %v.", displayscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/.Regionscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Region().(region), displayscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/.Languagescip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Language().(fr)) |
| 24 | p.Printlnscip-go gomod golang.org/x/text . `golang.org/x/text/message`/Printer#Println().() |
| 25 | } |
| 26 | |
| 27 | // Output: |
| 28 | // In France people speak French. |
| 29 | // In Frankrijk spreekt men Frans. |
| 30 | } |
| 31 | |
| 32 | func ExampleNamerscip-go gomod golang.org/x/text . `golang.org/x/text/language/display_test`/ExampleNamer().() { |
| 33 | supported := []string{ |
| 34 | "en-US", "en-GB", "ja", "zh", "zh-Hans", "zh-Hant", "pt", "pt-PT", "ko", "ar", "el", "ru", "uk", "pa", |
| 35 | } |
| 36 | |
| 37 | en := displayscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/.Englishscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/English..Languagesscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Dictionary#Languages().() |
| 38 | |
| 39 | for _, s := range supported { |
| 40 | t := languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.MustParsescip-go gomod golang.org/x/text . `golang.org/x/text/language`/MustParse().(s) |
| 41 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Printfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Printf().("%-20s (%s)\n", en.Namescip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Namer#Name.(t), displayscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/.Selfscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Self..Namescip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/SelfNamer#Name().(t)) |
| 42 | } |
| 43 | |
| 44 | // Output: |
| 45 | // American English (American English) |
| 46 | // British English (British English) |
| 47 | // Japanese (日本語) |
| 48 | // Chinese (中文) |
| 49 | // Simplified Chinese (简体中文) |
| 50 | // Traditional Chinese (繁體中文) |
| 51 | // Portuguese (português) |
| 52 | // European Portuguese (português europeu) |
| 53 | // Korean (한국어) |
| 54 | // Arabic (العربية) |
| 55 | // Greek (Ελληνικά) |
| 56 | // Russian (русский) |
| 57 | // Ukrainian (українська) |
| 58 | // Punjabi (ਪੰਜਾਬੀ) |
| 59 | } |
| 60 | |
| 61 | func ExampleTagsscip-go gomod golang.org/x/text . `golang.org/x/text/language/display_test`/ExampleTags().() { |
| 62 | n := displayscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/.Tagsscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Tags().(languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Englishscip-go gomod golang.org/x/text . `golang.org/x/text/language`/English.) |
| 63 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.25.0 fmt/Println().(n.Namescip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Namer#Name.(languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Makescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Make().("nl"))) |
| 64 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.25.0 fmt/Println().(n.Namescip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Namer#Name.(languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Makescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Make().("nl-BE"))) |
| 65 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.25.0 fmt/Println().(n.Namescip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Namer#Name.(languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Makescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Make().("nl-CW"))) |
| 66 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.25.0 fmt/Println().(n.Namescip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Namer#Name.(languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Makescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Make().("nl-Arab"))) |
| 67 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.25.0 fmt/Println().(n.Namescip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Namer#Name.(languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Makescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Make().("nl-Cyrl-RU"))) |
| 68 | |
| 69 | // Output: |
| 70 | // Dutch |
| 71 | // Flemish |
| 72 | // Dutch (Curaçao) |
| 73 | // Dutch (Arabic) |
| 74 | // Dutch (Cyrillic, Russia) |
| 75 | } |
| 76 | |
| 77 | // ExampleDictionary shows how to reduce the amount of data linked into your |
| 78 | // binary by only using the predefined Dictionary variables of the languages you |
| 79 | // wish to support. |
| 80 | func ExampleDictionaryscip-go gomod golang.org/x/text . `golang.org/x/text/language/display_test`/ExampleDictionary().() { |
| 81 | tags := []languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Tagscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Tag#{ |
| 82 | languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Englishscip-go gomod golang.org/x/text . `golang.org/x/text/language`/English., |
| 83 | languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Germanscip-go gomod golang.org/x/text . `golang.org/x/text/language`/German., |
| 84 | languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Japanesescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Japanese., |
| 85 | languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Russianscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Russian., |
| 86 | } |
| 87 | dicts := []*displayscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/.Dictionaryscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Dictionary#{ |
| 88 | displayscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/.Englishscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/English., |
| 89 | displayscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/.Germanscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/German., |
| 90 | displayscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/.Japanesescip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Japanese., |
| 91 | displayscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/.Russianscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Russian., |
| 92 | } |
| 93 | |
| 94 | m := languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.NewMatcherscip-go gomod golang.org/x/text . `golang.org/x/text/language`/NewMatcher().(tags) |
| 95 | |
| 96 | getDict := func(t languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Tagscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Tag#) *displayscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/.Dictionaryscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Dictionary# { |
| 97 | _, i, confidence := m.Matchscip-go gomod golang.org/x/text . `golang.org/x/text/language`/Matcher#Match.(t) |
| 98 | // Skip this check if you want to support a fall-back language, which |
| 99 | // will be the first one passed to NewMatcher. |
| 100 | if confidence == languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Noscip-go gomod golang.org/x/text . `golang.org/x/text/language`/No. { |
| 101 | return nil |
| 102 | } |
| 103 | return dicts[i] |
| 104 | } |
| 105 | |
| 106 | // The matcher will match Swiss German to German. |
| 107 | n := getDict(languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Makescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Make().("gsw")).Languagesscip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Dictionary#Languages().() |
| 108 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.25.0 fmt/Println().(n.Namescip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Namer#Name.(languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Germanscip-go gomod golang.org/x/text . `golang.org/x/text/language`/German.)) |
| 109 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.25.0 fmt/Println().(n.Namescip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Namer#Name.(languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Makescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Make().("de-CH"))) |
| 110 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.25.0 fmt/Println().(n.Namescip-go gomod golang.org/x/text . `golang.org/x/text/language/display`/Namer#Name.(languagescip-go gomod golang.org/x/text . `golang.org/x/text/language`/.Makescip-go gomod golang.org/x/text . `golang.org/x/text/language`/Make().("gsw"))) |
| 111 | |
| 112 | // Output: |
| 113 | // Deutsch |
| 114 | // Schweizer Hochdeutsch |
| 115 | // Schweizerdeutsch |
| 116 | } |