translator

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2022 License: MIT Imports: 9 Imported by: 1

README

Translator

Translator library with default Json and Memory driver.

Requirements

Translatable Structures

By default all translations read from config driver (json files or memory). Structures translations can resolved by structure itself and override global translations. For making structure translatable you must implement Translatable interface.

Note: Translatable functionality used by validator library.

Caution: use non-pointer implemantation for struct!

type Person struct {
    Name string
    Age  string
}

func (p Person) GetTranslation(locale string, key string, field string) string {
    if  key == "required" {
        switch locale {
        case "en":
            if field == "Name" {
                return "Name is required"
            }else{
                return "Age is required"
            }
        }
    }
    return ""
}

Create New Translator Driver

Translator library contains two different driver by default.

Json Driver

JSON driver use json file for managing translations.

// Signature:
NewJSONTranslator(fallbackLocale string, dir string) (Translator, error)

// Example:
import "github.com/bopher/translator"
jTrans, err := translator.NewJSONTranslator("en", "trans")
Memory Driver

Use in-memory array for keep translations.

// Signature:
NewMemoryTranslator(fallbackLocale string) Translator

// Example:
import "github.com/bopher/translator"
mTrans := translator.NewMemoryTranslator("en")

Usage

Translator interface contains following methods:

Register

Register new translation message for locale. Use placeholder in message for field name.

// Signature:
Register(locale string, key string, message string)

// Example:
t.Register("en", "welcome", "Hello {name}, welcome!")
Resolve

Resolve find translation for locale. If no translation found for locale return fallback translation or "".

// Signature:
Resolve(locale string, key string) string

// Example:
trans := t.Resolve("en", "welcome") // Hello {name}, welcome!
ResolveStruct

Find translation from translatable. If empty string returned from translatable or struct not translatable, default translation will resolved.

// Signature:
ResolveStruct(s any, locale string, key string, field string) string
Translate

Translate get translation for locale.

// Signature:
Translate(locale string, key string, placeholders map[string]string) string

// Example:
t.Translate("en", "welcome", map[string]string{ "name": "John" }) // Hello John, welcome!
TranslateStruct

Translate using translatable interface. if empty string returned from translatable or struct not translatable, default translation will resolved.

// Signature:
TranslateStruct(s any, locale string, key string, field string, placeholders map[string]string) string

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSONDriver

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

JSONDriver json driver

func (*JSONDriver) Load

func (this *JSONDriver) Load() error

Load load translations file to memory

func (*JSONDriver) Register

func (this *JSONDriver) Register(locale string, key string, message string)

Register new translation message for locale Use placeholder in message for field name @example: t.Register("en", "welcome", "Hello {name}, welcome!")

func (JSONDriver) Resolve

func (this JSONDriver) Resolve(locale string, key string) string

Resolve find translation for locale if no translation found for locale return fallback translation or nil

func (JSONDriver) ResolveStruct

func (this JSONDriver) ResolveStruct(s any, locale string, key string, field string) string

ResolveStruct find translation from translatable if empty string returned from translatable or struct not translatable, default translation will resolved

func (JSONDriver) Translate

func (this JSONDriver) Translate(locale string, key string, placeholders map[string]string) string

Translate get translation for locale @example: t.Translate("en", "welcome", map[string]string{ "name": "John" })

func (JSONDriver) TranslateStruct

func (this JSONDriver) TranslateStruct(s any, locale string, key string, field string, placeholders map[string]string) string

TranslateStruct translate using translatable interface if empty string returned from translatable or struct not translatable, default translation will resolved Caution: use non-pointer implemantation for struct

type Translatable

type Translatable interface {
	GetTranslation(locale string, key string, field string) string
}

Translatable interface for struct

type Translator

type Translator interface {
	// Register new translation message for locale
	// Use placeholder in message for field name
	// @example:
	// t.Register("en", "welcome", "Hello {name}, welcome!")
	Register(locale string, key string, message string)
	// Resolve find translation for locale
	// if no translation found for locale return fallback translation or nil
	Resolve(locale string, key string) string
	// ResolveStruct find translation from translatable
	// if empty string returned from translatable or struct not translatable, default translation will resolved
	ResolveStruct(s any, locale string, key string, field string) string
	// Translate get translation for locale
	// @example:
	// t.Translate("en", "welcome", map[string]string{ "name": "John" })
	Translate(locale string, key string, placeholders map[string]string) string
	// TranslateStruct translate using translatable interface
	// if empty string returned from translatable or struct not translatable, default translation will resolved
	// Caution: use non-pointer implemantation for struct
	TranslateStruct(s any, locale string, key string, field string, placeholders map[string]string) string
}

Translator interface

func NewJSONTranslator

func NewJSONTranslator(fallbackLocale string, dir string) (Translator, error)

NewJSONTranslator create a new memory based translator

func NewMemoryTranslator

func NewMemoryTranslator(fallbackLocale string) Translator

NewMemoryTranslator create a new memory based translator

Jump to

Keyboard shortcuts

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