i18next

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2023 License: MIT Imports: 11 Imported by: 0

README

go-i18next

go-i18next is a port of i18next in Go.

Installation

$ go get -u https://github.com/yuangwei/go-18next

Usage

import "github.com/yuangwei/go-18next"

var i18n i18next.I18n

func main() {
	i18n = i18next.Init(i18next.I18nOptions{
                Lng:        []string{"en", "cn"},
                DefaultLng: "en",
                Ns:         "json",
                Backend: i18next.Backend{
                LoadPath: []string{"./examples/datas/{{.Ns}}/{{.Lng}}/home.json"},
            },
        })
	text, err := i18n.T("title", struct {
            Name string
    }{Name: "Mike"})
	fmt.Println(val) // Hello, Mike
	
}

License

go-i18next is available under the MIT license. See the LICENSE file for more info.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

type Backend struct {
	LoadPath    []string // from network or local, usage: https://example.com/locales/{{.Lng]}/locale.json
	CrossDomain bool
}

type CodeOptions

type CodeOptions struct {
	LowerCaseLng bool // reverse lng to lowerCase, example: en-US => en-us
	CleanCode    bool // reverse lng to clean, example: en-US / en-us => en
}

type I18n

type I18n struct {
	// contains filtered or unexported fields
}

func Init

func Init(options I18nOptions) (I18n, error)

func (*I18n) ChangeLanguage

func (i *I18n) ChangeLanguage(lng string) error

func (*I18n) Exist

func (i *I18n) Exist(key string) bool

func (*I18n) T

func (i *I18n) T(key string, props interface{}) (string, error)

type I18nOptions

type I18nOptions struct {
	Lng         []string                     // example: ["en-US", "fr-FR"] usage: /locale/{{.Lng}}/locale.json or https://example.com/locales/{{.Lng]}/locale.json
	DefaultLng  string                       // example: "en-US"
	Ns          string                       // example: "dev", , not require. usage: https://example.com/locales/{{.Ns}}/{{.Lng]}.json
	Backend     Backend                      // load lngs from file or network
	Resources   map[string]map[string]string // load lngs from local
	CodeOptions CodeOptions
}

Jump to

Keyboard shortcuts

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