main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/go-playground/universal-translator"
  6. // DONE this way to avoid huge compile times + memory for all languages, although it would
  7. // be nice for all applications to support all languages... that's not reality
  8. _ "github.com/go-playground/universal-translator/resources/locales"
  9. )
  10. func main() {
  11. trans, _ := ut.GetTranslator("en")
  12. trans.PrintPluralRules()
  13. // OUTPUT:
  14. // Translator locale 'en' supported rules:
  15. //- PluralRuleOne
  16. //- PluralRuleOther
  17. // add a singular translation
  18. trans.Add(ut.PluralRuleOne, "homepage", "welcome_msg", "Welcome to site %s")
  19. // add singular + plural translation(s)
  20. trans.Add(ut.PluralRuleOne, "homepage", "day_warning", "You only have %d day left in your trial")
  21. trans.Add(ut.PluralRuleOther, "homepage", "day_warning", "You only have %d day's left in your trial")
  22. // translate singular
  23. translated := trans.T("welcome_msg", "Joey Bloggs")
  24. fmt.Println(translated)
  25. // OUTPUT: Welcome to site Joey Bloggs
  26. // What if something went wrong? then translated would output "" (blank)
  27. // How do I catch errors?
  28. translated, err := trans.TSafe("welcome_m", "Joey Bloggs")
  29. fmt.Println(translated)
  30. // OUTPUT: ""
  31. fmt.Println(err)
  32. // OUTPUT: ***** WARNING:***** Translation Key 'welcome_m' Not Found
  33. // NOTE: there is a Safe variant of most of the Translation and Formatting functions if you need them,
  34. // for brevity will be using the non safe ones for the rest of this example
  35. // The second parameter below, count, is needed as the final variable is a varadic and would not
  36. // know which one to use in applying the plural rules.
  37. // translate singular/plural
  38. translated = trans.P("day_warning", 3, 3)
  39. fmt.Println(translated)
  40. // OUTPUT: You only have 3 day's left in your trial
  41. translated = trans.P("day_warning", 1, 1)
  42. fmt.Println(translated)
  43. // OUTPUT: You only have 1 day left in your trial
  44. // There are Full, Long, Medium and Short function for each of the following
  45. dtString := "Jan 2, 2006 at 3:04:05pm"
  46. dt, _ := time.Parse(dtString, dtString)
  47. formatted := trans.FmtDateFull(dt)
  48. fmt.Println(formatted)
  49. // OUTPUT: Monday, January 2, 2006
  50. formatted = trans.FmtDateShort(dt)
  51. fmt.Println(formatted)
  52. // OUTPUT: 1/2/06
  53. formatted = trans.FmtTimeFull(dt)
  54. fmt.Println(formatted)
  55. // OUTPUT: 3:04:05 PM
  56. formatted = trans.FmtDateTimeFull(dt)
  57. fmt.Println(formatted)
  58. // OUTPUT: Monday, January 2, 2006 at 3:04:05 PM
  59. formatted = trans.FmtCurrency(ut.CurrencyStandard, "USD", 1234.50)
  60. fmt.Println(formatted)
  61. // OUTPUT: $1,234.50
  62. formatted = trans.FmtCurrency(ut.CurrencyStandard, "USD", -1234.50)
  63. fmt.Println(formatted)
  64. // OUTPUT: -$1,234.50
  65. formatted = trans.FmtCurrency(ut.CurrencyAccounting, "USD", -1234.50)
  66. fmt.Println(formatted)
  67. // OUTPUT: ($1,234.50)
  68. formatted = trans.FmtCurrencyWhole(ut.CurrencyStandard, "USD", -1234.50)
  69. fmt.Println(formatted)
  70. // OUTPUT: -$1,234
  71. formatted = trans.FmtNumber(1234.50)
  72. fmt.Println(formatted)
  73. // OUTPUT: 1,234.5
  74. formatted = trans.FmtNumberWhole(1234.50)
  75. fmt.Println(formatted)
  76. // OUTPUT: 1,234
  77. }