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:
- Code with SQL Scan and Value methods, where Null is SQL NULL
- English names with Code.LanguageName
- Code.Enums and Code.EnumStrings, generated by go-enum, to list all codes
- ISO6393Name and ISO6393Macrolanguage for the full ISO 639-3 tables, generated by the gen-lang-maps command
See the README.md of this package for the full API and how to add a language code.
Index ¶
- func ISO6393Macrolanguage(code string) string
- func ISO6393Name(code string) string
- type Code
- func (Code) EnumStrings() []string
- func (Code) Enums() []Code
- func (Code) JSONSchema() *jsonschema.Schema
- func (c Code) LanguageName() string
- func (c Code) MarshalJSON() ([]byte, error)
- func (c Code) MarshalText() ([]byte, error)
- func (c *Code) MarshalXMLAttr(name xml.Name) (xml.Attr, error)
- func (c Code) Normalized() (Code, error)
- func (c *Code) Scan(value any) error
- func (c *Code) ScanString(source string, validate bool) error
- func (c Code) String() string
- func (c *Code) UnmarshalJSON(data []byte) error
- func (c *Code) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
- func (c *Code) UnmarshalXMLAttr(attr xml.Attr) error
- func (c Code) Valid() bool
- func (c Code) ValidAndNormalized() bool
- func (c Code) Validate() error
- func (c Code) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ISO6393Macrolanguage ¶
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 ¶
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 ¶
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:
- An input that is already a canonical ISO 639-1 code ("en", "de") is returned unchanged without any further work.
- 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").
- 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 ¶
EnumStrings returns all valid values for Code as strings
func (Code) JSONSchema ¶
func (Code) JSONSchema() *jsonschema.Schema
JSONSchema returns the JSON schema definition for the Code type.
func (Code) LanguageName ¶
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 ¶
MarshalJSON implements encoding/json.Marshaler by writing the trimmed code, or "" for a blank Code.
func (Code) MarshalText ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) UnmarshalJSON ¶
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 ¶
UnmarshalXML implements encoding/xml.Unmarshaler by storing the text of the element like Scan stores it.
func (*Code) UnmarshalXMLAttr ¶
UnmarshalXMLAttr implements encoding/xml.UnmarshalerAttr by storing the attribute value like Scan stores it.
func (Code) Valid ¶
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 ¶
ValidAndNormalized returns true if the Code is valid and already normalized.
Source Files
¶
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: |