errors.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. locale string
  28. key interface{}
  29. rule locales.PluralRule
  30. text string
  31. }
  32. // Error returns ErrConflictingTranslation's internal error text
  33. func (e *ErrConflictingTranslation) Error() string {
  34. if _, ok := e.key.(string); !ok {
  35. 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)
  36. }
  37. 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)
  38. }
  39. // ErrRangeTranslation is the error representing a range translation error
  40. type ErrRangeTranslation struct {
  41. text string
  42. }
  43. // Error returns ErrRangeTranslation's internal error text
  44. func (e *ErrRangeTranslation) Error() string {
  45. return e.text
  46. }
  47. // ErrOrdinalTranslation is the error representing an ordinal translation error
  48. type ErrOrdinalTranslation struct {
  49. text string
  50. }
  51. // Error returns ErrOrdinalTranslation's internal error text
  52. func (e *ErrOrdinalTranslation) Error() string {
  53. return e.text
  54. }
  55. // ErrCardinalTranslation is the error representing a cardinal translation error
  56. type ErrCardinalTranslation struct {
  57. text string
  58. }
  59. // Error returns ErrCardinalTranslation's internal error text
  60. func (e *ErrCardinalTranslation) Error() string {
  61. return e.text
  62. }
  63. // ErrMissingPluralTranslation is the error signifying a missing translation given
  64. // the locales plural rules.
  65. type ErrMissingPluralTranslation struct {
  66. locale string
  67. key interface{}
  68. rule locales.PluralRule
  69. translationType string
  70. }
  71. // Error returns ErrMissingPluralTranslation's internal error text
  72. func (e *ErrMissingPluralTranslation) Error() string {
  73. if _, ok := e.key.(string); !ok {
  74. 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)
  75. }
  76. 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)
  77. }
  78. // ErrMissingBracket is the error representing a missing bracket in a translation
  79. // eg. This is a {0 <-- missing ending '}'
  80. type ErrMissingBracket struct {
  81. locale string
  82. key interface{}
  83. text string
  84. }
  85. // Error returns ErrMissingBracket error message
  86. func (e *ErrMissingBracket) Error() string {
  87. return fmt.Sprintf("error: missing bracket '{}', in translation. locale: '%s' key: '%v' text: '%s'", e.locale, e.key, e.text)
  88. }
  89. // ErrBadParamSyntax is the error representing a bad parameter definition in a translation
  90. // eg. This is a {must-be-int}
  91. type ErrBadParamSyntax struct {
  92. locale string
  93. param string
  94. key interface{}
  95. text string
  96. }
  97. // Error returns ErrBadParamSyntax error message
  98. func (e *ErrBadParamSyntax) Error() string {
  99. 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)
  100. }
  101. // import/export errors
  102. // ErrMissingLocale is the error representing an expected locale that could
  103. // not be found aka locale not registered with the UniversalTranslator Instance
  104. type ErrMissingLocale struct {
  105. locale string
  106. }
  107. // Error returns ErrMissingLocale's internal error text
  108. func (e *ErrMissingLocale) Error() string {
  109. return fmt.Sprintf("error: locale '%s' not registered.", e.locale)
  110. }
  111. // ErrBadPluralDefinition is the error representing an incorrect plural definition
  112. // usually found within translations defined within files during the import process.
  113. type ErrBadPluralDefinition struct {
  114. tl translation
  115. }
  116. // Error returns ErrBadPluralDefinition's internal error text
  117. func (e *ErrBadPluralDefinition) Error() string {
  118. return fmt.Sprintf("error: bad plural definition '%#v'", e.tl)
  119. }