aiji42 / zod-i18n

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

Not found when i add backend option #162

Open VigilioYonatan opened 1 year ago

VigilioYonatan commented 1 year ago

When i add backend option not found translation in express but zod translate zod found but when i remove resources option, translation it found. How can i fix it?

import i18next from "i18next";
import i18nextMiddleware from "i18next-http-middleware";
import i18nextBackend from "i18next-node-fs-backend";
import { setErrorMap, z } from "zod";
import { zodI18nMap } from "zod-i18n-map";
import translation from "zod-i18n-map/locales/es/zod.json";
import path from "path";
// lng and resources key depend on your locale.

i18next
    .use(i18nextBackend)
    .use(i18nextMiddleware.LanguageDetector)
    .init({
        // debug: true,
        lng: "es-ES",
        fallbackLng: "en-US",
        preload: ["en-US", "es-ES"],
        resources: {
            es: { zod: translation },
        },
        backend: {
            loadPath: path.resolve(
                __dirname,
                "..",
                "lang",
                "{{lng}}",
                "{{ns}}.json"
            ),
        },
        detection: {
            caches: ["cookie"],
        },
    });

setErrorMap(zodI18nMap);
jurajkrivda commented 1 year ago
const resources = languages.reduce(
  (acc, lang) => {
    acc[lang.key] = { zod: lang.zod }
    return acc
  },
  {} as Record<string, { zod: (typeof languages)[0]['zod'] }>,
)

export const i18n = {
  supportedLngs: languages.map(lang => lang.key),
  fallbackLng: 'en',
  defaultNS: 'common',
  nsSeparator: '::',
  react: { useSuspense: false },
  partialBundledLanguages: true,
  resources,
  debug: false,
}

Note: partialBundledLanguages must be set to true