i18n

package
v0.0.0-...-60b8695 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package i18n helps with internationalization of registered messages.

Example
package main

import (
	"fmt"

	"github.com/aureliar8/ttn-fork/pkg/i18n"
)

func main() {
	i18n.Define("welcome_message", "Welcome, {name}!")

	fmt.Println(i18n.Format("welcome_message", "en", map[string]interface{}{"name": "Alice"}))

}
Output:

Welcome, Alice!

Index

Examples

Constants

This section is empty.

Variables

Global registry.

Functions

func Format

func Format(id, language string, data map[string]interface{}) (msg string)

Format a message from the global registry in the given language.

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

func (m *MessageDescriptor) Format(language string, data map[string]interface{}) (msg string)

Format a message descriptor in the given language.

func (*MessageDescriptor) Load

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 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

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

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.

Jump to

Keyboard shortcuts

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