Help us shape the future of Go! Take the 2026 Go Developer Survey

langtag

package
v0.2.39 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: GPL-3.0 Imports: 1 Imported by: 0

Documentation

Overview

Package langtag is the single shared language-tag helper for Ether.

It exists so subtitles, trackprefs, plex and the transcode probes do not drift apart with private copies of the same normalisation: a tag is parsed as BCP 47 (primary, optional script, optional region), folded to a canonical comparable form, and matched by tier.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatchTier

func MatchTier(want, track string) int

MatchTier scores a wanted language against a track language, both normalised with Normalize:

  • 3: exact (pt-br × pt-br, pt × pt, por × pt after normalising);
  • 2: generic want × regional track (pt × pt-br, zh × zh-tw) — a generic preference accepts any region of the language;
  • 1: regional want × generic track (pt-br × pt, zh-tw × zh) — accepted, but weak: it never beats tier 3 or 2 for the same want;
  • 0: different languages, regions that differ (pt-br × pt-pt, zh-cn × zh-tw), or an empty/invalid side.

Because only pt/zh keep a region after normalising, script is never mistaken for region here: zh-TW × zh-Hant-TW both fold to zh-tw (3).

func Normalize

func Normalize(tag string) string

Normalize folds a language tag to its canonical comparable form: the primary subtag, plus the region only for Portuguese and Chinese (pt-br vs pt-pt and zh-cn vs zh-tw are distinct languages to every consumer; en-GB and en-US are not). The script is discarded, except that for Chinese a script without an explicit region implies one by convention: zh-Hans -> zh-cn, zh-Hant -> zh-tw (an explicit region always wins: zh-Hant-CN -> zh-cn). "pob" folds to "pt-br", ISO 639-2 primaries to ISO 639-1, and empty, invalid or undetermined tags to "".

Types

type Tag

type Tag struct {
	Primary string
	Script  string
	Region  string
}

Tag is a parsed BCP 47 tag. Primary is canonical: known ISO 639-2 codes fold to their ISO 639-1 form ("eng" -> "en", "por" -> "pt"), and the legacy OpenSubtitles code "pob" parses as primary "pt" with region "br". Script and Region are lower-case and empty when absent.

func ParseBCP47

func ParseBCP47(tag string) (Tag, bool)

ParseBCP47 parses a BCP 47 tag into primary, script and region.

The region is the 2-letter or 3-digit subtag that follows the optional 4-letter script subtag — never the script itself, so "zh-Hant-TW" yields primary "zh", script "hant", region "tw" (taking the second subtag as the region would read "hant" and lose "tw"). Variant and extension subtags after the region are validated but not reported. Garbage — empty subtags, a primary that is not 2-3 ASCII letters, stray short subtags — is rejected (ok is false).

Jump to

Keyboard shortcuts

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