i18n

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package i18n is a small, dependency-free internationalization helper: a message catalog (Bundle) loaded from nested JSON, with {placeholder} substitution, one/other pluralization, and a fallback locale. Request-scoped locale detection lives in Middleware.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Locale

func Locale(c *fiber.Ctx) string

Locale returns the resolved request locale (or the bundle fallback / "").

func Middleware

func Middleware(b *Bundle, opts ...MWOption) fiber.Handler

Middleware resolves the request locale (query > cookie > Accept-Language > fallback) and stores it, with the bundle, for T/N/Locale.

func N

func N(c *fiber.Ctx, key string, n int, args M) string

N translates the plural form of key for the request locale and count n.

func T

func T(c *fiber.Ctx, key string, args M) string

T translates key for the request locale.

Types

type Bundle

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

Bundle is a catalog of messages per locale.

Example
package main

import (
	"fmt"

	"github.com/rahmadafandi/fibr/i18n"
)

func main() {
	b := i18n.New(i18n.WithFallback("en"))
	b.LoadMap("en", map[string]any{
		"welcome": "Hello, {name}!",
		"cart":    map[string]any{"items": map[string]any{"one": "{n} item", "other": "{n} items"}},
	})
	fmt.Println(b.Translate("en", "welcome", i18n.M{"name": "Sam"}))
	fmt.Println(b.Plural("en", "cart.items", 3, nil))
}
Output:
Hello, Sam!
3 items

func New

func New(opts ...Option) *Bundle

New creates an empty bundle.

func (*Bundle) Fallback

func (b *Bundle) Fallback() string

Fallback returns the fallback locale.

func (*Bundle) Has

func (b *Bundle) Has(locale string) bool

Has reports whether the bundle has any messages for locale.

func (*Bundle) LoadFS

func (b *Bundle) LoadFS(fsys fs.FS, dir string) error

LoadFS loads every <locale>.json file in dir of fsys (file base name is the locale).

func (*Bundle) LoadJSON

func (b *Bundle) LoadJSON(locale string, data []byte) error

LoadJSON merges nested JSON messages into locale.

func (*Bundle) LoadMap

func (b *Bundle) LoadMap(locale string, nested map[string]any)

LoadMap merges a nested message map into locale (flattened to dotted keys).

func (*Bundle) Locales

func (b *Bundle) Locales() []string

Locales returns the loaded locales, sorted.

func (*Bundle) Plural

func (b *Bundle) Plural(locale, key string, n int, args M) string

Plural resolves "<key>.<category>" where category is the locale's CLDR plural form for n (one of zero/one/two/few/many/other), falling back to "<key>.other" then the normal fallback chain. {n} is set to n unless already in args.

func (*Bundle) Translate

func (b *Bundle) Translate(locale, key string, args M) string

Translate resolves key in locale, then the fallback locale, then returns key itself if missing. args fill {placeholders}.

type M

type M map[string]any

M is a placeholder argument set: {key} in a message is replaced by args[key].

type MWOption

type MWOption func(*mwConfig)

MWOption configures the middleware.

func WithCookieKey

func WithCookieKey(k string) MWOption

WithCookieKey sets the cookie checked for the locale (default "lang").

func WithQueryKey

func WithQueryKey(k string) MWOption

WithQueryKey sets the query parameter checked for the locale (default "lang").

type Option

type Option func(*Bundle)

Option configures a Bundle.

func WithFallback

func WithFallback(locale string) Option

WithFallback sets the locale used when a key is missing in the requested locale (default "en").

Jump to

Keyboard shortcuts

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