i18n

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

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

Go to latest
Published: May 12, 2021 License: MIT Imports: 11 Imported by: 0

README

i18n

The easiest solution for multi-language support for your Golang project.

What is i18n?

i18n is a Go package that helps you translate Go programs into multiple languages.

  • Supports reading message file from files or directories.
  • Supports reading from JSON, YAML, TOML, and HCL formats.
  • Supports nested messages key.
  • Supports render template with text/template.

Install

go get github.com/gnoah1379 /i18n

Message file naming convention

File name format:

anything.{language}.{extension}

File extension need in: json, yaml, yml, toml, hcl

LanguageBundle

type LanguageBundle{
	// private field
}

// Get keyword data in the corresponding language.
//
// When the language does not exist or cannot find a keyword in the default language
// If not found this keyword in the default language, 
// return the first value of default values if default value existed
// or an empty string.
func (l LanguageBundle) Get(language, key string, defaultValue ...string) string

// Get template of keyword via GetTemplate method and Execute this template with data
func (l LanguageBundle) Render(lang, templateKey string, data interface{}, defaultTemplate ...string) (string, error)

// call Render and panic when error
func (l LanguageBundle) MustRender(lang, templateKey string, data interface{}, defaultTemplate ...string) string

// Get value of keyword via Get method and Parse value become Template
func (l LanguageBundle) GetTemplate(lang, templateKey string, defaultTemplate ...string) (*template.Template, error)

Config

type Config struct {
    DefaultLanguage string                       `json:"defaultLanguage" yaml:"defaultLanguage"`
    LanguagePaths   map[string][]string          `json:"languagePaths" yaml:"languagePaths"`
    DataInject      map[string]map[string]string `json:"dataInject" yaml:"dataInject"`
    Delimiter       string                       `json:"delimiters" yaml:"delimiters"`
}


// Load any files with filename match with convention.
func (c *Config) FromDir(dirpath string)error

// Load file if filename match with convention.
func (c *Config) FromFile(filepath string)error

// Data will inject into the bundle after loading from the file done. 
func (c *Config) InjectLanguageData(lang string, languageData map[string]interface{}) error

// Build config become a LanguageBundle.
//
// When data inject or message unmarshal is nested
// Will convert it become key-value format (map[string]string) with delimiter (default is ".").
func (c Config) Build() (*LanguageBundle, error)

Usage

 cfg := i18n.NewConfig("default language") 
 // config message dir
 err := cfg.FromDir("language messages directory")
 ...
 bundle,err := cfg.Build()
 msg := bundle.Get("language name","key","default value")
 msg = bundle.MustRender("language name","key",map[string]interface{}{
 	"bind": "value"
},"default template")

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrFileExtensionNotSupport = errors.New("file extension not support")
View Source
var ErrInvalidLanguage = errors.New("invalid language")
View Source
var ErrInvalidLanguageData = errors.New("invalid language data")
View Source
var ErrRootBundleDoesNotInit = errors.New("root bundle does not init")

Functions

func Build

func Build(c Config) error

func Get

func Get(lang, templateKey string, defaultTemplate ...string) string

func GetTemplate

func GetTemplate(lang, templateKey string, defaultTemplate ...string) (*template.Template, error)

func MustRender

func MustRender(lang, templateKey string, data interface{}, defaultTemplate ...string) string

func Render

func Render(lang, templateKey string, data interface{}, defaultTemplate ...string) (string, error)

Types

type Config

type Config struct {
	DefaultLanguage string                       `json:"defaultLanguage" yaml:"defaultLanguage"`
	LanguagePaths   map[string][]string          `json:"languagePaths" yaml:"languagePaths"`
	DataInject      map[string]map[string]string `json:"dataInject" yaml:"dataInject"`
	Delimiter       string                       `json:"delimiters" yaml:"delimiters"`
}

func NewConfig

func NewConfig(defaultLanguage string) Config

func (Config) Build

func (c Config) Build() (*LanguageBundle, error)

func (*Config) FromDir

func (c *Config) FromDir(dirPath string) error

func (*Config) FromFile

func (c *Config) FromFile(filePath string) error

func (*Config) InjectLanguageData

func (c *Config) InjectLanguageData(lang string, languageData map[string]interface{}) error

type LanguageBundle

type LanguageBundle struct {
	// contains filtered or unexported fields
}

func Clone

func Clone() *LanguageBundle

clone of root

func (LanguageBundle) Get

func (l LanguageBundle) Get(lang, key string, defaultValue ...string) string

func (LanguageBundle) GetTemplate

func (l LanguageBundle) GetTemplate(lang, templateKey string, defaultTemplate ...string) (*template.Template, error)

func (LanguageBundle) MustRender

func (l LanguageBundle) MustRender(lang, templateKey string, data interface{}, defaultTemplate ...string) string

func (LanguageBundle) Render

func (l LanguageBundle) Render(lang, templateKey string, data interface{}, defaultTemplate ...string) (string, error)

Jump to

Keyboard shortcuts

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