Documentation ¶
Overview ¶
Package i18n helps with internationalization of registered messages.
Example ¶
package main import ( "fmt" "go.thethings.network/lorawan-stack/v3/pkg/i18n" ) func main() { i18n.Define("welcome_message", "Welcome, {name}!") fmt.Println(i18n.Format("welcome_message", "en", map[string]any{"name": "Alice"})) }
Output: Welcome, Alice!
Index ¶
- func Format(id, language string, data map[string]any) (msg string)
- type MessageDescriptor
- func (m *MessageDescriptor) Format(language string, data map[string]any) (msg string)
- func (m *MessageDescriptor) Load() error
- func (m *MessageDescriptor) SetSource(skip uint)
- func (m *MessageDescriptor) String() string
- func (m *MessageDescriptor) Touched() bool
- func (m *MessageDescriptor) Updated() bool
- type MessageDescriptorMap
- func (m MessageDescriptorMap) Cleanup() (deleted []string)
- func (m MessageDescriptorMap) Define(id, message string) *MessageDescriptor
- func (m MessageDescriptorMap) Get(id string) *MessageDescriptor
- func (m MessageDescriptorMap) MarshalJSON() ([]byte, error)
- func (m MessageDescriptorMap) Merge(other MessageDescriptorMap)
- func (m MessageDescriptorMap) Updated() (updated []string)
- func (m MessageDescriptorMap) WriteFile(filename string) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type MessageDescriptor ¶
type MessageDescriptor struct { Translations map[string]string `json:"translations,omitempty"` Description struct { Package string `json:"package,omitempty"` File string `json:"file,omitempty"` } `json:"description,omitempty"` // contains filtered or unexported fields }
MessageDescriptor describes a translatable message.
func Define ¶
func Define(id, message string) *MessageDescriptor
Define a message in the global registry.
func Get ¶
func Get(id string) *MessageDescriptor
Get returns the MessageDescriptor of a specific message from the global registry.
func (*MessageDescriptor) Format ¶ added in v3.9.2
func (m *MessageDescriptor) Format(language string, data map[string]any) (msg string)
Format a message descriptor in the given language.
func (*MessageDescriptor) Load ¶ added in v3.9.2
func (m *MessageDescriptor) Load() error
Load the messages
func (*MessageDescriptor) SetSource ¶
func (m *MessageDescriptor) SetSource(skip uint)
SetSource sets the source package and file name of the message descriptor. The argument skip is the number of stack frames to ascend, with 0 identifying the caller of SetSource.
func (*MessageDescriptor) String ¶
func (m *MessageDescriptor) String() string
func (*MessageDescriptor) Touched ¶
func (m *MessageDescriptor) Touched() bool
Touched returns whether the descriptor was touched (i.e. it is still used).
func (*MessageDescriptor) Updated ¶
func (m *MessageDescriptor) Updated() bool
Updated returns whether the descriptor was updated.
type MessageDescriptorMap ¶
type MessageDescriptorMap map[string]*MessageDescriptor
MessageDescriptorMap is a map of message descriptors.
func CloneGlobal ¶ added in v3.21.0
func CloneGlobal() MessageDescriptorMap
CloneGlobal returns a shallow clone of the global message descriptor registry.
func ReadFile ¶
func ReadFile(filename string) (MessageDescriptorMap, error)
ReadFile reads the descriptors from a file.
func (MessageDescriptorMap) Cleanup ¶
func (m MessageDescriptorMap) Cleanup() (deleted []string)
Cleanup removes unused message descriptors.
func (MessageDescriptorMap) Define ¶
func (m MessageDescriptorMap) Define(id, message string) *MessageDescriptor
Define a message.
func (MessageDescriptorMap) Get ¶
func (m MessageDescriptorMap) Get(id string) *MessageDescriptor
Get returns the MessageDescriptor of a specific message.
func (MessageDescriptorMap) MarshalJSON ¶
func (m MessageDescriptorMap) MarshalJSON() ([]byte, error)
MarshalJSON marshals the descriptors to JSON.
func (MessageDescriptorMap) Merge ¶
func (m MessageDescriptorMap) Merge(other MessageDescriptorMap)
Merge messages from the given descriptor map into the current registry.
func (MessageDescriptorMap) Updated ¶
func (m MessageDescriptorMap) Updated() (updated []string)
Updated returns updated message descriptors.
func (MessageDescriptorMap) WriteFile ¶
func (m MessageDescriptorMap) WriteFile(filename string) error
WriteFile writes the descriptors to a file.