-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18n.go
More file actions
68 lines (56 loc) · 1.47 KB
/
i18n.go
File metadata and controls
68 lines (56 loc) · 1.47 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package schema
import (
"fmt"
"github.com/nyxstack/i18n"
)
// ErrorMessage represents either a static string or an i18n translatable message
type ErrorMessage interface {
Resolve(ctx *ValidationContext) string
}
// StaticMessage represents a static error message
type StaticMessage string
func (s StaticMessage) Resolve(ctx *ValidationContext) string {
return string(s)
}
// I18nMessage represents an i18n translatable message
type I18nMessage i18n.TranslatedFunc
func (i I18nMessage) Resolve(ctx *ValidationContext) string {
return i18n.TranslatedFunc(i)(ctx.Locale)
}
// Msg creates a static error message
func Msg(message string) ErrorMessage {
return StaticMessage(message)
}
// I18n creates an i18n error message
func I18n(translatedFunc i18n.TranslatedFunc) ErrorMessage {
return I18nMessage(translatedFunc)
}
// Helper function to convert string or i18n function to ErrorMessage
func toErrorMessage(input interface{}) ErrorMessage {
switch v := input.(type) {
case string:
if v == "" {
return nil
}
return Msg(v)
case ErrorMessage:
return v
case i18n.TranslatedFunc:
return I18n(v)
case nil:
return nil
default:
return Msg(fmt.Sprintf("%v", v))
}
}
// Helper function to check if ErrorMessage is empty/nil
func isEmptyErrorMessage(em ErrorMessage) bool {
return em == nil
}
// Helper function to resolve ErrorMessage to string
func resolveErrorMessage(em ErrorMessage, ctx *ValidationContext) string {
if em == nil {
return ""
}
return em.Resolve(ctx)
}