language

package
v1.0.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 9 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

language

ISO 639-1 two-letter language codes. Code normalizes the language identifiers found in HTTP headers, locales and other standards ("EN", "deu", "ger", "en-US", "zh_Hant") to the canonical lower-case ISO 639-1 code, returns English language names, and reads and writes SQL, JSON and XML. Two lookup functions cover the full ISO 639-3 tables. The date package uses Code as a parsing hint.

import "github.com/domonda/go-types/language"

Code

type Code string //#enum

const Null Code = "" //#exclude

Value writes Null as SQL NULL and Scan reads SQL NULL as Null. The constant is marked //#exclude so that go-enum leaves it out of Enums(). Null is not a valid code: Null.Valid() is false and Null.Validate() returns an error. There is no nullable code type in this package.

Constants

The package declares 184 Code constants, one per ISO 639-1 code, named after the upper-case code with a lower-case value: EN Code = "en", DE Code = "de", ZH Code = "zh", ... Note that language.EL is Greek, unrelated to country.EL.

List them with the go-enum generated methods. Both return the codes in declaration order, which is close to alphabetical by English name:

codes := language.Code("").Enums()       // []language.Code{language.AA, language.AB, language.AF, ...}
names := language.Code("").EnumStrings() // []string{"aa", "ab", "af", ...}
Functions and methods
Identifier Description
ParseCode(str) Canonical code from a code, tag or English language name, see below.
Normalized() The canonical ISO 639-1 code for the input shapes listed below. On failure returns the input unchanged and an error that quotes it.
Valid() True if Normalized() succeeds, so Code("EN").Valid() is true.
Validate() The error of Normalized(), which quotes the input, or nil.
ValidAndNormalized() True only for a canonical lower-case code.
LanguageName() English name from the ISO 639-1 table. Normalizes first; "" if invalid.
String() The code as stored, generated by go-enum.
Enums(), EnumStrings() All 184 codes, generated by go-enum.
ISO6393Name(code) English reference name of an ISO 639-3 code ("deu" gives "German"), "" if unknown.
ISO6393Macrolanguage(code) ISO 639-3 macrolanguage of an individual language ("twi" gives "aka"), "" if none.

Implements: fmt.Stringer, database/sql.Scanner, database/sql/driver.Valuer, encoding/json.Marshaler, encoding/json.Unmarshaler, encoding.TextMarshaler, encoding/xml.Unmarshaler, encoding/xml.UnmarshalerAttr, encoding/xml.MarshalerAttr, types.NormalizableValidator[Code] and JSONSchema() ({"type":"string","pattern":"^[a-z]{2}$"}). ScanString makes it a strfmt.Scannable, which reads a code like Scan. Code has no UnmarshalText, so encoding/json reads the keys of a map[Code]V as given.

Valid and Validate are hand-written and carry a // #custom marker, so go-enum keeps them. They normalize first like those of country.Code and money.Currency. Reading and Value only trim. Call Normalized() for the ISO 639-1 code. String is the one go-enum generates: it returns the stored code.

Normalized input shapes

Normalized(), and so Valid() and LanguageName(), accept the spellings below. Reading and writing only trim, so "eng", "ger" and "en-US" are stored as given:

  • ISO 639-1 codes in any case with surrounding whitespace: "en", "EN", " en ".
  • ISO 639-2/T and ISO 639-3 codes that have an ISO 639-1 equivalent: "eng", "deu", "fra".
  • ISO 639-2/B bibliographic codes: "ger" gives "de", "fre" gives "fr", "chi" gives "zh".
  • BCP-47 tags and POSIX locales, using only the primary subtag: "en-US", "zh-Hant-CN", "sr-Latn", "en_US".

ISO 639-3 codes without an ISO 639-1 equivalent (most of the roughly 7900 codes, like "abq") are rejected.

ParseCode

ParseCode(str) tries the cheapest interpretation first:

  1. A canonical code ("en") is returned as is.
  2. Anything Normalized accepts.
  3. An English language name, matched case-insensitively: "German", "english", "Valencian". Names come from the ISO 639-1 table, including each synonym of entries like "Catalan; Valencian", and from the ISO 639-3 names of languages that have an ISO 639-1 code. The ISO 639-1 names win on conflicts.

On failure it returns the input unchanged and the error of Normalized.

ISO 639-3 lookups

