error.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package locale // import "miniflux.app/v2/internal/locale"
  4. import "errors"
  5. type LocalizedErrorWrapper struct {
  6. originalErr error
  7. translationKey string
  8. translationArgs []any
  9. }
  10. func NewLocalizedErrorWrapper(originalErr error, translationKey string, translationArgs ...any) *LocalizedErrorWrapper {
  11. return &LocalizedErrorWrapper{
  12. originalErr: originalErr,
  13. translationKey: translationKey,
  14. translationArgs: translationArgs,
  15. }
  16. }
  17. func (l *LocalizedErrorWrapper) Error() error {
  18. return l.originalErr
  19. }
  20. func (l *LocalizedErrorWrapper) Translate(language string) string {
  21. if l.translationKey == "" {
  22. return l.originalErr.Error()
  23. }
  24. return NewPrinter(language).Printf(l.translationKey, l.translationArgs...)
  25. }
  26. type LocalizedError struct {
  27. translationKey string
  28. translationArgs []any
  29. }
  30. func NewLocalizedError(translationKey string, translationArgs ...any) *LocalizedError {
  31. return &LocalizedError{translationKey: translationKey, translationArgs: translationArgs}
  32. }
  33. func (v *LocalizedError) String() string {
  34. return NewPrinter("en_US").Printf(v.translationKey, v.translationArgs...)
  35. }
  36. func (v *LocalizedError) Error() error {
  37. return errors.New(v.String())
  38. }
  39. func (v *LocalizedError) Translate(language string) string {
  40. return NewPrinter(language).Printf(v.translationKey, v.translationArgs...)
  41. }