main.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/go-playground/locales"
  5. "github.com/go-playground/universal-translator"
  6. )
  7. // only one instance as translators within are shared + goroutine safe
  8. var universalTraslator *ut.UniversalTranslator
  9. func main() {
  10. // NOTE: this example is omitting allot of error checking for brevity
  11. universalTraslator, _ = ut.New("en", "en", "en_CA", "nl", "fr")
  12. en := universalTraslator.GetTranslator("en")
  13. // generally used after parsing an http 'Accept-Language' header
  14. // and this will try to find a matching locale you support or
  15. // fallback locale.
  16. // en, _ := ut.FindTranslator([]string{"en", "en_CA", "nl"})
  17. // this will help
  18. fmt.Println("Cardinal Plural Rules:", en.PluralsCardinal())
  19. fmt.Println("Ordinal Plural Rules:", en.PluralsOrdinal())
  20. fmt.Println("Range Plural Rules:", en.PluralsRange())
  21. // add basic language only translations
  22. en.Add("welcome", "Welcome {0} to our test")
  23. // add language translations dependant on cardinal plural rules
  24. en.AddCardinal("days", "You have {0} day left to register", locales.PluralRuleOne)
  25. en.AddCardinal("days", "You have {0} days left to register", locales.PluralRuleOther)
  26. // add language translations dependant on ordinal plural rules
  27. en.AddOrdinal("day-of-month", "{0}st", locales.PluralRuleOne)
  28. en.AddOrdinal("day-of-month", "{0}nd", locales.PluralRuleTwo)
  29. en.AddOrdinal("day-of-month", "{0}rd", locales.PluralRuleFew)
  30. en.AddOrdinal("day-of-month", "{0}th", locales.PluralRuleOther)
  31. // add language translations dependant on range plural rules
  32. // NOTE: only one plural rule for range in 'en' locale
  33. en.AddRange("between", "It's {0}-{1} days away", locales.PluralRuleOther)
  34. // now lets use the translations we just added, in the same order we added them
  35. fmt.Println(en.T("welcome", "Joeybloggs"))
  36. fmt.Println(en.C("days", 1, 0, string(en.FmtNumber(1, 0)))) // you'd normally have variables defined for 1 and 0
  37. fmt.Println(en.C("days", 2, 0, string(en.FmtNumber(2, 0))))
  38. fmt.Println(en.C("days", 10456.25, 2, string(en.FmtNumber(10456.25, 2))))
  39. fmt.Println(en.O("day-of-month", 1, 0, string(en.FmtNumber(1, 0))))
  40. fmt.Println(en.O("day-of-month", 2, 0, string(en.FmtNumber(2, 0))))
  41. fmt.Println(en.O("day-of-month", 3, 0, string(en.FmtNumber(3, 0))))
  42. fmt.Println(en.O("day-of-month", 4, 0, string(en.FmtNumber(4, 0))))
  43. fmt.Println(en.O("day-of-month", 10456.25, 0, string(en.FmtNumber(10456.25, 0))))
  44. fmt.Println(en.R("between", 0, 0, 1, 0, string(en.FmtNumber(0, 0)), string(en.FmtNumber(1, 0))))
  45. fmt.Println(en.R("between", 1, 0, 2, 0, string(en.FmtNumber(1, 0)), string(en.FmtNumber(2, 0))))
  46. fmt.Println(en.R("between", 1, 0, 100, 0, string(en.FmtNumber(1, 0)), string(en.FmtNumber(100, 0))))
  47. }