langlib

package module
v0.0.0-...-4709d51 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

README

langlib

Test Go Reference Go Report Card License

Catalog of ISO-639-1 languages with their ISO-639-2 codes, and two value types for storing one: Code (two letters) and ID (catalog index).

Lookup accepts ISO-639-1 (en), ISO-639-2 (eng) and BCP-47 tags with either (en-US, zh-Hans, eng-US). Case does not matter. An unknown or short value resolves to Languages[0]: ID 0, ISO codes ** and ***, name Undefined. The catalog index is the language ID. Resolving a code does not allocate.

go get github.com/demdxx/langlib

Lookup

lang := langlib.GetLanguageByCodeString("en-US")
fmt.Println(lang.ISO2(), lang.Name, lang.ID) // en English 40

code := langlib.CodeFromString("DE")
fmt.Println(code.ISO2(), code.ID(), code.Language().Name) // de 51 German

id := langlib.IDFromString("fr")
fmt.Println(id.Language().Name) // French

if langlib.IDFromString("xx").IsUndefined() {
    fmt.Println(langlib.UndefinedISO2) // **
}

Languages is the full catalog. Languages[i].ID equals i.

ISO-639-2

ISO3() on ID, Code and Language returns the terminology code (T). Lookup also accepts the bibliographic code (B) for the 20 languages that have one, such as ger, fre, chi.

id := langlib.IDFromString("ger")
fmt.Println(id.ISO2(), id.ISO3()) // de deu

fmt.Println(langlib.CodeFromString("chi").ISO3()) // zho
fmt.Println(langlib.IDFromString("xyz").ISO3())   // ***

A three-letter subtag is matched only against ISO-639-2: chi is Chinese, not Chamorro (ch).

The ISO-639-2 code und (undetermined) is accepted as an unknown language. ISO3() still returns *** for it.

JSON

Code and ID marshal as an ISO-639-1 string. Unknown values marshal as "**".

type prefs struct {
    Lang langlib.ID   `json:"lang"`
    Code langlib.Code `json:"code"`
}

var p prefs
_ = json.Unmarshal([]byte(`{"lang":"en-US","code":"DE"}`), &p)
data, _ := json.Marshal(p)
fmt.Println(string(data)) // {"lang":"en","code":"de"}

Both also decode ISO-639-2 codes ("deu" becomes "de"). ID additionally accepts a JSON number, the catalog index.

Lists of codes

LangCodes2IDs keeps input order. LangCodes2SortedIDs sorts the IDs. The element type is any integer.

ids := langlib.LangCodes2IDs[uint32]([]string{"en", "xx", "DE"})
// 40, 0, 51

sorted := langlib.LangCodes2SortedIDs[uint32]([]string{"en", "xx", "DE"})
// 0, 40, 51

SQL

Code is stored as the two-letter string (en) and can be scanned from a two- or three-letter code. ID is stored as int64 and can be scanned from an integer or from a language code.

Documentation

Index

Constants

View Source
const UndefinedISO2 = "**"

UndefinedISO2 is the sentinel ISO-639-1 stand-in for an unknown language.

View Source
const UndefinedISO3 = "***"

UndefinedISO3 is the sentinel ISO-639-2 stand-in for an unknown language.

View Source
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

View Source
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...

View Source
var (
	ErrIDInvalidScanType = errors.New("[langlib.id] invalid scan type, supports only integer, bytes and string")
	ErrIDInvalidValue    = errors.New("[langlib.id] invalid value")
)
View Source
var Languages = languages[:]

Languages is the public catalog slice. Index equals Language.ID.

View Source
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

func CodeFromString(s string) Code

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

func (c Code) ID() ID

ID is the catalog language ID (UndefinedID for unknown / undefined).

func (Code) ISO2

func (c Code) ISO2() string

ISO2 returns the interned two-letter catalog code, or UndefinedISO2.

func (Code) ISO3

func (c Code) ISO3() string

ISO3 returns the ISO-639-2/T catalog code, or UndefinedISO3.

func (Code) Int

func (c Code) Int() uint

Int is the packed little-endian numeric form of the two bytes.

func (Code) IsUndefined

func (c Code) IsUndefined() bool

IsUndefined reports whether c maps to Languages[0].

func (Code) Language

func (c Code) Language() *Language

Language returns the catalog entry for c. Unknown codes resolve to Languages[0] (never nil).

func (Code) MarshalJSON

func (c Code) MarshalJSON() ([]byte, error)

MarshalJSON encodes the ISO-639-1 code. Unknown codes are UndefinedISO2.

func (*Code) Scan

func (c *Code) Scan(data any) error

Scan implementation of sql.Scanner interface. Accepts two- and three-letter codes.

func (Code) String

func (c Code) String() string

func (*Code) UnmarshalJSON

func (c *Code) UnmarshalJSON(data []byte) error

UnmarshalJSON accepts a two- or three-letter code string. Other lengths become UndefinedCode.

func (Code) Value

func (c Code) Value() (driver.Value, error)

Value implementation of sql driver.Valuer interface.

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

func IDFromString(s string) ID

IDFromString resolves an ISO-639-1, ISO-639-2 (T or B) or BCP-47 string. Unknown and short values return UndefinedID.

func (ID) Code

func (id ID) Code() Code

Code returns the catalog ISO code, or UndefinedCode.

func (ID) ISO2

func (id ID) ISO2() string

ISO2 returns the interned two-letter catalog code, or UndefinedISO2.

func (ID) ISO3

func (id ID) ISO3() string

ISO3 returns the ISO-639-2/T catalog code, or UndefinedISO3.

func (ID) IsUndefined

func (id ID) IsUndefined() bool

IsUndefined reports whether id maps to Languages[0].

func (ID) Language

func (id ID) Language() *Language

Language returns the catalog entry. Unknown IDs resolve to Languages[0] (never nil).

func (ID) MarshalJSON

func (id ID) MarshalJSON() ([]byte, error)

MarshalJSON encodes the ISO-639-1 code. Unknown IDs are UndefinedISO2.

func (*ID) Scan

func (id *ID) Scan(data any) error

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

func (id *ID) UnmarshalJSON(data []byte) error

UnmarshalJSON accepts a JSON number or a language-code string. Null, "", and "**" become UndefinedID.

func (ID) Value

func (id ID) Value() (driver.Value, error)

Value implementation of sql driver.Valuer interface. The value is int64.

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

func GetLanguageByCodeString(code string) *Language

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).

func (*Language) ISO2

func (l *Language) ISO2() string

ISO2 returns the interned two-letter code (never allocates).

func (*Language) ISO3

func (l *Language) ISO3() string

ISO3 returns the ISO-639-2/T code (never allocates).

type NumberType

type NumberType interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~int | ~int8 | ~int16 | ~int32 | ~int64
}

NumberType is the integer type of the returned language IDs.

Jump to

Keyboard shortcuts

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