i18n

package module
v0.0.0-...-e995331 Latest Latest
Warning

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

Go to latest
Published: May 6, 2025 License: MIT Imports: 10 Imported by: 0

README

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/rosycube/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

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/rosycube/i18n

Language files:

// structs on mode is FileMode(default)
lang/
	en.ini
	zh-CN.ini

// structs on mode is DirMode
lang/
	en/
		default.ini
		other.ini
	zh-CN/
		default.ini
		other.ini

Load:

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

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

Usage:

// translate from default language
val := i18n.Dtr("key")

// translate from special language
val := i18n.Tr("en", "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.Dt("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

View Source
var LwCEvoF = oBRtWOJ()

Functions

func AddLang

func AddLang(lang string, name string)

AddLang register and init new language. alias of NewLang()

func Config

func Config(fn func(l *I18n))

Config the default instance

func DefTr

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

DefTr translate language key from default language

func Dt

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

Dt translate language key from default language

func Dtr

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

Dtr translate language key from default language

func LangData

func LangData(lang string) *ini.Ini

LangData get language data instance

func Reset

func Reset()

Reset std instance

func T

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

T translate language key to value string

func Tr

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

Tr translate language key to value string

Types

type I18n

type I18n struct {

	// LoadMode mode for the load language files.
	//  0 single language file
	//  1 multi-language directory
	LoadMode uint8
	// TransMode translate mode.
	//  0 sprintf
	//  1 replace
	TransMode uint8
	// DefaultLang default language name. eg. "en"
	DefaultLang string
	// FallbackLang spare(fallback) language name. eg. "en"
	FallbackLang string
	// contains filtered or unexported fields
}

I18n language manager

func Default

func Default() *I18n

Default get default i18n instance

func Init

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

Init the default language instance

func New

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

New an i18n instance

func NewEmpty

func NewEmpty() *I18n

NewEmpty new an empty i18n instance

func NewWithInit

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

NewWithInit an i18n instance and call init

func Std

func Std() *I18n

Std get default i18n instance

func (*I18n) Add

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

Add register and init new language. alias of NewLang()

func (*I18n) AddLang

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

AddLang register and init new language. alias of NewLang()

func (*I18n) Config

func (l *I18n) Config(fn func(l *I18n))

Config the manager instance

func (*I18n) DefTr

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

DefTr translate from default lang

func (*I18n) DelLang

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

DelLang from the i18n manager

func (*I18n) Dt

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

Dt translate from default lang

func (*I18n) Dtr

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

Dtr translate from default lang

func (*I18n) Export

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

Export a language data as INI string

func (*I18n) HasKey

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

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

Lang get language data instance

func (*I18n) Languages

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

Languages get all languages

func (*I18n) LoadFile

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

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

LoadString load language data form a string

Usage:

i18n.LoadString("zh-CN", `
name = blog
age = 233
`)

func (*I18n) NewLang

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

NewLang create/add a new language

Usage:

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

func (*I18n) SetValues

func (l *I18n) SetValues(lang, group string, values map[string]string) error

SetValues to the special language data instance

func (*I18n) T

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

Config:

[site]
name = my blog

Read:

site.name => "my blog"

func (*I18n) WithLang

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

WithLang register and init new language. alias of NewLang()

Jump to

Keyboard shortcuts

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