Skip to content

Commit

Permalink
update example
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean Karn authored and Dean Karn committed Aug 29, 2016
1 parent 94bc451 commit 9cd1ab7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ import (
"fmt"

"github.com/go-playground/locales"
"github.com/go-playground/locales/en"
"github.com/go-playground/locales/en_CA"
"github.com/go-playground/locales/fr"
"github.com/go-playground/locales/nl"
"github.com/go-playground/universal-translator"
)

Expand All @@ -56,8 +60,8 @@ var universalTraslator *ut.UniversalTranslator
func main() {

// NOTE: this example is omitting allot of error checking for brevity

universalTraslator, _ = ut.New("en", "en", "en_CA", "nl", "fr")
e := en.New()
universalTraslator = ut.New(e, e, en_CA.New(), nl.New(), fr.New())

en := universalTraslator.GetTranslator("en")

Expand All @@ -72,21 +76,22 @@ func main() {
fmt.Println("Range Plural Rules:", en.PluralsRange())

// add basic language only translations
en.Add("welcome", "Welcome {0} to our test")
// last param indicates if it's ok to override the translation if one already exists
en.Add("welcome", "Welcome {0} to our test", false)

// add language translations dependant on cardinal plural rules
en.AddCardinal("days", "You have {0} day left to register", locales.PluralRuleOne)
en.AddCardinal("days", "You have {0} days left to register", locales.PluralRuleOther)
en.AddCardinal("days", "You have {0} day left to register", locales.PluralRuleOne, false)
en.AddCardinal("days", "You have {0} days left to register", locales.PluralRuleOther, false)

// add language translations dependant on ordinal plural rules
en.AddOrdinal("day-of-month", "{0}st", locales.PluralRuleOne)
en.AddOrdinal("day-of-month", "{0}nd", locales.PluralRuleTwo)
en.AddOrdinal("day-of-month", "{0}rd", locales.PluralRuleFew)
en.AddOrdinal("day-of-month", "{0}th", locales.PluralRuleOther)
en.AddOrdinal("day-of-month", "{0}st", locales.PluralRuleOne, false)
en.AddOrdinal("day-of-month", "{0}nd", locales.PluralRuleTwo, false)
en.AddOrdinal("day-of-month", "{0}rd", locales.PluralRuleFew, false)
en.AddOrdinal("day-of-month", "{0}th", locales.PluralRuleOther, false)

// add language translations dependant on range plural rules
// NOTE: only one plural rule for range in 'en' locale
en.AddRange("between", "It's {0}-{1} days away", locales.PluralRuleOther)
en.AddRange("between", "It's {0}-{1} days away", locales.PluralRuleOther, false)

// now lets use the translations we just added, in the same order we added them

Expand Down
25 changes: 15 additions & 10 deletions examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"fmt"

"github.com/go-playground/locales"
"github.com/go-playground/locales/en"
"github.com/go-playground/locales/en_CA"
"github.com/go-playground/locales/fr"
"github.com/go-playground/locales/nl"
"github.com/go-playground/universal-translator"
)

Expand All @@ -13,8 +17,8 @@ var universalTraslator *ut.UniversalTranslator
func main() {

// NOTE: this example is omitting allot of error checking for brevity

universalTraslator, _ = ut.New("en", "en", "en_CA", "nl", "fr")
e := en.New()
universalTraslator = ut.New(e, e, en_CA.New(), nl.New(), fr.New())

en := universalTraslator.GetTranslator("en")

Expand All @@ -29,21 +33,22 @@ func main() {
fmt.Println("Range Plural Rules:", en.PluralsRange())

// add basic language only translations
en.Add("welcome", "Welcome {0} to our test")
// last param indicates if it's ok to override the translation if one already exists
en.Add("welcome", "Welcome {0} to our test", false)

// add language translations dependant on cardinal plural rules
en.AddCardinal("days", "You have {0} day left to register", locales.PluralRuleOne)
en.AddCardinal("days", "You have {0} days left to register", locales.PluralRuleOther)
en.AddCardinal("days", "You have {0} day left to register", locales.PluralRuleOne, false)
en.AddCardinal("days", "You have {0} days left to register", locales.PluralRuleOther, false)

// add language translations dependant on ordinal plural rules
en.AddOrdinal("day-of-month", "{0}st", locales.PluralRuleOne)
en.AddOrdinal("day-of-month", "{0}nd", locales.PluralRuleTwo)
en.AddOrdinal("day-of-month", "{0}rd", locales.PluralRuleFew)
en.AddOrdinal("day-of-month", "{0}th", locales.PluralRuleOther)
en.AddOrdinal("day-of-month", "{0}st", locales.PluralRuleOne, false)
en.AddOrdinal("day-of-month", "{0}nd", locales.PluralRuleTwo, false)
en.AddOrdinal("day-of-month", "{0}rd", locales.PluralRuleFew, false)
en.AddOrdinal("day-of-month", "{0}th", locales.PluralRuleOther, false)

// add language translations dependant on range plural rules
// NOTE: only one plural rule for range in 'en' locale
en.AddRange("between", "It's {0}-{1} days away", locales.PluralRuleOther)
en.AddRange("between", "It's {0}-{1} days away", locales.PluralRuleOther, false)

// now lets use the translations we just added, in the same order we added them

Expand Down

0 comments on commit 9cd1ab7

Please sign in to comment.