ISO6393Name and ISO6393Macrolanguage read tables generated from the SIL ISO 639-3 data. Both are case-sensitive and expect the canonical lower-case code: ISO6393Name("DEU") is "". A code with several names returns its primary name. A macrolanguage is not a member of itself (ISO6393Macrolanguage("aka") is ""), and retired mappings are not returned.

Serialization

Code follows the rules of Reading and writing values: reading and writing only trim, so "EN" stays "EN", and fmt prints the stored code.

Value SQL Value() JSON (json.Marshal) MarshalText, XML fmt
Null, Code(" ") NULL "" "" as stored
Code("en") "en" "en" "en" en
Code("EN") "EN" "EN" "EN" EN
Code("xx") (invalid) "xx" "xx" "xx" xx
Input Result
Scan(nil) Null
Scan of a string or []byte, JSON string, XML element or attribute trimmed, Null if blank, case kept, without error
Scan of other types error
JSON null field left unchanged
other JSON values error

Gotchas

  • Valid accepts other spellings. Code("EN").Valid() and Code("EN").Validate() accept "EN", "eng" and "en-US", like types.Validate, types.DeepValidate and strfmt scanning. Check ValidAndNormalized to require the canonical code.
  • A code keeps its spelling. Scan, JSON and XML only trim, so "EN" stays "EN" and "en-US" stays "en-US". Call Normalized() for the ISO 639-1 code.
  • Optional languages. Null is SQL NULL but fails Validate, so an empty language field fails types.DeepValidate. Use a *language.Code for optional fields or check for Null before validating.
  • JSON is a string. Null encodes as "", not null. The JSON schema pattern ^[a-z]{2}$ describes a canonical code. MarshalJSON writes the trimmed code, so it can write "EN".
  • English names must match a table entry. ParseCode("Greek") fails, because the names are "Greek, Modern (1453-)" (ISO 639-1) and "Modern Greek (1453-)" (ISO 639-3).
  • Names may list synonyms. language.CA.LanguageName() is "Catalan; Valencian", language.NL.LanguageName() is "Dutch; Flemish".
  • Tags lose detail. "zh-Hant" and "zh-Hans" both normalize to "zh".
  • Date parsing hints. date.Normalize only distinguishes English from everything else, see the date package.

Examples

package main

import (
	"fmt"

	"github.com/domonda/go-types/language"
)

func main() {
	for _, s := range []string{"en", "EN", "deu", "ger", "en-US", "zh_Hant", "German", "Klingon"} {
		c, err := language.ParseCode(s)
		fmt.Println(c, err)
	}

	// Valid, Validate and Normalized accept other spellings
	c := language.Code("EN")
	fmt.Println(c.Valid(), c.Validate(), c.LanguageName())
	norm, err := c.Normalized()
	fmt.Println(norm, err, norm.Validate())

	// Output:
	// en <nil>
	// en <nil>
	// de <nil>
	// de <nil>
	// en <nil>
	// zh <nil>
	// de <nil>
	// Klingon invalid language.Code: "Klingon"
	// true <nil> English
	// en <nil> <nil>
}
package main

import (
	"encoding/json"
	"fmt"

	"github.com/domonda/go-types/language"
)

func main() {
	// Value and MarshalJSON write the trimmed code
	v, _ := language.Code("EN").Value()
	fmt.Println(v)
	j, _ := json.Marshal(language.Code("en-US"))
	fmt.Println(string(j))

	fmt.Println(language.ISO6393Name("deu"), language.ISO6393Macrolanguage("twi"))
	fmt.Println(language.CA.LanguageName())
	fmt.Println(len(language.Code("").Enums()))

	// Output:
	// EN
	// "en-US"
	// German aka
	// Catalan; Valencian
	// 184
}

Data files

File Content Maintained by
code.go Code, its methods and the ISO 639-1 constants; Enums() and EnumStrings() at the end hand; go-enum for Enums and EnumStrings
constants.go codeNames: ISO 639-1 code to English name hand
iso6393.go iso6393To1: ISO 639-2/T, 639-2/B and 639-3 codes to ISO 639-1, and the two lookup functions hand
iso6393names.go iso6393Names: ISO 639-3 code to English name gen-lang-maps
iso6393macro.go iso6393Macro: individual language to macrolanguage gen-lang-maps

Refresh the two generated tables with go generate ./language/... (needs network access), see gen-lang-maps.

