i18n

package
v13.9.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2024 License: Apache-2.0 Imports: 7 Imported by: 1

Documentation

Overview

Package i18n provides provides methods and structs for internationalization

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNilBundle = fmt.Errorf("One or more provided bundles is nil")
)

Functions

func Fallback

func Fallback(bundles ...any) (any, error)

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

func IsComplete(bundle any) (bool, []string)

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

type Data map[string]any

Data can be used for storing data for templates

func (Data) Plural

func (d Data) Plural(lang, prop string, values ...string) string

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

func (d Data) PrettyNum(prop string) string

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

func (d Data) PrettyPerc(prop string) string

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

func (d Data) PrettySize(prop string) string

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

func (s String) Add(prefix, suffix string) string

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) S

func (s String) S() string

S is shortcut for String

func (String) String

func (s String) 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

func (s String) With(data any) string

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL