6 "github.com/go-playground/locales"
9 // UniversalTranslator holds all locale & translation data
10 type UniversalTranslator struct {
11 translators map[string]Translator
15 // New returns a new UniversalTranslator instance set with
16 // the fallback locale and locales it should support
17 func New(fallback locales.Translator, supportedLocales ...locales.Translator) *UniversalTranslator {
19 t := &UniversalTranslator{
20 translators: make(map[string]Translator),
23 for _, v := range supportedLocales {
25 trans := newTranslator(v)
26 t.translators[strings.ToLower(trans.Locale())] = trans
28 if fallback.Locale() == v.Locale() {
33 if t.fallback == nil && fallback != nil {
34 t.fallback = newTranslator(fallback)
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.
43 func (t *UniversalTranslator) FindTranslator(locales ...string) (trans Translator, found bool) {
45 for _, locale := range locales {
47 if trans, found = t.translators[strings.ToLower(locale)]; found {
52 return t.fallback, false
55 // GetTranslator returns the specified translator for the given locale,
56 // or fallback if not found
57 func (t *UniversalTranslator) GetTranslator(locale string) (trans Translator, found bool) {
59 if trans, found = t.translators[strings.ToLower(locale)]; found {
63 return t.fallback, false
66 // GetFallback returns the fallback locale
67 func (t *UniversalTranslator) GetFallback() Translator {
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
75 func (t *UniversalTranslator) AddTranslator(translator locales.Translator, override bool) error {
77 lc := strings.ToLower(translator.Locale())
78 _, ok := t.translators[lc]
80 return &ErrExistingTranslator{locale: translator.Locale()}
83 trans := newTranslator(translator)
85 if t.fallback.Locale() == translator.Locale() {
87 // because it's optional to have a fallback, I don't impose that limitation
88 // don't know why you wouldn't but...
90 return &ErrExistingTranslator{locale: translator.Locale()}
96 t.translators[lc] = trans
101 // VerifyTranslations runs through all locales and identifies any issues
102 // eg. missing plural rules for a locale
103 func (t *UniversalTranslator) VerifyTranslations() (err error) {
105 for _, trans := range t.translators {
106 err = trans.VerifyTranslations()