Adding a language code

  1. Add the constant to the const block in language/code.go: XX Code = "xx" // Name.
  2. Add "xx": "Name" to codeNames in language/constants.go. TestCode_EnumsMatchCodeNames fails if the two lists differ.
  3. Add the ISO 639-2/T, 639-2/B and 639-3 codes of the language to iso6393To1 in language/iso6393.go, so that Normalized maps them.
  4. Run ./run-go-enum.sh from the repository root to regenerate Enums() and EnumStrings(), then go test ./....
  5. CI runs go-enum -validate and fails if the generated methods are stale.

See Add an enum code for the full steps.

Documentation

Overview

Package language provides ISO 639-1 two-letter language codes.

Code is a string type with a constant for every ISO 639-1 code (EN, DE, ...). Code.Normalized maps other identifiers to ISO 639-1: upper case, ISO 639-2/T and 639-2/B codes ("deu", "ger"), ISO 639-3 codes that have an ISO 639-1 equivalent, BCP-47 tags and POSIX locales ("en-US", "zh_Hant"). ParseCode also accepts English language names ("German").

Code.Valid and Code.Validate normalize first and accept every spelling that Code.Normalized accepts. Reading and Code.Value only trim, so "EN" stays "EN". Call Code.Normalized for the ISO 639-1 code. Code.ValidAndNormalized only accepts a canonical lower-case code.

The package includes:

See the README.md of this package for the full API and how to add a language code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ISO6393Macrolanguage

func ISO6393Macrolanguage(code string) string

ISO6393Macrolanguage returns the ISO 639-3 macrolanguage code that encompasses the given ISO 639-3 individual language code, as published in the SIL ISO 639-3 macrolanguage mappings. It returns an empty string if the code is not a member of any macrolanguage (or is not a known individual ISO 639-3 code).

For example "twi" (Twi) and "fat" (Fanti) both roll up to "aka" (Akan). The lookup is case-sensitive and expects the canonical lower-case form ("twi", not "TWI").

See https://iso639-3.sil.org/code_tables/download_tables for the data.

func ISO6393Name

func ISO6393Name(code string) string

ISO6393Name returns the English reference name of the language identified by the given ISO 639-3 three-letter code, as published in the SIL ISO 639-3 Name Index. It returns an empty string if the code is not a known ISO 639-3 code.

A code with several recorded names resolves to its primary name (for example "deu" → "German"). The lookup is case-sensitive and expects the canonical lower-case form ("deu", not "DEU").

See https://iso639-3.sil.org/code_tables/download_tables for the data.

Types

type Code

type Code string //#enum

Code represents a language code in its normalized form as an ISO 639-1 two-character language code. The empty Code is Null, there is no separate nullable type.

Scan, UnmarshalJSON, UnmarshalXML and ScanString without validate trim surrounding whitespace and leave the letter case unchanged, so " EN " becomes "EN" and "en-US" stays "en-US". A blank code is Null. Value, MarshalJSON and MarshalText write the trimmed text, and Value writes SQL NULL for a blank code. They do not reject an invalid code and do not strip region or script subtags. ScanString with validate stores the normalized code or returns the error of Normalized. fmt prints the code as stored. Code has no UnmarshalText, so encoding/json reads and writes the keys of a map[Code]V as given. See "Reading and writing values" in the README of the module.

