tulkki

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

README

tulkki

GitHub All Releases GitHub release (latest by date) GitHub tag (latest by date)

Translated HTML templates for Go

See example directory for example(s).

package main

import (
	"bytes"
	"fmt"
	"github.com/raspi/tulkki"
	"golang.org/x/text/language"
	"golang.org/x/text/message/catalog"
	"html/template"
	"time"
)

// Base template for all pages
var baseHTML = `
<html>
<head>
<title>{{.Title}}</title>
</head>
<body>
<main>
{{- template "content" .C -}}
</main>
</body>
</html>
`

// Example page
var testPageHTML = `
<p>
  <!-- Translated within template itself: -->
  {{T "test"}}
  {{T "test_formatted" "Template" "right now"}}
  <!-- Pre-translated to a template variable: -->
  {{.TranslatedToken}}
</p>
`

// example base struct which all pages will use
type exampleBase struct {
	Title string
	// C (short for content) is used in the base HTML template as a per-page variables
	// It needs to be abstract interface because different pages have different variables
	C interface{}
}

// example page
type examplePage struct {
	TranslatedToken string
}

func main() {
	useLanguage := language.English
	fallbackLanguage := language.English

	pagetranslations := catalog.NewBuilder(
		catalog.Fallback(fallbackLanguage),
	)

	// Translations are per-page for collision reasons
	pagetranslations.SetString(language.English, `test`, `this is a test string`)
	pagetranslations.SetString(language.English, `test_formatted`, `%s is testing things on %s`)

	// Global template functions accessible to all pages
	// Add CSRF etc generators here
	funcs := template.FuncMap{}

	tpl := tulkki.New(baseHTML, funcs)
	tpl.AddPage(`testpage`, testPageHTML, pagetranslations)

	// Generate a page template
	page := exampleBase{
		Title: "Hello, world!",
		C: examplePage{
			TranslatedToken: tpl.Translate(`testpage`, `test_formatted`, useLanguage, `Example`, time.Now().Truncate(time.Second)),
		},
	}

	// Render the HTML
	var tmp bytes.Buffer
	err := tpl.Render(&tmp, `testpage`, useLanguage, page)
	if err != nil {
		panic(err)
	}

	fmt.Print(tmp.String())

	fmt.Println(`example: ` + tpl.Translate(`testpage`, `test_formatted`, useLanguage, `Direct translation`, template.HTML(`<a href="#">here</a>`)))
}

Outputs:

<html>
<head>
<title>Hello, world!</title>
</head>
<body>
<main>
<p>
  
  this is a test string
  Template is testing things on right now
  
  Example is testing things on 2020-08-12 12:25:41 &#43;0300 EEST
</p>
</main>
</body>
</html>
example: Direct translation is testing things on <a href="#">here</a>

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Template

type Template struct {
	// contains filtered or unexported fields
}

Template contains HTML used as a base and all the pages that are using that base

func New

func New(baseContents string, funcs template.FuncMap) *Template

New creates translatable HTML pages

func (*Template) AddPage

func (t *Template) AddPage(name string, contents string, translations catalog.Catalog) error

AddPage adds a page which uses baseTemplateContents template as a source. Translations are per-page for collision reasons. If you need access to translation tokens which are used globally, simply inject them to the catalog before page specific tokens.

func (*Template) Render

func (t *Template) Render(w io.Writer, templatename string, language language.Tag, data interface{}) (err error)

Render outputs generated HTML to a writer

func (*Template) Translate

func (t *Template) Translate(templatename string, key string, language language.Tag, data ...interface{}) string

Translate a token with given language

Directories

Path Synopsis
_example

Jump to

Keyboard shortcuts

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