Skip to main content

Localize Form Validation Messages Without Leaving English Errors

2026-11-25

Localize Form Validation Messages Without Leaving English Errors

The labels are in German. The button says "Weiter". Then the user types a bad email address and the form answers in English: "Invalid email". Or the user switches the app to French while an error is visible, and the error stays in German until they type again.

Validation messages often slip past a localization workflow because they are generated away from the screen: by a schema library's defaults, by a translation call that ran once when a module loaded, by the browser's built-in validation, or by a server response. This guide walks through each source, shows where language leaks in, and gives a pattern that keeps every error in the user's chosen language. It is written for web and React Native engineers who use Zod, Yup, react-hook-form, or plain HTML constraint validation.

The short answer

  1. List every source of validation text in your forms before you translate anything.
  2. Keep schemas language-free. Let them produce an issue code or a translation key plus parameters, not a finished sentence.
  3. Translate at render time, in the component that shows the error.
  4. Load library default messages per locale, and on servers prefer per-request error maps over global configuration.
  5. Replace or disable native browser messages so they cannot appear in a different language.
  6. Test every launch locale by forcing each failure, and by switching language while errors are visible.

Where validation text actually comes from

A typical React form has at least four producers of validation text, and each one has its own language rules.

  • Schema library defaults. If a rule has no custom message, the library writes one. Zod's documentation says the regular zod package loads the English locale automatically, and Zod Mini does not load any locale, so every message defaults to "Invalid input" (Zod: Customizing errors).
  • Custom messages you wrote in the schema. These are the ones you think are translated. Whether they are depends on when the translation function runs.
  • The browser's constraint validation. Attributes such as required, type="email", minlength, and pattern produce messages that the browser writes, not your app (MDN: Client-side form validation).
  • Server responses. A 422 response with English prose in it will bypass every client-side fix. Returning stable codes instead of sentences is covered in Localize API error messages.

Make this inventory concrete. Search the codebase for schema definitions, for form components that read validationMessage, for HTML validation attributes, and for places that display error.message from a network call. Each hit belongs to one of the four producers above, and each producer needs its own fix.

Why messages get frozen in the wrong language

The most common bug is a translation that ran too early.

A widely viewed Stack Overflow question shows a Next.js form validated by Zod through react-hook-form. The schema lives at module scope and calls i18n?.t('zod-errors.email') for each message. When the user changes the locale, the messages do not change (Stack Overflow: Zod dynamic error messages based on active locale in Next.js, score 9 and about 29,000 views).

A module-level z.object({...}) is evaluated once, when the module is first imported. Every t() call inside it returns a string at that moment, in whatever language i18next had loaded at that moment, or the fallback key if resources were not ready yet. The schema then holds plain strings for the rest of the session.

There are two common ways out.

The first is a schema factory. You write createSchema(t) and build the schema inside the component with useMemo(() => createSchema(t), [t]), so a new schema is built when the translation function changes. The OpenMRS frontend conventions recommend exactly this, so schemas "are properly localized and can be memoized" (OpenMRS: Forms coding conventions).

The second is to keep translation out of the schema entirely. The highest-voted answer on the same question puts only a key in the schema, such as z.string().email({ message: 'emailError' }), and calls t(errors.email.message) in the component that renders the error. It notes that no extra library is needed.

The factory fixes new validation runs. It does not fix errors that are already stored. react-hook-form keeps the last validation result in form state, and that result holds whatever string the schema produced. If the user switches language while three errors are showing, those three strings stay in the old language until the fields are validated again.

That is why the key-based approach is the better default. Store a translation key or issue code in form state and translate it where the error renders, so a language switch also updates errors that are already on screen. The render function reads the current language every time it runs, and a language switch re-renders the component. Nothing needs to be revalidated.

A language-free schema pattern

A practical pattern combines both ideas. The schema produces a key and parameters. The component turns them into a sentence.

import * as z from "zod";

