Skip to content
/ i18n Public

🏳️An simple i18n messages manage implementation for Go.

License

Notifications You must be signed in to change notification settings

go-zoox/i18n

Repository files navigation

i18n - 🏳️An simple i18n messages manage implementation for Go.

PkgGoDev Build Status Go Report Card Coverage Status GitHub issues Release

Installation

To install the package, run:

go get github.com/go-zoox/i18n

Getting Started

package main

import (
	"context"

	"github.com/go-zoox/i18n"
)

func main() {
	i := i18n.New()
	err := i.Load(func (lang string) (map[string][string]string, error) {
		// loads locales
		return map[string][string]string{
			"en-US": {
				"product":                     "product",
				"design":                      "design",
				"frontend":                    "frontend",
				"backend":                     "backend",
				"test":                        "test",
				"who am i":                    "who am i",
				"i am {name}":                 "i am {name}",
				"where is the {place.name} ?": "where is the {place.name} ?",
			},
			"zh-CN": {
				"product":                     "产品",
				"design":                      "设计",
				"frontend":                    "前端",
				"backend":                     "后端",
				"test":                        "测试",
				"who am i":                    "我是谁",
				"i am {name}":                 "我是{name}",
				"where is the {place.name} ?": "{place.name}在哪里 ?",
			},
		}, nil
	})
	if err != nil {
		panic(err)
	}

	translation := i.T("en-US", "product")

	fmt.Println(translation) // product
}

Load From Directory

package main

import (
	"context"

	"github.com/go-zoox/i18n"
)

// The directory structure should be like this:
// lang/
//
//		en-US.json
//		zh-CN.json
//		en-US.yaml
//		en-US.toml
//	 	en-US.ini
//		...

func main() {
	i := i18n.New()
	err := i.LoadFromDir("./lang")
		if err != nil {
		panic(err)
	}

	translation := i.T("en-US", "product")

	fmt.Println(translation) // product
}

Load From URL

package main

import (
	"context"

	"github.com/go-zoox/i18n"
)

func main() {
	i := i18n.New()
	err := i.LoadFromURL("https://raw.githubusercontent.com/go-zoox/i18n/master/tests/locales.json")
		if err != nil {
		panic(err)
	}

	translation := i.T("en-US", "product")

	fmt.Println(translation) // product
}

License

GoZoox is released under the MIT License.

About

🏳️An simple i18n messages manage implementation for Go.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages