i18n

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

README

github.com/ubgo/auth/i18n

Ready translations of every error message the library and its plugins declare, in the 21 languages better-auth's @better-auth/i18n ships beside English: Arabic, Bengali, German, Spanish, Persian, French, Hindi, Indonesian, Italian, Japanese, Korean, Dutch, Polish, Portuguese, Russian, Swedish, Thai, Turkish, Ukrainian, Vietnamese and Chinese (Simplified).

Install

go get github.com/ubgo/auth

The package is in the core module and uses only the standard library (embed).

Use

a, err := auth.New(
	auth.WithDB(db, sqlb.PostgresDialect{}),
	auth.WithBaseURL("https://app.example.com"),
	auth.WithPlugins(email.New(email.Login()), password.New()),
	auth.WithMessages(i18n.Messages()),
)

A request with Accept-Language: de-AT then gets {"error":{"code":"auth.signed_out","message":"Melden Sie sich an, um fortzufahren.","originalMessage":"Sign in to continue."}}. The lookup is auth.Messages': the exact tag, then the base language, so pt-BR and zh-CN find pt and zh. A language that is not shipped, and a code added after the translations, get the declared English message; the code and the status never change.

Function What it returns
i18n.Messages() every shipped locale as one auth.Messages, a fresh map on each call
i18n.MessagesFor(tags...) only the named locales; an unshipped tag is an error naming the shipped ones
i18n.Locales the shipped tags, sorted

Your own wording

Messages() returns a map the caller owns, so an app replaces any entry before passing it:

m := i18n.Messages()
m["de"]["auth.signed_out"] = "Bitte anmelden."
a, err := auth.New(..., auth.WithMessages(m))

The files

Each language is locales/<tag>.json, one object of error code to message, the same shape as docs/reference/error-codes.json lists the English ones in. The tests check that every file is listed in Locales and the reverse, that every key is a code some package declares (a renamed code leaves no stale key), that no message is empty, and that every locale translates the same codes. A code added later is reported as untranslated by go test -v ./i18n and answers in English until a translator adds it.

Documentation

Overview

Package i18n ships ready translations of every error message the library and its plugins declare, in the 21 languages better-auth's i18n plugin ships beside English, as an auth.Messages catalogue:

a, err := auth.New(..., auth.WithMessages(i18n.Messages()))

The catalogue only changes the message: the code and the status of an error never change, so a client that matches codes sees no difference, and a code a locale does not translate (a plugin added after the translation) keeps its declared English message rather than an empty one. An app that wants its own voice merges or replaces entries: Messages returns a fresh map on every call, so editing it changes nothing anyone else holds.

The translations live in locales/<tag>.json, one object of code to message per language, the same shape as docs/reference/error-codes.json lists the English ones in, so a file can be corrected or added without touching Go.

Index

Constants

This section is empty.

Variables

View Source
var Locales = []string{"ar", "bn", "de", "es", "fa", "fr", "hi", "id", "it", "ja", "ko", "nl", "pl", "pt", "ru", "sv", "th", "tr", "uk", "vi", "zh"}

Locales lists the tags Messages translates into, sorted: Arabic, Bengali, German, Spanish, Persian, French, Hindi, Indonesian, Italian, Japanese, Korean, Dutch, Polish, Portuguese, Russian, Swedish, Thai, Turkish, Ukrainian, Vietnamese and Chinese (Simplified). English is not in it: the declared messages are English, and a request in English gets them.

Functions

func Messages

func Messages() auth.Messages

Messages returns the catalogue of every shipped locale, for auth.WithMessages. The lookup is auth.Messages': the request's exact tag, then its base language, so "pt-BR" and "zh-CN" find "pt" and "zh".

It panics if an embedded file does not parse, which only a broken build can cause: the files are compiled in and a test parses every one.

func MessagesFor

func MessagesFor(tags ...string) (auth.Messages, error)

MessagesFor is Messages restricted to the named locales, for an app that serves a few languages and wants no other in memory. A tag that is not in Locales is an error, so a typo does not silently leave a language out.

Types

This section is empty.

Jump to

Keyboard shortcuts

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