displaynames

package
v0.2.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package displaynames implements the ECMA-402 Intl.DisplayNames constructor.

locales, _ := locale.ParseList("en-US")
names, _ := displaynames.New(locales, displaynames.Options{Type: displaynames.Language})
name, ok, _ := names.Of("fr")
_, _ = name, ok

See README.md for usage examples and SPECS/44-displaynames.md for the contract.

Example

Example demonstrates Intl.DisplayNames.prototype.of from ECMA-402.

package main

import (
	"fmt"

	"github.com/agentable/go-intl/displaynames"
	"github.com/agentable/go-intl/locale"
)

func main() {
	names, err := displaynames.New(mustLocaleList("en-US"), displaynames.Options{
		Type: displaynames.Language,
	})
	if err != nil {
		panic(err)
	}

	name, ok, err := names.Of("fr")
	if err != nil {
		panic(err)
	}
	fmt.Println(name, ok)

}

func mustLocaleList(tags ...string) locale.List {
	locales, err := locale.ParseList(tags...)
	if err != nil {
		panic(err)
	}
	return locales
}
Output:
French true
Example (Options)

Example_options demonstrates Intl.DisplayNames constructor options from ECMA-402.

package main

import (
	"fmt"

	"github.com/agentable/go-intl/displaynames"
	"github.com/agentable/go-intl/locale"
)

func main() {
	names, err := displaynames.New(mustLocaleList("en-US"), displaynames.Options{
		Type:  displaynames.Region,
		Style: displaynames.ShortStyle,
	})
	if err != nil {
		panic(err)
	}

	name, ok, err := names.Of("US")
	if err != nil {
		panic(err)
	}
	fmt.Println(name, ok)

}

func mustLocaleList(tags ...string) locale.List {
	locales, err := locale.ParseList(tags...)
	if err != nil {
		panic(err)
	}
	return locales
}
Output:
US true

Index

Examples

Constants

View Source
const (
	LookupLocaleMatcher  LocaleMatcher = "lookup"
	BestFitLocaleMatcher LocaleMatcher = "best fit"

	Language      Type = "language"
	Region        Type = "region"
	Script        Type = "script"
	Currency      Type = "currency"
	Calendar      Type = "calendar"
	DateTimeField Type = "dateTimeField"

	LongStyle   Style = "long"
	ShortStyle  Style = "short"
	NarrowStyle Style = "narrow"

	CodeFallback Fallback = "code"
	NoneFallback Fallback = "none"

	DialectLanguageDisplay  LanguageDisplay = "dialect"
	StandardLanguageDisplay LanguageDisplay = "standard"
)

Variables

This section is empty.

Functions

func SupportedLocalesOf

func SupportedLocalesOf(locales locale.List, opts Options) (locale.List, error)

Types

type DisplayNames

type DisplayNames struct {
	// contains filtered or unexported fields
}

func New

func New(locales locale.List, opts Options) (*DisplayNames, error)

func (*DisplayNames) Of

func (d *DisplayNames) Of(code string) (string, bool, error)

Of returns the localized display name for a code. Invalid code shape returns gointl.ErrInvalidCode. When no name exists and Fallback is CodeFallback (the default), the canonicalized code is returned with ok=true; when Fallback is NoneFallback, the empty string is returned with ok=false. The (string, bool) pair is the Go bridge for the JS `string | undefined` return.

Example

ExampleDisplayNames_Of demonstrates Intl.DisplayNames.prototype.of from ECMA-402.

package main

import (
	"fmt"

	"github.com/agentable/go-intl/displaynames"
	"github.com/agentable/go-intl/locale"
)

func main() {
	names, err := displaynames.New(mustLocaleList("en-US"), displaynames.Options{
		Type:     displaynames.Language,
		Fallback: displaynames.NoneFallback,
	})
	if err != nil {
		panic(err)
	}

	name, ok, err := names.Of("zz")
	if err != nil {
		panic(err)
	}
	fmt.Println(name, ok)

}

func mustLocaleList(tags ...string) locale.List {
	locales, err := locale.ParseList(tags...)
	if err != nil {
		panic(err)
	}
	return locales
}
Output:
 false

func (*DisplayNames) ResolvedOptions

func (d *DisplayNames) ResolvedOptions() ResolvedOptions

type Fallback

type Fallback string

type LanguageDisplay

type LanguageDisplay string

type LocaleMatcher

type LocaleMatcher string

type Options

type Options struct {
	LocaleMatcher   LocaleMatcher
	Type            Type
	Style           Style
	Fallback        Fallback
	LanguageDisplay LanguageDisplay
}

type ResolvedOptions

type ResolvedOptions struct {
	Locale   locale.Locale `json:"locale"`
	Style    Style         `json:"style"`
	Type     Type          `json:"type"`
	Fallback Fallback      `json:"fallback"`
	// LanguageDisplay is nil when Type != Language. ECMA-402 §12.4.2
	// (resolvedOptions) only writes this property for Language-typed
	// DisplayNames instances; the pointer makes the omission unambiguous.
	LanguageDisplay *LanguageDisplay `json:"languageDisplay,omitempty"`
}

type Style

type Style string

type Type

type Type string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL