lang

package
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 22 Imported by: ? 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.

Documentation

Overview

Package lang says icebath's texts in a person's language: messages named by their English and said where they reach the person, the catalogs that hold a language's words for them, the plural rules, number, date and list forms of each language, and which way it is written. It keeps no language of its own: each request, window and chat carries the one it speaks in its context

Index

Constants

View Source
const (
	ContextOpen  = " ⟨"
	ContextClose = "⟩"
	PluralMark   = " ⟨plural⟩ "
)

Marks a key is read back by, which no English, form or context of a site holds: the opening and closing marks of a context at the end of a key, and the mark between the one and the other form of a text whose words follow a count. A text of any number of lines is never taken for a plural, as only the mark makes one

Variables

View Source
var English = New(Known{ID: "en", Endonym: "English", English: "English"})

English as the sites write it, with no catalog

Functions

func Agree

func Agree(english, translation string, form plural.Form, isPlural bool) bool

Whether a translation takes the arguments of its English with the same verbs: every one of them for a text and for the other form of a plural, a part of them for another form of a plural, which may leave the count out. A percent sign that starts no placeholder is free in both: a language writes a percentage its own way, such as German's "70 %"

func Errorf

func Errorf(english string, args ...any) error

An error a person reads, a whole sentence that says what happened and what to do, written as fmt.Errorf writes one: each %w wraps the error given for it, which errors.Is and errors.As then find

func Fallbacks

func Fallbacks(id string) []string

Ids the words of the language are looked for under, the language first and then each shorter tag it falls back to; Traditional Chinese never falls to Simplified

func FormName

func FormName(form plural.Form) string

Name a catalog writes the form by

func FormOf

func FormOf(name string) (plural.Form, bool)

Form a catalog writes by the name, false for a name of none

func Key

func Key(english, one, context string, plural, plain bool) string

Key of a text with the English, the one form of a plural, the context, whether it is a plural and whether it is data

func Marked

func Marked(text string) bool

Whether a text holds a character the marks of a key are made of, which no English, form or context of a site may hold, as a key made of it would read back as another

func Mirror

func Mirror(r rune) rune

The character a line shown right to left takes in place of the one given: its counterpart for one that points a way or opens a side, the same character for any other

func Needed

func Needed(id string) []plural.Form

Forms the words of the language take by its CLDR cardinal rules, in the order of CLDR: those of the whole counts, and other always, the form every plural falls back to and, in a language whose whole counts all take another (Russian, Ukrainian), the form of a count with a fraction, such as the 1.5 seconds of a length of time. What a plural entry of its catalog must hold

func Normalize

func Normalize(value string) string

Id as it is compared: lower case, trimmed, underscores as dashes, without the encoding or the modifier a locale names

func Parts

func Parts(key string) (english, one, context string, plural bool)

What a key stands for, as Key made it: its English, the other form of a plural; the one form and whether it is a plural; the context of a text said with one. A key with no mark is its English whatever it holds, line breaks among them

func Resolve

func Resolve(value string, have func(id string) bool) (string, bool)

The language a value stands for among those have holds: an id, a name of a language, a regional tag or a locale such as pt_BR.UTF-8, a regional tag standing for its base and a tag of Traditional Chinese for zh-hant. False when it stands for none of them

func Takes

func Takes(format string) int

Count of the arguments a format takes: the highest that any of its placeholders names, for its verb, its width or its precision; none for a format without a placeholder

func Valid

func Valid(value string) bool

Whether the value can be the id of a language: a tag the language data of the program reads

func Visual

func Visual(line []rune) (order []int, backward []bool)

Order a line of a right to left interface is shown in on a terminal that reorders nothing: for each place on the screen from the left, the index of the character of the line that goes there. The line is read as a paragraph of a right to left language by the Unicode bidirectional algorithm, so runs of right to left letters read from the right, words and numbers of left to right scripts keep their order, and what stood first stands rightmost. Each character is one rune standing for one cell, wide or not. Beside the order, whether the character at each place is read right to left, which takes the mirrored form of a character that points a way

func With

func With(ctx context.Context, l *Language) context.Context

The context carrying the language its work speaks to a person in

Types

type Arg

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

A value a language says its own way, given to a message as one of its arguments and written there with %s: a number, a length of time, a size, a date, a list, a switch's state, a place in an order, or registry copy inside a sentence

func Ago

func Ago(d time.Duration) Arg

How long ago something was, rounded down to its largest whole unit: 3 minutes ago

func Clock

func Clock(t time.Time) Arg

A time of day

func Copy

func Copy(name, english string) Arg

Registry copy inside a sentence, such as the title of a setting or of a chat app: said as Language.Copy says it

func Date

func Date(t time.Time) Arg

A date: the day, its month by name and the year

func Day

