errors.go raw

   1  package ut
   2  
   3  import (
   4  	"errors"
   5  	"fmt"
   6  
   7  	"github.com/go-playground/locales"
   8  )
   9  
  10  var (
  11  	// ErrUnknowTranslation indicates the translation could not be found
  12  	ErrUnknowTranslation = errors.New("Unknown Translation")
  13  )
  14  
  15  var _ error = new(ErrConflictingTranslation)
  16  var _ error = new(ErrRangeTranslation)
  17  var _ error = new(ErrOrdinalTranslation)
  18  var _ error = new(ErrCardinalTranslation)
  19  var _ error = new(ErrMissingPluralTranslation)
  20  var _ error = new(ErrExistingTranslator)
  21  
  22  // ErrExistingTranslator is the error representing a conflicting translator
  23  type ErrExistingTranslator struct {
  24  	locale string
  25  }
  26  
  27  // Error returns ErrExistingTranslator's internal error text
  28  func (e *ErrExistingTranslator) Error() string {
  29  	return fmt.Sprintf("error: conflicting translator for locale '%s'", e.locale)
  30  }
  31  
  32  // ErrConflictingTranslation is the error representing a conflicting translation
  33  type ErrConflictingTranslation struct {
  34  	locale string
  35  	key    interface{}
  36  	rule   locales.PluralRule
  37  	text   string
  38  }
  39  
  40  // Error returns ErrConflictingTranslation's internal error text
  41  func (e *ErrConflictingTranslation) Error() string {
  42  
  43  	if _, ok := e.key.(string); !ok {
  44  		return 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  
  47  	return 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
  51  type ErrRangeTranslation struct {
  52  	text string
  53  }
  54  
  55  // Error returns ErrRangeTranslation's internal error text
  56  func (e *ErrRangeTranslation) Error() string {
  57  	return e.text
  58  }
  59  
  60  // ErrOrdinalTranslation is the error representing an ordinal translation error
  61  type ErrOrdinalTranslation struct {
  62  	text string
  63  }
  64  
  65  // Error returns ErrOrdinalTranslation's internal error text
  66  func (e *ErrOrdinalTranslation) Error() string {
  67  	return e.text
  68  }
  69  
  70  // ErrCardinalTranslation is the error representing a cardinal translation error
  71  type ErrCardinalTranslation struct {
  72  	text string
  73  }
  74  
  75  // Error returns ErrCardinalTranslation's internal error text
  76  func (e *ErrCardinalTranslation) Error() string {
  77  	return e.text
  78  }
  79  
  80  // ErrMissingPluralTranslation is the error signifying a missing translation given
  81  // the locales plural rules.
  82  type ErrMissingPluralTranslation struct {
  83  	locale          string
  84  	key             interface{}
  85  	rule            locales.PluralRule
  86  	translationType string
  87  }
  88  
  89  // Error returns ErrMissingPluralTranslation's internal error text
  90  func (e *ErrMissingPluralTranslation) Error() string {
  91  
  92  	if _, ok := e.key.(string); !ok {
  93  		return 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  
  96  	return 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 '}'
 101  type ErrMissingBracket struct {
 102  	locale string
 103  	key    interface{}
 104  	text   string
 105  }
 106  
 107  // Error returns ErrMissingBracket error message
 108  func (e *ErrMissingBracket) Error() string {
 109  	return 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}
 114  type ErrBadParamSyntax struct {
 115  	locale string
 116  	param  string
 117  	key    interface{}
 118  	text   string
 119  }
 120  
 121  // Error returns ErrBadParamSyntax error message
 122  func (e *ErrBadParamSyntax) Error() string {
 123  	return 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
 130  type ErrMissingLocale struct {
 131  	locale string
 132  }
 133  
 134  // Error returns ErrMissingLocale's internal error text
 135  func (e *ErrMissingLocale) Error() string {
 136  	return 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.
 141  type ErrBadPluralDefinition struct {
 142  	tl translation
 143  }
 144  
 145  // Error returns ErrBadPluralDefinition's internal error text
 146  func (e *ErrBadPluralDefinition) Error() string {
 147  	return fmt.Sprintf("error: bad plural definition '%#v'", e.tl)
 148  }
 149