podman
148 строк · 4.6 Кб
1package ut
2
3import (
4"errors"
5"fmt"
6
7"github.com/go-playground/locales"
8)
9
10var (
11// ErrUnknowTranslation indicates the translation could not be found
12ErrUnknowTranslation = errors.New("Unknown Translation")
13)
14
15var _ error = new(ErrConflictingTranslation)
16var _ error = new(ErrRangeTranslation)
17var _ error = new(ErrOrdinalTranslation)
18var _ error = new(ErrCardinalTranslation)
19var _ error = new(ErrMissingPluralTranslation)
20var _ error = new(ErrExistingTranslator)
21
22// ErrExistingTranslator is the error representing a conflicting translator
23type ErrExistingTranslator struct {
24locale string
25}
26
27// Error returns ErrExistingTranslator's internal error text
28func (e *ErrExistingTranslator) Error() string {
29return fmt.Sprintf("error: conflicting translator for locale '%s'", e.locale)
30}
31
32// ErrConflictingTranslation is the error representing a conflicting translation
33type ErrConflictingTranslation struct {
34locale string
35key interface{}
36rule locales.PluralRule
37text string
38}
39
40// Error returns ErrConflictingTranslation's internal error text
41func (e *ErrConflictingTranslation) Error() string {
42
43if _, ok := e.key.(string); !ok {
44return fmt.Sprintf("error: conflicting key '%#v' rule '%s' with text '%s' for locale '%s', value being ignored", e.key, e.rule, e.text, e.locale)
45}
46
47return fmt.Sprintf("error: conflicting key '%s' rule '%s' with text '%s' for locale '%s', value being ignored", e.key, e.rule, e.text, e.locale)
48}
49
50// ErrRangeTranslation is the error representing a range translation error
51type ErrRangeTranslation struct {
52text string
53}
54
55// Error returns ErrRangeTranslation's internal error text
56func (e *ErrRangeTranslation) Error() string {
57return e.text
58}
59
60// ErrOrdinalTranslation is the error representing an ordinal translation error
61type ErrOrdinalTranslation struct {
62text string
63}
64
65// Error returns ErrOrdinalTranslation's internal error text
66func (e *ErrOrdinalTranslation) Error() string {
67return e.text
68}
69
70// ErrCardinalTranslation is the error representing a cardinal translation error
71type ErrCardinalTranslation struct {
72text string
73}
74
75// Error returns ErrCardinalTranslation's internal error text
76func (e *ErrCardinalTranslation) Error() string {
77return e.text
78}
79
80// ErrMissingPluralTranslation is the error signifying a missing translation given
81// the locales plural rules.
82type ErrMissingPluralTranslation struct {
83locale string
84key interface{}
85rule locales.PluralRule
86translationType string
87}
88
89// Error returns ErrMissingPluralTranslation's internal error text
90func (e *ErrMissingPluralTranslation) Error() string {
91
92if _, ok := e.key.(string); !ok {
93return fmt.Sprintf("error: missing '%s' plural rule '%s' for translation with key '%#v' and locale '%s'", e.translationType, e.rule, e.key, e.locale)
94}
95
96return fmt.Sprintf("error: missing '%s' plural rule '%s' for translation with key '%s' and locale '%s'", e.translationType, e.rule, e.key, e.locale)
97}
98
99// ErrMissingBracket is the error representing a missing bracket in a translation
100// eg. This is a {0 <-- missing ending '}'
101type ErrMissingBracket struct {
102locale string
103key interface{}
104text string
105}
106
107// Error returns ErrMissingBracket error message
108func (e *ErrMissingBracket) Error() string {
109return fmt.Sprintf("error: missing bracket '{}', in translation. locale: '%s' key: '%v' text: '%s'", e.locale, e.key, e.text)
110}
111
112// ErrBadParamSyntax is the error representing a bad parameter definition in a translation
113// eg. This is a {must-be-int}
114type ErrBadParamSyntax struct {
115locale string
116param string
117key interface{}
118text string
119}
120
121// Error returns ErrBadParamSyntax error message
122func (e *ErrBadParamSyntax) Error() string {
123return fmt.Sprintf("error: bad parameter syntax, missing parameter '%s' in translation. locale: '%s' key: '%v' text: '%s'", e.param, e.locale, e.key, e.text)
124}
125
126// import/export errors
127
128// ErrMissingLocale is the error representing an expected locale that could
129// not be found aka locale not registered with the UniversalTranslator Instance
130type ErrMissingLocale struct {
131locale string
132}
133
134// Error returns ErrMissingLocale's internal error text
135func (e *ErrMissingLocale) Error() string {
136return fmt.Sprintf("error: locale '%s' not registered.", e.locale)
137}
138
139// ErrBadPluralDefinition is the error representing an incorrect plural definition
140// usually found within translations defined within files during the import process.
141type ErrBadPluralDefinition struct {
142tl translation
143}
144
145// Error returns ErrBadPluralDefinition's internal error text
146func (e *ErrBadPluralDefinition) Error() string {
147return fmt.Sprintf("error: bad plural definition '%#v'", e.tl)
148}
149