Skip to content

Commit 9cd1ab7

Browse files
Dean KarnDean Karn
authored andcommitted
update example
1 parent 94bc451 commit 9cd1ab7

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

README.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ import (
4747
"fmt"
4848

4949
"github.com/go-playground/locales"
50+
"github.com/go-playground/locales/en"
51+
"github.com/go-playground/locales/en_CA"
52+
"github.com/go-playground/locales/fr"
53+
"github.com/go-playground/locales/nl"
5054
"github.com/go-playground/universal-translator"
5155
)
5256

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

5862
// NOTE: this example is omitting allot of error checking for brevity
59-
60-
universalTraslator, _ = ut.New("en", "en", "en_CA", "nl", "fr")
63+
e := en.New()
64+
universalTraslator = ut.New(e, e, en_CA.New(), nl.New(), fr.New())
6165

6266
en := universalTraslator.GetTranslator("en")
6367

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

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

7782
// add language translations dependant on cardinal plural rules
78-
en.AddCardinal("days", "You have {0} day left to register", locales.PluralRuleOne)
79-
en.AddCardinal("days", "You have {0} days left to register", locales.PluralRuleOther)
83+
en.AddCardinal("days", "You have {0} day left to register", locales.PluralRuleOne, false)
84+
en.AddCardinal("days", "You have {0} days left to register", locales.PluralRuleOther, false)
8085

8186
// add language translations dependant on ordinal plural rules
82-
en.AddOrdinal("day-of-month", "{0}st", locales.PluralRuleOne)
83-
en.AddOrdinal("day-of-month", "{0}nd", locales.PluralRuleTwo)
84-
en.AddOrdinal("day-of-month", "{0}rd", locales.PluralRuleFew)
85-
en.AddOrdinal("day-of-month", "{0}th", locales.PluralRuleOther)
87+
en.AddOrdinal("day-of-month", "{0}st", locales.PluralRuleOne, false)
88+
en.AddOrdinal("day-of-month", "{0}nd", locales.PluralRuleTwo, false)
89+
en.AddOrdinal("day-of-month", "{0}rd", locales.PluralRuleFew, false)
90+
en.AddOrdinal("day-of-month", "{0}th", locales.PluralRuleOther, false)
8691

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

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

examples/basic/main.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import (
44
"fmt"
55

66
"github.com/go-playground/locales"
7+
"github.com/go-playground/locales/en"
8+
"github.com/go-playground/locales/en_CA"
9+
"github.com/go-playground/locales/fr"
10+
"github.com/go-playground/locales/nl"
711
"github.com/go-playground/universal-translator"
812
)
913

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

1519
// NOTE: this example is omitting allot of error checking for brevity
16-
17-
universalTraslator, _ = ut.New("en", "en", "en_CA", "nl", "fr")
20+
e := en.New()
21+
universalTraslator = ut.New(e, e, en_CA.New(), nl.New(), fr.New())
1822

1923
en := universalTraslator.GetTranslator("en")
2024

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

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

3439
// add language translations dependant on cardinal plural rules
35-
en.AddCardinal("days", "You have {0} day left to register", locales.PluralRuleOne)
36-
en.AddCardinal("days", "You have {0} days left to register", locales.PluralRuleOther)
40+
en.AddCardinal("days", "You have {0} day left to register", locales.PluralRuleOne, false)
41+
en.AddCardinal("days", "You have {0} days left to register", locales.PluralRuleOther, false)
3742

3843
// add language translations dependant on ordinal plural rules
39-
en.AddOrdinal("day-of-month", "{0}st", locales.PluralRuleOne)
40-
en.AddOrdinal("day-of-month", "{0}nd", locales.PluralRuleTwo)
41-
en.AddOrdinal("day-of-month", "{0}rd", locales.PluralRuleFew)
42-
en.AddOrdinal("day-of-month", "{0}th", locales.PluralRuleOther)
44+
en.AddOrdinal("day-of-month", "{0}st", locales.PluralRuleOne, false)
45+
en.AddOrdinal("day-of-month", "{0}nd", locales.PluralRuleTwo, false)
46+
en.AddOrdinal("day-of-month", "{0}rd", locales.PluralRuleFew, false)
47+
en.AddOrdinal("day-of-month", "{0}th", locales.PluralRuleOther, false)
4348

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

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

0 commit comments

Comments
 (0)