i18n

package module
Version: v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2020 License: MIT Imports: 8 Imported by: 3

README

I18n

GoDoc Build Status Coverage Status Go Report Card GitHub tag (latest SemVer)

Use INI files, simple i18n manager implement.

中文说明

Features

  • Easy to use, supports loading multiple languages, multiple files
  • Two data loading modes: single file FileMode, folder DirMode; default is folder mode
  • Support to set the default language and fallback language; when the default language data is not found, it will automatically try to find the fallback language
  • Support parameter replacement, there are two modes: SprintfMode replaces parameters via fmt.Sprintf, ReplaceMode uses strings.Replacer

Install

go get github.com/gookit/i18n

Godoc

Usage

lang/
    en/
        default.ini
        ...
    zh-CN/
        default.ini
        ...
Init i18n
    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()
Translate
    // 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")
Parameter replacement mode

Use SprintfMode(defaults):

# en.ini
desc = I am %s, age is %d

Usage with parameters:

msg := i18n.Tr("en", "desc", "name", "tom", "age", 22)
// Output: "I am tom, age is 22"

Use ReplaceMode:

# en.ini
desc = I am {name}, age is {age}

Usage with map[string]interface{} params:

i18n.TransMode = i18n.ReplaceMode

msg := i18n.Tr("en", "desc", "desc", map[string]interface{}{
    "name": "tom",
    "age": 22,
})
// Output: "I am tom, age is 22"

Tests

go test -cover

Dep packages

  • gookit/ini is an INI config file/data manage implement

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.T("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 (

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

	// SprintfMode render message arguments by fmt.Sprintf
	SprintfMode uint8 = 0
	// ReplaceMode render message arguments by string replace
	ReplaceMode uint8 = 1
)

Variables

This section is empty.

Functions

func DefTr

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

DefTr translate language key from default language

func Dt added in v1.1.1

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

Dt translate language key from default language

func T added in v1.1.1

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

T translate language key to value string

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.
	//  0 single language file
	//  1 multi language directory
	LoadMode uint8
	// TODO translate mode.
	//  0 sprintf
	//  1 replace
	TransMode 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 Default added in v1.0.5

func Default() *I18n

Default get default i18n instance

func Init

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

Init the default language instance

func New

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

New an i18n instance

func NewEmpty

func NewEmpty() *I18n

NewEmpty nwe an 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) Dt added in v1.1.1

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

Dt translate from default lang

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) T added in v1.1.1

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

T translate from a lang by key

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

Source Files

Jump to

Keyboard shortcuts

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