fiberi18n

package module
v0.0.0-...-bf05d93 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2023 License: MIT Imports: 9 Imported by: 0

README


id: fiberi18n

Fiberi18n

Release Discord Test Security Linter

go-i18n support for Fiber.

Note: Requires Go 1.18 and above

Install

This middleware supports Fiber v2.

go get -u github.com/gofiber/fiber/v2
go get -u github.com/gofiber/contrib/fiberi18n

Signature

fiberi18n.New(config ...*fiberi18n.Config) fiber.Handler

Config

Property Type Description Default
Next func(c *fiber.Ctx) bool A function to skip this middleware when returned true. nil
RootPath string The i18n template folder path. "./example/localize"
AcceptLanguages []language.Tag A collection of languages that can be processed. []language.Tag{language.Chinese, language.English}
FormatBundleFile string The type of the template file. "yaml"
DefaultLanguage language.Tag The default returned language type. language.English
Loader Loader The implementation of the Loader interface, which defines how to read the file. We provide both os.ReadFile and embed.FS.ReadFile. LoaderFunc(os.ReadFile)
UnmarshalFunc i18n.UnmarshalFunc The function used for decoding template files. yaml.Unmarshal
LangHandler func(ctx *fiber.Ctx, defaultLang string) string Used to get the kind of language handled by *fiber.Ctx and defaultLang. Retrieved from the request header Accept-Language or query parameter lang.

Example

package main

import (
	"github.com/gofiber/contrib/fiberi18n"
	"github.com/gofiber/fiber/v2"
	"github.com/nicksnyder/go-i18n/v2/i18n"
	"golang.org/x/text/language"
)

func main() {
	app := fiber.New()
	app.Use(
		fiberi18n.New(&fiberi18n.Config{
			RootPath:        "./example/localize",
			AcceptLanguages: []language.Tag{language.Chinese, language.English},
			DefaultLanguage: language.Chinese,
		}),
	)
	app.Get("/", func(c *fiber.Ctx) error {
		return c.SendString(fiberi18n.MustGetMessage("welcome"))
	})
	app.Get("/:name", func(ctx *fiber.Ctx) error {
		return ctx.SendString(fiberi18n.MustGetMessage(&i18n.LocalizeConfig{
			MessageID: "welcomeWithName",
			TemplateData: map[string]string{
				"name": ctx.Params("name"),
			},
		}))
	})
	app.Listen("127.0.0.1:3000")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigDefault = &Config{
	RootPath:         "./example/localize",
	DefaultLanguage:  language.English,
	AcceptLanguages:  []language.Tag{language.Chinese, language.English},
	FormatBundleFile: "yaml",
	UnmarshalFunc:    yaml.Unmarshal,
	Loader:           LoaderFunc(os.ReadFile),
	LangHandler:      defaultLangHandler,
}

Functions

func GetMessage

func GetMessage(params interface{}) (string, error)

GetMessage get the i18n message

 param is one of these type: messageID, *i18n.LocalizeConfig
 Example:
	GetMessage("hello") // messageID is hello
	GetMessage(&i18n.LocalizeConfig{
			MessageID: "welcomeWithName",
			TemplateData: map[string]string{
				"name": context.Param("name"),
			},
	})

func MustGetMessage

func MustGetMessage(params interface{}) string

MustGetMessage get the i18n message without error handling

  param is one of these type: messageID, *i18n.LocalizeConfig
  Example:
	MustGetMessage("hello") // messageID is hello
	MustGetMessage(&i18n.LocalizeConfig{
			MessageID: "welcomeWithName",
			TemplateData: map[string]string{
				"name": context.Param("name"),
			},
	})

func New

func New(config ...*Config) fiber.Handler

New creates a new middleware handler

Types

type Config

type Config struct {
	// Next defines a function to skip this middleware when returned true.
	//
	// Optional. Default: nil
	Next func(c *fiber.Ctx) bool

	// RootPath is i18n template folder path
	//
	// Default: ./example/localize
	RootPath string

	// AcceptLanguages is a collection of languages that can be processed
	//
	// Optional. Default: []language.Tag{language.Chinese, language.English}
	AcceptLanguages []language.Tag

	// FormatBundleFile is type of template file.
	//
	// Optional. Default: "yaml"
	FormatBundleFile string

	// DefaultLanguage is the default returned language type
	//
	// Optional. Default: language.English
	DefaultLanguage language.Tag

	// Loader implements the Loader interface, which defines how to read the file.
	// We provide both os.ReadFile and embed.FS.ReadFile
	// Optional. Default: LoaderFunc(os.ReadFile)
	Loader Loader

	// UnmarshalFunc for decoding template files
	//
	// Optional. Default: yaml.Unmarshal
	UnmarshalFunc i18n.UnmarshalFunc

	// LangHandler is used to get the kind of language handled by *fiber.Ctx and defaultLang
	//
	// Optional. Default: The language type is retrieved from the request header: `Accept-Language` or query param : `lang`
	LangHandler func(ctx *fiber.Ctx, defaultLang string) string
	// contains filtered or unexported fields
}

type EmbedLoader

type EmbedLoader struct {
	FS embed.FS
}

func (*EmbedLoader) LoadMessage

func (e *EmbedLoader) LoadMessage(path string) ([]byte, error)

type Loader

type Loader interface {
	LoadMessage(path string) ([]byte, error)
}

type LoaderFunc

type LoaderFunc func(path string) ([]byte, error)

func (LoaderFunc) LoadMessage

func (f LoaderFunc) LoadMessage(path string) ([]byte, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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