Documentation
¶
Overview ¶
Package i18ngen reads the catalogs of an app's messages, checks them, and writes their typed accessors: Go functions in the catalogs' directory, which embed and register them, and a TypeScript module that formats the same messages in pages with golet-runtime. `golet generate`, `golet dev`, `golet build` and `golet i18n check` use it, and so does internal/l10n for Golet's own strings.
Index ¶
- Constants
- func Fallbacks(c *Catalog, tag string) []string
- func Go(c *Catalog, pkg string) ([]byte, error)
- func Load(fsys fs.FS, opts Options) (*Catalog, Diagnostics, error)
- func MessagesJSON(f *File) []byte
- func PackageName(dir string) (string, error)
- func TS(c *Catalog) ([]byte, error)
- type Catalog
- type Diagnostic
- type Diagnostics
- type File
- type Key
- type Options
- type Param
Constants ¶
const GoFile = "catalog_gen.go"
GoFile is the file of the Go accessors, in the catalogs' directory.
const MessagesDir = "messages"
MessagesDir is the directory, in the catalogs', of the catalog of the source language without its metadata that the accessors of Golet's own strings embed (Options.Own).
Variables ¶
This section is empty.
Functions ¶
func Fallbacks ¶
Fallbacks returns the languages of the catalog a message in tag is looked up in: tag, its CLDR parents the catalog has, then the source language.
func Go ¶
Go returns the Go accessors of a catalog, the source of GoFile in package pkg: an embed of the catalogs, a function per key that says its message in the language the app shows (i18n.Locale), and In, whose methods say them in another language. An app's catalog is registered with package i18n as the package initializes; a library's is a i18n.NewLibraryCatalog.
func Load ¶
Load reads the catalogs at the root of fsys, "<tag>.json", and checks them. It fails only when it cannot read them; problems of their contents are its diagnostics, and a catalog with errors is still returned, with what could be read.
func MessagesJSON ¶
MessagesJSON returns the catalog of f without its metadata: its messages, in their order, as the accessors of Golet's own strings embed them (Options.Own).
func PackageName ¶
PackageName returns the name of the Go package in dir: that of its Go files other than GoFile and tests, else one made of the directory's name, as "locales".
func TS ¶
TS returns the TypeScript module of a catalog, which pages import: the messages of every language, which golet-runtime formats in the language the page shows (its locale()), with a typed function per key, as the Go accessors have.
The messages are in the module, not fetched: the frontend's build bundles them, so the first paint is in the right language without a round trip, also in a page served by a dev server or opened in a browser.
Types ¶
type Catalog ¶
type Catalog struct {
Source string
Namespace string
// Languages are the languages of the files: the source first, then
// the others sorted.
Languages []string
// Keys are the messages of the source language, in the order of its
// file.
Keys []*Key
// Files are the files by language.
Files map[string]*File
// Translations are the translations of Golet's own strings to the
// app's languages, which its accessors import: those of
// Options.Translations that suit a language of the catalogs best.
Translations []string
// Own is Options.Own.
Own bool
}
Catalog is the catalogs of the languages of an app or a library.
type Diagnostic ¶
type Diagnostic struct {
File string
Line int // 0 when it is the whole file's
Warning bool
Msg string
}
Diagnostic is a problem of a catalog.
func (Diagnostic) String ¶
func (d Diagnostic) String() string
type Diagnostics ¶
type Diagnostics []Diagnostic
Diagnostics are the problems of catalogs, in the order of their files.
func (Diagnostics) Err ¶
func (ds Diagnostics) Err() error
Err returns the errors as one error, or nil without any.
func (Diagnostics) Errors ¶
func (ds Diagnostics) Errors() int
Errors counts the problems that are not warnings.
func (Diagnostics) Warnings ¶
func (ds Diagnostics) Warnings() Diagnostics
Warnings returns the warnings.
type File ¶
type File struct {
Tag string
Name string // as diagnostics name it, "locales/de.json"
// Keys are the keys of its messages in the order they come, those of
// libraries ("golet.cancel") too.
Keys []string
Messages map[string]string
// Parsed holds the messages that parse.
Parsed map[string]*i18n.Message
Lines map[string]int // of each key, metadata too
}
File is the catalog of one language.
type Key ¶
type Key struct {
Name string
Message string
Description string
Params []Param
// GoName is the name of its Go function: Name, exported.
GoName string
}
Key is a message of the source language, which the accessors say.
type Options ¶
type Options struct {
// Dir names the directory of the catalogs in diagnostics, as
// "locales".
Dir string
// Source is the language the messages are written in first, whose
// file has every key and the metadata (default "en").
Source string
// Namespace is that of a library's catalog, as "golet" for Golet's own
// strings, "" for an app's.
Namespace string
// Libraries are the catalogs of libraries whose messages an app's
// keys "<namespace>.<key>" change, by namespace.
Libraries map[string]*Catalog
// Translations are the languages Golet's own strings are translated
// to, each in a package of github.com/HyperparameterSI/golet/backend/locales, which the
// accessors of an app's catalogs import for the app's languages.
Translations []string
// Own is set for Golet's own strings (internal/l10n), whose accessors
// embed messages/, the catalog of the source language without its
// metadata (MessagesJSON), and make the catalog through
// internal/i18nlink, which links no formatting of dates: every app
// carries them.
Own bool
}
Options say what catalogs to read.