temblate_go

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: May 10, 2023 License: AGPL-3.0 Imports: 8 Imported by: 0

README

temblate-go

Minimalistic embedded multi-language text template generation for go programs

Usage

  1. Add the lib to your project:
    go get github.com/Rainc1oud/temblate-go
    
  2. Make a file temblate/generate.go with the following content:
    package temblate
    //go:generate go run -mod=mod github.com/Rainc1oud/temblate-go/cmd/gen ./templates ./messages.go
    
    and define your templates under ./temblate/templates/ with the file name formatted as <keyname>.<lang>.gotmpl (e.g. mytopic.en.gotmpl).

    <keyname> must match the regex [.\w_-]* and <lang> must be present and match the regex [a-z]{2,3}.
  3. Do
    go generate ./temblate/
    
    This will generate ./temblate/messages.go (the name can be changed in the //go:generate line) and you can now use
    mymsg := temblate.GetMessage(lang, key, &data)
    
    where data is a struct to the members of which you refer in the template as {{ .MyField }} (standard go template format)

What's with the name

It's not a typo, temblate is a play on embedded template; the main feature compared to other go templating libs is that all template content is generated into the temblate package of your project and initialised at program start. That means you don't have to worry about files not found and multi-file resource bundling/distribution.

Documentation

Overview

temblate is a generator for language-message-mapped templates as a literal map invoke by go generate

Index

Constants

View Source
const Version = "v0.2.1"

Version exposes the current package version.

Variables

This section is empty.

Functions

func Generate

func Generate(tgtfile, tmpldir string) error

func GetMessage

func GetMessage(lang, key string, data interface{}) string

func InitTemplates

func InitTemplates(templates *MessageTemplates)

Types

type LangTemplates

type LangTemplates map[string]string

LangTemplates maps a language (2-char string) to MessageTemplates

type MessageTemplates

type MessageTemplates map[string]LangTemplates

MessageTemplate maps a key to a string message; the string is intended to contain a go template

Directories

Path Synopsis
cmd
gen

Jump to

Keyboard shortcuts

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