const (
	AA Code = "aa" // Afar
	AB Code = "ab" // Abkhazian
	AF Code = "af" // Afrikaans
	AK Code = "ak" // Akan
	SQ Code = "sq" // Albanian
	AM Code = "am" // Amharic
	AR Code = "ar" // Arabic
	AN Code = "an" // Aragonese
	HY Code = "hy" // Armenian
	AS Code = "as" // Assamese
	AV Code = "av" // Avaric
	AE Code = "ae" // Avestan
	AY Code = "ay" // Aymara
	AZ Code = "az" // Azerbaijani
	BA Code = "ba" // Bashkir
	BM Code = "bm" // Bambara
	EU Code = "eu" // Basque
	BE Code = "be" // Belarusian
	BN Code = "bn" // Bengali
	BH Code = "bh" // Bihari languages
	BI Code = "bi" // Bislama
	BS Code = "bs" // Bosnian
	BR Code = "br" // Breton
	BG Code = "bg" // Bulgarian
	MY Code = "my" // Burmese
	CA Code = "ca" // Catalan; Valencian
	CH Code = "ch" // Chamorro
	CE Code = "ce" // Chechen
	ZH Code = "zh" // Chinese
	CU Code = "cu" // Church Slavic; Old Slavonic; Church Slavonic; Old Bulgarian; Old Church Slavonic
	CV Code = "cv" // Chuvash
	KW Code = "kw" // Cornish
	CO Code = "co" // Corsican
	CR Code = "cr" // Cree
	CS Code = "cs" // Czech
	DA Code = "da" // Danish
	DV Code = "dv" // Divehi; Dhivehi; Maldivian
	NL Code = "nl" // Dutch; Flemish
	DZ Code = "dz" // Dzongkha
	EN Code = "en" // English
	EO Code = "eo" // Esperanto
	ET Code = "et" // Estonian
	EE Code = "ee" // Ewe
	FO Code = "fo" // Faroese
	FJ Code = "fj" // Fijian
	FI Code = "fi" // Finnish
	FR Code = "fr" // French
	FY Code = "fy" // Western Frisian
	FF Code = "ff" // Fulah
	KA Code = "ka" // Georgian
	DE Code = "de" // German
	GD Code = "gd" // Gaelic; Scottish Gaelic
	GA Code = "ga" // Irish
	GL Code = "gl" // Galician
	GV Code = "gv" // Manx
	EL Code = "el" // Greek, Modern (1453-)
	GN Code = "gn" // Guarani
	GU Code = "gu" // Gujarati
	HT Code = "ht" // Haitian; Haitian Creole
	HA Code = "ha" // Hausa
	HE Code = "he" // Hebrew
	HZ Code = "hz" // Herero
	HI Code = "hi" // Hindi
	HO Code = "ho" // Hiri Motu
	HR Code = "hr" // Croatian
	HU Code = "hu" // Hungarian
	IG Code = "ig" // Igbo
	IS Code = "is" // Icelandic
	IO Code = "io" // Ido
	II Code = "ii" // Sichuan Yi; Nuosu
	IU Code = "iu" // Inuktitut
	IE Code = "ie" // Interlingue; Occidental
	IA Code = "ia" // Interlingua (International Auxiliary Language Association)
	ID Code = "id" // Indonesian
	IK Code = "ik" // Inupiaq
	IT Code = "it" // Italian
	JV Code = "jv" // Javanese
	JA Code = "ja" // Japanese
	KL Code = "kl" // Kalaallisut; Greenlandic
	KN Code = "kn" // Kannada
	KS Code = "ks" // Kashmiri
	KR Code = "kr" // Kanuri
	KK Code = "kk" // Kazakh
	KM Code = "km" // Central Khmer
	KI Code = "ki" // Kikuyu; Gikuyu
	RW Code = "rw" // Kinyarwanda
	KY Code = "ky" // Kirghiz; Kyrgyz
	KV Code = "kv" // Komi
	KG Code = "kg" // Kongo
	KO Code = "ko" // Korean
	KJ Code = "kj" // Kuanyama; Kwanyama
	KU Code = "ku" // Kurdish
	LO Code = "lo" // Lao
	LA Code = "la" // Latin
	LV Code = "lv" // Latvian
	LI Code = "li" // Limburgan; Limburger; Limburgish
	LN Code = "ln" // Lingala
	LT Code = "lt" // Lithuanian
	LB Code = "lb" // Luxembourgish; Letzeburgesch
	LU Code = "lu" // Luba-Katanga
	LG Code = "lg" // Ganda
	MK Code = "mk" // Macedonian
	MH Code = "mh" // Marshallese
	ML Code = "ml" // Malayalam
	MI Code = "mi" // Maori
	MR Code = "mr" // Marathi
	MS Code = "ms" // Malay
	MG Code = "mg" // Malagasy
	MT Code = "mt" // Maltese
	MN Code = "mn" // Mongolian
	NA Code = "na" // Nauru
	NV Code = "nv" // Navajo; Navaho
	NR Code = "nr" // Ndebele, South; South Ndebele
	ND Code = "nd" // Ndebele, North; North Ndebele
	NG Code = "ng" // Ndonga
	NE Code = "ne" // Nepali
	NN Code = "nn" // Norwegian Nynorsk; Nynorsk, Norwegian
	NB Code = "nb" // Bokmål, Norwegian; Norwegian Bokmål
	NO Code = "no" // Norwegian
	NY Code = "ny" // Chichewa; Chewa; Nyanja
	OC Code = "oc" // Occitan (post 1500); Provençal
	OJ Code = "oj" // Ojibwa
	OR Code = "or" // Oriya
	OM Code = "om" // Oromo
	OS Code = "os" // Ossetian; Ossetic
	PA Code = "pa" // Panjabi; Punjabi
	FA Code = "fa" // Persian
	PI Code = "pi" // Pali
	PL Code = "pl" // Polish
	PT Code = "pt" // Portuguese
	PS Code = "ps" // Pushto; Pashto
	QU Code = "qu" // Quechua
	RM Code = "rm" // Romansh
	RO Code = "ro" // Romanian; Moldavian; Moldovan
	RN Code = "rn" // Rundi
	RU Code = "ru" // Russian
	SG Code = "sg" // Sango
	SA Code = "sa" // Sanskrit
	SI Code = "si" // Sinhala; Sinhalese
	SK Code = "sk" // Slovak
	SL Code = "sl" // Slovenian
	SE Code = "se" // Northern Sami
	SM Code = "sm" // Samoan
	SN Code = "sn" // Shona
	SD Code = "sd" // Sindhi
	SO Code = "so" // Somali
	ST Code = "st" // Sotho, Southern
	ES Code = "es" // Spanish; Castilian
	SC Code = "sc" // Sardinian
	SR Code = "sr" // Serbian
	SS Code = "ss" // Swati
	SU Code = "su" // Sundanese
	SW Code = "sw" // Swahili
	SV Code = "sv" // Swedish
	TY Code = "ty" // Tahitian
	TA Code = "ta" // Tamil
	TT Code = "tt" // Tatar
	TE Code = "te" // Telugu
	TG Code = "tg" // Tajik
	TL Code = "tl" // Tagalog
	TH Code = "th" // Thai
	BO Code = "bo" // Tibetan
	TI Code = "ti" // Tigrinya
	TO Code = "to" // Tonga (Tonga Islands)
	TN Code = "tn" // Tswana
	TS Code = "ts" // Tsonga
	TK Code = "tk" // Turkmen
	TR Code = "tr" // Turkish
	TW Code = "tw" // Twi
	UG Code = "ug" // Uighur; Uyghur
	UK Code = "uk" // Ukrainian
	UR Code = "ur" // Urdu
	UZ Code = "uz" // Uzbek
	VE Code = "ve" // Venda
	VI Code = "vi" // Vietnamese
	VO Code = "vo" // Volapük
	CY Code = "cy" // Welsh
	WA Code = "wa" // Walloon
	WO Code = "wo" // Wolof
	XH Code = "xh" // Xhosa
	YI Code = "yi" // Yiddish
	YO Code = "yo" // Yoruba
	ZA Code = "za" // Zhuang; Chuang
	ZU Code = "zu" // Zulu
)
const Null Code = "" //#exclude

