errors.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package ut
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/go-playground/locales"
  6. )
  7. var (
  8. // ErrUnknowTranslation indicates the translation could not be found
  9. ErrUnknowTranslation = errors.New("Unknown Translation")
  10. )
  11. var _ error = new(ErrConflictingTranslation)
  12. var _ error = new(ErrRangeTranslation)
  13. var _ error = new(ErrOrdinalTranslation)
  14. var _ error = new(ErrCardinalTranslation)
  15. var _ error = new(ErrMissingPluralTranslation)
  16. var _ error = new(ErrExistingTranslator)
  17. // ErrExistingTranslator is the error representing a conflicting translator
  18. type ErrExistingTranslator struct {
  19. locale string
  20. }
  21. // Error returns ErrExistingTranslator's internal error text
  22. func (e *ErrExistingTranslator) Error() string {
  23. return fmt.Sprintf("error: conflicting translator for locale '%s'", e.locale)
  24. }
  25. // ErrConflictingTranslation is the error representing a conflicting translation
  26. type ErrConflictingTranslation struct {
  27. key interface{}
  28. rule locales.PluralRule
  29. text string
  30. }
  31. // Error returns ErrConflictingTranslation's internal error text
  32. func (e *ErrConflictingTranslation) Error() string {
  33. if _, ok := e.key.(string); !ok {
  34. return fmt.Sprintf("error: conflicting key '%#v' rule '%s' with text '%s', value being ignored", e.key, e.rule, e.text)
  35. }
  36. return fmt.Sprintf("error: conflicting key '%s' rule '%s' with text '%s', value being ignored", e.key, e.rule, e.text)
  37. }
  38. // ErrRangeTranslation is the error representing a range translation error
  39. type ErrRangeTranslation struct {
  40. text string
  41. }
  42. // Error returns ErrRangeTranslation's internal error text
  43. func (e *ErrRangeTranslation) Error() string {
  44. return e.text
  45. }
  46. // ErrOrdinalTranslation is the error representing an ordinal translation error
  47. type ErrOrdinalTranslation struct {
  48. text string
  49. }
  50. // Error returns ErrOrdinalTranslation's internal error text
  51. func (e *ErrOrdinalTranslation) Error() string {
  52. return e.text
  53. }
  54. // ErrCardinalTranslation is the error representing a cardinal translation error
  55. type ErrCardinalTranslation struct {
  56. text string
  57. }
  58. // Error returns ErrCardinalTranslation's internal error text
  59. func (e *ErrCardinalTranslation) Error() string {
  60. return e.text
  61. }
  62. // ErrMissingPluralTranslation is the error signifying a missing translation given
  63. // the locales plural rules.
  64. type ErrMissingPluralTranslation struct {
  65. key interface{}
  66. rule locales.PluralRule
  67. translationType string
  68. }
  69. // Error returns ErrMissingPluralTranslation's internal error text
  70. func (e *ErrMissingPluralTranslation) Error() string {
  71. if _, ok := e.key.(string); !ok {
  72. return fmt.Sprintf("error: missing '%s' plural rule '%s' for translation with key '%#v'", e.translationType, e.rule, e.key)
  73. }
  74. return fmt.Sprintf("error: missing '%s' plural rule '%s' for translation with key '%s'", e.translationType, e.rule, e.key)
  75. }
  76. // import/export errors
  77. // ErrMissingLocale is the error representing an expected locale that could
  78. // not be found aka locale not registered with the UniversalTranslator Instance
  79. type ErrMissingLocale struct {
  80. locale string
  81. }
  82. // Error returns ErrMissingLocale's internal error text
  83. func (e *ErrMissingLocale) Error() string {
  84. return fmt.Sprintf("error: locale '%s' not registered.", e.locale)
  85. }
  86. // ErrBadPluralDefinition is the error representing an incorrect plural definition
  87. // usually found within translations defined within files during the import process.
  88. type ErrBadPluralDefinition struct {
  89. tl translation
  90. }
  91. // Error returns ErrBadPluralDefinition's internal error text
  92. func (e *ErrBadPluralDefinition) Error() string {
  93. return fmt.Sprintf("error: bad plural definition '%#v'", e.tl)
  94. }