main.go 799 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "golang.org/x/text/message"
  6. "golang.org/x/text/language"
  7. _ "golang.org/x/text/message"
  8. )
  9. //go:generate gotext -srclang=en update -out=catalog.go -lang=fr,en
  10. type ErrorKind int
  11. const (
  12. NoError ErrorKind = iota
  13. Unimplemented
  14. )
  15. var Errors = map[ErrorKind]string{
  16. NoError: "No error",
  17. Unimplemented: "Not yet implemented",
  18. }
  19. type Error struct {
  20. error
  21. Kind ErrorKind
  22. }
  23. func MakeError(kind ErrorKind, detail string) Error {
  24. var message string
  25. if len(detail) == 0 {
  26. message = Errors[kind]
  27. } else {
  28. message = fmt.Sprintf("%s: %s", Errors[kind], detail)
  29. }
  30. return Error{
  31. Kind: kind,
  32. error: errors.New(message),
  33. }
  34. }
  35. func main() {
  36. p := message.NewPrinter(language.French)
  37. err := MakeError(NoError, "all is well")
  38. p.Printf(err)
  39. }