i18n

package
v0.42.2 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2022 License: Apache-2.0 Imports: 3 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLangFromRequest

func GetLangFromRequest(c MessageCatalog, r *http.Request) language.Tag

GetLangFromRequest is a helper func to get the language tag based on the HTTP request and the constructed message catalog.

func GetMessage

func GetMessage(c MessageCatalog, ID string, tag language.Tag, v ...interface{}) string

GetMessage is a helper func to get the translated message based on the message ID and lang. If no matching message is found, it uses ID as the message itself.

func GetMessageOrDefault

func GetMessageOrDefault(c MessageCatalog, ID string, tag language.Tag, def string, v ...interface{}) string

GetMessageOrDefault is a helper func to get the translated message based on the message ID and lang. If no matching message is found, it returns the 'def' message.

Types

type DefaultLocaleBundle

type DefaultLocaleBundle struct {
	LangTag  string            `json:"lang"`
	Messages []*DefaultMessage `json:"messages"`
}

DefaultLocaleBundle is a bundle of messages for the specified locale. The language tag can be arbitrary to allow for unsupported/unknown languages used by custom clients.

func (*DefaultLocaleBundle) Init

func (l *DefaultLocaleBundle) Init() error

Init initializes the default catalog with the list of messages. The lang tag must parse, otherwise this func will panic.

type DefaultMessage

type DefaultMessage struct {
	ID               string `json:"id"`
	FormattedMessage string `json:"msg"`
}

DefaultMessage is a single message in the locale bundle identified by 'ID'.

type MessageCatalog

type MessageCatalog interface {
	GetMessage(ID string, tag language.Tag, v ...interface{}) string
	GetLangFromRequest(r *http.Request) language.Tag
}

MessageCatalog declares the interface to get globalized messages

func NewDefaultMessageCatalog

func NewDefaultMessageCatalog(bundles []*DefaultLocaleBundle) MessageCatalog

Jump to

Keyboard shortcuts

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