11## universal-translator
22<img align =" right " src =" https://raw.githubusercontent.com/go-playground/universal-translator/master/logo.png " >
3- ![ Project status] ( https://img.shields.io/badge/version-0.14 .0-green.svg )
3+ ![ Project status] ( https://img.shields.io/badge/version-0.15 .0-green.svg )
44[ ![ Build Status] ( https://semaphoreci.com/api/v1/joeybloggs/universal-translator/branches/master/badge.svg )] ( https://semaphoreci.com/joeybloggs/universal-translator )
55[ ![ Coverage Status] ( https://coveralls.io/repos/github/go-playground/universal-translator/badge.svg )] ( https://coveralls.io/github/go-playground/universal-translator )
66[ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/go-playground/universal-translator )] ( https://goreportcard.com/report/github.com/go-playground/universal-translator )
@@ -25,8 +25,8 @@ Features
2525- [x] Contains Date & Time formatting functions
2626- [x] Contains Number, Currency, Accounting and Percent formatting functions
2727- [x] Supports the "Gregorian" calendar only ( my time isn't unlimited, had to draw the line somewhere )
28- - [ ] Support loading translations from files
29- - [ ] Exporting translations to file, mainly for getting them professionally translated
28+ - [x ] Support loading translations from files
29+ - [x ] Exporting translations to file(s) , mainly for getting them professionally translated
3030- [ ] Code Generation for translation files -> Go code.. i.e. after it has been professionally translated
3131- [ ] Tests for all languages, I need help with this, please see [ here] ( https://github.com/go-playground/locales/issues/1 )
3232
@@ -35,84 +35,50 @@ Installation
3535
3636Use go get
3737
38- ``` go
38+ ``` shell
3939go get github.com/go-playground/universal-translator
4040```
4141
42- Usage
42+ Usage & Documentation
4343-------
44- ``` go
45- package main
4644
47- import (
48- " fmt"
45+ Please see https://godoc.org/github.com/go-playground/universal-translator for usage docs
4946
50- " github.com/go-playground/locales"
51- " github.com/go-playground/locales/en"
52- " github.com/go-playground/locales/en_CA"
53- " github.com/go-playground/locales/fr"
54- " github.com/go-playground/locales/nl"
55- " github.com/go-playground/universal-translator"
56- )
47+ ##### Examples:
5748
58- // only one instance as translators within are shared + goroutine safe
59- var universalTraslator *ut.UniversalTranslator
49+ - [ Basic] ( https://github.com/go-playground/universal-translator/tree/master/examples/basic )
50+ - [ Full - no files] ( https://github.com/go-playground/universal-translator/tree/master/examples/full-no-files )
51+ - [ Full - with files] ( https://github.com/go-playground/universal-translator/tree/master/examples/full-with-files )
6052
61- func main () {
53+ File formatting
54+ --------------
55+ All types, Plain substitution, Cardinal, Ordinal and Range translations can all be contained withing the same file(s);
56+ they are only separated for easy viewing.
6257
63- // NOTE: this example is omitting a lot of error checking for brevity
64- e := en.New ()
65- universalTraslator = ut.New (e, e, en_CA.New (), nl.New (), fr.New ())
58+ ##### Examples:
6659
67- en , _ := universalTraslator. GetTranslator ( " en " )
60+ - [ Formats ] ( https://github.com/go-playground/universal-translator/tree/master/examples/file-formats )
6861
69- // generally used after parsing an http 'Accept-Language' header
70- // and this will try to find a matching locale you support or
71- // fallback locale.
72- // en, _ := ut.FindTranslator([]string{"en", "en_CA", "nl"})
73-
74- // this will help
75- fmt.Println (" Cardinal Plural Rules:" , en.PluralsCardinal ())
76- fmt.Println (" Ordinal Plural Rules:" , en.PluralsOrdinal ())
77- fmt.Println (" Range Plural Rules:" , en.PluralsRange ())
78-
79- // add basic language only translations
80- // last param indicates if it's ok to override the translation if one already exists
81- en.Add (" welcome" , " Welcome {0} to our test" , false )
82-
83- // add language translations dependant on cardinal plural rules
84- en.AddCardinal (" days" , " You have {0} day left to register" , locales.PluralRuleOne , false )
85- en.AddCardinal (" days" , " You have {0} days left to register" , locales.PluralRuleOther , false )
86-
87- // add language translations dependant on ordinal plural rules
88- en.AddOrdinal (" day-of-month" , " {0}st" , locales.PluralRuleOne , false )
89- en.AddOrdinal (" day-of-month" , " {0}nd" , locales.PluralRuleTwo , false )
90- en.AddOrdinal (" day-of-month" , " {0}rd" , locales.PluralRuleFew , false )
91- en.AddOrdinal (" day-of-month" , " {0}th" , locales.PluralRuleOther , false )
92-
93- // add language translations dependant on range plural rules
94- // NOTE: only one plural rule for range in 'en' locale
95- en.AddRange (" between" , " It's {0}-{1} days away" , locales.PluralRuleOther , false )
96-
97- // now lets use the translations we just added, in the same order we added them
98-
99- fmt.Println (en.T (" welcome" , " Joeybloggs" ))
100-
101- fmt.Println (en.C (" days" , 1 , 0 , en.FmtNumber (1 , 0 ))) // you'd normally have variables defined for 1 and 0
102- fmt.Println (en.C (" days" , 2 , 0 , en.FmtNumber (2 , 0 )))
103- fmt.Println (en.C (" days" , 10456.25 , 2 , en.FmtNumber (10456.25 , 2 )))
104-
105- fmt.Println (en.O (" day-of-month" , 1 , 0 , en.FmtNumber (1 , 0 )))
106- fmt.Println (en.O (" day-of-month" , 2 , 0 , en.FmtNumber (2 , 0 )))
107- fmt.Println (en.O (" day-of-month" , 3 , 0 , en.FmtNumber (3 , 0 )))
108- fmt.Println (en.O (" day-of-month" , 4 , 0 , en.FmtNumber (4 , 0 )))
109- fmt.Println (en.O (" day-of-month" , 10456.25 , 0 , en.FmtNumber (10456.25 , 0 )))
110-
111- fmt.Println (en.R (" between" , 0 , 0 , 1 , 0 , en.FmtNumber (0 , 0 ), en.FmtNumber (1 , 0 )))
112- fmt.Println (en.R (" between" , 1 , 0 , 2 , 0 , en.FmtNumber (1 , 0 ), en.FmtNumber (2 , 0 )))
113- fmt.Println (en.R (" between" , 1 , 0 , 100 , 0 , en.FmtNumber (1 , 0 ), en.FmtNumber (100 , 0 )))
62+ ##### Basic Makeup
63+ NOTE: not all fields are needed for all translation types, see [ examples] ( https://github.com/go-playground/universal-translator/tree/master/examples/file-formats )
64+ ``` json
65+ {
66+ "locale" : " en" ,
67+ "key" : " days-left" ,
68+ "trans" : " You have {0} day left." ,
69+ "type" : " Cardinal" ,
70+ "rule" : " One" ,
71+ "override" : false
11472}
11573```
74+ | Field| Description|
75+ | ---| ---|
76+ | locale| The locale for which the translation is for.|
77+ | key| The translation key that will be used to store and lookup each translation; normally it is a string or integer.|
78+ | trans| The actual translation text.|
79+ | type| The type of translation Cardinal, Ordinal, Range or "" for a plain substitution(not required to be defined if plain used)|
80+ | rule| The plural rule for which the translation is for eg. One, Two, Few, Many or Other.(not required to be defined if plain used)|
81+ | override| If you wish to override an existing translation that has already been registered, set this to 'true'. 99% of the time there is no need to define it.|
11682
11783Help With Tests
11884---------------
0 commit comments