Null is an empty string and will be treated as SQL NULL. language.Null.Valid() == false

Marked with //#exclude so that go-enum does not pick it up as one of the Code enum values: Null is not a valid language code and must stay out of Code.Enums() and Code.EnumStrings().

func ParseCode

func ParseCode(str string) (Code, error)

ParseCode parses and normalizes a language identifier given in any of the formats this package understands, returning the canonical ISO 639-1 two-letter Code.

It tries the cheapest and most common interpretations first and only falls back to the more expensive ones:

  1. An input that is already a canonical ISO 639-1 code ("en", "de") is returned unchanged without any further work.
  2. Otherwise the input is run through Normalized, which accepts case and whitespace variants, ISO 639-2/T and 639-2/B codes, ISO 639-3 codes with a 639-1 equivalent, BCP-47 tags, and POSIX locale strings ("EN", " eng ", "ger", "en-US", "zh_Hant").
  3. As a last resort the input is matched case-insensitively against the English language names of the ISO 639-1 and ISO 639-3 tables ("German", "english", "Catalan").

ParseCode returns an error wrapping the original input if no ISO 639-1 code can be derived. Like Normalized, the error preserves the unnormalized value for diagnostics.

func (Code) EnumStrings

func (Code) EnumStrings() []string

EnumStrings returns all valid values for Code as strings

func (Code) Enums

func (Code) Enums() []Code

Enums returns all valid values for Code

func (Code) JSONSchema

func (Code) JSONSchema() *jsonschema.Schema

JSONSchema returns the JSON schema definition for the Code type.