func Day(t time.Time) Arg

The day of the week by name

func Either

func Either(items ...string) Arg

Items as the language lists them, the last joined by its word for or: a, b or c

func List

func List(items ...string) Arg

Items as the language lists them, the last joined by its word for and: a, b and c

func ListMore

func ListMore(parts int, items ...string) Arg

Items in at most the parts given: all of them when they fit, else the first ones and how many more, such as a, b and 6 more

func Number

func Number(x any) Arg

A number with the grouping, decimal mark and digits of the language, up to three decimals

func OnOff

func OnOff(on bool) Arg

State of a switch: On or Off

func Ordinal

func Ordinal(n int) Arg

A place in an order as a number with its ending: 1st, 2nd, 3rd, 4th

func Size

func Size(bytes float64) Arg

A count of bytes in the largest unit that fits: 512 bytes, 1.5 KB, 5 MB

func Span

func Span(d time.Duration) Arg

A length of time in the largest unit that divides it, in words: 1 hour, 90 minutes, 1.5 seconds

func When

func When(t time.Time) Arg

A date and its time of day

func (Arg) MarshalJSON

func (a Arg) MarshalJSON() ([]byte, error)

func (Arg) String

func (a Arg) String() string

The value in English

func (*Arg) UnmarshalJSON

func (a *Arg) UnmarshalJSON(data []byte) error

type Catalog

type Catalog map[string]Entry

Words of a language by the key of each text

func Parse

func Parse(data []byte) (Catalog, error)

Catalog of a YAML file: one mapping of keys to the words of each, or to a mapping of the forms of a plural to their words. A key whose words are empty is a text not translated yet, and is left out

type Entry

type Entry struct {
	Text  string
	Forms map[plural.Form]string
}

One entry of a catalog: the language's words for a text, or the forms of words that follow a count

func (Entry) Form

func (e Entry) Form(form plural.Form) string

Words of the form, those of other when the entry has no such form; the words of an entry that is no plural

func (Entry) Named

func (e Entry) Named() []plural.Form

Forms of a plural entry in the order of CLDR

func (Entry) Plural

func (e Entry) Plural() bool

Whether the entry's words follow a count

type Error

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

An error a person reads: a message said in their language where it reaches them, and the errors its %w verbs wrap behind it. Its Error is the English

func (*Error) Error

func (e *Error) Error() string

The English of the error

func (*Error) Text

func (e *Error) Text() Text

The message of the error, which a person's language says

func (*Error) Unwrap

func (e *Error) Unwrap() []error

The errors the message wraps

type Known

type Known struct {
	ID      string
	Endonym string
	English string
	RTL     bool
}

A language: its id, what it calls itself, what English calls it (which the model is told) and whether it is written right to left

func Bundled

func Bundled() []Known

The languages icebath ships catalogs for, English first: English is said at the sites, the sixteen others from bundled catalogs

func Describe

func Describe(id string) (Known, bool)

The language of the id, as a bundled one names it, else what the language data of the program knows of it; false for an id that names no language

type Language

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

A language as a request, a window or a chat speaks it: what it is, and its catalogs in the order their words win

func New

func New(known Known, layers ...Catalog) *Language

The language with its catalogs, the first layer's words winning; what no layer holds is said in the English of its site

func Of

func Of(ctx context.Context) *Language

Language the work of the context speaks to a person in, English when it carries none

func Spoken

func Spoken(ctx context.Context) (*Language, bool)

Language the context carries, false when it carries none, such as the work of a scheduled job no person asked for

func (*Language) Ago

func (l *Language) Ago(d time.Duration) string

How long ago something was, rounded down to its largest whole unit: 45 seconds ago, 3 minutes ago, 2 days ago

func (*Language) As

func (l *Language) As(context, english string, args ...any) string

A message whose English is said two ways, said in the language at once

func (*Language) Capital

func (l *Language) Capital(text string) string

The text with its first letter in capitals by the casing of the language

func (*Language) Clock

func (l *Language) Clock(t time.Time) string

A time of day as the language tells it, on a clock of 24 hours or of 12: the language's words for 12 on its clock say which

func (*Language) Copy

func (l *Language) Copy(name, english string) string

