golangloader

package module
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2024 License: MIT Imports: 11 Imported by: 0

README

golangloader

An improved translation package to help developers to customize the localization of the field names.  Furthermore, it also supports loading from .json files.

Note: Translation files is rewritten from go-playground/validator

Basic Usage

Create a json file on ./lang/en/validation.json in your project:

{
    "attributes": {
        "Username": "Username",
        "Password": "Password"
    }
}

Example of how to use golangloader with go-playground/validator

package main

import (
	"fmt"

	"github.com/go-playground/validator/v10"
	"github.com/wolftotem4/golangloader"
	"github.com/wolftotem4/golangloader/defaults"
)

func main() {
	// Assign the fallback locale
	fallback := "en"

	// Create a new translator and load the default translations
	ut, err := defaults.Locales.NewUniversalTranslator(fallback)
	if err != nil {
		panic(err)
	}

	// Load more translations from .json files
	err = golangloader.LoadTranslate("lang", ut)
	if err != nil {
		panic(err)
	}

	// Uncomment this line to change the field name key
	// golangloader.FieldNameKey = "validation.attributes.%s"

	// Register the translator
	validate := validator.New()
	err = defaults.Locales.RegisterValidateTranslation(validate, ut)
	if err != nil {
		panic(err)
	}

	// Validate required fields
	err = validate.Struct(struct {
		Username string `validate:"required"`
		Password string `validate:"required"`
	}{
		Username: "",
		Password: "",
	})
	validateErrors := err.(validator.ValidationErrors)

	/* Output:

	Username: Username is a required field
	Password: Password is a required field

	*/
	{
		trans, _ := ut.GetTranslator("en")
		fmt.Println(validateErrors.Translate(trans))
	}

	/* Output:

	Username: 會員名稱為必填欄位
	Password: 密碼為必填欄位

	*/
	{
		trans, _ := ut.GetTranslator("zh_Hant_TW")
		fmt.Println(validateErrors.Translate(trans))
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FieldNameKey = "validation.attributes.%s"

FieldNameKey is the key used to get the field name translation

Functions

func FieldName

func FieldName(trans ut.Translator, fieldName string) string

FieldName returns the translated field name

func LoadTranslate

func LoadTranslate(dir string, uni *ut.UniversalTranslator) error

loads the translations from the .json files in the specified directory

func LoadTranslateFS added in v0.9.1

func LoadTranslateFS(fsys fs.FS, uni *ut.UniversalTranslator) error

loads the translations from the .json files in the specified directory

Types

type Locale

type Locale struct {
	Translator          locales.Translator
	ValidateTranslation func(v *validator.Validate, trans ut.Translator) error
}

type MapLocales

type MapLocales map[string]Locale

func (MapLocales) NewUniversalTranslator

func (l MapLocales) NewUniversalTranslator(fallback string) (*ut.UniversalTranslator, error)

returns a new *ut.UniversalTranslator instance

func (MapLocales) RegisterValidateTranslation

func (l MapLocales) RegisterValidateTranslation(v *validator.Validate, uni *ut.UniversalTranslator) error

registers the translations for all locales

Jump to

Keyboard shortcuts

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