Documentation
¶
Overview ¶
Package lingo streamlines language translations.
Features:
- Automatically finds and loads translation files from specified directories
- Follows BCP 47 language tags
- Graceful fallback to default language when translations are missing
- Support for dynamic content with template data
- Supports pluralization
- Defaults to https://github.com/nicksnyder/go-i18n for translation management
- Allows you to implement your own custom solution by implementing the LocalizerService interface
Supported extensions (specific to go-i18n):
- JSON
- YAML / YML
- TOML
For more details, see README.md.
Package lingo is a generated GoMock package.
Index ¶
- func GetLocalizer(language language.Tag) (interface{}, bool, error)
- func MustTranslate(localizer interface{}, message *Message) string
- func SetLocalizerService(service LocalizerService) func()
- func Translate(localizer interface{}, message *Message) (string, bool, error)
- type I18nLocalizerService
- type LocalizerService
- type Message
- type MockLocalizerService
- func (m *MockLocalizerService) EXPECT() *MockLocalizerServiceMockRecorder
- func (m *MockLocalizerService) GetLocalizer(arg0 language.Tag) (any, bool, error)
- func (m *MockLocalizerService) MustTranslate(localizer any, message *Message) string
- func (m *MockLocalizerService) Translate(localizer any, message *Message) (string, bool, error)
- type MockLocalizerServiceMockRecorder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetLocalizer ¶
GetLocalizer Directly exposes the current service GetLocalizer function.
func MustTranslate ¶
MustTranslate Directly exposes the current service MustTranslate function.
func SetLocalizerService ¶
func SetLocalizerService(service LocalizerService) func()
SetLocalizerService affect a new repository to the global service singleton
Types ¶
type I18nLocalizerService ¶
type I18nLocalizerService struct {
// contains filtered or unexported fields
}
I18nLocalizerService implements the LocalizerService interface using i18n
func (*I18nLocalizerService) GetLocalizer ¶
func (t *I18nLocalizerService) GetLocalizer(language language.Tag) (interface{}, bool, error)
GetLocalizer returns the requested localizer and a boolean indicating if the localizer was found If the requested localizer is not found, returns the default language localizer
func (*I18nLocalizerService) MustTranslate ¶
func (t *I18nLocalizerService) MustTranslate(localizer interface{}, message *Message) string
MustTranslate returns a localized message, panicking on error This is useful when you're confident the translation should always work
func (*I18nLocalizerService) Translate ¶
func (t *I18nLocalizerService) Translate(localizer interface{}, message *Message) (string, bool, error)
Translate returns a localized message for the given localizer and message Returns the translated message, a boolean indicating success, and an error if something went wrong
type LocalizerService ¶
type LocalizerService interface {
GetLocalizer(language language.Tag) (interface{}, bool, error)
Translate(localizer interface{}, message *Message) (string, bool, error)
MustTranslate(localizer interface{}, message *Message) string
}
LocalizerService defines the interface for handling localizers
func GetLocalizerService ¶
func GetLocalizerService() LocalizerService
GetLocalizerService is used to access the global service singleton
func NewI18n ¶
func NewI18n(defaultLang language.Tag, translationsPath string, filePrefixes ...string) (LocalizerService, error)
NewI18n returns a new instance of I18nLocalizerService with a custom file prefix defaultLang: the default language to use when a requested language is not available translationsPath: path to the directory containing translation files filePrefixes: the prefixes that translation files can have (e.g., "active" for "active.en.toml")
type Message ¶
type Message struct {
ID string
Data interface{}
PluralCount interface{}
}
Message represents a translatable message item
func NewMessage ¶
NewMessage creates a new Message instance with the given ID
func (*Message) WithPluralCount ¶
WithPluralCount sets the plural count for the message
type MockLocalizerService ¶
type MockLocalizerService struct {
// contains filtered or unexported fields
}
MockLocalizerService is a mock of LocalizerService interface.
func NewMockLocalizerService ¶
func NewMockLocalizerService(ctrl *gomock.Controller) *MockLocalizerService
NewMockLocalizerService creates a new mock instance.
func (*MockLocalizerService) EXPECT ¶
func (m *MockLocalizerService) EXPECT() *MockLocalizerServiceMockRecorder
EXPECT returns an object that allows the caller to indicate expected use.
func (*MockLocalizerService) GetLocalizer ¶
GetLocalizer mocks base method.
func (*MockLocalizerService) MustTranslate ¶
func (m *MockLocalizerService) MustTranslate(localizer any, message *Message) string
MustTranslate mocks base method.
type MockLocalizerServiceMockRecorder ¶
type MockLocalizerServiceMockRecorder struct {
// contains filtered or unexported fields
}
MockLocalizerServiceMockRecorder is the mock recorder for MockLocalizerService.
func (*MockLocalizerServiceMockRecorder) GetLocalizer ¶
func (mr *MockLocalizerServiceMockRecorder) GetLocalizer(arg0 any) *gomock.Call
GetLocalizer indicates an expected call of GetLocalizer.
func (*MockLocalizerServiceMockRecorder) MustTranslate ¶
func (mr *MockLocalizerServiceMockRecorder) MustTranslate(localizer, message any) *gomock.Call
MustTranslate indicates an expected call of MustTranslate.