github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/text/display/examples_test.go (about)

     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_test
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"golang.org/x/text/display"
    11  	"golang.org/x/text/language"
    12  )
    13  
    14  func ExampleNamer() {
    15  	supported := []string{
    16  		"en-US", "en-GB", "ja", "zh", "zh-Hans", "zh-Hant", "pt", "pt-PT", "ko", "ar", "el", "ru", "uk", "pa",
    17  	}
    18  
    19  	en := display.English.Languages()
    20  
    21  	for _, s := range supported {
    22  		t := language.MustParse(s)
    23  		fmt.Printf("%-20s (%s)\n", en.Name(t), display.Self.Name(t))
    24  	}
    25  
    26  	// Output:
    27  	// American English     (American English)
    28  	// British English      (British English)
    29  	// Japanese             (日本語)
    30  	// Chinese              (中文)
    31  	// Simplified Chinese   (简体中文)
    32  	// Traditional Chinese  (繁體中文)
    33  	// Portuguese           (português)
    34  	// European Portuguese  (português europeu)
    35  	// Korean               (한국어)
    36  	// Arabic               (العربية)
    37  	// Greek                (Ελληνικά)
    38  	// Russian              (русский)
    39  	// Ukrainian            (українська)
    40  	// Punjabi              (ਪੰਜਾਬੀ)
    41  }
    42  
    43  func ExampleTags() {
    44  	n := display.Tags(language.English)
    45  	fmt.Println(n.Name(language.Make("nl")))
    46  	fmt.Println(n.Name(language.Make("nl-BE")))
    47  	fmt.Println(n.Name(language.Make("nl-CW")))
    48  	fmt.Println(n.Name(language.Make("nl-Arab")))
    49  	fmt.Println(n.Name(language.Make("nl-Cyrl-RU")))
    50  
    51  	// Output:
    52  	// Dutch
    53  	// Flemish
    54  	// Dutch (Curaçao)
    55  	// Dutch (Arabic)
    56  	// Dutch (Cyrillic, Russia)
    57  }
    58  
    59  // ExampleDictionary shows how to reduce the amount of data linked into your
    60  // binary by only using the predefined Dictionary variables of the languages you
    61  // wish to support.
    62  func ExampleDictionary() {
    63  	tags := []language.Tag{
    64  		language.English,
    65  		language.German,
    66  		language.Japanese,
    67  		language.Russian,
    68  	}
    69  	dicts := []*display.Dictionary{
    70  		display.English,
    71  		display.German,
    72  		display.Japanese,
    73  		display.Russian,
    74  	}
    75  
    76  	m := language.NewMatcher(tags)
    77  
    78  	getDict := func(t language.Tag) *display.Dictionary {
    79  		_, i, confidence := m.Match(t)
    80  		// Skip this check if you want to support a fall-back language, which
    81  		// will be the first one passed to NewMatcher.
    82  		if confidence == language.No {
    83  			return nil
    84  		}
    85  		return dicts[i]
    86  	}
    87  
    88  	// The matcher will match Swiss German to German.
    89  	n := getDict(language.Make("gsw")).Languages()
    90  	fmt.Println(n.Name(language.German))
    91  	fmt.Println(n.Name(language.Make("de-CH")))
    92  	fmt.Println(n.Name(language.Make("gsw")))
    93  
    94  	// Output:
    95  	// Deutsch
    96  	// Schweizer Hochdeutsch
    97  	// Schweizerdeutsch
    98  }