Skip to content
/ i18n Public

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

License

Notifications You must be signed in to change notification settings

gookit/i18n

Repository files navigation

I18n

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

Management and use of multilingual data using INI files.

中文说明

Features

  • Easy to use, supports loading multiple languages, multiple files
  • Two data loading modes: single file FileMode(default), folder DirMode
  • 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 func strings.Replacer

Install

go get github.com/gookit/i18n

Godoc

Usage

Structs on use single FileMode mode:

lang/
    en.ini
    ru.ini
    zh-CN.ini
    zh-TW.ini
    ... ...

Structs on use folder DirMode mode:

lang/
    en/
        default.ini
        ... ...
    zh-CN/
        default.ini
        ... ...
    zh-TW/
        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 message

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

// Translate from the specified language
msg := i18n.Tr("en", "key")

Function list:

// Translate from default language
func Dt(key string, args ...interface{}) string
func Dtr(key string, args ...interface{}) string
func DefTr(key string, args ...interface{}) string

// Translate from the specified language
func T(lang, key string, args ...interface{}) string
func Tr(lang, key string, args ...interface{}) string

Parameters replacement mode

Use sprintf mode

TIP: default mode is SprintfMode

# 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 replace mode

Enable replace mode:

// set mode
i18n.Std().TransMode = i18n.ReplaceMode

// OR
i18n.Config(func(l *i18n.I18n) {
    l.TransMode = i18n.ReplaceMode
})

Examples for language data:

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

Usage with parameters:

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

Usage with kv-map parameters:

msg := i18n.Tr("en", "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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages