i18next

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

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

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 8 Imported by: 0

README

I18next Go

GoDoc

Sample:

import (
	"fmt"
	"net/http"

	i18next "codeberg.org/lil5/i18next_go"
)

func main() {
	i18n := i18next.Init(i18next.Options{
		Languages:   []string{"en", "de"},
		FallbackLng: "en",
		Ns:          []string{"translation"},
		DefaultNs:   "translation",
		Resources: map[string]map[string]map[string]string{
			"en": {
				"translation": {
					"helloWorld": "Hello world, this is translated using: {{product}}!",
				},
			},
			"de": {
				"translation": {
					"helloWorld": "Hallo Welt, dies wird übersetzt mit: {{product}}!",
				},
			},
		},
	})
	mux := http.NewServeMux()

	mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
		t := i18n.GetContextWithLanguageDetect(r, nil)

		w.Header().Set("Content-Type", "text/html;charset=UTF-8")
		w.WriteHeader(http.StatusOK)
		fmt.Fprint(w, t.T("helloWorld", map[string]any{"product": "i18next"}))
	})

	http.ListenAndServe("localhost:8090", mux)
}

Getting started

  1. Create a i18n variable.
i18n := i18next.Init(i18next.Options{
	Languages: []string{"en", "de"},
	FallbackLng: "en",
	Ns: []string{"translation"},
	// // Add translations in code (optional)
	// Resources: map[string]map[string]map[string]string{
	//   "en": { "translation": { "lorem": "Lorem" } },
	//   "de": { "translation": { "lorem": "Lörem" } },
	// }
})
  1. Add translations from json files (optional)

    First embed the files into the binary, then use {{lng}} and {{ns}} to denote the language and namespace respectively.

//go:embed locales
var localesFS embed.FS

i18n.AddLocaleFS(localesFS, "locales/{{lng}}/{{ns}}.json")
// This would load the following files:
// -> locales/en/translation.json
// -> locales/de/translation.json
  1. Create a t variable

    I a request handler like in mux.HandleFunc use the request to find a requested language.

t := i18n.GetContextWithLanguageDetect(r, nil)
// Or if you know which language to use:
t := i18n.GetContext("de")
  1. Translate with keys
t.T("lorem")
// -> Lörem

Interpolation

Basic translations t.T
// { "hello": "Hallo" }
t.T("hello")
// -> Hallo

// { "dearName": "Lieber {{name}}" }
t.T("dearName", map[string]any{"name":"John"})
// -> Lieber John

// { "day_one": "ein Tag", "day_other": "{{count}} Tage" }
t.T("day", map[string]any{"count":1})
// -> ein Tag
t.T("day", map[string]any{"count":2})
// -> 2 Tage

Support for setting namespace is available as well

// "de": {
//   "translation": { "hello": "Hallo" },
//   "errPage": { "experiencingTechDiff": "Wir haben derzeit technische Schwierigkeiten." },
t.T("experiencingTechDiff", map[string]any{"ns": "errPage"})
// -> Wir haben derzeit technische Schwierigkeiten.
Element replacements t.Trans
// { "pleaseClick<0>Here</0>": "Bitte klicken Sie <0>hier</0>" }
t.Trans("pleaseClick<0>Here</0>", map[string]any{
	"0": func (s string) string {
		return `<a href="#">`+s+"</a>"
	}
})
// -> Bitte klicken Sie <a href="#">hier</a>

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	OrderCookie   = Order(0)
	OrderAccept   = Order(1)
	OrderFallback = Order(2)
)

Functions

func IsRtl

func IsRtl(lng string) bool

Returns if language is right-to-left, expects lng argument to start with the two-letter language code (ISO 639-1)

Types

type Context

type Context struct {
	Lng string
	// contains filtered or unexported fields
}

func (*Context) T

func (c *Context) T(key string, pp ...map[string]any) string

func (*Context) Trans

func (c *Context) Trans(key string, pp ...map[string]any) string

type I18next

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

func Init

func Init(o Options) *I18next

func (*I18next) AddLocale

func (i18n *I18next) AddLocale(pattern string) *I18next

func (*I18next) AddLocaleFS

func (i18n *I18next) AddLocaleFS(dir fs.FS, pattern string) *I18next

pattern should be ./{{lng}}/{{ns}}.json

func (*I18next) GetContext

func (i18n *I18next) GetContext(lng string) *Context

func (*I18next) GetContextWithLanguageDetect

func (i18n *I18next) GetContextWithLanguageDetect(r *http.Request, o *LanguageDetectOptions) *Context

type LanguageDetectOptions

type LanguageDetectOptions struct {
	// Language detection method order
	//
	// Default: `[OrderCookie, OrderAccept, OrderFallback]`
	Order []Order
	// Fallback language, by default set to the Options.FallbackLng
	Fallback string
	// Supported languages to select from, by default set to the Options.Languages
	SupportedLanguages []string
}

type OComponent

type OComponent = func(string) string

type OCount

type OCount = int

type Options

type Options struct {
	Languages   []string
	FallbackLng string
	Ns          []string
	DefaultNs   string
	/** [Language][Ns][Key]Translation */
	Resources map[string]map[string]map[string]string
}

type Order

type Order = int

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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