translator

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: MIT Imports: 12 Imported by: 0

README

translator — i18n via universal-translator

import "github.com/downsized-devs/sdk-go/translator"

Stability: Stable — see STABILITY.md

Wraps go-playground/universal-translator with English and Indonesian locales pre-registered. Reads the user's locale from appcontext.

Features

  • Translate(ctx, key, params) — locale-aware translation.
  • English + Indonesian locales registered out of the box.

Installation

go get github.com/downsized-devs/sdk-go/translator

Quick Start

tr := translator.Init(translator.Config{
    Translations: []translator.Translation{
        { Key: "welcome", En: "Welcome, {0}", Id: "Selamat datang, {0}" },
    },
}, log)

ctx := appcontext.SetAcceptLanguage(context.Background(), language.Indonesian)
msg, _ := tr.Translate(ctx, "welcome", "Alice")
// msg == "Selamat datang, Alice"

API Reference

Symbol Signature
Init func Init(cfg Config, log logger.Interface) Interface
Interface.Translate (ctx, key string, params ...any) (string, error)
Config { Translations []Translation }
Translation { Key, En, Id string }

Error Handling

Unknown keys return a coded error from codes. Decide per-call whether to fall back to a literal.

Dependencies

  • Internal: appcontext, codes, errors, language, logger
  • External: github.com/go-playground/locales, .../locales/en, .../locales/id, github.com/go-playground/universal-translator

Testing

go test ./translator/...

Contributing

See CONTRIBUTING.md. Adding a locale: register it in translator.go and extend Translation to include the new field.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	FallbackLanguageID   string
	SupportedLanguageIDs []string
	TranslationDir       string
}

type Interface

type Interface interface {
	Translate(ctx context.Context, key interface{}, params ...string) (string, error)
}

func Init

func Init(conf Config, log logger.Interface) Interface

Jump to

Keyboard shortcuts

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