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 3, 2018 License: MIT Imports: 7 Imported by: 0

README

i18n

GoDoc Build Status Coverage Status Go Report Card

Use INI files, simple i18n manager implement.

  • Easy to use,can load multi language, multi files
  • Support set default language, fallback language
  • Support parameter replacement

中文说明

Godoc

Usage

conf/
    lang/
        en.ini
        zh-CN.ini
        ...
  • init
    import "github/gookit/i18n"

    languages := map[string]string{
        "en": "English",
        "zh-CN": "简体中文",
        // "zh-TW": "繁体中文",
    }

    // The default instance initialized directly here
    i18n.Init("conf/lang", "en", languages)
    
    // Create a custom new instance
    // i18n.New(langDir string, defLang string, languages)
    // i18n.NewEmpty()
  • usage
    // translate from special language
    msg := i18n.Tr("en", "key")

    // translate from default language
    msg = i18n.DefTr("key")
    // with arguments. 
    msg = i18n.DefTr("key1", "arg1", "arg2")

Tests

go test -cover

Dep packages

License

MIT

Documentation

Overview

Package i18n is a simple language manager, use INI format file.

Source code and other details for the project are available at GitHub:

https://github.com/gookit/i18n

lang files:

conf/
    lang/
        en.ini
        zh-CN.ini

init:

import "github/gookit/i18n"

languages := map[string]string{
    "en": "English",
    "zh-CN": "简体中文",
    "zh-TW": "繁体中文",
}

i18n.Init("conf/lang", "en", languages)

usage:

// translate from special language
val := i18n.Tr("en", "key")

// translate from default language
val := i18n.DefTr("key")
Example
languages := map[string]string{
	"en":    "English",
	"zh-CN": "简体中文",
	// "zh-TW": "繁体中文",
}

l := New("testdata", "en", languages)
l.Init()

fmt.Printf("name %s\n", l.Tr("en", "name"))
fmt.Printf("name %s\n", l.DefTr("name"))
fmt.Printf("name %s\n", l.Tr("zh-CN", "name"))
fmt.Printf("use args: %s\n", l.DefTr("argMsg", "inhere"))
Output:

name Blog
name Blog
name 博客
use args: hello inhere, welcome

Index

Examples

Constants

View Source
const (
	// language name is file name. "en" -> "lang/en.ini"
	FileMode uint8 = 0
	// language name is dir name, will load all file in the dir. "en" -> "lang/en/*.ini"
	DirMode uint8 = 1
)

language file load mode

Variables

This section is empty.

Functions

func DefTr

func DefTr(key string, args ...interface{}) string

DefTr translate language key from default language

func Tr

func Tr(lang string, key string, args ...interface{}) string

Tr translate language key to value string

Types

type I18n

type I18n struct {

	// mode for the load language files. mode: 0 single file, 1 multi dir
	LoadMode uint8
	// default language name. eg. "en"
	DefaultLang string
	// spare(fallback) language name. eg. "en"
	FallbackLang string
	// contains filtered or unexported fields
}

I18n language manager

func Init

func Init(langDir string, defLang string, languages map[string]string) *I18n

Init the default language instance

func Instance added in v1.0.3

func Instance() *I18n

Instance get default i18n instance

func New

func New(langDir string, defLang string, languages map[string]string) *I18n

New a i18n instance

func NewEmpty

func NewEmpty() *I18n

NewEmpty a empty i18n instance

func NewWithInit added in v1.0.1

func NewWithInit(langDir string, defLang string, languages map[string]string) *I18n

NewWithInit a i18n instance and call init

func (*I18n) Add

func (l *I18n) Add(lang string, name string)

Add new language

func (*I18n) DefTr

func (l *I18n) DefTr(key string, args ...interface{}) string

DefTr translate from default lang

func (*I18n) DelLang added in v1.0.1

func (l *I18n) DelLang(lang string) bool

DelLang from the i18n manager

func (*I18n) Export added in v1.0.2

func (l *I18n) Export(lang string) string

Export a language data as INI string

func (*I18n) HasKey added in v1.0.2

func (l *I18n) HasKey(lang, key string) (ok bool)

HasKey in the language data

func (*I18n) HasLang

func (l *I18n) HasLang(lang string) bool

HasLang in the manager

func (*I18n) Init

func (l *I18n) Init() *I18n

Init load add language files

func (*I18n) Lang added in v1.0.1

func (l *I18n) Lang(lang string) *ini.Ini

Lang get language data instance

func (*I18n) Languages added in v1.0.1

func (l *I18n) Languages() map[string]string

Languages get all languages

func (*I18n) LoadFile added in v1.0.1

func (l *I18n) LoadFile(lang string, file string) (err error)

LoadFile append data to a exist language usage:

i18n.LoadFile("zh-CN", "path/to/zh-CN.ini")

func (*I18n) LoadString added in v1.0.1

func (l *I18n) LoadString(lang string, data string) (err error)

LoadString load language data form a string usage: i18n.Set("zh-CN", "name = blog")

func (*I18n) NewLang added in v1.0.1

func (l *I18n) NewLang(lang string, name string)

NewLang create/add a new language usage:

i18n.NewLang("zh-CN", "简体中文")

func (*I18n) Tr

func (l *I18n) Tr(lang, key string, args ...interface{}) string

Tr translate from a lang by key site.name => [site]

name = my blog

Jump to

Keyboard shortcuts

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