zod icon indicating copy to clipboard operation
zod copied to clipboard

[i18n] Request for internationalization (locales)

Open colinhacks opened this issue 6 months ago • 34 comments

Zod 4 has a more mature system for internationalizaton. Here is an example of loading the Spanish locale:

import * as z from "zod";

z.config(z.locales.es());

const result = z.string().safeParse(123);
result.error!.issues[0].message;
// => Entrada inválida: se esperaba string, recibido número

I'm putting out an open call for locales to the Zod ecosystem. I think a human touch is important to make error messages friendly and useful.

Instructions

Refer to this pull request for an example of creating a locale: https://github.com/colinhacks/zod/pull/4158/files

That PR doesn't update the Nouns map, but you should (to the best of your ability). Didn't realize this before linking it as an example here.

To summarize:

1 Create a new file under packages/core/src/locales/<code>.ts

Use the language code for your language. Dialects (pt-BR) are also encouraged.

Using the English locale as a starting point, translate all error messages: https://github.com/colinhacks/zod/blob/v4/packages/core/src/locales/en.ts

2 Export your dialect from packages/core/locales.ts

// other languages...
import zz from "./locales/zz.js";

export { 
  // ... other languages
  zz
};

3 Add to docs

Add your new language variant to the Error customization page under the "Locales" section. Keep the list in alphabetical order.

4 Submit the PR

Once you submit a PR, add a comment below so others can quickly see which languages are already covered.


Thank you for your help on this!

colinhacks avatar Apr 15 '25 23:04 colinhacks