Documentation
¶
Overview ¶
Package langpick negotiates a site language from an Accept-Language header and names languages in their own script (endonyms) for language switchers.
Negotiate understands more than the two-letter prefix of each tag: an exact tag ("pt-BR") matches a supported code of the same spelling, and Chinese tags are mapped to the script subtags sites usually ship ("zh-CN", "zh-SG" → zh-Hans; "zh-TW", "zh-HK", "zh-MO" → zh-Hant), which a plain prefix match cannot tell apart.
Index ¶
Constants ¶
const ( ChineseSimplified = "zh-Hans" ChineseTraditional = "zh-Hant" )
Script subtags for Chinese.
Variables ¶
This section is empty.
Functions ¶
func Name ¶
Name returns the endonym for code ("de" → "Deutsch", "zh-Hant" → "繁體中文"), or code itself when it is not known.
func Negotiate ¶
Negotiate returns the supported code for the first Accept-Language tag the site can serve, or "" when none matches. Tags are taken in header order; quality values are ignored because browsers already send tags in preference order. Per tag, in order:
- An exact, case-insensitive match ("pt-br" → "pt-BR").
- For Chinese: the script variant derived from the tag (Hant or a TW/HK/MO region → zh-Hant, anything else → zh-Hans), then bare "zh".
- The primary language subtag ("de-AT" → "de").
The returned value is always spelled as in supported.
Types ¶
This section is empty.