i18n

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SupportedLanguages = map[string]LanguageConfig{
	"en": {
		Code:       "en",
		Name:       "English",
		NativeName: "English",
		Direction:  LTR,
		DateFormat: "Jan 2, 2006",
		TimeFormat: "3:04 PM",
		NumberFormat: NumberFormat{
			DecimalSeparator:  ".",
			ThousandSeparator: ",",
			Digits:            "0123456789",
		},
		Currency: CurrencyFormat{
			Symbol:           "$",
			Code:             "USD",
			Position:         "before",
			DecimalPlaces:    2,
			SpaceAfterSymbol: false,
		},
	},
	"es": {
		Code:       "es",
		Name:       "Spanish",
		NativeName: "Español",
		Direction:  LTR,
		DateFormat: "2 de Jan de 2006",
		TimeFormat: "15:04",
		NumberFormat: NumberFormat{
			DecimalSeparator:  ",",
			ThousandSeparator: ".",
			Digits:            "0123456789",
		},
		Currency: CurrencyFormat{
			Symbol:           "€",
			Code:             "EUR",
			Position:         "after",
			DecimalPlaces:    2,
			SpaceAfterSymbol: true,
		},
	},
	"fr": {
		Code:       "fr",
		Name:       "French",
		NativeName: "Français",
		Direction:  LTR,
		DateFormat: "2 Jan 2006",
		TimeFormat: "15:04",
		NumberFormat: NumberFormat{
			DecimalSeparator:  ",",
			ThousandSeparator: " ",
			Digits:            "0123456789",
		},
		Currency: CurrencyFormat{
			Symbol:           "€",
			Code:             "EUR",
			Position:         "after",
			DecimalPlaces:    2,
			SpaceAfterSymbol: true,
		},
	},
	"de": {
		Code:       "de",
		Name:       "German",
		NativeName: "Deutsch",
		Direction:  LTR,
		DateFormat: "2. Jan 2006",
		TimeFormat: "15:04",
		NumberFormat: NumberFormat{
			DecimalSeparator:  ",",
			ThousandSeparator: ".",
			Digits:            "0123456789",
		},
		Currency: CurrencyFormat{
			Symbol:           "€",
			Code:             "EUR",
			Position:         "after",
			DecimalPlaces:    2,
			SpaceAfterSymbol: true,
		},
	},
	"ar": {
		Code:       "ar",
		Name:       "Arabic",
		NativeName: "العربية",
		Direction:  RTL,
		DateFormat: "2 Jan 2006",
		TimeFormat: "15:04",
		NumberFormat: NumberFormat{
			DecimalSeparator:  "٫",
			ThousandSeparator: "٬",
			Digits:            "٠١٢٣٤٥٦٧٨٩",
		},
		Currency: CurrencyFormat{
			Symbol:           "ر.س",
			Code:             "SAR",
			Position:         "after",
			DecimalPlaces:    2,
			SpaceAfterSymbol: true,
		},
	},
	"he": {
		Code:       "he",
		Name:       "Hebrew",
		NativeName: "עברית",
		Direction:  RTL,
		DateFormat: "2 Jan 2006",
		TimeFormat: "15:04",
		NumberFormat: NumberFormat{
			DecimalSeparator:  ".",
			ThousandSeparator: ",",
			Digits:            "0123456789",
		},
		Currency: CurrencyFormat{
			Symbol:           "₪",
			Code:             "ILS",
			Position:         "before",
			DecimalPlaces:    2,
			SpaceAfterSymbol: true,
		},
	},
	"fa": {
		Code:       "fa",
		Name:       "Persian",
		NativeName: "فارسی",
		Direction:  RTL,
		DateFormat: "2 Jan 2006",
		TimeFormat: "15:04",
		NumberFormat: NumberFormat{
			DecimalSeparator:  "٫",
			ThousandSeparator: "٬",
			Digits:            "۰۱۲۳۴۵۶۷۸۹",
		},
		Currency: CurrencyFormat{
			Symbol:           "﷼",
			Code:             "IRR",
			Position:         "after",
			DecimalPlaces:    0,
			SpaceAfterSymbol: true,
		},
	},
	"ur": {
		Code:       "ur",
		Name:       "Urdu",
		NativeName: "اردو",
		Direction:  RTL,
		DateFormat: "2 Jan 2006",
		TimeFormat: "15:04",
		NumberFormat: NumberFormat{
			DecimalSeparator:  ".",
			ThousandSeparator: ",",
			Digits:            "۰۱۲۳۴۵۶۷۸۹",
		},
		Currency: CurrencyFormat{
			Symbol:           "Rs",
			Code:             "PKR",
			Position:         "before",
			DecimalPlaces:    2,
			SpaceAfterSymbol: true,
		},
	},
	"ja": {
		Code:       "ja",
		Name:       "Japanese",
		NativeName: "日本語",
		Direction:  LTR,
		DateFormat: "2006年1月2日",
		TimeFormat: "15:04",
		NumberFormat: NumberFormat{
			DecimalSeparator:  ".",
			ThousandSeparator: ",",
			Digits:            "0123456789",
		},
		Currency: CurrencyFormat{
			Symbol:           "¥",
			Code:             "JPY",
			Position:         "before",
			DecimalPlaces:    0,
			SpaceAfterSymbol: false,
		},
	},
	"zh": {
		Code:       "zh",
		Name:       "Chinese",
		NativeName: "中文",
		Direction:  LTR,
		DateFormat: "2006年1月2日",
		TimeFormat: "15:04",
		NumberFormat: NumberFormat{
			DecimalSeparator:  ".",
			ThousandSeparator: ",",
			Digits:            "0123456789",
		},
		Currency: CurrencyFormat{
			Symbol:           "¥",
			Code:             "CNY",
			Position:         "before",
			DecimalPlaces:    2,
			SpaceAfterSymbol: false,
		},
	},
	"pl": {
		Code:       "pl",
		Name:       "Polish",
		NativeName: "Polski",
		Direction:  LTR,
		DateFormat: "2 Jan 2006",
		TimeFormat: "15:04",
		NumberFormat: NumberFormat{
			DecimalSeparator:  ",",
			ThousandSeparator: " ",
			Digits:            "0123456789",
		},
		Currency: CurrencyFormat{
			Symbol:           "zł",
			Code:             "PLN",
			Position:         "after",
			DecimalPlaces:    2,
			SpaceAfterSymbol: true,
		},
	},
	"pt": {
		Code:       "pt",
		Name:       "Portuguese",
		NativeName: "Português",
		Direction:  LTR,
		DateFormat: "2 de Jan de 2006",
		TimeFormat: "15:04",
		NumberFormat: NumberFormat{
			DecimalSeparator:  ",",
			ThousandSeparator: ".",
			Digits:            "0123456789",
		},
		Currency: CurrencyFormat{
			Symbol:           "R$",
			Code:             "BRL",
			Position:         "before",
			DecimalPlaces:    2,
			SpaceAfterSymbol: true,
		},
	},
	"tlh": {
		Code:       "tlh",
		Name:       "Klingon",
		NativeName: "tlhIngan Hol",
		Direction:  LTR,
		DateFormat: "2 Jan 2006",
		TimeFormat: "15:04",
		NumberFormat: NumberFormat{
			DecimalSeparator:  ".",
			ThousandSeparator: ",",
			Digits:            "0123456789",
		},
		Currency: CurrencyFormat{
			Symbol:           "DarSeq",
			Code:             "DRK",
			Position:         "after",
			DecimalPlaces:    2,
			SpaceAfterSymbol: true,
		},
	},
	"ru": {
		Code:       "ru",
		Name:       "Russian",
		NativeName: "Русский",
		Direction:  LTR,
		DateFormat: "2 Jan 2006",
		TimeFormat: "15:04",
		NumberFormat: NumberFormat{
			DecimalSeparator:  ",",
			ThousandSeparator: " ",
			Digits:            "0123456789",
		},
		Currency: CurrencyFormat{
			Symbol:           "₽",
			Code:             "RUB",
			Position:         "after",
			DecimalPlaces:    2,
			SpaceAfterSymbol: true,
		},
	},
	"uk": {
		Code:       "uk",
		Name:       "Ukrainian",
		NativeName: "Українська",
		Direction:  LTR,
		DateFormat: "2 Jan 2006",
		TimeFormat: "15:04",
		NumberFormat: NumberFormat{
			DecimalSeparator:  ",",
			ThousandSeparator: " ",
			Digits:            "0123456789",
		},
		Currency: CurrencyFormat{
			Symbol:           "₴",
			Code:             "UAH",
			Position:         "after",
			DecimalPlaces:    2,
			SpaceAfterSymbol: true,
		},
	},
}

