vertimas

package module
v0.0.0-...-c160c85 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2023 License: MIT Imports: 6 Imported by: 0

README

Vertimas Build Status

A simple Golang translation handling library. "Vertimas" is a Lithuanian word meaning translation.

Usage

This library assumes that translations will be stored in .json files where filename is canonical language name and file will contain key/value pairs.

Example content of translations/web/en.json:

{
    "greating": "Hello",
    "farewell": "Bye",
    "greeting_name": "Hello, {name}"
}

Example content of translations/web/lt.json:

{
    "greating": "Labas",
    "farewell": "Viso gero",
    "greeting_name": "Labas, {name}"
}
import (
    "github.com/v3nom/vertimas"
    "golang.org/x/text/language"
)

// Creates instance, preloads translations from "translations/web/" folder and sets initial language to first language in the array (English)
instance, err := vertimas.CreateInstance("translations/web/", []language.Tag{
    language.English,
    language.Lithuanian,
})

// Get translation for key
instance.GetTranslation("greating") // returns "Hello"

// Get translation by key and replacing placeholders with dynamic values
instance.GetParametrizedTranslation("greeting_name", map[string]string{
	"name": "Tomas",
})

// Get all translations for current language
instance.GetTranslations() // returns map of translations for current language

// Tries to find given language in supported language list and creates new instance without reloading translation files
lithuanianVertimas := instance.SetLanguage("lt") // returns new instance where current language is updated

// Tries to find language from HTTP Accept-Language header in supported language list and creates new instance without reloading translation files
newInstance := instance.SetLanguageFromRequest(r) // returns new instance with current language set from HTTP request

// Gets current language
instance.GetLanguage() // returns current language

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Instance

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

Instance vertimas instance

func CreateInstance

func CreateInstance(path string, supported []language.Tag) (*Instance, error)

CreateInstance creates vertimas instance and loads .json translation files from given path

func CreateInstanceWithPanic

func CreateInstanceWithPanic(path string, supported []language.Tag) *Instance

CreateInstanceWithPanic creates vertimas instance with panic if file loading fails

func (Instance) GetLanguage

func (v Instance) GetLanguage() language.Tag

GetLanguage gets current language

func (Instance) GetParametrizedTranslation

func (v Instance) GetParametrizedTranslation(name string, values map[string]string) string

GetParametrizedTranslation gets translation from current language with {token} replaced from values map

func (Instance) GetTranslation

func (v Instance) GetTranslation(name string) string

GetTranslation gets translation from current language

func (Instance) GetTranslations

func (v Instance) GetTranslations() map[string]string

GetTranslations gets translation map for current language

func (*Instance) SetLanguage

func (v *Instance) SetLanguage(lang string) *Instance

SetLanguage sets current language

func (*Instance) SetLanguageFromRequest

func (v *Instance) SetLanguageFromRequest(r *http.Request) *Instance

SetLanguageFromRequest sets current language from http.Request

Jump to

Keyboard shortcuts

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