podman
113 строк · 2.8 Кб
1package ut
2
3import (
4"strings"
5
6"github.com/go-playground/locales"
7)
8
9// UniversalTranslator holds all locale & translation data
10type UniversalTranslator struct {
11translators map[string]Translator
12fallback Translator
13}
14
15// New returns a new UniversalTranslator instance set with
16// the fallback locale and locales it should support
17func New(fallback locales.Translator, supportedLocales ...locales.Translator) *UniversalTranslator {
18
19t := &UniversalTranslator{
20translators: make(map[string]Translator),
21}
22
23for _, v := range supportedLocales {
24
25trans := newTranslator(v)
26t.translators[strings.ToLower(trans.Locale())] = trans
27
28if fallback.Locale() == v.Locale() {
29t.fallback = trans
30}
31}
32
33if t.fallback == nil && fallback != nil {
34t.fallback = newTranslator(fallback)
35}
36
37return t
38}
39
40// FindTranslator trys to find a Translator based on an array of locales
41// and returns the first one it can find, otherwise returns the
42// fallback translator.
43func (t *UniversalTranslator) FindTranslator(locales ...string) (trans Translator, found bool) {
44
45for _, locale := range locales {
46
47if trans, found = t.translators[strings.ToLower(locale)]; found {
48return
49}
50}
51
52return t.fallback, false
53}
54
55// GetTranslator returns the specified translator for the given locale,
56// or fallback if not found
57func (t *UniversalTranslator) GetTranslator(locale string) (trans Translator, found bool) {
58
59if trans, found = t.translators[strings.ToLower(locale)]; found {
60return
61}
62
63return t.fallback, false
64}
65
66// GetFallback returns the fallback locale
67func (t *UniversalTranslator) GetFallback() Translator {
68return t.fallback
69}
70
71// AddTranslator adds the supplied translator, if it already exists the override param
72// will be checked and if false an error will be returned, otherwise the translator will be
73// overridden; if the fallback matches the supplied translator it will be overridden as well
74// NOTE: this is normally only used when translator is embedded within a library
75func (t *UniversalTranslator) AddTranslator(translator locales.Translator, override bool) error {
76
77lc := strings.ToLower(translator.Locale())
78_, ok := t.translators[lc]
79if ok && !override {
80return &ErrExistingTranslator{locale: translator.Locale()}
81}
82
83trans := newTranslator(translator)
84
85if t.fallback.Locale() == translator.Locale() {
86
87// because it's optional to have a fallback, I don't impose that limitation
88// don't know why you wouldn't but...
89if !override {
90return &ErrExistingTranslator{locale: translator.Locale()}
91}
92
93t.fallback = trans
94}
95
96t.translators[lc] = trans
97
98return nil
99}
100
101// VerifyTranslations runs through all locales and identifies any issues
102// eg. missing plural rules for a locale
103func (t *UniversalTranslator) VerifyTranslations() (err error) {
104
105for _, trans := range t.translators {
106err = trans.VerifyTranslations()
107if err != nil {
108return
109}
110}
111
112return
113}
114