intl

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2022 License: MIT Imports: 6 Imported by: 0

README

intl

Internationalization library based on different DB storages

Go Report Card Build and test GoDoc codecov

Installation
import "github.com/arthurkushman/intl"
Migrations

You'll need a migration for particular database engine: postgresql, mysql, mssql, sqlite, oci which u can find in db/migrations folder. After running one u'll be set up, then u need to insert data e.g.:

source_message table

1,delivery.datetime.price,"Hi, your delivery date is {{.Date}} and a price is {{.Price}}"

message table (in this example the last field localize_config u need only if using plurals)

1,fr-FR,"Bonjour, votre date de livraison est le {{.Date}} et le prix est le {{.Price}}","{\"DefaultMessage\": {\"ID\": \"Delivery\", \"One\": \"Bonjour, votre date de livraison est le {{.Date}} et le prix est le {{.Price}}\", \"Other\": \"Bonjour, votre date de livraison est le {{.Date}} many et le prix est le {{.Price}} many\"}, \"TemplateData\": {\"Date\": \"demain\", \"Price\": 123}, \"PluralCount\": 2}"
Message translation example
// assuming that in db there is a source message in English: "Hi, your delivery date is {{.Date}} and a price is {{.Price}}"
// and for French (in message table) is: "Bonjour, votre date de livraison est le {{.Date}} et le prix est le {{.Price}}"

msg, err := intl.Translate("some.actual.key", "fr-FR", &Delivery{Date: "demain", Price: 123})
// msg: "Bonjour, votre date de livraison est le demain et le prix est le 123"
Plural message translation example
// to set up plurals correctly see an example above + intl_test.go
msg, err := intl.TranslatePlurals("some.actual.key", "fr-FR")
// msg: "Bonjour, votre date de livraison est le demain many et le prix est le 123 many"

Documentation

Index

Constants

View Source
const DefaultTemplateName = "translate"

Variables

This section is empty.

Functions

This section is empty.

Types

type Intl

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

func NewIntl

func NewIntl(db *sql.DB) *Intl

func (*Intl) GetMessage

func (i *Intl) GetMessage(key, lang string) (*Translation, error)

GetMessage gets message from db by key+lang

func (*Intl) SetTblNames

func (i *Intl) SetTblNames(sourceMsgTbl, msgTbl string)

func (*Intl) Translate

func (i *Intl) Translate(key, lang string, params any) (string, error)

Translate translates message by key and language, replacing with params from custom struct there should be templated message in db (for particular language) to perform such transformation ex.: Hello {{.Name}}, it's {{.Time}}. Basket: {{.Product}} price is {{.Price}}.

func (*Intl) TranslatePlurals

func (i *Intl) TranslatePlurals(key, lang string) (string, error)

TranslatePlurals translates any message with plurals based on LocalizeConfig object see: github.com/nicksnyder/go-i18n/v2/i18n it tries to find translation language and makes fallback if not found

ex. of LocalizeConfig:

localizer.Localize(&i18n.LocalizeConfig{
    DefaultMessage: &i18n.Message{
        ID: "PersonCats",
        One: "{{.Name}} has {{.Count}} cat.",
        Other: "{{.Name}} has {{.Count}} cats.",
    },
    TemplateData: map[string]interface{}{
        "Name": "Nick",
        "Count": 2,
    },
    PluralCount: 2,
}) // Nick has 2 cats.

type Translation

type Translation struct {
	Message                  string
	LocalizeConfig           string
	TranslatedMsg            string
	TranslatedLocalizeConfig string
}

type Translator

type Translator interface {
	Translate(key, lang string, params any) (string, error)
	TranslatePlurals(key, lang string) (string, error)
}

Jump to

Keyboard shortcuts

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