msg: Index | Files

package msg

import ""

Package msg manages translations of text labels ("messages") in a web application.

New messages are defined like this:

  Set("en", "Hello, world").
  Set("nl", "Hallo wereld")
  Set("en", "Hi").
  Set("nl", "Hoi")

The user's language is determined from the "Accept-Language" request header. Pass the http.Request pointer to Translator():

t := msg.Translator(r)

Then get the translation:

message := t.Get("Hi")

Environment variables:

MSG_DEFAULT: determines the default language to use, if no translation is found matching the Accept-Language header. The default value for MSG_DEFAULT is "en".

GO_ENV: if not set to "production", then translations that resorted to the default language get prepended with "D-", and failed translations, falling back to the message key, get prepended with "X-".

Messages and Translators are stored in memory. Translators are cached on their Accept-Language header value.

Messages can also be used as multi-language text fields in data records:

type Entity struct {
	Label msg.MessageType
entity := &Entity{Label: msg.New()}
entity.Label.Set("en", "entity")
entity.Label.Set("nl", "entiteit")
t := msg.Translator(r)
label := t.Select(entity.Label)


Package Files



var NumLang = 10

NumLang sets the initial capacity for translations in a new message.

type MessageType Uses

type MessageType map[string]string

MessageType hold the translations for a message.

func Key Uses

func Key(key string) (message MessageType)

Key returns the message stored under the given key, if it doesn't exist yet, it gets created.

func New Uses

func New() MessageType

New initialises a new MessageType.

func (MessageType) Set Uses

func (m MessageType) Set(language, translation string) MessageType

Set stores the translation of the message for the given language. Any old value is overwritten.

type TranslatorType Uses

type TranslatorType struct {
    // contains filtered or unexported fields

TranslatorType knows about translations for the user's accepted languages.

func Translator Uses

func Translator(r *http.Request) (t *TranslatorType)

Translator returns a (cached) TranslatorType.

func (*TranslatorType) File Uses

func (t *TranslatorType) File(location, dir, base string, opt_extension ...string) (inner string, err error)

File searches for an "inner" template fitting the "base" template, matching the user's accepted languages.

Template names are without file name extension. The default extension is ".ace".

Example: if MSG_DEFAULT is "en", and the Accept-Languages header is empty,

msg.File("/resources/templates", "home", "HomePage", ".tpl")


"HomePage-en", nil

if the file "/resources/templates/home/HomePage-en.tpl" exists.

func (*TranslatorType) Get Uses

func (t *TranslatorType) Get(key string) (translation string)

Get returns the translation for the message with the given key.

func (*TranslatorType) Select Uses

func (t *TranslatorType) Select(message MessageType, opt_default ...string) (translation string)

Select returns the translation for a message.

Package msg imports 4 packages (graph) and is imported by 5 packages. Updated 2017-07-10. Refresh now. Tools for package owners.