...

Source file src/github.com/go-playground/universal-translator/_examples/basic/main.go

Documentation: github.com/go-playground/universal-translator/_examples/basic

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"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"
    11  	"github.com/go-playground/universal-translator"
    12  )
    13  
    14  // only one instance as translators within are shared + goroutine safe
    15  var universalTraslator *ut.UniversalTranslator
    16  
    17  func main() {
    18  
    19  	// NOTE: this example is omitting a lot of error checking for brevity
    20  	e := en.New()
    21  	universalTraslator = ut.New(e, e, en_CA.New(), nl.New(), fr.New())
    22  
    23  	en, _ := universalTraslator.GetTranslator("en")
    24  
    25  	// generally used after parsing an http 'Accept-Language' header
    26  	// and this will try to find a matching locale you support or
    27  	// fallback locale.
    28  	// en, _ := ut.FindTranslator([]string{"en", "en_CA", "nl"})
    29  
    30  	// this will help
    31  	fmt.Println("Cardinal Plural Rules:", en.PluralsCardinal())
    32  	fmt.Println("Ordinal Plural Rules:", en.PluralsOrdinal())
    33  	fmt.Println("Range Plural Rules:", en.PluralsRange())
    34  
    35  	// add basic language only translations
    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)
    38  
    39  	// add language translations dependant on cardinal plural rules
    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)
    42  
    43  	// add language translations dependant on ordinal plural rules
    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)
    48  
    49  	// add language translations dependant on range plural rules
    50  	// NOTE: only one plural rule for range in 'en' locale
    51  	en.AddRange("between", "It's {0}-{1} days away", locales.PluralRuleOther, false)
    52  
    53  	// now lets use the translations we just added, in the same order we added them
    54  
    55  	fmt.Println(en.T("welcome", "Joeybloggs"))
    56  
    57  	fmt.Println(en.C("days", 1, 0, en.FmtNumber(1, 0))) // you'd normally have variables defined for 1 and 0
    58  	fmt.Println(en.C("days", 2, 0, en.FmtNumber(2, 0)))
    59  	fmt.Println(en.C("days", 10456.25, 2, en.FmtNumber(10456.25, 2)))
    60  
    61  	fmt.Println(en.O("day-of-month", 1, 0, en.FmtNumber(1, 0)))
    62  	fmt.Println(en.O("day-of-month", 2, 0, en.FmtNumber(2, 0)))
    63  	fmt.Println(en.O("day-of-month", 3, 0, en.FmtNumber(3, 0)))
    64  	fmt.Println(en.O("day-of-month", 4, 0, en.FmtNumber(4, 0)))
    65  	fmt.Println(en.O("day-of-month", 10456.25, 0, en.FmtNumber(10456.25, 0)))
    66  
    67  	fmt.Println(en.R("between", 0, 0, 1, 0, en.FmtNumber(0, 0), en.FmtNumber(1, 0)))
    68  	fmt.Println(en.R("between", 1, 0, 2, 0, en.FmtNumber(1, 0), en.FmtNumber(2, 0)))
    69  	fmt.Println(en.R("between", 1, 0, 100, 0, en.FmtNumber(1, 0), en.FmtNumber(100, 0)))
    70  }
    71  

View as plain text