Documentation
¶
Index ¶
- func FormatDate(t time.Time, lang string) string
- func FormatDateISO(t time.Time) string
- func FormatMoney(amountCents int64, currency, locale string) string
- func ParseDateISO(dateStr string) time.Time
- type DateFormat
- type DateFormatter
- type MoneyFormatter
- type SessionLanguageStore
- type SessionStore
- type Translations
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatDate ¶ added in v0.4.7
FormatDate is a convenience function to format a date with locale awareness. Supported languages: "de" (German), "en" (English), default is ISO format.
func FormatDateISO ¶ added in v0.4.7
FormatDateISO formats a date in ISO 8601 format (2006-01-02).
func FormatMoney ¶ added in v0.4.7
FormatMoney is a convenience function that formats an amount using the specified locale. Supported locales: "de", "en". Defaults to "en" for unknown locales.
func ParseDateISO ¶ added in v0.4.7
ParseDateISO parses a date string in ISO 8601 format. Returns the current time if parsing fails.
Types ¶
type DateFormat ¶ added in v0.4.7
type DateFormat string
DateFormat defines common date format patterns.
const ( // DateFormatISO is the ISO 8601 format: 2006-01-02 DateFormatISO DateFormat = "2006-01-02" // DateFormatEU is the European format: 02.01.2006 DateFormatEU DateFormat = "02.01.2006" // DateFormatUS is the US format: 01/02/2006 DateFormatUS DateFormat = "January 2, 2006" // DateFormatLong is the long format: January 2, 2006 DateFormatLong DateFormat = "January 2, 2006" )
type DateFormatter ¶ added in v0.4.7
type DateFormatter struct {
// contains filtered or unexported fields
}
DateFormatter formats dates according to locale conventions.
func NewDateFormatterDE ¶ added in v0.4.7
func NewDateFormatterDE() *DateFormatter
NewDateFormatterDE creates a German date formatter. Format: 02.01.2006 (e.g., "25.12.2024")
func NewDateFormatterEN ¶ added in v0.4.7
func NewDateFormatterEN() *DateFormatter
NewDateFormatterEN creates an English (US) date formatter. Format: January 2, 2006 (e.g., "December 25, 2024")
func NewDateFormatterISO ¶ added in v0.4.7
func NewDateFormatterISO() *DateFormatter
NewDateFormatterISO creates an ISO date formatter. Format: 2006-01-02 (e.g., "2024-12-25")
func (*DateFormatter) Format ¶ added in v0.4.7
func (f *DateFormatter) Format(t time.Time) string
Format formats a time value according to the formatter's locale settings.
func (*DateFormatter) WithFormat ¶ added in v0.4.7
func (f *DateFormatter) WithFormat(format DateFormat) *DateFormatter
WithFormat sets a custom format pattern.
func (*DateFormatter) WithLocation ¶ added in v0.4.7
func (f *DateFormatter) WithLocation(loc *time.Location) *DateFormatter
WithLocation sets a custom timezone for the formatter.
type MoneyFormatter ¶ added in v0.4.7
type MoneyFormatter struct {
// DecimalSeparator separates the whole and fractional parts (e.g., "," for DE, "." for EN).
DecimalSeparator string
// ThousandSeparator groups digits (e.g., "." for DE, "," for EN).
ThousandSeparator string
// CurrencyPosition determines where the currency symbol appears.
// "suffix" places it after the amount (e.g., "25,00 EUR"), "prefix" before (e.g., "EUR 25.00").
CurrencyPosition string
}
MoneyFormatter formats monetary amounts for different locales. Amounts are expected in the smallest currency unit (e.g., cents for EUR/USD).
func NewMoneyFormatterDE ¶ added in v0.4.7
func NewMoneyFormatterDE() *MoneyFormatter
NewMoneyFormatterDE creates a formatter for German locale. Format: 1.234,56 EUR
func NewMoneyFormatterEN ¶ added in v0.4.7
func NewMoneyFormatterEN() *MoneyFormatter
NewMoneyFormatterEN creates a formatter for English locale. Format: 1,234.56 EUR
func (*MoneyFormatter) Format ¶ added in v0.4.7
func (a *MoneyFormatter) Format(amountCents int64, currency string) string
Format formats an amount in the smallest currency unit (e.g., cents). If currency is empty, no currency suffix/prefix is added.
func (*MoneyFormatter) FormatWithoutCurrency ¶ added in v0.4.7
func (a *MoneyFormatter) FormatWithoutCurrency(amountCents int64) string
FormatWithoutCurrency formats an amount without the currency symbol.
type SessionLanguageStore ¶ added in v0.4.7
type SessionLanguageStore struct {
// contains filtered or unexported fields
}
SessionLanguageStore stores language preferences per session ID.
func NewSessionLanguageStore ¶ added in v0.4.7
func NewSessionLanguageStore() *SessionLanguageStore
NewSessionLanguageStore creates a new session language store.
func (*SessionLanguageStore) Clear ¶ added in v0.4.7
func (a *SessionLanguageStore) Clear(sessionID string)
Clear removes the language preference for the given session ID.
func (*SessionLanguageStore) Get ¶ added in v0.4.7
func (a *SessionLanguageStore) Get(sessionID string) string
Get returns the language for the given session ID. Returns empty string if not found.
func (*SessionLanguageStore) Set ¶ added in v0.4.7
func (a *SessionLanguageStore) Set(sessionID, lang string)
Set sets the language for the given session ID.
type SessionStore ¶ added in v0.4.7
type SessionStore[V any] struct { // contains filtered or unexported fields }
SessionStore stores values of any type per session ID. This is a generic version of SessionLanguageStore that can store any type.
func NewSessionStore ¶ added in v0.4.7
func NewSessionStore[V any]() *SessionStore[V]
NewSessionStore creates a new generic session store.
func (*SessionStore[V]) Clear ¶ added in v0.4.7
func (a *SessionStore[V]) Clear(sessionID string)
Clear removes the value for the given session ID.
func (*SessionStore[V]) Get ¶ added in v0.4.7
func (a *SessionStore[V]) Get(sessionID string) (V, bool)
Get returns the value for the given session ID. Returns the zero value and false if not found.
func (*SessionStore[V]) Set ¶ added in v0.4.7
func (a *SessionStore[V]) Set(sessionID string, value V)
Set sets the value for the given session ID.
type Translations ¶
type Translations struct {
// contains filtered or unexported fields
}
Translations holds all loaded translations keyed by language code.
func NewTranslations ¶
func NewTranslations() *Translations
NewTranslations creates a new Translations instance.
func (*Translations) Load ¶
func (a *Translations) Load(efs embed.FS, lang, path string) error
Load parses a YAML file from the embedded FS and registers it under the given language code.
func (*Translations) T ¶
func (a *Translations) T(lang, key string) string
T returns the translation for a dot-separated key in the given language. Falls back to the key itself if not found. Example: T("de", "nav.dashboard") -> "Dashboard"
func (*Translations) TMap ¶
func (a *Translations) TMap(lang string, keys ...string) map[string]string
TMap returns a map of translations for a given language and list of keys. This is useful for passing a pre-resolved translation map to templates. Example: TMap("de", "nav.dashboard", "nav.availability", "action.logout") Returns: map[string]string{"nav.dashboard": "Dashboard", "nav.availability": "Verfügbarkeit", ...}