language

package
v0.9.0-dev.39 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package language is subflux's language model: canonical BCP 47 tags over langtag, the subtitle targets a configuration asks for, how close an offered tag is to a target and whether automation may take it, and the filename spellings subflux writes and reads.

Parse is the one canonicalizer. ReadFilename is the one filename reader; its exception table is read by nothing else, so a spelling it accepts never becomes a configured target. Every function computes and returns; none logs.

Index

Constants

View Source
const FormatVersion uint16 = 1

FormatVersion changes with any tag's canonical text: a langtag or x/text bump that respells one, a fold rule, a language entering or leaving MultiScript. Storage folds it into every tag-holding codec version, so a bump resets core state; testdata/format-v<N>.golden pins version N.

Variables

This section is empty.

Functions

func EnglishName

func EnglishName(t Tag) string

EnglishName is the English name of any tag ("Portuguese (Brazil)"), its canonical text when langtag names none, "" for the zero Tag.

func FilenameTag

func FilenameTag(t Tag) string

FilenameTag is the language segment subflux writes into a subtitle file name: "" for the zero Tag, zh-CN, zh-TW and zh-HK for zh-Hans, zh-Hant and zh-Hant-HK (the region forms media servers read), and the canonical text of every other tag, at most 12 bytes of [A-Za-z0-9-].

func ISO3

func ISO3(t Tag) (string, bool)

ISO3 is the ISO 639-3 code of t's own language subtag (fra for fr-CA, nob for nb, nor for no), the one way subflux turns a tag into a three-letter code; false for the zero Tag and a tag langtag gives no code.

Types

type Class

type Class uint8

Class is what subflux may do with an offered tag for a target. Zero is invalid.

const (
	Exact Class = iota + 1
	Substitute
	ManualOnly
	ClassUnrelated
)

The classes: Exact and Substitute may be automatic (Policy.Automatic), ManualOnly is listed for a person to pick, Unrelated is never listed.

func (Class) MarshalText

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

MarshalText is exact, substitute, manual_only or unrelated.

func (*Class) UnmarshalText

func (c *Class) UnmarshalText(b []byte) error

UnmarshalText refuses every spelling but MarshalText's.

type CloseName

type CloseName struct {
	English   string
	Tag       Tag
	Closeness Closeness
	Class     Class
}

CloseName is one pickable tag graded against a Name's tag as the target.

type Closeness

type Closeness uint8

Closeness is how far an offered tag sits from a target, nearest first. Zero is invalid.

const (
	Identical Closeness = iota + 1
	SameLanguage
	OtherRegion
	Intelligible
	OtherScript
	ClosenessUnrelated
)

The closenesses. OtherScript sits after Intelligible because automation never crosses scripts, whatever langtag's tier order says.

func (Closeness) MarshalText

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

MarshalText is identical, same_language, other_region, intelligible, other_script or unrelated.

func (*Closeness) UnmarshalText

func (c *Closeness) UnmarshalText(b []byte) error

UnmarshalText refuses every spelling but MarshalText's.

type Flags

type Flags struct{ HI, Forced bool }

Flags is what a file, track or candidate carries; all four combinations are valid.

func (Flags) Serves

func (f Flags) Serves(v Variant) bool

Serves reports whether a file carrying f serves a target asking for v: HI needs HI set, Forced needs Forced set, Standard needs neither. No flags serve an invalid Variant.

type Grade

type Grade struct {
	Reason    string
	Closeness Closeness
	Class     Class
}

Grade is one target's judgement of one offered tag; Reason is one English line describing it.

type Name

type Name struct {
	Tag     Tag
	English string
	Close   []CloseName
}

Name is one tag a person can pick as a target, with its English name and the other pickable tags it grades Exact, Substitute or ManualOnly.

func Names

func Names() []Name

Names lists the pickable targets sorted by English name, each Close list sorted by class, closeness and name. The answer is the caller's to change.

type Policy

type Policy struct {
	// contains filtered or unexported fields
}

Policy is the parsed languages section: the targets each item wants and the close-varieties switch. It is immutable; the zero Policy has no targets, the switch off and a fixed digest.

func ParsePolicy

func ParsePolicy(n *yaml.Node) (Policy, []formschema.FieldError)

ParsePolicy reads the languages section, n being its value node (nil when the section is absent), and returns every field error at once, each path absolute from the config root (languages.rules[1].targets[0].tag). On any error the Policy is the zero Policy. Every scalar is read by its text whatever its YAML tag, and afterwards, valid or not, every scalar under n is marked formschema.PlainTag: the section holds no secret.

func (Policy) AutoQuery

func (p Policy) AutoQuery(needing, offered []Tag) []Tag

AutoQuery is the offered tags a provider is asked for automatically: each offered tag Automatic for some target in needing, once, ordered by the position in needing of the first target it serves, then its closeness to that target, then offered order. It never returns a ManualOnly tag.

func (Policy) Automatic

func (p Policy) Automatic(g Grade) bool

Automatic reports whether a grade lets subflux take the offer without a person: Exact always, Substitute while close varieties are on.

func (Policy) CloseVarieties

func (p Policy) CloseVarieties() bool

CloseVarieties reports the close-varieties switch: while on, a Substitute grade is automatic.

func (Policy) Digest

func (p Policy) Digest() [32]byte

Digest is a SHA-256 of the section as configured (the switch, every list, tag and variant, in order), so equal sections share it and any change moves it. It names the section only, never the grading code.

