display - ActiveState ActiveGo 1.8
...

Package display

import "golang.org/x/text/language/display"
Overview
Index
Examples

Overview ▾

Package display provides display names for languages, scripts and regions in a requested language.

The data is based on CLDR's localeDisplayNames. It includes the names of the draft level "contributed" or "approved". The resulting tables are quite large. The display package is designed so that users can reduce the linked-in table sizes by cherry picking the languages one wishes to support. There is a Dictionary defined for a selected set of common languages for this purpose.

Constants

CLDRVersion is the CLDR version from which the tables in this package are derived.

const CLDRVersion = "30"

Version is deprecated. Use CLDRVersion.

const Version = "30"

Variables

var (
    // Supported lists the languages for which names are defined.
    Supported language.Coverage

    // The set of all possible values for which names are defined. Note that not
    // all Namer implementations will cover all the values of a given type.
    // A Namer will return the empty string for unsupported values.
    Values language.Coverage
)

type Dictionary

A Dictionary holds a collection of Namers for a single language. One can reduce the amount of data linked in to a binary by only referencing Dictionaries for the languages one needs to support instead of using the generic Namer factories.

type Dictionary struct {
    // contains filtered or unexported fields
}
var (
    Afrikaans            *Dictionary = &af        // af
    Amharic              *Dictionary = &am        // am
    Arabic               *Dictionary = &ar        // ar
    ModernStandardArabic *Dictionary = Arabic     // ar-001
    Azerbaijani          *Dictionary = &az        // az
    Bulgarian            *Dictionary = &bg        // bg
    Bengali              *Dictionary = &bn        // bn
    Catalan              *Dictionary = &ca        // ca
    Czech                *Dictionary = &cs        // cs
    Danish               *Dictionary = &da        // da
    German               *Dictionary = &de        // de
    Greek                *Dictionary = &el        // el
    English              *Dictionary = &en        // en
    AmericanEnglish      *Dictionary = English    // en-US
    BritishEnglish       *Dictionary = English    // en-GB
    Spanish              *Dictionary = &es        // es
    EuropeanSpanish      *Dictionary = Spanish    // es-ES
    LatinAmericanSpanish *Dictionary = Spanish    // es-419
    Estonian             *Dictionary = &et        // et
    Persian              *Dictionary = &fa        // fa
    Finnish              *Dictionary = &fi        // fi
    Filipino             *Dictionary = &fil       // fil
    French               *Dictionary = &fr        // fr
    Gujarati             *Dictionary = &gu        // gu
    Hebrew               *Dictionary = &he        // he
    Hindi                *Dictionary = &hi        // hi
    Croatian             *Dictionary = &hr        // hr
    Hungarian            *Dictionary = &hu        // hu
    Armenian             *Dictionary = &hy        // hy
    Indonesian           *Dictionary = &id        // id
    Icelandic            *Dictionary = &is        // is
    Italian              *Dictionary = &it        // it
    Japanese             *Dictionary = &ja        // ja
    Georgian             *Dictionary = &ka        // ka
    Kazakh               *Dictionary = &kk        // kk
    Khmer                *Dictionary = &km        // km
    Kannada              *Dictionary = &kn        // kn
    Korean               *Dictionary = &ko        // ko
    Kirghiz              *Dictionary = &ky        // ky
    Lao                  *Dictionary = &lo        // lo
    Lithuanian           *Dictionary = &lt        // lt
    Latvian              *Dictionary = &lv        // lv
    Macedonian           *Dictionary = &mk        // mk
    Malayalam            *Dictionary = &ml        // ml
    Mongolian            *Dictionary = &mn        // mn
    Marathi              *Dictionary = &mr        // mr
    Malay                *Dictionary = &ms        // ms
    Burmese              *Dictionary = &my        // my
    Nepali               *Dictionary = &ne        // ne
    Dutch                *Dictionary = &nl        // nl
    Norwegian            *Dictionary = &no        // no
    Punjabi              *Dictionary = &pa        // pa
    Polish               *Dictionary = &pl        // pl
    Portuguese           *Dictionary = &pt        // pt
    BrazilianPortuguese  *Dictionary = Portuguese // pt-BR
    EuropeanPortuguese   *Dictionary = &ptPT      // pt-PT
    Romanian             *Dictionary = &ro        // ro
    Russian              *Dictionary = &ru        // ru
    Sinhala              *Dictionary = &si        // si
    Slovak               *Dictionary = &sk        // sk
    Slovenian            *Dictionary = &sl        // sl
    Albanian             *Dictionary = &sq        // sq
    Serbian              *Dictionary = &sr        // sr
    SerbianLatin         *Dictionary = &srLatn    // sr
    Swedish              *Dictionary = &sv        // sv
    Swahili              *Dictionary = &sw        // sw
    Tamil                *Dictionary = &ta        // ta
    Telugu               *Dictionary = &te        // te
    Thai                 *Dictionary = &th        // th
    Turkish              *Dictionary = &tr        // tr
    Ukrainian            *Dictionary = &uk        // uk
    Urdu                 *Dictionary = &ur        // ur
    Uzbek                *Dictionary = &uz        // uz
    Vietnamese           *Dictionary = &vi        // vi
    Chinese              *Dictionary = &zh        // zh
    SimplifiedChinese    *Dictionary = Chinese    // zh-Hans
    TraditionalChinese   *Dictionary = &zhHant    // zh-Hant
    Zulu                 *Dictionary = &zu        // zu
)

Example

ExampleDictionary shows how to reduce the amount of data linked into your binary by only using the predefined Dictionary variables of the languages you wish to support.

