...

Source file src/golang.org/x/text/language/display/examples_test.go

Documentation: golang.org/x/text/language/display

     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/language"
    11  	"golang.org/x/text/language/display"
    12  	"golang.org/x/text/message"
    13  )
    14  
    15  func ExampleFormatter() {
    16  	message.SetString(language.Dutch, "In %v people speak %v.", "In %v spreekt men %v.")
    17  
    18  	fr := language.French
    19  	region, _ := fr.Region()
    20  	for _, tag := range []string{"en", "nl"} {
    21  		p := message.NewPrinter(language.Make(tag))
    22  
    23  		p.Printf("In %v people speak %v.", display.Region(region), display.Language(fr))
    24  		p.Println()
    25  	}
    26  
    27  	// Output:
    28  	// In France people speak French.
    29  	// In Frankrijk spreekt men Frans.
    30  }
    31  
    32  func 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 := display.English.Languages()
    38  
    39  	for _, s := range supported {
    40  		t := language.MustParse(s)
    41  		fmt.Printf("%-20s (%s)\n", en.Name(t), display.Self.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 ExampleTags() {
    62  	n := display.Tags(language.English)
    63  	fmt.Println(n.Name(language.Make("nl")))
    64  	fmt.Println(n.Name(language.Make("nl-BE")))
    65  	fmt.Println(n.Name(language.Make("nl-CW")))
    66  	fmt.Println(n.Name(language.Make("nl-Arab")))
    67  	fmt.Println(n.Name(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 ExampleDictionary() {
    81  	tags := []language.Tag{
    82  		language.English,
    83  		language.German,
    84  		language.Japanese,
    85  		language.Russian,
    86  	}
    87  	dicts := []*display.Dictionary{
    88  		display.English,
    89  		display.German,
    90  		display.Japanese,
    91  		display.Russian,
    92  	}
    93  
    94  	m := language.NewMatcher(tags)
    95  
    96  	getDict := func(t language.Tag) *display.Dictionary {
    97  		_, i, confidence := m.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 == language.No {
   101  			return nil
   102  		}
   103  		return dicts[i]
   104  	}
   105  
   106  	// The matcher will match Swiss German to German.
   107  	n := getDict(language.Make("gsw")).Languages()
   108  	fmt.Println(n.Name(language.German))
   109  	fmt.Println(n.Name(language.Make("de-CH")))
   110  	fmt.Println(n.Name(language.Make("gsw")))
   111  
   112  	// Output:
   113  	// Deutsch
   114  	// Schweizer Hochdeutsch
   115  	// Schweizerdeutsch
   116  }
   117  

View as plain text