lang

package
v0.0.0-...-13d2ada Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package lang parses, validates, and format language tags. Language is formatted according to the RFC 5646 (IETF language tag).

Each language tag is composed of one or more "subtags" separated by hyphens (-). Each subtag is composed of basic Latin letters or digits only. Subtags occur in the following order:

  1. A single primary language subtag based on a two-letter language code from ISO 639-1
  2. Up to three optional extended language subtags composed of three letters each, separated by hyphens; (There is currently no extended language subtag registered in the Language Subtag Registry without an equivalent and preferred primary language subtag
  3. ...

Examples:

  • en
  • en-GB
  • fr-CH
  • de-CH
  • de-DE

Index

Constants

This section is empty.

Variables

View Source
var (
	Mul             = Tag{T: language.MustParse("mul")}
	English         = Tag{T: language.MustParse("en")}
	French          = Tag{T: language.MustParse("fr")}
	German          = Tag{T: language.MustParse("de")}
	Italian         = Tag{T: language.MustParse("it")}
	BritishEnglish  = Tag{T: language.MustParse("en-GB")}
	AmericanEnglish = Tag{T: language.MustParse("en-US")}
	Spanish         = Tag{T: language.MustParse("es")}
	SpainSpanish    = Tag{T: language.MustParse("es-ES")}
	SwissFrench     = Tag{T: language.MustParse("fr-CH")}
	SwissGerman     = Tag{T: language.MustParse("de-CH")}
)
View Source
var (
	// ErrUnknownTag indicates that the language tag is well-formed, but unknown
	ErrUnknownTag = errors.New("unknown language tag")
	// ErrInvalidTag indicates that the language tag cannot be parsed
	ErrInvalidTag = errors.New("invalid language tag")
)

Functions

This section is empty.

Types

type Matcher

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

func NewMatcher

func NewMatcher(supported []Tag) *Matcher

func (*Matcher) Match

func (m *Matcher) Match(preferred ...*Tag) *Tag

Match finds the best supported language based on the preferred list and the languages for which there exists translations

type Tag

type Tag struct {
	T language.Tag
}

Tag is a language tag

func Must

func Must(t *Tag, err error) *Tag

Must panics when there is an error

func Parse

func Parse(s string) (*Tag, error)

Parse parses a 2- or 3-letter ISO 639 code. It returns an ErrInvalidLang if s is a well-formed but unknown language identifier or another error if another error occurred.

func ParseAcceptLanguage

func ParseAcceptLanguage(s string) ([]*Tag, error)

ParseAcceptLanguage parses the contents of a Accept-Language header as defined in http://www.ietf.org/rfc/rfc2616.txt

func (Tag) Base

func (t Tag) Base() string

Base returns the base language of the language tag. If the base language is unspecified, an attempt will be made to infer it from the context.

func (Tag) DeepCopy

func (t Tag) DeepCopy(dst interface{}) error

func (*Tag) GobDecode

func (t *Tag) GobDecode(data []byte) error

GobDecode implements the gob.GobDecoder interface for gob serialization.

func (Tag) GobEncode

func (t Tag) GobEncode() ([]byte, error)

GobEncode implements the gob.GobEncoder interface for gob serialization.

func (Tag) MarshalJSON

func (t Tag) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Tag) MarshalText

func (t Tag) MarshalText() ([]byte, error)

func (Tag) String

func (t Tag) String() string

func (*Tag) UnmarshalJSON

func (t *Tag) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Tag) UnmarshalText

func (t *Tag) UnmarshalText(p []byte) error

Jump to

Keyboard shortcuts

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