Registry copy in the language, such as the title of a setting: the words of its structural name where a layer has them, then those of its English, then, for copy made from data, the words of the template its English was made from with the data in its holes (a structural name with * for the data's part saying it differently there), else the English

func (*Language) Date

func (l *Language) Date(t time.Time) string

A date: the day, its month by name and the year, in the order of the language

func (*Language) Day

func (l *Language) Day(t time.Time) string

The day of the week by name

func (*Language) DayMonth

func (l *Language) DayMonth(t time.Time) string

A day of a month with the month's short name and no year, as an axis of days marks it: 2 Nov

func (*Language) Either

func (l *Language) Either(items ...string) string

Items listed, the last joined by the language's word for or: a, b or c

func (*Language) Endonym

func (l *Language) Endonym() string

What the language calls itself

func (*Language) EnglishName

func (l *Language) EnglishName() string

What English calls the language

func (*Language) Error

func (l *Language) Error(err error) string

The error as a person reads it in the language: every message of its chain said in it, what is no message as it is written

func (*Language) Has

func (l *Language) Has(key string) bool

Whether a layer of the language holds words under the key

func (*Language) ID

func (l *Language) ID() string

Id of the language, such as de or zh-hant

func (*Language) Known

func (l *Language) Known() Known

The language as a person picks it

func (*Language) List

func (l *Language) List(items ...string) string

Items listed, the last joined by the language's word for and: a, b and c

func (*Language) ListMore

func (l *Language) ListMore(parts int, items ...string) string

Items in at most the parts given: all of them when they fit, else the first ones and how many more

func (*Language) MonthYear

func (l *Language) MonthYear(t time.Time) string

A month by its short name with its year, as an axis of months marks it: Nov 2026

func (*Language) N

func (l *Language) N(count any, one, other string, args ...any) string

A message whose words follow a count, said in the language at once

func (*Language) Number

func (l *Language) Number(x any) string

A number with the grouping, decimal mark and digits of the language, up to three decimals

func (*Language) OnOff

func (l *Language) OnOff(on bool) string

State of a switch: On or Off

func (*Language) Ordinal

func (l *Language) Ordinal(n int) string

A place in an order as a number with the ending the language's ordinal rules give it

func (*Language) RightToLeft

func (l *Language) RightToLeft() bool

Whether the language is written right to left

func (*Language) Say

func (l *Language) Say(t Text) string

The text in the language

func (*Language) Size

func (l *Language) Size(n float64) string

A count of bytes in the largest unit that fits, the units of 1024 for a count they divide and of 1000 for any other

func (*Language) Span

func (l *Language) Span(d time.Duration) string

A length of time in the largest unit that divides it, in words: 1 hour, 90 minutes, 1.5 seconds

func (*Language) T

func (l *Language) T(english string, args ...any) string

A message said in the language at once, for a text made for the person who asked

func (*Language) Tag

func (l *Language) Tag() language.Tag

Tag of the language for the language data of the program

func (*Language) Upper

func (l *Language) Upper(text string) string

The text in capitals by the casing of the language, such as Turkish keeping the dot of its i

func (*Language) When

func (l *Language) When(t time.Time) string

A date and its time of day

func (*Language) Year

func (l *Language) Year(t time.Time) string

The year in the digits of the language, as an axis of years marks it

type Text

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

A text icebath says to a person: the English it is written in at its site and its arguments, said in a person's language where it reaches them. Its English is its name: a catalog of a language holds the language's words under it. The zero Text says nothing

func As

func As(context, english string, args ...any) Text

A message whose English is said two ways, such as Off as a state and as a command: a lower case word chosen at the site tells this way apart from the others, so a language says each its own way

func N

func N(count any, one, other string, args ...any) Text

A message whose words follow a count, such as one session and three sessions: the count picks the form by the plural rules of the language it is said in, one and other being the English forms. The count is not an argument unless it is given among the args too

func Plain

func Plain(text string) Text

Data that reads the same in every language, such as a path or a name a person gave, where a text is asked for

func T

func T(english string, args ...any) Text

A message: its English as a format of fmt verbs (%s, %d, %[2]s) and its arguments. An argument that is a Text, an Arg or an error is said in the language the message is said in, written with %s or %v; any other is data and is written as fmt writes it

func (Text) Empty

func (t Text) Empty() bool

Whether the text says nothing

func (Text) Key

func (t Text) Key() string

Key a catalog keeps the text's words under: its English; the English one form, the plural mark and the other form of a text whose words follow a count; the English and its context in the marks of a context for a text said with one. Empty for data. Parts reads it back

func (Text) MarshalJSON

func (t Text) MarshalJSON() ([]byte, error)

func (Text) String

func (t Text) String() string

The text in English, as its site writes it, every argument said in English too

func (*Text) UnmarshalJSON

func (t *Text) UnmarshalJSON(data []byte) error

type Verb

type Verb struct {
	Arg  int
	Verb string
}

A placeholder of a format: the argument it takes, counted from 1, and its verb with its flags, width and precision as fmt reads them, written one way whatever way the format writes them: the flags in the order # 0 + -, the width, a point and the precision, the verb; a width or precision read from an argument as * with that argument in brackets

func Verbs

func Verbs(format string) []Verb

Placeholders of a format in the order they stand, each with the argument it takes as fmt counts them

Jump to

Keyboard shortcuts

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