Documentation
¶
Overview ¶
Package i18n resolves user-facing message codes into localized text.
Services raise errors with a stable code rather than a sentence:
httpserver.ConflictError(err, messages.PhoneNumberInUse)
The HTTP error handler looks the code up in the catalog registered on the server and renders it in the locale the caller asked for, so the response carries both the code a client can branch on and text a person can read.
Index ¶
Constants ¶
const ( CodeBadRequest = "BAD_REQUEST" CodeValidation = "VALIDATION_ERROR" CodeForbidden = "FORBIDDEN" CodeNotFound = "NOT_FOUND" CodeConflict = "CONFLICT" CodeInternal = "INTERNAL_ERROR" )
Generic message codes, one per status a handler can return without naming a specific rule. They are the fallback the error handler reports when a message is literal prose rather than a catalog code, so every error carries a code a client can branch on even before a service is fully migrated.
const CodeValidationInvalid = validationCodePrefix + "INVALID"
CodeValidationInvalid is the template used for a validate tag with no translation of its own, so an unknown rule still reads as a sentence.
Variables ¶
This section is empty.
Functions ¶
func CodeForStatus ¶
CodeForStatus is the generic code reported for an HTTP status.
func FieldCode ¶ added in v1.6.0
FieldCode returns the catalog code for a request field, so `phoneNumber` resolves to FIELD_PHONE_NUMBER. The name is whichever one the validator reports: the json tag when a field has one, and the Go field name otherwise, which is what query and path parameters fall back to.
Acronyms stay whole, so `vendorID` and `VendorID` both resolve to FIELD_VENDOR_ID rather than splitting every capital.
func ValidationCode ¶ added in v1.6.0
ValidationCode returns the catalog code for a validate tag, so `required` resolves to VALIDATION_REQUIRED.
func ValidationMessage ¶ added in v1.6.0
ValidationMessage renders one field failure in locale. The template comes from the tag and the field name from the catalog, so a service localizes its request fields by registering labels rather than by touching a handler.
A field with no registered label falls back to the name the client sent, which keeps the message useful while a service's labels are still landing.
Types ¶
type Catalog ¶
Catalog maps a message code to its text in each locale. A code missing an entry for the requested locale falls back to Default.
func BuiltinCatalog ¶
func BuiltinCatalog() Catalog
BuiltinCatalog returns text for the generic codes plus a template for every validate tag. Services merge their own domain catalog over it; a service that wants a plain localized "not found" can raise CodeNotFound directly instead of inventing a code.