export const signupSchema = z.object({
  email: z.string().email({ error: "validation.email.invalid" }),
  password: z.string().min(12, { error: "validation.password.tooShort" }),
});
function FieldError({ name }: { name: "email" | "password" }) {
  const { t } = useTranslation();
  const { errors } = useFormState();
  const error = errors[name];
  if (!error?.message) return null;
  return <p role="alert">{t(error.message, { min: 12 })}</p>;
}

Four details matter in this pattern.

Parameters belong to the message, not the code. "Password must be at least 12 characters" combines a field, a rule, and a number. If you concatenate a translated field name with a translated rule, word order breaks in many languages. Keep one full message per rule and pass the number as a parameter. The same reasoning is covered in depth in Stop concatenating strings.

Plurals need message formatting. "1 character" and "12 characters" are two forms in English, but Polish needs more. The Client-Side Form guide shows a Polish catalog with one, few, many, and other branches for the same minimum-length error (Client-Side Form: Localising form error messages). Use ICU plural syntax or your library's plural support rather than adding an "s".

Field-specific overrides beat generic ones. "Enter your email address" reads better than "Email is required" in most languages, and some languages need the article or possessive to agree with the field noun. Phrase's walkthrough looks up a field-specific key first and falls back to a generic rule key (Phrase: Localized form validation). Keep that fallback order in your lookup function.

Unknown keys need a safe fallback. If a key is missing in a locale, show a generic localized message rather than the raw key. Then make missing keys fail in CI so the fallback stays rare.

Translating library defaults in Zod 4

You will still have rules without custom messages, and you probably want Zod's defaults translated too. Zod 4 added first-party locales for this.

