l10n

package
v0.0.0-...-ebba861 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

package l10n holds translation mechanics that are used by user facing services (notifications, userlog, graph)

Index

Constants

This section is empty.

Variables

View Source
var (
	// HeaderAcceptLanguage is the header key for the accept-language header
	HeaderAcceptLanguage = "Accept-Language"

	// ErrUnsupportedType is returned when the type is not supported
	ErrUnsupportedType = errors.New("unsupported type")
)

Functions

func GetUserLocale

func GetUserLocale(ctx context.Context, userID string, vc settingssvc.ValueService) (string, error)

GetUserLocale returns the locale of the user

func MustGetUserLocale

func MustGetUserLocale(ctx context.Context, userID string, preferedLang string, vc settingssvc.ValueService) string

MustGetUserLocale returns the locale the user wants to use, omitting errors

func Template

func Template(s string) string

Template marks a string as translatable

func TranslateEntity

func TranslateEntity(tr func(string, ...any) string, entity any, opts ...TranslateOption) error

TranslateEntity translates a slice, array or struct See Translator.TranslateEntity for more information

Types

type FieldType

type FieldType int

FieldType is used to specify the type of field to translate

const (
	// FieldTypeString is a string field
	FieldTypeString FieldType = iota
	// FieldTypeStruct is a struct field
	FieldTypeStruct
	// FieldTypeIterable is a slice or array field
	FieldTypeIterable
	// FieldTypeMap is a map field
	FieldTypeMap
)

type TranslateOption

type TranslateOption func() (string, FieldType, []TranslateOption)

TranslateOption is used to specify fields in structs to translate

func TranslateEach

func TranslateEach(fieldName string, args ...TranslateOption) TranslateOption

TranslateEach function provides the generic way to translate the necessary fields in slices or nested entities.

func TranslateField

func TranslateField(fieldName string) TranslateOption

TranslateField function provides the generic way to translate the necessary field in composite entities.

func TranslateMap

func TranslateMap(fieldName string, args ...TranslateOption) TranslateOption

TranslateMap function provides the generic way to translate the necessary fields in maps.

func TranslateStruct

func TranslateStruct(fieldName string, args ...TranslateOption) TranslateOption

TranslateStruct function provides the generic way to translate the nested fields in composite entities.

type Translator

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

Translator is able to translate strings

func NewTranslator

func NewTranslator(defaultLocale string, domain string, fsys fs.FS) Translator

NewTranslator creates a Translator with library path and language code and load default domain

func NewTranslatorFromCommonConfig

func NewTranslatorFromCommonConfig(defaultLocale string, domain string, path string, fsys fs.FS, fsSubPath string) Translator

NewTranslatorFromCommonConfig creates a new Translator from legacy config

func (Translator) Locale

func (t Translator) Locale(locale string) *gotext.Locale

Locale returns the gotext.Locale, use `.Get` method to translate strings

func (Translator) Translate

func (t Translator) Translate(str, locale string) string

Translate translates a string to the locale

func (Translator) TranslateEntity

func (t Translator) TranslateEntity(locale string, entity any, opts ...TranslateOption) error

TranslateEntity function provides the generic way to translate a struct, array or slice. Support for maps is also provided, but non-pointer values will not work. The function also takes the entity with fields to translate. The function supports nested structs and slices of structs.

tr := NewTranslator("en", _domain, _fsys)

// a slice of translatables can be passed directly val := []string{"description", "display name"} err := tr.TranslateEntity(tr, s, val)

// string maps work the same way

val := map[string]string{
	"entryOne": "description",
	"entryTwo": "display name",
}

err := TranslateEntity(tr, val)

// struct fields need to be specified

type Struct struct {
	Description string
	DisplayName string
	MetaInformation string
}

val := Struct{} err := TranslateEntity(tr, val,

l10n.TranslateField("Description"),
l10n.TranslateField("DisplayName"),

)

// nested structures are supported

type InnerStruct struct {
	Description string
	Roles []string
}
type OuterStruct struct {
	DisplayName string
	First InnerStruct
	Others map[string]InnerStruct
}

val := OuterStruct{} err := TranslateEntity(tr, val,

l10n.TranslateField("DisplayName"),
l10n.TranslateStruct("First",
	l10n.TranslateField("Description"),
	l10n.TranslateEach("Roles"),
),
l10n.TranslateMap("Others",
	l10n.TranslateField("Description"),
},

Jump to

Keyboard shortcuts

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