i18np

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: Apache-2.0 Imports: 4 Imported by: 71

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	// Key is the error key to be used in the translation
	// It is required
	// If it is empty, the error is not an error
	Key string

	// Params is the error params to be used in the translation
	// It is optional
	// If it is nil, the error params will be empty
	Params *P

	// Details is the error details to be used in the translation
	// It is optional
	// If it is nil, the error details will be empty
	Details interface{}
}

I18nError is a struct that contains the error key and the error params to be used in the translation It implements the error interface It can be used to return errors from functions

func NewError

func NewError(key string, params ...P) *Error

NewError returns a new I18nError It is used to return errors from functions example: i18n.NewError("error.key", i18np.P{"param1": "value1"})

func NewErrorDetails

func NewErrorDetails(key string, details interface{}, params ...P) *Error

NewErrorDetails returns a new I18nError with details

It is used to return errors from functions

example: i18n.NewErrorDetails("error.key", "details", i18np.P{"param1": "value1"})

func (*Error) Error

func (e *Error) Error() string

Error returns the error key and the error params It implements the error interface

func (*Error) IsDetails

func (e *Error) IsDetails() bool

IsDetails returns true if the error has details

func (*Error) IsErr

func (e *Error) IsErr() bool

IsErr returns true if the error is an error

type I18n

type I18n struct {

	// Fallback is default language
	Fallback string
	// contains filtered or unexported fields
}

I18n is base struct for i18n b is i18n bundle Fallback is default language

func New

func New(fallback string) *I18n

New is constructor for I18n fallback is default language return I18n

func (*I18n) AddMessages

func (i *I18n) AddMessages(lang string, messages ...*i18n.Message) error

AddMessages is add i18n message lang is language messages is i18n message example: i18n.AddMessages("en", &i18n.Message{ID: "hello", Other: "Hello!"})

func (*I18n) Load

func (i *I18n) Load(ld string, languages ...string)

Load is load i18n file ld is directory path languages is language list example: i18n.Load("./i18n", "en", "ja")

func (*I18n) Translate

func (i *I18n) Translate(key string, languages ...string) string

Translate is translate i18n message key is i18n key languages is language list example: i18n.Translate("hello", "en") example: i18n.Translate("hello", "en", "ja") example: i18n.Translate("hello", "ja", "en")

func (*I18n) TranslateFromError

func (i *I18n) TranslateFromError(err Error, languages ...string) string

TranslateFromError is translate i18n message from I18nError err is I18nError

languages is language list

example: i18n.TranslateFromError(err, "en") example: i18n.TranslateFromError(err, "en", "ja")

func (*I18n) TranslateFromErrorDetail

func (i *I18n) TranslateFromErrorDetail(err Error, languages ...string) (string, interface{})

TranslateFromErrorDetail is translate i18n message from I18nError err is I18nError languages is language list return string, interface{} example: i18n.TranslateFromErrorDetail(err, "en") example: i18n.TranslateFromErrorDetail(err, "en", "ja") example: i18n.TranslateFromErrorDetail(err, "ja", "en")

func (*I18n) TranslateWithParams

func (i *I18n) TranslateWithParams(key string, params interface{}, languages ...string) string

TranslateWithParams is translate i18n message with params key is i18n key params is i18n params languages is language list example: i18n.TranslateWithParams("hello", i18n.P{"Name": "John"}, "en")

type P

type P map[string]interface{}

P is a map of params to be used in the translation It is used to pass the params to the I18nError It is used to pass the params to the I18n.TranslateWithParams function

Jump to

Keyboard shortcuts

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