func (Code) LanguageName

func (c Code) LanguageName() string

LanguageName returns the English name of the language for the code. The code is normalized first, see Normalized. Returns an empty string if the code is invalid.

func (Code) MarshalJSON

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

MarshalJSON implements encoding/json.Marshaler by writing the trimmed code, or "" for a blank Code.

func (Code) MarshalText

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

MarshalText implements encoding.TextMarshaler like MarshalJSON, so that encoding/xml and strfmt write the trimmed code. encoding/json writes map keys of string types as they are and doesn't call MarshalText for them.

func (*Code) MarshalXMLAttr

func (c *Code) MarshalXMLAttr(name xml.Name) (xml.Attr, error)

MarshalXMLAttr implements encoding/xml.MarshalerAttr by writing the text of MarshalText, and no attribute for a nil pointer, because encoding/xml calls MarshalText through a nil pointer attribute field and panics.

func (Code) Normalized

func (c Code) Normalized() (Code, error)

Normalized returns the normalized ISO 639-1 two-letter language code.

Accepted input shapes:

  • ISO 639-1 two-letter codes, case-insensitive, with surrounding whitespace trimmed ("en", " EN ", "En").
  • ISO 639-2 three-letter codes, both the bibliographic (B) and terminologic (T) variants where they differ ("eng", "deu", "ger").
  • ISO 639-3 three-letter codes, where a 639-1 equivalent exists ("eng", "deu", "fra"). 639-3 codes for languages without a 639-1 assignment are rejected.
  • BCP-47 language tags: only the leading language subtag is consulted; script, region, variant, and extension subtags are dropped ("en-US" → "en", "zh-Hant-CN" → "zh", "sr-Latn" → "sr"). POSIX-style underscores ("en_US") are accepted as separators.

Returns an error wrapping the original input if no 639-1 code can be derived. The error preserves the unnormalized value for diagnostics.

See:

func (*Code) Scan

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

Scan implements the database/sql.Scanner interface. Surrounding whitespace is trimmed and the letter case is left unchanged. SQL NULL and a blank string are stored as Null. An invalid code is kept after that trim, without an error.

func (*Code) ScanString

func (c *Code) ScanString(source string, validate bool) error

ScanString implements the strfmt.Scannable interface. Without validate it stores source like Scan, with validate it stores the normalized code or returns the error of Normalized, also for a blank source.

func (Code) String

func (c Code) String() string

String implements the fmt.Stringer interface for Code

func (*Code) UnmarshalJSON

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

UnmarshalJSON implements encoding/json.Unmarshaler. A JSON string is stored like Scan stores it, JSON null leaves the Code unchanged like encoding/json does for a string type, and any other JSON value is an error.

func (*Code) UnmarshalXML

func (c *Code) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements encoding/xml.Unmarshaler by storing the text of the element like Scan stores it.

func (*Code) UnmarshalXMLAttr

func (c *Code) UnmarshalXMLAttr(attr xml.Attr) error

UnmarshalXMLAttr implements encoding/xml.UnmarshalerAttr by storing the attribute value like Scan stores it.

func (Code) Valid

func (c Code) Valid() bool

Valid returns true if c can be normalized to a valid ISO 639-1 language code, so it accepts other spellings like "EN", "eng" and "en-US". See Normalized for the accepted input shapes and ValidAndNormalized to require the canonical lower-case code.

Hand-written: normalizes first like country.Code and money.Currency, instead of the exact-match switch over every enum value that go-enum would generate.

#custom

func (Code) ValidAndNormalized

func (c Code) ValidAndNormalized() bool

ValidAndNormalized returns true if the Code is valid and already normalized.

func (Code) Validate

func (c Code) Validate() error

Validate returns an error if c can not be normalized to a valid ISO 639-1 language code, see Valid.

Hand-written: returns the error from Normalized, which names the offending input, instead of the generic error that go-enum would generate.

#custom

func (Code) Value

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

Value implements the driver database/sql/driver.Valuer interface. It returns the trimmed code, or SQL NULL for Null or a blank Code. An invalid code is written after that trim, not rejected.

Directories

Path Synopsis
Command gen-lang-maps regenerates the language package's ISO 639-3 lookup tables from the SIL reference data:
Command gen-lang-maps regenerates the language package's ISO 639-3 lookup tables from the SIL reference data:

Jump to

Keyboard shortcuts

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