Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/raspi/tulkki
Translated HTML templates for Go
https://github.com/raspi/tulkki
go golang html i18n internationalization localization template
Last synced: 5 days ago
JSON representation
Translated HTML templates for Go
- Host: GitHub
- URL: https://github.com/raspi/tulkki
- Owner: raspi
- License: apache-2.0
- Created: 2020-08-12T09:03:07.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-03-06T09:27:25.000Z (over 1 year ago)
- Last Synced: 2024-06-20T12:02:47.642Z (5 months ago)
- Topics: go, golang, html, i18n, internationalization, localization, template
- Language: Go
- Homepage:
- Size: 287 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tulkki
![GitHub All Releases](https://img.shields.io/github/downloads/raspi/tulkki/total?style=for-the-badge)
![GitHub release (latest by date)](https://img.shields.io/github/v/release/raspi/tulkki?style=for-the-badge)
![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/raspi/tulkki?style=for-the-badge)Translated HTML templates for Go
See [example directory](_example) for example(s).
```go
package mainimport (
"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 = `{{.Title}}
{{- template "content" .C -}}
`
// Example page
var testPageHTML = `
{{T "test"}}
{{T "test_formatted" "Template" "right now"}}
{{.TranslatedToken}}
`// 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.Englishpagetranslations := 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(`here`)))
}
```Outputs:
```html
Hello, world!
this is a test string
Template is testing things on right now
Example is testing things on 2020-08-12 12:25:41 +0300 EESTexample: Direct translation is testing things on here
```