Documentation
¶
Index ¶
- Variables
- type CacheOptions
- type CacheStats
- type InMemoryCache
- func (c *InMemoryCache) Clear()
- func (c *InMemoryCache) Delete(keys ...string)
- func (c *InMemoryCache) Get(key string) ([]Translation, bool)
- func (c *InMemoryCache) ResetStats()
- func (c *InMemoryCache) Set(key string, value []Translation, ttl time.Duration)
- func (c *InMemoryCache) Stats() CacheStats
- type Locale
- type Translatable
- type Translation
- type TranslationCache
- type TranslationRepository
- type Translator
- type TranslatorOptions
Constants ¶
This section is empty.
Variables ¶
var ErrEmptyEntityName = errors.New("entity name cannot be empty")
ErrEmptyEntityName is returned when an entity name is empty.
Functions ¶
This section is empty.
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
// BatchSize defines the maximum number of IDs to fetch in a single query.
// Default is 1000 if not set or set to 0.
BatchSize int
// DefaultContextTimeout specifies default timeout for cache operations.
// Zero means no timeout.
DefaultContextTimeout time.Duration
}
CacheOptions configures the caching behaviour of a cached repository.
type CacheStats ¶ added in v1.2.0
CacheStats provides statistics about cache performance.
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) ResetStats ¶ added in v1.2.0
func (c *InMemoryCache) ResetStats()
ResetStats resets cache statistics to zero.
func (*InMemoryCache) Set ¶ added in v1.1.0
func (c *InMemoryCache) Set(key string, value []Translation, ttl time.Duration)
func (*InMemoryCache) Stats ¶ added in v1.2.0
func (c *InMemoryCache) Stats() CacheStats
Stats returns cache statistics.
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 AllLocales ¶ added in v1.2.0
func AllLocales() []Locale
AllLocales returns all supported locales in an unspecified order, excluding LocaleNone.
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 is the interface every translatable entity must implement. TranslatableFields returns a map: struct field name → translation field ID in DB. Example: map[string]string{"Title": "title", "Description": "desc"} TranslationEntityName returns the name stored in the translations table.
type Translation ¶
type Translation struct {
// ID is the database primary key (omitted for inserts).
ID int
// Entity is the entity type name (e.g., "product", "parameter").
Entity string
// EntityID is the unique identifier of the entity instance.
EntityID int
// Field is the database field identifier (e.g., "title", "description").
Field string
// Locale is the language variant for this translation.
Locale Locale
// Value contains the translated text.
Value string
}
Translation represents a translated field value for a specific entity and locale. ID is typically auto-incremented by the database and may be omitted for insert operations. Entity is the type name (e.g., "product"), EntityID identifies the specific entity instance. Field maps to the translatable field name (e.g., "title", "description"). Locale specifies the language variant. Value contains the translated text.
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()
// Stats returns cache statistics (hits, misses, etc).
Stats() CacheStats
// ResetStats resets cache statistics to zero.
ResetStats()
}
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)
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 // DeleteTranslations removes translations for specific entity IDs, locale and fields. DeleteTranslations(ctx context.Context, locale Locale, entityIDs []int, fields []string) error // DeleteTranslationsByEntity removes all translations for the given entity IDs across all locales. DeleteTranslationsByEntity(ctx context.Context, entityIDs []int) error }
Translator is the main interface for translation operations. The entity name is derived from T at construction, so it does not need to be passed to the delete methods — the translator already knows it.
func NewTranslator ¶
func NewTranslator[T Translatable](repo TranslationRepository) Translator[T]
NewTranslator creates a translator for entity type T. The entity name and field index are resolved once from a zero value of T.
func NewTranslatorWithOptions ¶ added in v1.2.0
func NewTranslatorWithOptions[T Translatable](opts TranslatorOptions[T]) Translator[T]
NewTranslatorWithOptions creates a translator with advanced options including default context timeout. Use this when you want to set a default timeout for operations.
trans := NewTranslatorWithOptions(TranslatorOptions[Product]{
Repository: repo,
DefaultContextTimeout: 30 * time.Second,
})
type TranslatorOptions ¶ added in v1.2.0
type TranslatorOptions[T Translatable] struct { // Repository is the translation repository (required). Repository TranslationRepository // DefaultContextTimeout specifies a default timeout for translation operations. // If set to a positive value, contexts without a deadline will be wrapped with this timeout. // Zero means no default timeout is applied. DefaultContextTimeout time.Duration }
TranslatorOptions provides configuration options for a Translator.
Directories
¶
| Path | Synopsis |
|---|---|
|
example
|
|
|
advanced
command
|
|
|
basic
command
|
|
|
caching
command
|
|
|
error-handling
command
|
|
|
performance
command
|
|