i18np

package module
v0.0.0-...-2969b27 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: MIT Imports: 4 Imported by: 17

README

this repository has been moved into cillop.

I18n Plus

I18n Plus allows you to translate your entire application.

Dependencies

Installation

go get github.com/mixarchitecture/i18np

Usage

package main

import (
    "fmt"

    "github.com/mixarchitecture/i18np"
)

func main() {
 i18n := i18np.New("en") // generate a new i18n instance with fallback locale "en"
    i18n.Load("./locales", "en", "tr") // load locales from "./locales" directory for "en" and "tr" locales
    i18n.Translate("my_key") // translate "my_key" with "en" locale
    i18n.Translate("my_key", "tr") // translate "my_key" with "tr" locale
    i18n.Translate("my_key", "tr", "en") // translate "my_key" with "tr" locale, fallback to "en" locale

    i18n.TranslateWithParams("my_param_key", i18np.P{"Name": "John"}) // translate "my_param_key" with "en" locale and "Name" parameter

    err := i18n.NewError("myKey") // generate translateable error
    translatedErr := i18n.TranslateFromError(err) // translate error with "en" locale
}
Bad Usage
package example

func Example() error {
    return errors.New("blabla")
}
Good Usage
package example

import (
    "github.com/mixarchitecture/i18np"
)

func Example() *i18np.Error {
    return i18np.NewError("my_errorKey")
}

func Example2() *i18np.Error {
    return i18np.NewError("my_errorKey", i18np.P{"Name": "John"})
}

Documentation

Documentation is available at pkg.go.dev.

Contributing

Contributions are always welcome!

License

MIT

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