i18n

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2025 License: ISC Imports: 8 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

i18n Library

GitHub Tag Go Reference License Go Report Card Contributors Issues

The i18n library provides a simple and extensible solution for handling translations, pluralization, and localization in Go applications. It supports multiple locales, JSON-based translation files, and custom pluralization rules.

Features

  • JSON-based translation management.
  • Pluralization support for different languages.
  • Customizable localization with placeholders.
  • Thread-safe operations for concurrent use.

Installation

go get github.com/go-universal/i18n

Sample usage:

package main

import (
    "fmt"
    "github.com/go-universal/i18n"
    "golang.org/x/text/language"
)

func main() {
    // Define translations
    en := `{
        "welcome": "Hello {name}, Welcome!",
        "notification": {
            "zero": "No new messages",
            "one": "You have a new message",
            "other": "You have {count} new messages"
        }
    }`
    fa := `{
        "welcome": "سلام {name}, خوش آمدید!",
        "notification": {
            "zero": "پیام جدیدی ندارید",
            "one": "یک پیام جدید دارید",
            "other": "{count} پیام جدید دارید"
        }
    }`

    // Create a translator
    tr := i18n.NewTranslator("en", language.English)
    tr.AddLocale("fa", &language.Persian)
    tr.LoadBytes("en", []byte(en))
    tr.LoadBytes("fa", []byte(fa))

    // Translate a simple message
    fmt.Println(tr.Translate("en", "welcome", map[string]any{"name": "John"}))
    // Output: Hello John, Welcome!

    // Translate a plural message
    fmt.Println(tr.Plural("fa", "notification", 0, nil))
    // Output: پیام جدیدی ندارید

    fmt.Println(tr.Plural("en", "notification", 5, map[string]any{"count": 5}))
    // Output: You have 5 new messages
}

API Documentation

  • NewTranslator(locale string, formatter language.Tag) Translator: Creates a new translator with the default locale and formatter.
  • AddLocale(key string, formatter *language.Tag): Adds a new locale with the given key and optional formatter.
  • LoadBytes(locale string, content ...[]byte): Loads translation JSON content from byte slices for the specified locale.
  • LoadFiles(locale string, files ...string) error: Loads translation content from JSON files for the specified locale.
  • AddMessage(locale, key, message string, options ...PluralOption): Adds a new message with the given key and message string, with optional pluralization rules.
  • Translate(locale, key string, values map[string]any) string: Translates a message identified by the key using the provided values.
  • Plural(locale, key string, count int, values map[string]any) string: Translates a plural message identified by the key based on the count and provided values.

License

This library is licensed under the ISC License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PluralOption

type PluralOption func(*pluralMessage)

PluralOption defines a function type for modifying a pluralMessage.

func PluralFew

func PluralFew(msg string) PluralOption

PluralFew sets the message for a few counts (e.g., 3-10).

func PluralMany

func PluralMany(msg string) PluralOption

PluralMany sets the message for many counts (e.g., >10).

func PluralOne

func PluralOne(msg string) PluralOption

PluralOne sets the message for count = 1.

func PluralTwo

func PluralTwo(msg string) PluralOption

PluralTwo sets the message for count = 2.

func PluralZero

func PluralZero(msg string) PluralOption

PluralZero sets the message for count = 0.

type Translator

type Translator interface {
	// AddLocale adds a new locale with the given key and formatter.
	AddLocale(key string, formatter *language.Tag)

	// LoadBytes loads translation JSON content from byte slices for the specified locale.
	LoadBytes(locale string, content ...[]byte)

	// LoadFiles loads translation content from JSON files for the specified locale.
	LoadFiles(locale string, files ...string) error

	// AddMessage adds a new message with the given key and message string.
	AddMessage(locale, key, message string, options ...PluralOption)

	// Translate translates a message identified by the key using the provided values.
	Translate(locale, key string, values map[string]any) string

	// Plural translates a plural message identified by the key based on the count and provided values.
	Plural(locale, key string, count int, values map[string]any) string
}

Translator defines an interface for handling translations and localization.

func NewTranslator

func NewTranslator(locale string, formatter language.Tag) Translator

NewTranslator creates a new translator with the default locale and formatter.

Jump to

Keyboard shortcuts

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