90dy / zod-i18n

Home Page:zod-i18n.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

npm version codecov CI

hero

Zod Internationalization

This library is used to translate Zod's default error messages.

Installation

yarn add zod-i18n-map i18next

This library depends on i18next.

How to Use

import i18next from 'i18next'
import { z } from 'zod'
import { zodI18nMap } from 'zod-i18n-map'
// Import your language translation files
import translation from 'zod-i18n-map/locales/ja/zod.json'

// lng and resources key depend on your locale.
i18next.init({
  lng: 'ja',
  resources: {
    ja: { zod: translation },
  },
  interpolation: {
    // https://www.i18next.com/translation-function/nesting#passing-nesting-to-interpolated
    skipOnVariables: false
  }
});
z.setErrorMap(zodI18nMap)

const schema = z.string().email()
schema.parse('foo') // メールアドレスの形式で入力してください。

Translation Files

zod-i18n-map contains translation files for several locals. See here if your language is included.

It is also possible to create and edit translation files. You can use this English translation file as a basis for rewriting it in your language.

If you have created a translation file for a language not yet in the repository, please send us a pull request.

Use with next-i18next

Many users will want to use it with next-i18next (i.e. on Next.js). This example summarizes how to use with it.

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

License

This project is licensed under the MIT License - see the LICENSE file for details

About

zod-i18n.vercel.app

License:MIT License


Languages

Language:TypeScript 99.4%Language:Shell 0.6%