github.com/gookit/i18n
go get github.com/gookit/i18n

github.com/gookit/i18n

🏳️An simple i18n messages manage implement, use INI files. 使用INI文件,实现的简单方便的语言加载与管理

by gookit

v1.1.3 (see all)License:MIT
go get github.com/gookit/i18n
Readme

I18n

GoDoc Actions Status Coverage Status Go Report Card GitHub tag (latest SemVer) GitHub go.mod Go version

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 like sprintf:

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

Use ReplaceMode:

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

Usage with parameters:

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

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

GitHub Stars

11

LAST COMMIT

10mos ago

MAINTAINERS

0

CONTRIBUTORS

4

OPEN ISSUES

0

OPEN PRs

0
VersionTagPublished
v1.1.4-0.20210729033844-7172fdd29880
10mos ago
v1.1.4-0.20210622014532-883a348dc999
1yr ago
v1.1.4-0.20210208091153-58b38c0e919c
1yr ago
v1.1.3
1yr ago
No alternatives found
No tutorials found
Add a tutorial