The call that sets a locale is z.config(z.locales.es()). The Zod maintainer introduced it in the issue that opened the call for community translations, with the example that a failed z.string() check then reports "Entrada inválida: se esperaba string, recibido número" (GitHub: colinhacks/zod #4168). The documentation lists the built-in locales and shows lazy loading with a dynamic import of the locale file before calling z.config() (Zod: Customizing errors).

Zod also defines a clear order of precedence when several customizations apply. From highest to lowest: an error defined on the individual check, an error defined on the schema, a per-parse error map passed to .parse(), a global error map set with z.config({ customError }), and finally the locale error map. Use that order on purpose:

  • Put product-specific wording on checks or schemas, as keys.
  • Use the locale map for generic defaults you have not customized.
  • Use a per-parse map when the language depends on the request.

Be careful with global configuration on servers

The documentation calls z.config() global error customization. In a browser tab that is fine: one user, one language at a time. On a server that renders pages or validates requests for many users, global means the setting is shared by every request in that process.

This is a recommendation from first principles rather than a documented Zod warning, but the consequence follows from the API. On a server that renders several locales, a process-wide z.config locale is shared by every request, so pass a per-parse error map built for the request's locale instead. If request A sets French and request B sets Japanese before A finishes validating, A's errors can come back in Japanese. Bugs like this appear only under concurrent load, so they are hard to reproduce locally.

A per-parse map avoids shared state. Zod's docs show the shape: schema.parse(data, { error: (iss) => ... }), where iss.code tells you which rule failed. Build that function from the request's translator and map each code to a key in your own catalog. Return undefined for codes you do not handle so Zod falls through to the next level.

If you are migrating from Zod 3

Much of the advice online predates Zod 4. The Zod 4 changelog says the invalid_type_error and required_error params were dropped and should be expressed with the new error param, for example a function that checks whether issue.input is undefined to tell a missing value from a wrong type (Zod: v4 changelog). The Stack Overflow example above uses the old params, so copying it into a Zod 4 project will not work as written.

Library support moves with the major version too. zod-i18n-map, a popular package that translates Zod's default messages through i18next, now documents a Zod 4 setup with z.config({ customError: zodI18nMap }) and tells Zod 3 users to stay on zod-i18n-map@2 (GitHub: aiji42/zod-i18n). Zod 4 support arrived in version 3.0.0, after an issue the Zod maintainer opened in April 2025 noting that issue codes had changed (GitHub: aiji42/zod-i18n #222). Before adopting a helper, check which Zod major version it targets. Teams on next-intl rather than i18next ask the same question (Stack Overflow: Translating zod error messages using next-intl), and the key-plus-render pattern works with any translation library because it only needs a t function at render time.

Yup and other schema libraries

Yup users usually reach for setLocale. A Stack Overflow question asking for the Zod equivalent shows the typical setup: yup.setLocale({ mixed: { required: () => t("validation:required") }, string: { min: ({ min }) => t("validation:minStr", { value: min }) } }) (Stack Overflow: Zod equivalent for Yup setLocale).

Because those entries are functions, t() runs when validation runs, not when the module loads. That avoids the frozen-at-import bug. It still stores a finished sentence in form state, so the language-switch problem from earlier applies. If you want errors to follow a switch without revalidating, return keys and parameters from the locale functions and translate in the component, the same as with Zod.

The same split works for hand-written validators. The validator reports what failed, and the view chooses the wording.

Native browser messages ignore your language picker

HTML constraint validation costs nothing to add, but the browser writes its messages.

MDN's validation guide lists two drawbacks of the automatic messages: you cannot restyle them with CSS, and they depend on the browser locale, which means a page can be in one language while the error appears in another (MDN: Client-side form validation). The validationMessage property returns "a localized message" written by the browser (MDN: HTMLInputElement validationMessage). Developers have asked how to change that language for over a decade (Stack Overflow: Internationalization at HTML5).

For an app that follows the device language this may be acceptable. For an app with its own language picker it produces mixed-language forms: a user who chose Spanish on an English laptop gets English validation bubbles. Native browser validation messages follow the browser locale, not the page language, so an app with its own language picker must replace them with setCustomValidity or turn them off with novalidate.

You have two options:

  1. Keep native validation and supply your own text. Call setCustomValidity() with a translated message when a constraint fails, and with an empty string when it passes. MDN's constraint validation guide shows this pattern for a postal code rule that changes with the selected country (MDN: Constraint validation guide). Remember to reset it on every input event, or the field stays invalid.
  2. Turn off the native UI and render errors yourself. Add novalidate to the form, keep the attributes for semantics and mobile keyboards, and let your schema produce errors through the same key pipeline as everything else.

Option 2 gives one consistent path for every message, which makes testing simpler. Option 1 is reasonable for small forms without a schema library.

Testing errors in every locale

Validation messages rarely appear in screenshots or happy-path tests, so translation gaps survive release. Add a small, targeted suite:

  • Force each failure in each launch locale. Submit empty required fields, an invalid email, a too-short password, and a value that hits a plural boundary such as 1, 2, and 5 characters. Assert that the visible text is not the source-language string and not a raw key.
  • Switch language with errors visible. Trigger three errors, change the language, and assert that all three messages changed. This is the test that catches the frozen form-state bug.
  • Check library defaults. Validate a field that has no custom message and confirm the default is localized, or that your fallback message appears.
  • Check native messages. With a browser set to English and the app set to another language, submit an invalid field and confirm no browser-language bubble appears.
  • Run server validation concurrently. If schemas run on the server, send interleaved requests in two locales and assert that each response matches its own locale.

Keeping the catalog healthy

Once errors are keys, they behave like any other string and can go through the same workflow. Put validation keys in their own namespace so translators see them together, and give each key a short description of when it appears. Run the missing-key and placeholder checks you already use for UI strings, and fail the build if a locale is missing a validation key. If you translate catalogs with an AI workflow such as i18nAgent, keep the ICU placeholders and plural branches intact and review the result in context, since error messages are short and easy to misread without the field next to them.

Common mistakes

  • Calling t() inside a module-level schema and expecting it to follow language changes.
  • Translating at validation time but storing the finished sentence, so a language switch leaves old errors behind.
  • Setting z.config() per request on a server that handles several locales at once.
  • Copying Zod 3 examples with required_error into a Zod 4 project.
  • Leaving required and type="email" to show browser-language bubbles in an app with its own language picker.
  • Building messages by joining a translated field name and a translated rule.

What to do next

Pick your most used form, usually sign up or checkout. List every message it can show and which of the four producers writes it. Move each schema message to a key, translate in the error component, and add the language-switch test. Then repeat for the next form. If your server also returns validation prose, pair this work with Localize API error messages so both sides use codes.