aiji42 / zod-i18n

Useful for translating zod error messages.
https://zod-i18n.vercel.app
MIT License
669 stars 72 forks source link

Zod error translate prints null on express #125

Closed FranAM1 closed 1 year ago

FranAM1 commented 1 year ago

When I initialize setErrorMap with zodI18nMap make all the error messages to null. It works fine without the translation. image image

I import the i18n configuration to my index.js

i18n,js

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/es/zod.json" assert { type: "json" };

// lng and resources key depend on your locale.

i18next.init({
    lng: "es",
    resources: {
      es: { zod: translation },
    },
});
z.setErrorMap(zodI18nMap);  

index.js

import app from './app.js'
import { sequelize } from './db/db.js'
import { createRoles } from './seeders/roles.seeder.js'
import { createUsers } from './seeders/users.seeder.js'
import dotenv from 'dotenv'
dotenv.config()
import './utils/i18n.js'

const PORT = process.env.SERVER_PORT || 3000

async function main() {
    try{
        // false = no elimina la base de datos | true = elimina la base de datos
        await sequelize.sync({force: true}) 
        await createRoles()
        await createUsers()
        app.listen(PORT)
        console.log(`Server is running on http://localhost:${PORT}`)
    }catch(error){
        "Error iniciar servidor: " + error
    }
}

main()
hmnd commented 1 year ago

Did you figure this out by any chance?

FranAM1 commented 1 year ago

I still haven’t figured it out so I decided to leave the error messages in English for now

hmnd commented 1 year ago

@FranAM1 I think I just figured it out! Try to replace zodI18nMap with makeZodI18nMap({ t: i18next.t })

FranAM1 commented 1 year ago

IT WORKED, THANK YOU image

FadiAboMsalam commented 1 year ago

@hmnd you are life saver I love you bro 🎉

verheyenkoen commented 1 year ago

@FranAM1 I think I just figured it out! Try to replace zodI18nMap with makeZodI18nMap({ t: i18next.t })

Life saver! Thanks!

Sleepful commented 3 months ago

this should be shown in the README!