i18n

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2022 License: MIT Imports: 4 Imported by: 6

README

I18n for Go

Install

go get -u github.com/fitv/go-i18n

Usage

YAML files

├── locales
│   ├── en.yml
│   ├── zh.yml
└── main.go
package main

import "github.com/fitv/go-i18n"

//go:embed locales/*.yml
var fs embed.FS

func main() {
    i18n, err := i18n.New(fs, "locales")
    i18n.SetDefaultLocale("en")

    i18n.Trans("hello.world") // World
    i18n.Locale("zh").Trans("hello.world") // 世界

    // with params
    user := map[string]interface{}{"name": "Jack", "email": "jack@example.com"}
    // foo %s
    i18n.Trans("hello.foo", "bar") // foo bar
    // Name: {name}, Email: {email}
    i18n.Trans("user.description", user) // Name: Jack, Email: jack@example.com
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type I18n added in v1.0.2

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

func New added in v1.0.2

func New(fs embed.FS, path string) (*I18n, error)

New returns an I18n instance

func (*I18n) Locale added in v1.0.2

func (i *I18n) Locale(locale string) *translator.Translator

Locale returns the translator instance by the given locale

func (*I18n) SetDefaultLocale added in v1.0.2

func (i *I18n) SetDefaultLocale(local string)

SetDefaultLocale set the default locale

func (*I18n) Trans added in v1.0.2

func (i *I18n) Trans(key string, args ...interface{}) string

Trans returns language translation by the given key

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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