aboutsummaryrefslogtreecommitdiff
path: root/internal/locale/error.go
blob: eba4322775a9c44b6a4ff3241e3896f7d0f8e694 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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...)
}