SupportedLanguages contains configuration for all supported languages.

Functions

func Button

func Button(lang, key string, args ...interface{}) string

Button returns a translated button text.

func ConvertDigits

func ConvertDigits(number string, lang string) string

ConvertDigits converts Western digits to locale-specific digits.

func Error

func Error(lang, key string, args ...interface{}) string

Error returns a translated error message.

func FormatCurrency

func FormatCurrency(amount float64, lang string) string

FormatCurrency formats currency according to language configuration.

func FormatNumber

func FormatNumber(value float64, lang string, decimals int) string

FormatNumber formats a number according to language configuration.

func GetCSSClass

func GetCSSClass(lang string) string

GetCSSClass returns CSS classes for language-specific styling.

func GetHTMLAttributes

func GetHTMLAttributes(lang string) map[string]string

GetHTMLAttributes returns HTML attributes for language support.

func Initialize

func Initialize(config *Config) error

Initialize initializes the i18n system.

func IsRTL

func IsRTL(code string) bool

IsRTL checks if a language is right-to-left.

func Label

func Label(lang, key string, args ...interface{}) string

Label returns a translated label.

func Message

func Message(lang, key string, args ...interface{}) string

Message returns a translated message.

func Success

func Success(lang, key string, args ...interface{}) string

Success returns a translated success message.

