i18n

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2026 License: MIT Imports: 3 Imported by: 1

README

go-i18n

CI

Lightweight internationalization for Go services built on top of nicksnyder/go-i18n, with request language detection and a Gin middleware.

Features

  • Resolve the active language from context.Context, falling back to a default.
  • Per-language localizer with automatic fallback to the default language.
  • Gin middleware reading X-Language / Accept-Language.
  • Bundle initializer that loads translation files from configured paths.

Install

go get github.com/tech-nimble/go-i18n

Configuration

Translation files are JSON named <lang>.json (ru.json, kk.json, en.json). Their paths are read from environment variables:

RU_TRANSLATE_RESOURCE_PATH
KK_TRANSLATE_RESOURCE_PATH

Usage

import (
	"github.com/tech-nimble/go-i18n"
	i18ninit "github.com/tech-nimble/go-i18n/initializers/i18n"
)

func main() {
	bundle := i18ninit.Initialize()
	lang := i18n.NewDefault(bundle)

	// Gin middleware
	router.Use(lang /* via */ )

	localize := i18n.NewLocalize(bundle, "ru")
	fmt.Println(localize.Tf("privileges.remain_days_from_day", map[string]any{"Days": "1"}, 1))
}

If templateData and pluralCount are omitted, the other form is used.

Supported languages

Defaults: ru (default), kk, en. Override via NewLang.

License

MIT © Nimble Tech

Documentation

Overview

Package i18n provides language detection, localization and a Gin middleware for selecting the request language.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lang

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

Lang resolves the active language from a context.

func NewDefault

func NewDefault(bundle *i18n.Bundle) *Lang

NewDefault builds a Lang with the default set of languages.

func NewLang

func NewLang(defaultLang string, availableLanguages []string, bundle *i18n.Bundle) *Lang

NewLang builds a Lang from the given default and available languages.

func (*Lang) GetDefaultLang

func (s *Lang) GetDefaultLang() string

GetDefaultLang returns the default language.

func (*Lang) GetLangFromContext

func (s *Lang) GetLangFromContext(ctx context.Context) string

GetLangFromContext returns the language stored in the context, or the default.

func (*Lang) GetLocalize

func (s *Lang) GetLocalize(ctx context.Context) *Localize

GetLocalize returns the localizer for the context language.

func (*Lang) IsAvailableLang

func (s *Lang) IsAvailableLang(lang string) bool

IsAvailableLang reports whether the language is supported. An empty string is treated as unsupported.

func (*Lang) SetLangToContext

func (s *Lang) SetLangToContext(ctx context.Context, lang string) context.Context

SetLangToContext stores the language in the context if it is supported.

type Localize

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

Localize translates message ids for a single language.

func NewLocalize

func NewLocalize(bundle *base.Bundle, langs ...string) *Localize

NewLocalize builds a localizer for the given languages.

func (*Localize) T

func (l *Localize) T(message string) string

T translates a message id.

func (*Localize) Tf

func (l *Localize) Tf(message string, templateData map[string]any, pluralCount any) string

Tf translates a message id with template data and a plural count.

type Middleware

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

Middleware stores the request language in the context.

func NewMiddleware

func NewMiddleware(settings Settings) *Middleware

NewMiddleware builds the middleware.

func (*Middleware) SetLanguage

func (m *Middleware) SetLanguage(ctx *gin.Context)

SetLanguage extracts the language from the request and stores it in the context.

type Responder

type Responder struct {
	I18nBundle *bi18n.Bundle
}

Responder exposes the translation bundle.

func NewResponder

func NewResponder(i18nBundle *bi18n.Bundle) *Responder

NewResponder builds a Responder.

type Settings

type Settings interface {
	GetDefaultLang() string
	IsAvailableLang(lang string) bool
	SetLangToContext(ctx context.Context, lang string) context.Context
}

Settings exposes the language configuration the middleware relies on.

Directories

Path Synopsis
initializers
i18n
Package i18n initializes the translation bundle from configured resources.
Package i18n initializes the translation bundle from configured resources.

Jump to

Keyboard shortcuts

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