2026-02-16 19:51:27 +00:00
|
|
|
package i18n
|
|
|
|
|
|
|
|
|
|
// TranslationContext provides disambiguation for translations.
|
|
|
|
|
//
|
|
|
|
|
// T("direction.right", C("navigation")) // "rechts" (German)
|
|
|
|
|
// T("status.right", C("correctness")) // "richtig" (German)
|
|
|
|
|
type TranslationContext struct {
|
|
|
|
|
Context string
|
|
|
|
|
Gender string
|
2026-04-01 23:53:16 +00:00
|
|
|
Location string
|
2026-02-16 19:51:27 +00:00
|
|
|
Formality Formality
|
2026-04-02 07:59:58 +00:00
|
|
|
count int
|
|
|
|
|
countSet bool
|
2026-02-16 19:51:27 +00:00
|
|
|
Extra map[string]any
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// C creates a TranslationContext.
|
|
|
|
|
func C(context string) *TranslationContext {
|
2026-04-02 07:59:58 +00:00
|
|
|
return &TranslationContext{Context: context, count: 1}
|
2026-02-16 19:51:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *TranslationContext) WithGender(gender string) *TranslationContext {
|
2026-04-01 23:53:16 +00:00
|
|
|
if c == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-02-16 19:51:27 +00:00
|
|
|
c.Gender = gender
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 23:53:16 +00:00
|
|
|
func (c *TranslationContext) In(location string) *TranslationContext {
|
|
|
|
|
if c == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
c.Location = location
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-16 19:51:27 +00:00
|
|
|
func (c *TranslationContext) Formal() *TranslationContext {
|
2026-04-01 23:53:16 +00:00
|
|
|
if c == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-02-16 19:51:27 +00:00
|
|
|
c.Formality = FormalityFormal
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *TranslationContext) Informal() *TranslationContext {
|
2026-04-01 23:53:16 +00:00
|
|
|
if c == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-02-16 19:51:27 +00:00
|
|
|
c.Formality = FormalityInformal
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *TranslationContext) WithFormality(f Formality) *TranslationContext {
|
2026-04-01 23:53:16 +00:00
|
|
|
if c == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-02-16 19:51:27 +00:00
|
|
|
c.Formality = f
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 07:59:58 +00:00
|
|
|
// Count sets the count used for plural-sensitive translations.
|
|
|
|
|
func (c *TranslationContext) Count(n int) *TranslationContext {
|
|
|
|
|
if c == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
c.count = n
|
|
|
|
|
c.countSet = true
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-16 19:51:27 +00:00
|
|
|
func (c *TranslationContext) Set(key string, value any) *TranslationContext {
|
2026-04-01 23:53:16 +00:00
|
|
|
if c == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-02-16 19:51:27 +00:00
|
|
|
if c.Extra == nil {
|
|
|
|
|
c.Extra = make(map[string]any)
|
|
|
|
|
}
|
|
|
|
|
c.Extra[key] = value
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *TranslationContext) Get(key string) any {
|
2026-04-01 23:53:16 +00:00
|
|
|
if c == nil || c.Extra == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-02-16 19:51:27 +00:00
|
|
|
return c.Extra[key]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *TranslationContext) ContextString() string {
|
2026-04-01 23:53:16 +00:00
|
|
|
if c == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2026-02-16 19:51:27 +00:00
|
|
|
return c.Context
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 01:53:57 +00:00
|
|
|
func (c *TranslationContext) String() string {
|
|
|
|
|
if c == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2026-04-02 07:37:58 +00:00
|
|
|
return c.Context
|
2026-04-02 01:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-16 19:51:27 +00:00
|
|
|
func (c *TranslationContext) GenderString() string {
|
2026-04-01 23:53:16 +00:00
|
|
|
if c == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2026-02-16 19:51:27 +00:00
|
|
|
return c.Gender
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 23:53:16 +00:00
|
|
|
func (c *TranslationContext) LocationString() string {
|
|
|
|
|
if c == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return c.Location
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-16 19:51:27 +00:00
|
|
|
func (c *TranslationContext) FormalityValue() Formality {
|
2026-04-01 23:53:16 +00:00
|
|
|
if c == nil {
|
|
|
|
|
return FormalityNeutral
|
|
|
|
|
}
|
2026-02-16 19:51:27 +00:00
|
|
|
return c.Formality
|
|
|
|
|
}
|
2026-04-02 07:59:58 +00:00
|
|
|
|
|
|
|
|
// CountInt returns the current count value.
|
|
|
|
|
func (c *TranslationContext) CountInt() int {
|
|
|
|
|
if c == nil {
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
return c.count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CountString returns the current count value formatted as text.
|
|
|
|
|
func (c *TranslationContext) CountString() string {
|
|
|
|
|
if c == nil {
|
|
|
|
|
return "1"
|
|
|
|
|
}
|
2026-04-02 08:12:33 +00:00
|
|
|
return FormatNumber(int64(c.count))
|
2026-04-02 07:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsPlural reports whether the count is plural.
|
|
|
|
|
func (c *TranslationContext) IsPlural() bool {
|
|
|
|
|
return c != nil && c.count != 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *TranslationContext) countValue() (int, bool) {
|
|
|
|
|
if c == nil {
|
|
|
|
|
return 1, false
|
|
|
|
|
}
|
|
|
|
|
return c.count, c.countSet
|
|
|
|
|
}
|