func (Policy) Grade

func (Policy) Grade(target, have Tag) Grade

Grade judges have as an offer for target: how close it is and whether it may serve automatically, ignoring the close-varieties switch (Automatic applies it). Either tag zero grades Unrelated.

func (Policy) ManualQuery

func (p Policy) ManualQuery(target Tag, offered []Tag) []Tag

ManualQuery is the offered tags a person may pick for target, whatever the close-varieties switch: each graded Exact, Substitute or ManualOnly, once, ordered by class, then closeness, then offered order.

func (Policy) Targets

func (p Policy) Targets() []Target

Targets is every configured target once, the defaults first then each rule's in order, a tag's variants the union of its listings in Variant order. The answer is the caller's to change.

func (Policy) TargetsFor

func (p Policy) TargetsFor(original Tag, audio []Tag) []Target

TargetsFor answers the targets of an item: the closest-tier rule matching original, ties by config order, then each audio track the same way, else the default targets. A matched rule with no targets stops the chain, so the item wants nothing. The answer is the caller's to change.

type ProviderMap

type ProviderMap struct {
	// contains filtered or unexported fields
}

ProviderMap is one provider's two-way table between its language labels and tags. Lookups are exact: normalizing a label is the adapter's job. The zero ProviderMap is empty.

func MustProviderMap

func MustProviderMap(pairs []WirePair) ProviderMap

MustProviderMap is NewProviderMap for package-level adapter tables; it panics on NewProviderMap's error.

func NewProviderMap

func NewProviderMap(pairs []WirePair) (ProviderMap, error)

NewProviderMap builds a map from pairs, refusing an empty, over-64-byte or invalid UTF-8 label, a zero tag, and a label or tag listed twice; the error names the first bad pair.

func (ProviderMap) Tag

func (m ProviderMap) Tag(wire string) (Tag, bool)

Tag is the tag the provider means by wire.

func (ProviderMap) Tags

func (m ProviderMap) Tags() []Tag

Tags is every mapped tag in pair order, in a slice the caller owns.

func (ProviderMap) Wire

func (m ProviderMap) Wire(t Tag) (string, bool)

Wire is the provider's label for t.

type Tag

type Tag struct {
	// contains filtered or unexported fields
}

Tag is a canonical BCP 47 language tag, folded to its representative: Parse and every reader built on it fold zh-CN to zh-Hans, zh-TW to zh-Hant and zh-HK to zh-Hant-HK, the inverse of FilenameTag, and a region implying its script states it (ku-IQ to ku-Arab-IQ). == is canonical equality; the zero Tag means no language.

func FromName

func FromName(name string) (Tag, bool)

FromName reads a language as an English name (CLDR's, a pickable tag's composed name, or Sonarr's and Radarr's, in any ASCII case) or, failing that, as a code Parse reads. It answers false for empty input, input over 64 bytes after trimming, and the arr values Unknown, Any and Original.

func Parse

func Parse(raw string) (Tag, bool)

Parse canonicalizes raw (ISO 639-1, 639-2, 639-3 or BCP 47, any ASCII case) and folds it to its representative. "" is the zero Tag and true; any other input naming no language is the zero Tag and false. Parse reads no filename exception: ReadFilename does.

func ReadFilename

func ReadFilename(segment string) (Tag, bool)

ReadFilename reads one language segment of a subtitle file name: a spelling other tools commonly write (pb, cht, es-la, in any ASCII case) reads as the tag it means there, and anything else as Parse reads it. "" is the zero Tag and true.

func RefineFromTitle

func RefineFromTitle(t Tag, title string) Tag

RefineFromTitle adds to t what an embedded track's title states and t lacks: a script for a Chinese, Cantonese or Serbian tag stating none, a region for a Portuguese, Spanish or French tag stating none. A title naming two results refines nothing, and the language never changes. Bounding the title is the caller's.

func (Tag) MarshalText

func (t Tag) MarshalText() ([]byte, error)

MarshalText is the canonical text, empty for the zero Tag.

func (Tag) String

func (t Tag) String() string

String is the canonical text, "" for the zero Tag; Parse is its inverse.

func (*Tag) UnmarshalText

func (t *Tag) UnmarshalText(b []byte) error

UnmarshalText decodes wire and config input strictly: empty text is the zero Tag, and any text other than a tag's canonical text is refused, naming the canonical spelling when it parses. Stored state decodes through Parse.

type Target

type Target struct {
	Tag      Tag
	Variants []Variant
}

Target is one subtitle language a configuration asks for, with the variants wanted in it.

type Variant

type Variant uint8

Variant is what a target asks for. Zero is invalid.

const (
	Standard Variant = iota + 1
	HI
	Forced
)

Standard asks for full subtitles, HI for subtitles for the deaf and hard of hearing, Forced for only the lines in another language or on screen.

func (Variant) Flags

func (v Variant) Flags() Flags

Flags is the flags that serve v with nothing extra, which rank first among the files serving it; the zero Flags for an invalid Variant.

func (Variant) MarshalText

func (v Variant) MarshalText() ([]byte, error)

MarshalText is standard, hi or forced.

func (*Variant) UnmarshalText

func (v *Variant) UnmarshalText(b []byte) error

UnmarshalText refuses every spelling but MarshalText's.

type WirePair

type WirePair struct {
	Wire string
	Tag  Tag
}

WirePair is one provider language label and the tag it means.

Jump to

Keyboard shortcuts

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