i18n

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2023 License: MIT Imports: 8 Imported by: 1

README

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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Version = "1.0.3"

Version is the current version of the i18n package.

Functions

This section is empty.

Types

type I18n

type I18n interface {
	// Load loads the given locale and translations into the I18n instance.
	Load(fn func() (map[string]Translations, error)) error

	// Translate translates the given key with the given arguments.
	Translate(locale string, key string, data ...map[string]any) (string, error)
	// T is an alias for Translate.
	T(locale string, key string, data ...map[string]any) string

	// LoadFromFile loads the given locale and translations into the I18n instance.
	LoadFromFile(filepath string) error
	// LoadFromDir loads the given locale and translations into the I18n instance.
	LoadFromDir(dir string) error
	// LoadFromURL loads the given locale and translations into the I18n instance.
	LoadFromURL(url string) error

	//
	IsLocalesLoaded() bool

	// GetLocales returns the loaded locales.
	GetLocales() map[string]Translations
}

I18n ...

func New

func New() I18n

New creates a new I18n instance with the given locale and translations.

type Translations

type Translations = map[string]string

Translations ...

Jump to

Keyboard shortcuts

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