diff options
Diffstat (limited to 'internal/locale/error.go')
-rw-r--r-- | internal/locale/error.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/internal/locale/error.go b/internal/locale/error.go new file mode 100644 index 00000000..eba43227 --- /dev/null +++ b/internal/locale/error.go @@ -0,0 +1,52 @@ +// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +package locale // import "miniflux.app/v2/internal/locale" + +import "errors" + +type LocalizedErrorWrapper struct { + originalErr error + translationKey string + translationArgs []any +} + +func NewLocalizedErrorWrapper(originalErr error, translationKey string, translationArgs ...any) *LocalizedErrorWrapper { + return &LocalizedErrorWrapper{ + originalErr: originalErr, + translationKey: translationKey, + translationArgs: translationArgs, + } +} + +func (l *LocalizedErrorWrapper) Error() error { + return l.originalErr +} + +func (l *LocalizedErrorWrapper) Translate(language string) string { + if l.translationKey == "" { + return l.originalErr.Error() + } + return NewPrinter(language).Printf(l.translationKey, l.translationArgs...) +} + +type LocalizedError struct { + translationKey string + translationArgs []any +} + +func NewLocalizedError(translationKey string, translationArgs ...any) *LocalizedError { + return &LocalizedError{translationKey: translationKey, translationArgs: translationArgs} +} + +func (v *LocalizedError) String() string { + return NewPrinter("en_US").Printf(v.translationKey, v.translationArgs...) +} + +func (v *LocalizedError) Error() error { + return errors.New(v.String()) +} + +func (v *LocalizedError) Translate(language string) string { + return NewPrinter(language).Printf(v.translationKey, v.translationArgs...) +} |