Documentation
¶
Index ¶
- Constants
- func Close(catalog MessageCatalog) error
- func Reload(catalog MessageCatalog) error
- func ResetStats(catalog MessageCatalog) error
- type Config
- type ContextKey
- type DefaultError
- type DefaultMessageCatalog
- func (dmc *DefaultMessageCatalog) Close()
- func (dmc *DefaultMessageCatalog) GetErrorWithCtx(ctx context.Context, msgKey string, params Params) error
- func (dmc *DefaultMessageCatalog) GetMessageWithCtx(ctx context.Context, msgKey string, params Params) *Message
- func (dmc *DefaultMessageCatalog) LoadMessages(lang string, messages []RawMessage) error
- func (dmc *DefaultMessageCatalog) Reload() error
- func (dmc *DefaultMessageCatalog) ResetStats()
- func (dmc *DefaultMessageCatalog) SnapshotStats() MessageCatalogStats
- func (dmc *DefaultMessageCatalog) WrapErrorWithCtx(ctx context.Context, err error, msgKey string, params Params) error
- type Error
- type Message
- type MessageCatalog
- type MessageCatalogStats
- type MessageDef
- type Messages
- type Observer
- type OptionalCode
- type OptionalGroup
- type Params
- type RawMessage
Constants ¶
const ( // RuntimeKeyPrefix is required for message keys loaded via LoadMessages (e.g. "sys."). RuntimeKeyPrefix = "sys." CodeMissingMessage = "msgcat.missing_message" CodeMissingLanguage = "msgcat.missing_language" )
const MessageCatalogNotFound = "Unexpected error in message catalog, language [%s] not found. %s"
Variables ¶
This section is empty.
Functions ¶
func Close ¶
func Close(catalog MessageCatalog) error
func Reload ¶
func Reload(catalog MessageCatalog) error
func ResetStats ¶
func ResetStats(catalog MessageCatalog) error
Types ¶
type ContextKey ¶
type ContextKey string
type DefaultError ¶
type DefaultError struct {
// contains filtered or unexported fields
}
func (DefaultError) Error ¶
func (ce DefaultError) Error() string
func (*DefaultError) ErrorCode ¶
func (ce *DefaultError) ErrorCode() string
func (*DefaultError) ErrorKey ¶ added in v1.1.0
func (ce *DefaultError) ErrorKey() string
func (*DefaultError) GetLongMessage ¶
func (ce *DefaultError) GetLongMessage() string
func (*DefaultError) GetShortMessage ¶
func (ce *DefaultError) GetShortMessage() string
func (*DefaultError) Unwrap ¶
func (ce *DefaultError) Unwrap() error
type DefaultMessageCatalog ¶
type DefaultMessageCatalog struct {
// contains filtered or unexported fields
}
func (*DefaultMessageCatalog) Close ¶
func (dmc *DefaultMessageCatalog) Close()
func (*DefaultMessageCatalog) GetErrorWithCtx ¶
func (*DefaultMessageCatalog) GetMessageWithCtx ¶
func (*DefaultMessageCatalog) LoadMessages ¶
func (dmc *DefaultMessageCatalog) LoadMessages(lang string, messages []RawMessage) error
func (*DefaultMessageCatalog) Reload ¶
func (dmc *DefaultMessageCatalog) Reload() error
func (*DefaultMessageCatalog) ResetStats ¶
func (dmc *DefaultMessageCatalog) ResetStats()
func (*DefaultMessageCatalog) SnapshotStats ¶
func (dmc *DefaultMessageCatalog) SnapshotStats() MessageCatalogStats
func (*DefaultMessageCatalog) WrapErrorWithCtx ¶
type Error ¶
type Error interface {
Error() string
Unwrap() error
ErrorCode() string // Optional; user-defined. Empty when not set. Use ErrorKey() when empty.
ErrorKey() string // Message key (e.g. "error.not_found"); use as identifier when ErrorCode() is empty.
GetShortMessage() string
GetLongMessage() string
}
Error is the catalog error type. When ErrorCode() is empty, use ErrorKey() as the API identifier.
type Message ¶
type Message struct {
LongText string
ShortText string
Code string // Optional; user-defined (e.g. "404", "ERR_001"). Empty when not set. Use Key when empty.
Key string // Message key (e.g. "greeting.hello"); set when found or when missing (requested key).
}
Message is the resolved message for a request. Key is always the message key used for lookup. Code is optional (from catalog); when empty, use Key as the API identifier (e.g. in JSON responses).
type MessageCatalog ¶
type MessageCatalog interface {
// LoadMessages adds or replaces messages for a language. Keys must have prefix RuntimeKeyPrefix (e.g. "sys.").
LoadMessages(lang string, messages []RawMessage) error
GetMessageWithCtx(ctx context.Context, msgKey string, params Params) *Message
WrapErrorWithCtx(ctx context.Context, err error, msgKey string, params Params) error
GetErrorWithCtx(ctx context.Context, msgKey string, params Params) error
}
func NewMessageCatalog ¶
func NewMessageCatalog(cfg Config) (MessageCatalog, error)
type MessageCatalogStats ¶
type MessageCatalogStats struct {
LanguageFallbacks map[string]int
MissingLanguages map[string]int
MissingMessages map[string]int
TemplateIssues map[string]int
DroppedEvents map[string]int
LastReloadAt time.Time
}
func SnapshotStats ¶
func SnapshotStats(catalog MessageCatalog) (MessageCatalogStats, error)
type MessageDef ¶ added in v1.1.0
type MessageDef struct {
Key string // Message key (e.g. "person.cats"). Required.
Short string // Short template (or use ShortForms for CLDR).
Long string // Long template (or use LongForms for CLDR).
ShortForms map[string]string `yaml:"short_forms,omitempty"` // Optional CLDR forms: zero, one, two, few, many, other.
LongForms map[string]string `yaml:"long_forms,omitempty"`
PluralParam string `yaml:"plural_param,omitempty"` // Param name for plural selection (default "count").
Code OptionalCode `yaml:"code,omitempty"`
}
MessageDef defines a message that can be extracted to YAML via the msgcat CLI (extract -source). Use in Go for "messages in Go" workflow; at runtime the catalog loads from YAML. Key is required.
type Messages ¶
type Messages struct {
Group OptionalGroup `yaml:"group,omitempty"` // Optional; int or string (e.g. group: 0 or group: "api"). Catalog does not interpret it.
Default RawMessage `yaml:"default"`
Set map[string]RawMessage `yaml:"set"`
}
type OptionalCode ¶ added in v1.1.0
type OptionalCode string
OptionalCode is the optional "code" field for catalog entries. Use it when your project already has error or message codes (HTTP statuses, legacy numbers, string ids like "ERR_NOT_FOUND") and you want to store that value in the catalog and return it from Message.Code and ErrorCode(). It can be any value; uniqueness is not enforced. YAML accepts int or string (e.g. code: 404 or code: "ERR_NOT_FOUND"). In Go use CodeInt or CodeString when building RawMessage.
func CodeInt ¶ added in v1.1.0
func CodeInt(i int) OptionalCode
CodeInt returns an OptionalCode from an int (e.g. HTTP status 503). Use when building RawMessage in code.
func CodeString ¶ added in v1.1.0
func CodeString(s string) OptionalCode
CodeString returns an OptionalCode from a string (e.g. "ERR_NOT_FOUND"). Use when building RawMessage in code.
func (*OptionalCode) UnmarshalYAML ¶ added in v1.1.0
func (c *OptionalCode) UnmarshalYAML(unmarshal func(interface{}) error) error
UnmarshalYAML allows code to be given as int or string in YAML.
type OptionalGroup ¶ added in v1.1.0
type OptionalGroup string
OptionalGroup is the optional "group" field for message files. Use it to tag a file (or later, an entry) with a group that can be an integer or a string (e.g. group: 0 or group: "api") for organization or tooling. The catalog does not interpret group; it is only stored. YAML accepts int or string.
func (OptionalGroup) MarshalYAML ¶ added in v1.1.0
func (g OptionalGroup) MarshalYAML() (interface{}, error)
MarshalYAML emits int when the value is numeric, otherwise string, for readable round-trip (group: 0 vs group: "api").
func (*OptionalGroup) UnmarshalYAML ¶ added in v1.1.0
func (g *OptionalGroup) UnmarshalYAML(unmarshal func(interface{}) error) error
UnmarshalYAML allows group to be given as int or string in YAML.
type Params ¶ added in v1.1.0
type Params map[string]interface{}
Params is the type for named template parameters. Use msgcat.Params{"name": value}.
type RawMessage ¶
type RawMessage struct {
LongTpl string `yaml:"long"`
ShortTpl string `yaml:"short"`
Code OptionalCode `yaml:"code"` // Optional. In YAML: code: 404 or code: "ERR_NOT_FOUND". Use Key when empty.
ShortForms map[string]string `yaml:"short_forms,omitempty"` // Optional CLDR forms: zero, one, two, few, many, other.
LongForms map[string]string `yaml:"long_forms,omitempty"`
PluralParam string `yaml:"plural_param,omitempty"` // Param name for plural selection (default "count").
// Key is set when loading via LoadMessages (runtime); YAML uses the map key as the message key.
Key string `yaml:"-"`
}
RawMessage is one catalog entry. Code is optional and can be any value the user wants (e.g. "404", "ERR_NOT_FOUND"); it is for projects that map their own error/message codes into the catalog. Uniqueness is not enforced. Optional ShortForms/LongForms enable CLDR plural forms (zero, one, two, few, many, other); when set, the plural_param (default "count") is used to select the form. See docs/CLDR_AND_GO_MESSAGES_PLAN.md.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
msgcat
command
|
|
|
examples
|
|
|
basic
command
Basic demonstrates: NewMessageCatalog, GetMessageWithCtx (nil params and with Params), GetErrorWithCtx, WrapErrorWithCtx, and the msgcat.Error interface.
|
Basic demonstrates: NewMessageCatalog, GetMessageWithCtx (nil params and with Params), GetErrorWithCtx, WrapErrorWithCtx, and the msgcat.Error interface. |
|
cldr_plural
command
Cldr_plural demonstrates CLDR plural forms (short_forms/long_forms) in the message catalog.
|
Cldr_plural demonstrates CLDR plural forms (short_forms/long_forms) in the message catalog. |
|
http
command
|
|
|
load_messages
command
Load_messages demonstrates runtime loading via LoadMessages: keys must have the sys.
|
Load_messages demonstrates runtime loading via LoadMessages: keys must have the sys. |
|
metrics
command
|
|
|
msgdef
command
Msgdef demonstrates defining messages in Go with msgcat.MessageDef.
|
Msgdef demonstrates defining messages in Go with msgcat.MessageDef. |
|
reload
command
Reload demonstrates: Reload(catalog) re-reads YAML from disk.
|
Reload demonstrates: Reload(catalog) re-reads YAML from disk. |
|
stats
command
Stats demonstrates SnapshotStats, ResetStats, and stat keys (LanguageFallbacks, MissingLanguages, MissingMessages, TemplateIssues, DroppedEvents, LastReloadAt).
|
Stats demonstrates SnapshotStats, ResetStats, and stat keys (LanguageFallbacks, MissingLanguages, MissingMessages, TemplateIssues, DroppedEvents, LastReloadAt). |
|
strict
command
Strict demonstrates StrictTemplates: when a template parameter is missing, the placeholder is replaced with <missing:paramName> and observer/stats record the issue.
|
Strict demonstrates StrictTemplates: when a template parameter is missing, the placeholder is replaced with <missing:paramName> and observer/stats record the issue. |
|
internal
|
|
|
plural
Package plural provides CLDR plural form selection for a given language and count.
|
Package plural provides CLDR plural form selection for a given language and count. |
|
mock
Package mock_msgcat is a generated GoMock package.
|
Package mock_msgcat is a generated GoMock package. |