RespKit

Localization

The default locale is id. RespKit embeds its built-in Indonesian (id) and English (en) catalogs from JSON files, so applications do not need to read locale files from disk at runtime.

RespKit checks the Accept-Language header for each request and supports regional tags such as id-ID. It selects a supported catalog for the response and sets Content-Language to the language used. A single response uses one effective locale so its messages stay consistent.

Override the language for one response

Use resp.Locale to select a language for an individual response:

return resp.Fail(ctx, resp.ErrNotFound, resp.Locale("en"))

The response option takes precedence over the request's Accept-Language value.

Set a default locale and add messages

err := resp.Configure(
	resp.WithDefaultLocale("en"),
	resp.WithTranslations(map[string]map[string]string{
		"en": {"USER_SUSPENDED": "Your account is suspended."},
	}),
)
if err != nil {
	return err
}

Call Configure during startup before serving requests. The resulting configuration must include a catalog for the selected default locale. WithTranslations adds or replaces entries in the catalogs.

Applications can add other languages by providing their own message catalog. Add a localized entry for each supported locale, keep the public error code the same across languages, and translate only the user-facing message.

Translation parameters

Some built-in validation messages use placeholders such as {field} and {min}. Supply their values through resp.FieldError.Params. Avoid putting secrets or private user data in translation parameters.

On this page