i18n

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: MIT Imports: 10 Imported by: 0

README

I18n

i18n of different languages based go-i18n.

Usage

go get -u github.com/superproj/onex/pkg/i18n

add language files

mkdir locales

cat <<EOF > locales/en.yml
hello.world: Hello world!
EOF

cat <<EOF > locales/zh.yml
hello.world: 你好, 世界!
EOF
import (
	"embed"
	"fmt"
	"golang.org/x/text/language"
  "github.com/superproj/onex/pkg/i18n"
)

//go:embed locales
var locales embed.FS

func main() {
	i := i18n.New(
		i18n.WithFormat("yml"),
		// with absolute files
		i18n.WithFile("locales/en.yml"),
		i18n.WithFile("locales/zh.yml"),
		// with go embed files
		// i18n.WithFs(locales),
		i18n.WithLanguage(language.Chinese),
	)

	// print string
	fmt.Println(i.T("hello.world"))
	// 你好, 世界!

	// print error
	fmt.Println(i.E("hello.world").Error() == "你好, 世界!")
	// true

	// override default language
	fmt.Println(i.Select(language.English).T("hello.world"))
	// Hello world!
}

Options

  • WithFormat - language file format, default yml
  • WithLanguage - set default language file format, default en
  • WithFile - set language files by file system
  • WithFs - set language files by go embed file

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewContext

func NewContext(ctx context.Context, i *I18n) context.Context

func WithFS

func WithFS(fs embed.FS) func(*Options)

func WithFile

func WithFile(f string) func(*Options)

func WithFormat

func WithFormat(format string) func(*Options)

func WithLanguage

func WithLanguage(lang language.Tag) func(*Options)

Types

type I18n

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

I18n is used to store the options and configurations for internationalization.

func FromContext

func FromContext(ctx context.Context) *I18n

func New

func New(options ...func(*Options)) (rp *I18n)

New creates a new instance of the I18n struct with the given options. It takes a variadic parameter of functional options and returns a pointer to the I18n struct.

func (*I18n) Add

func (i *I18n) Add(f string)

Add is add language file or dir(auto get language by filename).

func (*I18n) AddFS

func (i *I18n) AddFS(fs embed.FS)

AddFS is add language embed files.

func (I18n) E

func (i I18n) E(id string) error

E is a wrapper for T that converts the localized string to an error type and returns it.

func (I18n) Language

func (i I18n) Language() language.Tag

Language get current language.

func (I18n) LocalizeE

func (i I18n) LocalizeE(message *i18n.Message) error

LocalizeE is a wrapper for LocalizeT method that converts the localized string to an error type and returns it.

func (I18n) LocalizeT

func (i I18n) LocalizeT(message *i18n.Message) (rp string)

LocalizeT localizes the given message and returns the localized string. If unable to translate, it returns the message ID as the default message.

func (I18n) Select

func (i I18n) Select(lang language.Tag) *I18n

Select can change language.

func (I18n) T

func (i I18n) T(id string) (rp string)

T localizes the message with the given ID and returns the localized string. It uses the LocalizeT method to perform the translation.

type Options

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

Jump to

Keyboard shortcuts

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