gotrans

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

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

Go to latest
Published: Nov 28, 2025 License: MIT Imports: 5 Imported by: 0

README

gotrans

This repository provides a lightweight, framework-agnostic translation module for Golang applications. It is designed to manage multi-language content directly within backend business logic, without relying on heavy external localization frameworks.

Mysql table structure

CREATE TABLE IF NOT EXISTS translations (
    id BIGINT AUTO_INCREMENT,
    entity VARCHAR(100) NOT NULL,
    entity_id BIGINT NOT NULL,
    field VARCHAR(100) NOT NULL,
    locale VARCHAR(10) NOT NULL,
    value TEXT NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY uniq_translation (entity, entity_id, field, locale)
)
COLLATE = utf8mb4_unicode_ci;

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Locale

type Locale int16
const (
	LocaleNone Locale = iota
	LocaleSQ          // Albanian
	LocaleAR          // Arabic
	LocaleAZ          // Azerbaijani
	LocaleBS          // Bosnian
	LocaleBG          // Bulgarian
	LocaleZH          // Chinese
	LocaleHR          // Croatian
	LocaleCS          // Czech
	LocaleDA          // Danish
	LocaleNL          // Dutch
	LocaleEN          // English
	LocaleET          // Estonian
	LocaleFI          // Finnish
	LocaleFR          // French
	LocaleKA          // Georgian
	LocaleDE          // German
	LocaleEL          // Greek
	LocaleHE          // Hebrew
	LocaleHU          // Hungarian
	LocaleID          // Indonesian
	LocaleJA          // Japanese
	LocaleKK          // Kazakh
	LocaleKO          // Korean
	LocaleLV          // Latvian
	LocaleLT          // Lithuanian
	LocaleMK          // Macedonian
	LocaleNO          // Norwegian
	LocalePL          // Polish
	LocalePT          // Portuguese
	LocaleRO          // Romanian
	LocaleRU          // Russian
	LocaleSR          // Serbian
	LocaleSK          // Slovak
	LocaleSL          // Slovenian
	LocaleES          // Spanish
	LocaleSV          // Swedish
	LocaleTH          // Thai
	LocaleTR          // Turkish
	LocaleUK          // Ukrainian
	LocaleVI          // Vietnamese
	LocaleIT          // Italian
)

func ParseLocale

func ParseLocale(code string) (Locale, bool)

ParseLocale returns a Locale enum from a language code (ISO-639-1). Returns (LocaleNone, false) for unknown codes.

func ParseLocaleList

func ParseLocaleList(list string) []Locale

ParseLocaleList converts "en,ru,uk" into []Locale.

func (Locale) Code

func (l Locale) Code() string

Code returns the ISO-639-1 code for a language.

func (Locale) Name

func (l Locale) Name() string

Name returns the human-readable language name.

func (Locale) String

func (l Locale) String() string

type TranslatableEntity

type TranslatableEntity interface {
	TranslationEntityID() int
}

type TranslateField

type TranslateField map[Locale]string

func (TranslateField) Get

func (tf TranslateField) Get(locale Locale) string

func (TranslateField) IsEmpty

func (tf TranslateField) IsEmpty() bool

type Translation

type Translation struct {
	ID       int    `db:"id"`
	Entity   string `db:"entity"`
	EntityID int    `db:"entity_id"`
	Field    string `db:"field"`
	Locale   Locale `db:"locale"`
	Value    string `db:"value"`
}

type TranslationRepository

type TranslationRepository interface {
	GetTranslations(
		ctx context.Context,
		locales []Locale,
		entity string,
		entityIDs []int,
	) ([]Translation, error)

	MassCreate(
		ctx context.Context,
		translations []Translation,
	) error

	MassDelete(
		ctx context.Context,
		Entity string,
		EntityIDs []int,
		Fields []string,
		Locales []Locale,
	) error

	MassCreateOrUpdate(
		ctx context.Context,
		translations []Translation,
	) error
}

func NewTranslationRepository

func NewTranslationRepository(db *sqlx.DB) TranslationRepository

type Translator

type Translator[T TranslatableEntity] interface {
	LoadTranslations(ctx context.Context, locales []Locale, entities []T) ([]T, error)
	SaveTranslations(ctx context.Context, entities []T) error
	DeleteTranslations(
		ctx context.Context,
		Entity string,
		EntityIDs []int,
		Fields []string,
		Locales []Locale,
	) error
	SupportedLocales() []Locale
}

func NewTranslator

func NewTranslator[T TranslatableEntity](
	locales []Locale,
	translationRepository TranslationRepository,
) Translator[T]

Jump to

Keyboard shortcuts

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