Documentation
¶
Index ¶
- Constants
- Variables
- func LangCodes2IDs[T NumberType](codes []string) []T
- func LangCodes2SortedIDs[T NumberType](codes []string) []T
- type Code
- func (c Code) ID() ID
- func (c Code) ISO2() string
- func (c Code) ISO3() string
- func (c Code) Int() uint
- func (c Code) IsUndefined() bool
- func (c Code) Language() *Language
- func (c Code) MarshalJSON() ([]byte, error)
- func (c *Code) Scan(data any) error
- func (c Code) String() string
- func (c *Code) UnmarshalJSON(data []byte) error
- func (c Code) Value() (driver.Value, error)
- type ID
- func (id ID) Code() Code
- func (id ID) ISO2() string
- func (id ID) ISO3() string
- func (id ID) IsUndefined() bool
- func (id ID) Language() *Language
- func (id ID) MarshalJSON() ([]byte, error)
- func (id *ID) Scan(data any) error
- func (id *ID) UnmarshalJSON(data []byte) error
- func (id ID) Value() (driver.Value, error)
- type Language
- type NumberType
Constants ¶
const UndefinedISO2 = "**"
UndefinedISO2 is the sentinel ISO-639-1 stand-in for an unknown language.
const UndefinedISO3 = "***"
UndefinedISO3 is the sentinel ISO-639-2 stand-in for an unknown language.
const UndeterminedISO3 = "und"
UndeterminedISO3 is the ISO-639-2 code for an undetermined language. Lookup accepts it as an unknown language; ISO3 still returns UndefinedISO3.
Variables ¶
var ( ErrCodeInvalidScanType = errors.New("[langlib.code] invalid scan type, supports only bytes and string") ErrCodeInvalidValueSize = errors.New("[langlib.code] invalid value size, supports only 2 or 3 chars") )
Error list...
var ( ErrIDInvalidScanType = errors.New("[langlib.id] invalid scan type, supports only integer, bytes and string") ErrIDInvalidValue = errors.New("[langlib.id] invalid value") )
var Languages = languages[:]
Languages is the public catalog slice. Index equals Language.ID.
var UndefinedCode = Code{UndefinedISO2[0], UndefinedISO2[1]}
UndefinedCode is the two-byte form of UndefinedISO2.
Functions ¶
func LangCodes2IDs ¶
func LangCodes2IDs[T NumberType](codes []string) []T
LangCodes2IDs converts language codes to language IDs, in input order. Unknown codes are 0. An empty input returns nil.
func LangCodes2SortedIDs ¶
func LangCodes2SortedIDs[T NumberType](codes []string) []T
LangCodes2SortedIDs converts language codes to language IDs, sorted ascending. Unknown codes are 0. An empty input returns nil.
Types ¶
type Code ¶
type Code [2]byte
Code is a 2-letter ISO-639-1 language code.
func CodeFromString ¶
CodeFromString returns the canonical catalog code for s. Accepts ISO-639-1, ISO-639-2 (`deu`, `ger`) and BCP-47 tags (`en-US` → en). Unknown input is UndefinedCode.
func (Code) ID ¶
ID is the catalog language ID (UndefinedID for unknown / undefined).
func (Code) ISO2 ¶
ISO2 returns the interned two-letter catalog code, or UndefinedISO2.
func (Code) ISO3 ¶
ISO3 returns the ISO-639-2/T catalog code, or UndefinedISO3.
func (Code) IsUndefined ¶
IsUndefined reports whether c maps to Languages[0].
func (Code) Language ¶
Language returns the catalog entry for c. Unknown codes resolve to Languages[0] (never nil).
func (Code) MarshalJSON ¶
MarshalJSON encodes the ISO-639-1 code. Unknown codes are UndefinedISO2.
func (*Code) Scan ¶
Scan implementation of sql.Scanner interface. Accepts two- and three-letter codes.
func (*Code) UnmarshalJSON ¶
UnmarshalJSON accepts a two- or three-letter code string. Other lengths become UndefinedCode.
type ID ¶
type ID uint8
ID is a catalog language identifier. Index in Languages equals the ID. Zero and out-of-range values resolve to Languages[0].
const UndefinedID ID = 0
UndefinedID is the catalog ID of an unknown language (Languages[0]).
func IDFromString ¶
IDFromString resolves an ISO-639-1, ISO-639-2 (T or B) or BCP-47 string. Unknown and short values return UndefinedID.
func (ID) ISO2 ¶
ISO2 returns the interned two-letter catalog code, or UndefinedISO2.
func (ID) ISO3 ¶
ISO3 returns the ISO-639-2/T catalog code, or UndefinedISO3.
func (ID) IsUndefined ¶
IsUndefined reports whether id maps to Languages[0].
func (ID) Language ¶
Language returns the catalog entry. Unknown IDs resolve to Languages[0] (never nil).
func (ID) MarshalJSON ¶
MarshalJSON encodes the ISO-639-1 code. Unknown IDs are UndefinedISO2.
func (*ID) Scan ¶
Scan implementation of sql.Scanner interface. Integers are catalog IDs. Strings and byte slices are either a decimal ID or a language code (IDFromString).
func (*ID) UnmarshalJSON ¶
UnmarshalJSON accepts a JSON number or a language-code string. Null, "", and "**" become UndefinedID.
type Language ¶
type Language struct {
ID ID `json:"ID"`
Code Code `json:"code"`
Name string `json:"name"`
NativeName string `json:"native_name"`
}
Language is a catalog entry. Slice index equals ID.
func GetLanguageByCodeString ¶
GetLanguageByCodeString resolves an ISO-639-1, ISO-639-2 (T or B) or BCP-47 string. Unknown and short values return Languages[0] (never nil).