i18n

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package i18n implements a simple approach to deal with translations and date/time localization.

Example:

package main

import (
	"fmt"
	"time"

	"github.com/whity/go-i18n"
	"github.com/whity/go-i18n/storage"
)

func main() {
	// Reading from yaml embedded files
	// yamlStore := storage.NewYaml(*embed.FS)

	// Map store
	store := storage.NewMap(storage.Data{
		"en": storage.Data{
			"translations": storage.Data{
				"hello": "world",
				"new_message": storage.Data{
					"1":     "you have a new message",
					"2..":   "you have {{.count}} new messages",
					"other": "no message",
				},
			},
			"formats": storage.Data{
				"time": storage.Data{
					"formats": storage.Data{
						"default": "%I:%M:%S %p",
					},
				},
				"date": storage.Data{
					"abbr_month_names": []any{
						"jan",
						"feb",
						"mar",
						"apr",
						"may",
						"jun",
						"jul",
						"aug",
						"sep",
						"oct",
						"nov",
						"dec",
					},
					"month_names": []any{
						"january",
						"february",
						"march",
						"april",
						"may",
						"june",
						"july",
						"august",
						"september",
						"october",
						"november",
						"december",
					},
					"abbr_day_names": []any{
						"sun",
						"mon",
						"tue",
						"wed",
						"thu",
						"fri",
						"sat",
					},
					"day_names": []any{
						"sunday",
						"monday",
						"tuesday",
						"wednesday",
						"thursday",
						"friday",
						"saturday",
					},
					"formats": storage.Data{
						"default": "%Y-%m-%d",
						"long":    "%d of %b %Y",
					},
				},
			},
		},
		"pt": storage.Data{
			"translations": storage.Data{
				"hello": "mundo",
				"new_message": storage.Data{
					"1":     "tem uma nova mensagem",
					"2..":   "tem {{.count}} novas mensagens",
					"other": "sem mensagens",
				},
			},
			"formats": storage.Data{
				"time": storage.Data{
					"formats": storage.Data{
						"default": "%H:%M:%S",
					},
				},
			},
		},
	})

	i, err := i18n.New(store, "en")
	if err != nil {
		panic(err)
	}

	fmt.Println(i.Translate("hello"))
	fmt.Println(i.Translate("new_message", i18n.TranslateWithCount(1)))
	fmt.Println(i.Translate("new_message", i18n.TranslateWithCount(0)))
	fmt.Println(i.Translate("new_message", i18n.TranslateWithCount(4)))
	fmt.Println(i.Date(time.Now(), i18n.WithFormat("long")))
	fmt.Println(i.Time(time.Now()))
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(st storage.Storage, locale string) (*i18n, error)

func TranslateWithCount

func TranslateWithCount(count int) translateOption

func WithFormat

func WithFormat(name string) localizeOption

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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