Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetEntityNameFromType ¶ added in v1.1.0
GetEntityNameFromType returns the snake_case entity name from a type. This is a helper function for when you don't want to implement TranslationEntityName() Example: "Product" → "product", "GeoTag" → "geo_tag"
func GetEntityNameFromValue ¶ added in v1.1.0
GetEntityNameFromValue returns the snake_case entity name from a value. This is a helper function for when you don't want to implement TranslationEntityName()
Types ¶
type CacheOptions ¶ added in v1.1.0
type CacheOptions struct {
// TTL defines how long a cached entry remains valid.
// Zero means entries never expire.
TTL time.Duration
}
CacheOptions configures the caching behaviour of a cached repository.
type InMemoryCache ¶ added in v1.1.0
type InMemoryCache struct {
// contains filtered or unexported fields
}
InMemoryCache is a thread-safe in-memory implementation of TranslationCache. It is the default backend used by NewCachedRepositoryInMemory.
func NewInMemoryCache ¶ added in v1.1.0
func NewInMemoryCache() *InMemoryCache
NewInMemoryCache returns a ready-to-use in-memory cache.
func (*InMemoryCache) Clear ¶ added in v1.1.0
func (c *InMemoryCache) Clear()
func (*InMemoryCache) Delete ¶ added in v1.1.0
func (c *InMemoryCache) Delete(keys ...string)
func (*InMemoryCache) Get ¶ added in v1.1.0
func (c *InMemoryCache) Get(key string) ([]Translation, bool)
func (*InMemoryCache) Set ¶ added in v1.1.0
func (c *InMemoryCache) Set(key string, value []Translation, ttl time.Duration)
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 ¶
ParseLocale returns a Locale enum from a language code (ISO-639-1). Returns (LocaleNone, false) for unknown codes.
func ParseLocaleList ¶
ParseLocaleList converts "en,ru,uk" into []Locale.
type Translatable ¶ added in v1.1.0
type Translatable interface {
TranslationEntityID() int
TranslationEntityName() string
TranslationEntityLocale() Locale
TranslatableFields() map[string]string
}
Translatable interface for explicit association between struct fields and translation field IDs TranslatableFields returns a map: key = struct field name, value = translation field ID in DB Example: map[string]string{"Title": "title", "Description": "desc", "Recommendation": "rec"} TranslationEntityName returns the name of the entity as stored in translations table
type Translation ¶
type Translation struct {
ID int
Entity string
EntityID int
Field string
Value string
// contains filtered or unexported fields
}
Translation represents a translated field value for a specific entity and locale
func NewTranslation ¶ added in v1.1.0
func NewTranslation(id int, entity string, entityID int, field string, locale Locale, value string) Translation
NewTranslation creates a new Translation instance with the given parameters
func (Translation) GetLocale ¶ added in v1.1.0
func (t Translation) GetLocale() Locale
GetLocale returns the locale of the translation
type TranslationCache ¶ added in v1.1.0
type TranslationCache interface {
// Get retrieves cached translations. Returns the slice and true on a hit.
Get(key string) ([]Translation, bool)
// Set stores translations under the given key. A zero TTL means no expiration.
Set(key string, value []Translation, ttl time.Duration)
// Delete removes one or more entries by key.
Delete(keys ...string)
// Clear removes all entries from the cache.
Clear()
}
TranslationCache is the interface for pluggable cache backends. Implementations can be in-memory, Redis, Memcached, or any custom store.
type TranslationRepository ¶
type TranslationRepository interface {
GetTranslations(
ctx context.Context,
locale Locale,
entity string,
entityIDs []int,
) ([]Translation, error)
MassCreate(
ctx context.Context,
translations []Translation,
) error
MassDelete(
ctx context.Context,
locale Locale,
entity string,
entityIDs []int,
fields []string,
) error
MassCreateOrUpdate(
ctx context.Context,
locale Locale,
translations []Translation,
) error
}
func NewCachedRepository ¶ added in v1.1.0
func NewCachedRepository(repo TranslationRepository, cache TranslationCache, opts CacheOptions) TranslationRepository
NewCachedRepository wraps repo with the provided cache backend. Use this when you want to supply your own cache implementation (e.g. Redis).
cache := gotrans.NewInMemoryCache()
cachedRepo := gotrans.NewCachedRepository(repo, cache, gotrans.CacheOptions{TTL: 5 * time.Minute})
translator := gotrans.NewTranslator[Product](cachedRepo)
func NewCachedRepositoryInMemory ¶ added in v1.1.0
func NewCachedRepositoryInMemory(repo TranslationRepository, opts CacheOptions) TranslationRepository
NewCachedRepositoryInMemory wraps repo with the built-in in-memory cache. This is the simplest way to add caching with no external dependencies.
cachedRepo := gotrans.NewCachedRepositoryInMemory(repo, gotrans.CacheOptions{TTL: 5 * time.Minute})
translator := gotrans.NewTranslator[Product](cachedRepo)
type Translator ¶
type Translator[T Translatable] interface { LoadTranslations(ctx context.Context, entities []T) ([]T, error) SaveTranslations(ctx context.Context, entities []T) error DeleteTranslationsByEntity(ctx context.Context, entity string, entityIDs []int) error DeleteTranslations( ctx context.Context, locale Locale, entity string, entityIDs []int, fields []string, ) error }
Translator interface for single-locale translation operations All translation operations now work with locale from entity (via TranslationEntityLocale() method) Example usage: LoadTranslations(ctx, entities)
func NewTranslator ¶
func NewTranslator[T Translatable](translationRepository TranslationRepository) Translator[T]