diff options
author | 2023-10-21 19:50:29 -0700 | |
---|---|---|
committer | 2023-10-22 13:09:30 -0700 | |
commit | 14e25ab9fe09b9951b38e56af2bdff7a0737b280 (patch) | |
tree | 1e466305ccf868d0253b09895af29f811a3e3393 /internal/locale/error.go | |
parent | 120aabfbcef4ef453d70861aece3b107b603a911 (diff) | |
download | v2-14e25ab9fe09b9951b38e56af2bdff7a0737b280.tar.gz v2-14e25ab9fe09b9951b38e56af2bdff7a0737b280.tar.zst v2-14e25ab9fe09b9951b38e56af2bdff7a0737b280.zip |
Refactor HTTP Client and LocalizedError packages
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...) +} |