Tutorial

I18n Validation Messages

Localize validation messages by passing a message key and parameters to the schema, then resolving them through a translation function at render time.

On this page

  1. Introduction
  2. Emit a key and params
  3. Carry the locale
  4. Resolve at render
  5. Handle plurals
  6. Fail on missing keys
  7. Test two locales
  8. Sources

Introduction

This guide covers internationalized validation messages, a recurring concern in the form-validation scenario. A validation message hard-coded in English locks the form to one locale, and copy-pasting the schema per language is a maintenance trap. The fix is to have the schema emit a message key and parameters, and resolve them through a translation function at render time, so one schema serves every locale.

Emit a key and params

Have the validator return a key and params instead of a literal string, so the schema stays locale-free and the message is resolved where the locale is known.

The trade-off worth naming is that have the validator return a key and params instead of a literal string, so the schema stays locale-free and the message is resolved where the locale is known adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

const S = z.string().min(8, { key: 'too_short', params: { min: 8 } });
const r = S.safeParse('ab');
if (!r.success) {
  const i = r.error.issues[0];
  // i.message is the key; i.params carry the args
}

Carry the locale

Thread the locale through the resolver so the same schema runs on the server with a request locale and on the client with the UI locale without duplication.

The trade-off worth naming is that thread the locale through the resolver so the same schema runs on the server with a request locale and on the client with the ui locale without duplication adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Resolve at render

Map each key through a translation function that knows the locale and plural rules, so the schema never branches on language and the translator owns the wording.

The trade-off worth naming is that map each key through a translation function that knows the locale and plural rules, so the schema never branches on language and the translator owns the wording adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

const dict = {
  en: { too_short: (p) => 'Must be at least ' + p.min + ' characters' },
  zh: { too_short: (p) => '至少需要 ' + p.min + ' 个字符' },
};
function resolve(key, params, locale) { return dict[locale][key](params); }

Handle plurals

Pass the count as a param and let the translation function pick the plural form, since plural rules belong to the locale, not the schema.

The trade-off worth naming is that pass the count as a param and let the translation function pick the plural form, since plural rules belong to the locale, not the schema adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Fail on missing keys

Make the resolver throw in dev when a key is missing, so an untranslated rule surfaces immediately instead of shipping an empty message to users.

The trade-off worth naming is that make the resolver throw in dev when a key is missing, so an untranslated rule surfaces immediately instead of shipping an empty message to users adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Test two locales

Run each rule in at least two locales and assert the resolved string is non-empty and distinct, so a missing key or a copy-paste error fails the build.

The trade-off worth naming is that run each rule in at least two locales and assert the resolved string is non-empty and distinct, so a missing key or a copy-paste error fails the build adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sources

Sources: Zod error map, i18next.

FAQ

Should the schema hold the translation key or the resolved string?
The key and params; resolving at render keeps the schema locale-free and lets the same validator run on the server where there is no UI locale yet.
How do I handle pluralization in messages?
Pass the count as a param and let the translation function pick the plural form, since the schema should not know the locale plural rules.

Related Examples