i18n

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

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 ...any) 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 ...any) 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 ...any) 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