Documentation
¶
Overview ¶
Package i18n provides provides methods and structs for internationalization
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
ErrNilBundle = fmt.Errorf("One or more provided bundles is nil")
)
Functions ¶
func Fallback ¶
Fallback copies values between bundles to use it as a fallback
Example ¶
en := &Bundle{ DateFormat: "%D", TimeFormat: "%l:%M %p", GREETING: "Hello!", ERRORS: &Errors{ UNKNOWN_USER: "Unknown user {{.Username}}", UNKNOWN_ID: "Unknown ID {{.ID}}", }, } ru := &Bundle{ DateFormat: "%Y/%m/%d", TimeFormat: "%H:%M", GREETING: "Привет!", ERRORS: &Errors{ UNKNOWN_USER: "Неизвестный пользователь {{.Username}}", }, } kz := &Bundle{ DateFormat: "%Y/%m/%d", TimeFormat: "%H:%M", GREETING: "Сәлеметсіз бе!", } loc, _ := Fallback(en, ru, kz) l := loc.(*Bundle) data := Data{ "Username": "johndoe", "ID": 183, } fmt.Println(l.GREETING) fmt.Println(l.ERRORS.UNKNOWN_USER.With(data)) fmt.Println(l.ERRORS.UNKNOWN_ID.With(data))
Output: Сәлеметсіз бе! Неизвестный пользователь johndoe Unknown ID 183
func IsComplete ¶
IsComplete checks if given bundle is complete and returns slice with empty fields
Example ¶
en := &Bundle{ GREETING: "Hello!", MESSAGE: "Hi user!", ERRORS: &Errors{ UNKNOWN_USER: "Unknown user {{.Username}}", UNKNOWN_ID: "Unknown ID {{.ID}}", }, } ru := &Bundle{ GREETING: "Привет!", ERRORS: &Errors{ UNKNOWN_USER: "Неизвестный пользователь {{.Username}}", }, } isComplete, _ := IsComplete(en) fmt.Printf("EN is complete: %t\n", isComplete) isComplete, fields := IsComplete(ru) fmt.Printf("RU is complete: %t (empty: %s)\n", isComplete, strings.Join(fields, ", "))
Output: EN is complete: true RU is complete: false (empty: MESSAGE, ERRORS.UNKNOWN_ID)
Types ¶
type Data ¶
Data can be used for storing data for templates
func (Data) Plural ¶
Plural prints plural form of word based on language and value
Note that this method only supports int, int64, uint, uint64, and float64
Example ¶
en := &Bundle{ GREETING: "Hello!", MESSAGE: `You started {{.ServerNum}} {{.Plural "EN" "ServerNum" "server" "servers"}}`, } data := Data{ "ServerNum": 12, } fmt.Println(en.MESSAGE.With(data))
Output: You started 12 servers
func (Data) PrettyNum ¶
PrettyNum formats number to "pretty" form (e.g 1234567 -> 1,234,567)
Example ¶
en := &Bundle{ GREETING: "Hello!", MESSAGE: `Your balance is ${{.PrettyNum "Balance"}}`, } data := Data{ "Balance": 3193, } fmt.Println(en.MESSAGE.With(data))
Output: Your balance is $3,193
func (Data) PrettyPerc ¶
PrettySize formats value to "pretty" size (e.g 1478182 -> 1.34 Mb)
Note that this method only supports float64
Example ¶
en := &Bundle{ GREETING: "Hello!", MESSAGE: `Copied {{.PrettyPerc "Progress"}} of files`, } data := Data{ "Progress": 45.31, } fmt.Println(en.MESSAGE.With(data))
Output: Copied 45.3% of files
func (Data) PrettySize ¶
PrettySize formats value to "pretty" size (e.g 1478182 -> 1.34 Mb)
Note that this method only supports int, int64, uint, uint64, and float64
Example ¶
en := &Bundle{ GREETING: "Hello!", MESSAGE: `Found {{.PrettyNum "Files"}} {{.Plural "EN" "Files" "file" "files"}} (size: {{.PrettySize "Size"}})`, } data := Data{ "Files": 731, "Size": 103810746, } fmt.Println(en.MESSAGE.With(data))
Output: Found 731 files (size: 99MB)
type String ¶
type String string
String is a simple string
func (String) Add ¶
Add adds prefix and/or suffix to result string
Example ¶
en := &Bundle{ GREETING: "Hello", ERRORS: &Errors{ UNKNOWN_USER: "Unknown user {{.Username}}", UNKNOWN_ID: "Unknown ID {{.ID}}", }, } fmt.Println(en.GREETING.Add("> ", "!"))
Output: > Hello!
func (String) String ¶
String converts String to string type
Example ¶
en := &Bundle{ GREETING: "Hello!", ERRORS: &Errors{ UNKNOWN_USER: "Unknown user {{.Username}}", UNKNOWN_ID: "Unknown ID {{.ID}}", }, } fmt.Println(en.GREETING.String())
Output: Hello!
func (String) With ¶
With uses String as a templates and applies payload from given data to it
Example ¶
en := &Bundle{ GREETING: "Hello!", ERRORS: &Errors{ UNKNOWN_USER: "Unknown user {{.Username}}", UNKNOWN_ID: "Unknown ID {{.ID}}", }, } data := Data{ "Username": "johndoe", "ID": 183, } fmt.Println(en.ERRORS.UNKNOWN_USER.With(data)) fmt.Println(en.ERRORS.UNKNOWN_ID.With(data))
Output: Unknown user johndoe Unknown ID 183