Code:

tags := []language.Tag{
    language.English,
    language.German,
    language.Japanese,
    language.Russian,
}
dicts := []*display.Dictionary{
    display.English,
    display.German,
    display.Japanese,
    display.Russian,
}

m := language.NewMatcher(tags)

getDict := func(t language.Tag) *display.Dictionary {
    _, i, confidence := m.Match(t)
    // Skip this check if you want to support a fall-back language, which
    // will be the first one passed to NewMatcher.
    if confidence == language.No {
        return nil
    }
    return dicts[i]
}

// The matcher will match Swiss German to German.
n := getDict(language.Make("gsw")).Languages()
fmt.Println(n.Name(language.German))
fmt.Println(n.Name(language.Make("de-CH")))
fmt.Println(n.Name(language.Make("gsw")))

Output:

Deutsch
Schweizer Hochdeutsch
Schweizerdeutsch

func (*Dictionary) Languages

func (d *Dictionary) Languages() Namer

Languages returns a Namer for naming languages. It returns nil if there is no data for the given tag. The type passed to Name must be either language.Base or language.Tag. Note that the result may differ between passing a tag or its base language. For example, for English, passing "nl-BE" would return Flemish whereas passing "nl" returns "Dutch".

func (*Dictionary) Regions

func (d *Dictionary) Regions() Namer

Regions returns a Namer for naming regions. It returns nil if there is no data for the given tag. The type passed to Name must be either a language.Region or a language.Tag. It will not attempt to infer a region for tags with an unspecified region.

func (*Dictionary) Scripts

func (d *Dictionary) Scripts() Namer

Scripts returns a Namer for naming scripts. It returns nil if there is no data for the given tag. The type passed to Name must be either a language.Script or a language.Tag. It will not attempt to infer a script for tags with an unspecified script.

func (*Dictionary) Tags

func (d *Dictionary) Tags() Namer

Tags returns a Namer for giving a full description of a tag. The names of scripts and regions that are not already implied by the language name will in appended within parentheses. It returns nil if there is not data for the given tag. The type passed to Name must be a tag.

type Namer

A Namer is used to get the name for a given value, such as a Tag, Language, Script or Region.

type Namer interface {
    // Name returns a display string for the given value. A Namer returns an
    // empty string for values it does not support. A Namer may support naming
    // an unspecified value. For example, when getting the name for a region for
    // a tag that does not have a defined Region, it may return the name for an
    // unknown region. It is up to the user to filter calls to Name for values
    // for which one does not want to have a name string.
    Name(x interface{}) string
}

Example

Code:

supported := []string{
    "en-US", "en-GB", "ja", "zh", "zh-Hans", "zh-Hant", "pt", "pt-PT", "ko", "ar", "el", "ru", "uk", "pa",
}

en := display.English.Languages()

for _, s := range supported {
    t := language.MustParse(s)
    fmt.Printf("%-20s (%s)\n", en.Name(t), display.Self.Name(t))
}

Output:

American English     (American English)
British English      (British English)
Japanese             (日本語)
Chinese              (中文)
Simplified Chinese   (简体中文)
Traditional Chinese  (繁體中文)
Portuguese           (português)
European Portuguese  (português europeu)
Korean               (한국어)
Arabic               (العربية)
Greek                (Ελληνικά)
Russian              (русский)
Ukrainian            (українська)
Punjabi              (ਪੰਜਾਬੀ)

func Languages

func Languages(t language.Tag) Namer

Languages returns a Namer for naming languages. It returns nil if there is no data for the given tag. The type passed to Name must be either language.Base or language.Tag. Note that the result may differ between passing a tag or its base language. For example, for English, passing "nl-BE" would return Flemish whereas passing "nl" returns "Dutch".

func Regions

func Regions(t language.Tag) Namer

Regions returns a Namer for naming regions. It returns nil if there is no data for the given tag. The type passed to Name must be either a language.Region or a language.Tag. It will not attempt to infer a region for tags with an unspecified region.

func Scripts

func Scripts(t language.Tag) Namer

Scripts returns a Namer for naming scripts. It returns nil if there is no data for the given tag. The type passed to Name must be either a language.Script or a language.Tag. It will not attempt to infer a script for tags with an unspecified script.

func Tags

func Tags(t language.Tag) Namer

Tags returns a Namer for giving a full description of a tag. The names of scripts and regions that are not already implied by the language name will in appended within parentheses. It returns nil if there is not data for the given tag. The type passed to Name must be a tag.

Example

Code:

n := display.Tags(language.English)
fmt.Println(n.Name(language.Make("nl")))
fmt.Println(n.Name(language.Make("nl-BE")))
fmt.Println(n.Name(language.Make("nl-CW")))
fmt.Println(n.Name(language.Make("nl-Arab")))
fmt.Println(n.Name(language.Make("nl-Cyrl-RU")))

Output:

Dutch
Flemish
Dutch (Curaçao)
Dutch (Arabic)
Dutch (Cyrillic, Russia)

type SelfNamer

A SelfNamer implements a Namer that returns the name of language in this same language. It provides a very compact mechanism to provide a comprehensive list of languages to users in their native language.

type SelfNamer struct {
    // Supported defines the values supported by this Namer.
    Supported language.Coverage
}
var (
    // Self is a shared instance of a SelfNamer.
    Self *SelfNamer = &self
)

func (SelfNamer) Name

func (n SelfNamer) Name(x interface{}) string

Name returns the name of a given language tag in the language identified by this tag. It supports both the language.Base and language.Tag types.