Documentation
¶
Overview ¶
Package i18n is the runtime that code generated by kanna-i18n calls, and what renders its messages.
kanna-i18n compiles a directory of locale files into a package of typed constructors, each returning a Message, together with the translations as Catalogs. Catalog, Entry, Plural, Template, and Segment are what it writes those as, in literals, and CountParam the name it gives the count of a plural message: a program need build none of them. Nothing is parsed at run time: what is left here is what depends on run-time values, which is choosing the language, picking the CLDR plural form a count needs, and formatting numbers the way the language writes them.
The generator itself is the command in cmd/kanna-i18n.
Index ¶
- Constants
- func ErrorMessages[P Problem](l Localizer, problems []P) map[string][]string
- func FullMessages[P Problem](l Localizer, problems []P) []string
- func IsBuiltin(key string) bool
- type Arg
- type Bundle
- type Catalog
- type Entry
- type Localizer
- type Message
- type Plural
- type Problem
- type Segment
- type Template
Constants ¶
const CountParam = "count"
CountParam is the argument whose value, an int, picks the form of a plural message. It is exported for kanna-i18n, which names the count of every plural constructor so.
Variables ¶
This section is empty.
Functions ¶
func ErrorMessages ¶ added in v0.0.4
ErrorMessages renders problems as Rails renders errors.messages: by path, each the message keyed errors.messages.<code>, in the order they come. It suits an API response, which shows each message beside its field.
func FullMessages ¶ added in v0.0.4
FullMessages renders problems as Rails renders errors.full_messages: each a sentence naming its field, put together by errors.format, such as "Title can't be blank" or "タイトルを入力してください". A problem at base reads as its message alone.
The field is named by errors.attributes: the path with any index dropped, address.zip or tags, then its last part, zip, and failing both, the last part written out, Completed at for completed_at. A confirmation's attribute, the other field it names, is named the same way.
Types ¶
type Arg ¶
Arg is the value of the placeholder Name.
A string or an int is rendered as it is, and a float64 in the number format of the language rendering it, under CLDR's default pattern, which keeps three fraction digits at most: that covers everything a generated constructor passes. Any other value is rendered by fmt.
type Bundle ¶
type Bundle struct {
// contains filtered or unexported fields
}
Bundle holds the catalogs of every language a package of messages was generated from. It never changes once built, so it and the Localizers it hands out can be used from any number of goroutines.
The zero Bundle has no languages, and its Localizers render every message as its key.
func NewBundle ¶
NewBundle builds a Bundle from def, the catalog of the default language, and rest, those of the other languages. It copies what the catalogs hold, so a change to one afterwards changes nothing the Bundle renders.
It panics on a language that is not a valid tag and on two catalogs of one language. Generated code passes neither, so meeting one means a Bundle put together by hand, and failing at once shows the mistake where it was made.
func (Bundle) Localizer ¶
Localizer returns a Localizer for the language of b closest to langs.
Each of langs is a language tag, such as "ja" or "pt-BR", or an Accept-Language value, such as "fr, ja;q=0.8". They are tried in order until one is close to a language of b, as CLDR judges closeness: en-US is served by en, and nb by da when b has Danish but no Norwegian. So a language the user picked can come before the ones the browser asks for. A value that does not parse is skipped as a whole, one bad entry and all, and when nothing matches, the default language is used.
type Catalog ¶
type Catalog struct {
// Lang is the language as a BCP 47 tag, such as "en" or "pt-BR".
Lang string
Entries map[string]Entry
}
Catalog holds the messages of one language. It and what it is made of, Entry, Plural, Template, and Segment, are what kanna-i18n compiles a locale file into, written into the generated code as literals; the generator has checked them against every other language already, so nothing here checks them again.
type Entry ¶
Entry is one message of one language: Single holds a message with one form, and Plural, when set, one whose form depends on its count.
type Localizer ¶
type Localizer struct {
// contains filtered or unexported fields
}
Localizer renders messages in the language a Bundle chose for it. The zero Localizer renders every message as its key.
func (Localizer) Localize ¶
Localize renders m. A message missing from the language of l is looked up in its parent languages and then in the default language, and is rendered under the plural rules and number format of the language it is found in.
Localize never fails: a message no language has renders as its key.
type Message ¶
Message is a message to render, with the arguments its placeholders take. The constructors kanna-i18n generates return one, so that code that does not know the reader's language can hand it to code that does.
func Coded ¶ added in v0.0.3
Coded returns the message keyed prefix.code, such as "errors.messages.too_long", with params as its arguments. It is how an error reported as a code and params, as kanna-validate and kanna-value report one, gets its text from the locale files; FullMessages and ErrorMessages use it for every problem they render. An empty prefix leaves the key code alone.
The arguments come sorted by name, so that one map always makes the same Message. Each renders by its type, as Arg says: the kind a locale file gives a placeholder types only the generated constructor, which Coded bypasses.
type Plural ¶
type Plural struct {
Zero, One, Two, Few, Many, Other Template
}
Plural holds the forms of a plural message, one for each CLDR plural category the language tells apart. Other is always there, and a count whose category has no form, left nil, renders with it. Zero, when set, is the form of a count of 0 in every language, English and others whose rules have no zero included.
type Problem ¶ added in v0.0.4
Problem is a broken rule as a validation reports it: the path of the field it broke on, such as "address.zip" or "tags[1]", or "base" for the value as a whole, and the code and params naming the rule. validate.FieldError of go-kanna/validate is one, and so is anything with these methods: this package imports no validation package.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
kanna-i18n
command
Command kanna-i18n compiles a directory of locale files into a package of typed message constructors, with the translations themselves compiled in.
|
Command kanna-i18n compiles a directory of locale files into a package of typed message constructors, with the translations themselves compiled in. |
|
internal
|
|
|
gen
Package gen compiles a directory of locale files into a Go package of typed message constructors, along with the translations themselves.
|
Package gen compiles a directory of locale files into a Go package of typed message constructors, along with the translations themselves. |