func TemplateFuncs

func TemplateFuncs(lang string) template.FuncMap

TemplateFuncs returns template functions for i18n.

func Validation

func Validation(lang, key string, args ...interface{}) string

Validation returns a translated validation message.

Types

type Config

type Config struct {
	DefaultLanguage    string
	SupportedLanguages []string
}

Config represents i18n configuration.

type CurrencyFormat

type CurrencyFormat struct {
	Symbol           string `json:"symbol"`
	Code             string `json:"code"`
	Position         string `json:"position"` // before or after
	DecimalPlaces    int    `json:"decimal_places"`
	SpaceAfterSymbol bool   `json:"space_after_symbol"`
}

CurrencyFormat represents currency formatting configuration.

type I18n

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

I18n handles internationalization.

var Instance *I18n

Instance is the global i18n instance.

func GetInstance

func GetInstance() *I18n

GetInstance returns the global i18n instance.

func (*I18n) AddTranslation

func (i *I18n) AddTranslation(lang, key, value string)

AddTranslation adds or updates a translation.

func (*I18n) GetAllKeys

func (i *I18n) GetAllKeys(lang string) []string

GetAllKeys returns all translation keys for a language in dot notation.

func (*I18n) GetDefaultLanguage

func (i *I18n) GetDefaultLanguage() string

GetDefaultLanguage returns the default language.

func (*I18n) GetSupportedLanguages

func (i *I18n) GetSupportedLanguages() []string

GetSupportedLanguages returns the list of supported languages.

func (*I18n) GetTranslations

func (i *I18n) GetTranslations(lang string) map[string]interface{}

GetTranslations returns all translations for a language.

func (*I18n) LoadCustomTranslations

func (i *I18n) LoadCustomTranslations(lang string, jsonData string) error

LoadCustomTranslations loads custom translations from a JSON string.

func (*I18n) SetDefaultLanguage

func (i *I18n) SetDefaultLanguage(lang string) error

SetDefaultLanguage sets the default language.

func (*I18n) T

func (i *I18n) T(lang, key string, args ...interface{}) string

T translates a key to the specified language.

func (*I18n) Translate

func (i *I18n) Translate(lang, key string, args ...interface{}) string

Translate is an alias for T.

type LanguageConfig

type LanguageConfig struct {
	Code         string            `json:"code"`
	Name         string            `json:"name"`
	NativeName   string            `json:"native_name"`
	Direction    LanguageDirection `json:"direction"`
	DateFormat   string            `json:"date_format"`
	TimeFormat   string            `json:"time_format"`
	NumberFormat NumberFormat      `json:"number_format"`
	Currency     CurrencyFormat    `json:"currency"`
	Enabled      bool              `json:"enabled"`
}

LanguageConfig contains configuration for each language.

func GetEnabledLanguages

func GetEnabledLanguages() []LanguageConfig

GetEnabledLanguages returns languages that have translation files. A language is considered enabled if its JSON file exists in the embedded translations.

func GetLanguageConfig

func GetLanguageConfig(code string) (LanguageConfig, bool)

GetLanguageConfig returns configuration for a language.

type LanguageDirection

type LanguageDirection string

LanguageDirection represents text direction.

const (
	// LTR represents left-to-right text direction.
	LTR LanguageDirection = "ltr"
	// RTL represents right-to-left text direction.
	RTL LanguageDirection = "rtl"
)

func GetDirection

func GetDirection(code string) LanguageDirection

GetDirection returns the text direction for a language.

type NumberFormat

type NumberFormat struct {
	DecimalSeparator  string `json:"decimal_separator"`
	ThousandSeparator string `json:"thousand_separator"`
	Digits            string `json:"digits"` // For languages with different digit systems
}

NumberFormat represents number formatting configuration.

Jump to

Keyboard shortcuts

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