lingo package - github.com/Zapharaos/lingo - Go Packages

lingo

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2025 License: MIT Imports: 13 Imported by: 0

README

PkgGoDev Go Version Go Report Card GitHub License

GitHub Release GitHub Actions Workflow Status codecov

lingo

Go library for streamlining language translation handling.

lingo automatically discovers translation configuration files in the input path, loads the translations, and sets up a localizer service. Users can either use the default go-i18n implementation or implement their own custom solution. It provides a simple, unified interface for managing internationalization in Go applications.

Features

  • Auto-discovery: Automatically finds and loads translation files from specified directories
  • Flexible implementation: Use the built-in go-i18n implementation or create your own
  • Fallback support: Graceful fallback to default language when translations are missing
  • Template variables: Support for dynamic content with template data
  • Pluralization: Built-in support for plural forms

Supported File Formats

lingo supports the following translation file formats through go-i18n:

  • TOML (.toml)
  • JSON (.json)
  • YAML (.yaml, .yml)

Installation

go get github.com/Zapharaos/lingo

Note: Spit uses Go Modules to manage dependencies.

Usage Examples

1. Create translation files

Create a translation file (e.g., messages.en.toml):

[hello_world]
other = "Hello, World!"
2. Initialize and use lingo
package main

import (
    "fmt"
    "log"
    
    "github.com/Zapharaos/lingo"
    "golang.org/x/text/language"
)

func main() {
    // Initialize the localizer service
    i18n, err := lingo.NewI18n(
        language.English, // default language
        "config/",        // translations directory
        "messages",       // file prefix
    )
    if err != nil {
        log.Fatalf("Failed to initialize: %v", err)
    }
    
    // Set as global service
    lingo.SetLocalizerService(i18n)
    
    // Translate a message
    result := lingo.MustTranslate(language.English, &lingo.Message{
        ID: "hello_world",
    })
    fmt.Println(result) // Output: Hello, World!
}

For comprehensive usage examples including template variables, pluralization, and fallback behavior, see the examples package.

Development

Install dependencies:

make dev-deps

Run unit tests and generate coverage report:

make test-unit

Run linters:

make lint

Some linter violations can automatically be fixed:

make fmt

Contributing

We welcome contributions to the lingo library! If you have a bug fix, feature request, or improvement, please open an issue or pull request on GitHub. We appreciate your help in making lingo better for everyone. If you are interested in contributing to the lingo library, please check out our contributing guidelines for more information on how to get started.

License

The project is licensed under the MIT License.

Documentation

Overview

Package lingo streamlines language translations.

Features:

  • Automatically finds and loads translation files from specified directories
  • Follows BCP 47 language tags
  • Graceful fallback to default language when translations are missing
  • Support for dynamic content with template data
  • Supports pluralization
  • Defaults to https://github.com/nicksnyder/go-i18n for translation management
  • Allows you to implement your own custom solution by implementing the LocalizerService interface

Supported extensions (specific to go-i18n):

  • JSON
  • YAML / YML
  • TOML

For more details, see README.md.

Package lingo is a generated GoMock package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLocalizer

func GetLocalizer(language language.Tag) (interface{}, bool, error)

GetLocalizer Directly exposes the current service GetLocalizer function.

func MustTranslate

func MustTranslate(localizer interface{}, message *Message) string

MustTranslate Directly exposes the current service MustTranslate function.

func SetLocalizerService

func SetLocalizerService(service LocalizerService) func()

SetLocalizerService affect a new repository to the global service singleton

func Translate

func Translate(localizer interface{}, message *Message) (string, bool, error)

Translate Directly exposes the current service Translate function.

Types

type I18nLocalizerService

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

I18nLocalizerService implements the LocalizerService interface using i18n

func (*I18nLocalizerService) GetLocalizer

func (t *I18nLocalizerService) GetLocalizer(language language.Tag) (interface{}, bool, error)

GetLocalizer returns the requested localizer and a boolean indicating if the localizer was found If the requested localizer is not found, returns the default language localizer

func (*I18nLocalizerService) MustTranslate

func (t *I18nLocalizerService) MustTranslate(localizer interface{}, message *Message) string

MustTranslate returns a localized message, panicking on error This is useful when you're confident the translation should always work

func (*I18nLocalizerService) Translate

func (t *I18nLocalizerService) Translate(localizer interface{}, message *Message) (string, bool, error)

Translate returns a localized message for the given localizer and message Returns the translated message, a boolean indicating success, and an error if something went wrong

type LocalizerService

type LocalizerService interface {
	GetLocalizer(language language.Tag) (interface{}, bool, error)
	Translate(localizer interface{}, message *Message) (string, bool, error)
	MustTranslate(localizer interface{}, message *Message) string
}

LocalizerService defines the interface for handling localizers

func GetLocalizerService

func GetLocalizerService() LocalizerService

GetLocalizerService is used to access the global service singleton

func NewI18n

func NewI18n(defaultLang language.Tag, translationsPath string, filePrefixes ...string) (LocalizerService, error)

NewI18n returns a new instance of I18nLocalizerService with a custom file prefix defaultLang: the default language to use when a requested language is not available translationsPath: path to the directory containing translation files filePrefixes: the prefixes that translation files can have (e.g., "active" for "active.en.toml")

type Message

type Message struct {
	ID          string
	Data        interface{}
	PluralCount interface{}
}

Message represents a translatable message item

func NewMessage

func NewMessage(id string) *Message

NewMessage creates a new Message instance with the given ID

func (*Message) WithData

func (m *Message) WithData(data interface{}) *Message

WithData sets the ID for the message

func (*Message) WithPluralCount

func (m *Message) WithPluralCount(count interface{}) *Message

WithPluralCount sets the plural count for the message

type MockLocalizerService

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

MockLocalizerService is a mock of LocalizerService interface.

func NewMockLocalizerService

func NewMockLocalizerService(ctrl *gomock.Controller) *MockLocalizerService

NewMockLocalizerService creates a new mock instance.

func (*MockLocalizerService) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockLocalizerService) GetLocalizer

func (m *MockLocalizerService) GetLocalizer(arg0 language.Tag) (any, bool, error)

GetLocalizer mocks base method.

func (*MockLocalizerService) MustTranslate

func (m *MockLocalizerService) MustTranslate(localizer any, message *Message) string

MustTranslate mocks base method.

func (*MockLocalizerService) Translate

func (m *MockLocalizerService) Translate(localizer any, message *Message) (string, bool, error)

Translate mocks base method.

type MockLocalizerServiceMockRecorder

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

MockLocalizerServiceMockRecorder is the mock recorder for MockLocalizerService.

func (*MockLocalizerServiceMockRecorder) GetLocalizer

func (mr *MockLocalizerServiceMockRecorder) GetLocalizer(arg0 any) *gomock.Call

GetLocalizer indicates an expected call of GetLocalizer.

func (*MockLocalizerServiceMockRecorder) MustTranslate

func (mr *MockLocalizerServiceMockRecorder) MustTranslate(localizer, message any) *gomock.Call

MustTranslate indicates an expected call of MustTranslate.

func (*MockLocalizerServiceMockRecorder) Translate

func (mr *MockLocalizerServiceMockRecorder) Translate(localizer, message any) *gomock.Call

Translate indicates an expected call of Translate.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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